|
| 1 | +import tkinter as tk |
| 2 | +from functools import partial |
| 3 | +import time |
| 4 | + |
| 5 | +gui=tk.Tk() |
| 6 | +gui.geometry('500x200') |
| 7 | +gui.title('Tic Tac Toe') |
| 8 | + |
| 9 | +movesPlayed=[] |
| 10 | +game_over=False |
| 11 | + |
| 12 | +Xcount=0 |
| 13 | +Ocount=0 |
| 14 | +player,turn=1,'x' |
| 15 | + |
| 16 | +def clicked(obj): |
| 17 | + global player,turn,Xcount,Ocount |
| 18 | + |
| 19 | + button_clicked=''.join(obj.split()) |
| 20 | + #row,column=obj.split() |
| 21 | + #row=row.split('w')[1] |
| 22 | + |
| 23 | + |
| 24 | + if button_clicked not in movesPlayed: |
| 25 | + globals()[button_clicked].configure(text=turn.center(6)) |
| 26 | + movesPlayed.append(button_clicked) |
| 27 | + globals()[button_clicked]['state']='disabled' |
| 28 | + if player==1: |
| 29 | + player,turn=2,'0' |
| 30 | + Xcount+=1 |
| 31 | + player_data.configure(text='Player-2(0) chance') |
| 32 | + |
| 33 | + |
| 34 | + else: |
| 35 | + player,turn=1,'x' |
| 36 | + Ocount+=1 |
| 37 | + player_data.configure(text='Player-1(x) chance') |
| 38 | + |
| 39 | + win_check() |
| 40 | + else: |
| 41 | + print('Move already played') |
| 42 | + |
| 43 | +def finish(string): |
| 44 | + global game_over |
| 45 | + game_over=True |
| 46 | + player_data.configure(text='') |
| 47 | + player_win.configure(text=string) |
| 48 | + player_win.pack() |
| 49 | + for i in ('row1','row2','row3'): |
| 50 | + for j in range(3): |
| 51 | + globals()[i+str(j+1)]['state']='disabled' |
| 52 | + rtry=tk.Button(gui,text='Retry',command=Retry) |
| 53 | + rtry.pack() |
| 54 | + finish.rtry=rtry |
| 55 | + |
| 56 | +def Retry(): |
| 57 | + global movesPlayed,player,turn,Xcount,Ocount,rtry |
| 58 | + movesPlayed=[] |
| 59 | + player,turn=1,'x' |
| 60 | + Xcount,Ocount=0,0 |
| 61 | + for i in ('row1','row2','row3'): |
| 62 | + for j in range(3): |
| 63 | + |
| 64 | + globals()[i+str(j+1)].configure(text=f' ') |
| 65 | + globals()[i+str(j+1)]['state']='active' |
| 66 | + finish.rtry.pack_forget() |
| 67 | + player_win.pack_forget() |
| 68 | + player_data.configure(text='Player-1(x) chance') |
| 69 | + |
| 70 | +def win_check(): |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + if (row11['text'] == row12['text'] == row13['text'] != ' ' or |
| 76 | + row21['text'] == row22['text'] == row23['text'] != ' ' or |
| 77 | + row31['text'] == row32['text'] == row33['text'] != ' ' or |
| 78 | + row11['text'] == row21['text'] == row31['text'] != ' ' or |
| 79 | + row12['text'] == row22['text'] == row32['text'] != ' ' or |
| 80 | + row13['text'] == row23['text'] == row33['text'] != ' ' or |
| 81 | + row11['text'] == row22['text'] == row33['text'] != ' ' or |
| 82 | + row13['text'] == row22['text'] == row31['text'] != ' ' |
| 83 | + ): |
| 84 | + |
| 85 | + |
| 86 | + if Xcount>Ocount: |
| 87 | + string='Player 1(X) won' |
| 88 | + else: |
| 89 | + string='Player 2(O) won' |
| 90 | + finish(string) |
| 91 | + |
| 92 | + if not game_over: |
| 93 | + if (Xcount==5 and Ocount==4 or |
| 94 | + Xcount==4 and Ocount==5): |
| 95 | + finish('Draw match') |
| 96 | +tk.Label(gui,text="PLAYER-1=X\nPLAYER-2=0").pack() |
| 97 | +player_data=tk.Label(gui,text='Player-1(x) chance') |
| 98 | +player_data.pack() |
| 99 | + |
| 100 | +row1=tk.Frame(gui) |
| 101 | +row1.pack() |
| 102 | +row2=tk.Frame(gui) |
| 103 | +row2.pack() |
| 104 | +row3=tk.Frame(gui) |
| 105 | +row3.pack() |
| 106 | + |
| 107 | +player_win=tk.Label(gui,text='') |
| 108 | +for i in ('row1','row2','row3'): |
| 109 | + for j in range(3): |
| 110 | + |
| 111 | + vars()[i+str(j+1)]=tk.Button(vars()[i], text=f' ',bd='1',command=partial(clicked,i+' '+str(j+1))) |
| 112 | + vars()[i+str(j+1)].pack(side='left') |
| 113 | + |
| 114 | + |
| 115 | +gui.mainloop() |
0 commit comments