1616
1717bclick = True
1818flag = 0
19+ move_history = []
20+ move_number = 1
1921current_player_name = p1 .get () if p1 .get () else 'X'
2022
2123
@@ -74,11 +76,17 @@ def checkForWin():
7476
7577
7678def resetGame ():
77- global bclick , flag , current_player_name
79+ global bclick , flag , current_player_name , move_history , move_number
80+
7881 for i in range (3 ):
7982 for j in range (3 ):
8083 buttons [i ][j ]["text" ] = " "
8184 buttons [i ][j ].config (bg = 'black' , state = NORMAL )
85+
86+ move_history = []
87+ move_number = 1
88+ history_box .delete (1.0 , END )
89+
8290 bclick = True
8391 flag = 0
8492 current_player_name = p1 .get () if p1 .get () else 'X'
@@ -92,26 +100,45 @@ def resetGame():
92100 fg = 'white' , height = 4 , width = 8 ) for _ in range (3 )] for _ in range (3 )]
93101
94102
95- def btnClick (button ):
96- global bclick , flag
103+ def btnClick (row , col ):
104+ global bclick , flag , move_number
105+
106+ button = buttons [row ][col ]
107+
97108 if button ["text" ] == " " :
109+
98110 if bclick :
99111 button ["text" ] = "X"
112+ player = p1 .get () if p1 .get () else "Player 1"
100113 else :
101114 button ["text" ] = "O"
115+ player = p2 .get () if p2 .get () else "Player 2"
116+
117+ # cleaner move history
118+ move = f"{ move_number } . { player } -> ({ row + 1 } ,{ col + 1 } )"
119+ move_history .append (move )
120+ history_box .insert (END , move + "\n " )
121+
122+ move_number += 1
123+
102124 bclick = not bclick
103125 flag += 1
104126 checkForWin ()
127+
105128 else :
106129 tkinter .messagebox .showinfo ("Tic-Tac-Toe" , "Button already Clicked!" )
107130
108131
109132for i in range (3 ):
110133 for j in range (3 ):
111134 buttons [i ][j ].configure (command = lambda row = i ,
112- col = j : btnClick (buttons [ row ][ col ] ))
135+ col = j : btnClick (row , col ))
113136 buttons [i ][j ].grid (row = i + 3 , column = j )
114137reset_button = Button (tk , text = "Reset Game" , font = 'Times 16 bold' ,
115138 bg = 'white' , fg = 'black' , command = resetGame )
116139reset_button .grid (row = 6 , column = 0 , columnspan = 3 )
140+ Label (tk , text = "Move History" , font = 'Times 16 bold' , bg = 'yellow' ).grid (row = 0 , column = 10 , padx = 80 )
141+
142+ history_box = Text (tk , height = 18 , width = 28 , font = ("Consolas" , 11 ))
143+ history_box .grid (row = 1 , column = 10 , rowspan = 6 , padx = 80 )
117144tk .mainloop ()
0 commit comments