Self Self Self: Tictactoe - Init
Self Self Self: Tictactoe - Init
self.current_player = 'X'
self.moves = 0
def make_move(self, row, col):
if self.buttons[row][col]['text'] == '' and not self.check_winner():
self.buttons[row][col]['text'] = self.current_player
self.moves += 1
if self.check_winner():
messagebox.showinfo("Tic-Tac-Toe", f"Player {self.current_player} wins!")
self.reset_board()
elif self.moves == 9:
messagebox.showinfo("Tic-Tac-Toe", "It's a tie!")
self.reset_board()
else:
self.current_player = 'O' if self.current_player == 'X' else 'X'
def check_winner(self):
for i in range(3):
if self.buttons[i][0]['text'] == self.buttons[i][1]['text'] == self.buttons[i][2]['text'] != '':
return True
if self.buttons[0][i]['text'] == self.buttons[1][i]['text'] == self.buttons[2][i]['text'] != '':
return True
if self.buttons[0][0]['text'] == self.buttons[1][1]['text'] == self.buttons[2][2]['text'] != '':
return True
root.mainloop()
In [ ]: