Python Forum
Tic-Tac-Toe restart
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tic-Tac-Toe restart
#1
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()
Reply
#2
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") 
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020