Advertisement
here2share

# Tk_tictactoe_str_replace.py

Feb 16th, 2021
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. # Tk_tictactoe_str_replace.py
  2.  
  3. # magic trick for less coding than the typical way --
  4. # layout[1]='123 456 789 147 258 369 159 357' ### for win checking
  5. # layout[z]=layout[z].replace(play,['.',p][z])
  6.  
  7. from Tkinter import *
  8. import math
  9.  
  10. root = Tk()
  11. root.title("Tk Tic-Tac-Toe")
  12.  
  13. ww = 520
  14. hh = 520
  15.  
  16. from time import sleep as x
  17. import os
  18. cls=lambda: os.system('cls')
  19.  
  20. r=range
  21. def console(s=''):
  22.     cls()
  23.     t=t2
  24.     if '!' in s:
  25.         t=''
  26.     mv="Player %s's Turn..."%p
  27.     print(layout[0]+t.replace('@',mv)+s)
  28.  
  29. def button(frame):
  30.     b=Button(frame,padx=1,bg="lightyellow",width=3,text="",font=('arial',40,'bold'),relief="sunken",bd=3)
  31.     return b
  32.    
  33. def reset(e=None):
  34.     global sss
  35.     sss = ''
  36.    
  37. def click(row,col):
  38.     global play
  39.     if '!' not in sss:
  40.         b[row][col].config(text=p,state=DISABLED,disabledforeground=colour[p])
  41.         play = str(row*3+col+1)
  42.  
  43. colour={'O':"deep sky blue",'X':"lawn green"}
  44.  
  45. lbl=Label(font=('arial',30))
  46. lbl.grid(row=3,column=0,columnspan=3,sticky='EW')
  47. lbl2=Label(text="Click Here To Reset",font=('arial',10))
  48. lbl2.grid(row=4,column=0,columnspan=3,sticky='EW')
  49. lbl.bind("<Button-1>", reset)
  50. lbl2.bind("<Button-1>", reset)
  51.  
  52. play = '?'
  53. sss = 'go'
  54. while play.lower() != 'q':
  55.     p='O'
  56.     layout = {}
  57.     b=[[],[],[]]
  58.     for i in range(3):
  59.         for j in range(3):
  60.             b[i].append(button(root))
  61.             b[i][j].config(command=lambda row=i,col=j:click(row,col))
  62.             b[i][j].grid(row=i,column=j)
  63.     lbl.config(text=p+"'s Turn")
  64.    
  65.    
  66.     t2='''
  67.     +++
  68.  
  69.     @
  70.     '''
  71.     layout[0]='''
  72.      1 | 2 | 3        |   |  
  73.     ---+---+---    ---+---+---
  74.      4 | 5 | 6        |   |  
  75.     ---+---+---    ---+---+---
  76.      7 | 8 | 9        |   |  
  77.      '''
  78.     layout[1]='123 456 789 147 258 369 159 357' ### for win checking
  79.     console()
  80.     while sss:
  81.         if play in layout[1]:
  82.             t=list(layout[0])
  83.             t[layout[0].index(play)+15]=p # updates the right-side of console layout
  84.             layout[0]=''.join(t)
  85.            
  86.             ### a minimalistic approach is using the replace method
  87.             for z in (0,1):
  88.                 layout[z]=layout[z].replace(play,['.',p][z])
  89.             if p*3 in layout[1]:
  90.                 sss = "Player "+p+" Won !!!"
  91.                 console("\n\t"+sss)
  92.             elif layout[0].count('.') == 9:
  93.                 sss = "Tied Game!"
  94.                 console("\n\t"+sss)
  95.             else:
  96.                 console()
  97.            
  98.             p = ('X' if p == 'O' else 'O')
  99.             lbl.config(text=p+"'s Turn")
  100.         play = '?'
  101.         if sss != 'go':
  102.             lbl.config(text=sss)
  103.         root.update()
  104.     play = '?'
  105.     sss = 'go'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement