Python Forum

Full Version: Tic-Tac-Toe restart
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like to create an opportunity of restarting this game of Tic-Tac-Toe after it is finished. I was thinking of checking if all the created buttons have either "x" or "O" texts, and offering to restart then, but I could not find a way to do that.
Here is the code I would like to add to:

import tkinter as tk
from functools import partial


class ButtonTest():
    def __init__(self, master):
        self.parent = master

        self.buttons_list = []

        for i in range(5):
            for j in range(3):

                button_num = i * 3 + j
                button = tk.Button(self.parent, height=6, width=12)
                button.grid(row=i, column=j)


                button.bind("<Button-1>", partial(self.click, button_num))


                self.buttons_list.append(button)


        self.counter = []

        def check_winner():
            



    def click(self, button_num, event):
        this_button = self.buttons_list[button_num]
        the_actual_counter = len(self.counter)

        if the_actual_counter % 2 == 0:
            this_button["text"] = "X"


        else:
            this_button["text"] = "O"

        self.counter.append("element")





master = tk.Tk()
BT = ButtonTest(master)
master.mainloop()
Set the text to "" for all of the buttons, and self.counter=[]. Note that you do not check for clicking the same button twice. You could use
if button_num not in self.counter:
    self.counter.append(button_num)
else:
    print("That space is already occupied")