I am creating a tic tac toe. In this, I have created a reset button which refers a reset function. In that function, I have reset the board and stop_game value. When I click on reset button in middle of game, it works fine. But, when someone wins and then I click on reset button, it just resets the board but on pressing board buttons, it does nothing. Please help me to solve this problem.
from tkinter import *
from copy import deepcopy
import random
game = Tk()
game.title("TIC TAC TOE")
game.geometry("450x500")
#game.configure(bg = '#b3b3b3')
player = 'X'
stop_game = False
def callback(r, c):
global player
if player == 'X' and states[r][c] == 0 and stop_game == False:
board[r][c].configure(text = 'X', fg = '#f64c72')
states[r][c] = 'X'
player = 'O'
if player == 'O' and states[r][c] == 0 and stop_game == False:
board[r][c].configure(text = 'O', fg = '#f64c72')
states[r][c] = 'O'
player = 'X'
checkWinner()
def checkWinner():
global stop_game
win_color = '#3b5b5b'
for i in range(3):
if states[i][0] == states[i][1] == states[i][2] != 0:
board[i][0].config(bg = win_color)
board[i][1].config(bg = win_color)
board[i][2].config(bg = win_color)
stop_game = True
for i in range(3):
if states[0][i] == states[1][i] == states[2][i] != 0:
board[0][i].config(bg = win_color)
board[1][i].config(bg = win_color)
board[2][i].config(bg = win_color)
stop_game = True
if states[0][0] == states[1][1] == states[2][2] != 0:
board[0][0].configure(bg = win_color)
board[1][1].configure(bg = win_color)
board[2][2].configure(bg = win_color)
stop_game = True
if states[2][0] == states[1][1] == states[0][2] != 0:
board[2][0].configure(bg = win_color)
board[1][1].configure(bg = win_color)
board[0][2].configure(bg = win_color)
stop_game = True
f = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
board = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
states = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
def reset():
for i in range(3):
for j in range(3):
board[i][j].configure(text = ' ', fg = '#ffda30', bg = "#242582")
states[i][j] = 0
stop_game = False
for i in range(3):
for j in range(3):
f[i][j] = Frame(game, width = 150, height = 150)
f[i][j].propagate(False)
f[i][j].grid(row = i, column = j, sticky = "nsew", padx = 1, pady = 1)
board[i][j] = Button(f[i][j], font = ("Helvatica", 70), bg = "#242582", fg = "#ffda30",
command = lambda r = i, c = j: callback(r, c))
board[i][j].pack(expand = True, fill = BOTH)
reset_game = Button(text = "Reset the game!", font = ("Helvatica", 13), bg = "#ffda30", fg = "#000000",
command = lambda :reset())
reset_game.grid(row = 3, column = 0, columnspan = 2, sticky = 'nsew')
quit_game = Button(text = "Quit game!", font = ("Helvatica", 13), bg = "#ffda30", fg = "red",
command = lambda :game.destroy())
quit_game.grid(row = 3, column = 2, sticky = 'nsew')
game.resizable(False, False)
game.mainloop()
You need to declare stop_game
as global inside Reset()
function.