Tie window that pops up when the game is tied

42 Views Asked by At

I have this code project:

import tkinter as tk
import tkinter.ttk as ttk
from tkinter import font as tkFont

# initializes a 3x3 list named 'board' with empty strings as elements
board = [['' for _ in range(3)] for _ in range(3)]

# defines the function to switch to the game screen
def play_game():
    # creates a top-level window (appears above all other windows when pressed)
    game_window = tk.Toplevel(win)
    game_window.title("Tic-Tac-Toe Game")
    game_window.geometry("600x600")
    game_window.resizable(False, False)


    # creates a canvas to draw lines
    canvas = tk.Canvas(game_window, width=600, height=600)
    canvas.pack()

    # creates horizontal lines
    for i in range(1, 3):
        canvas.create_line(0, i * 200, 600, i * 200, fill="black")

    # creates vertical lines
    for i in range(1, 3):
        canvas.create_line(i * 200, 0, i * 200, 600, fill="black")

    # creates the buttons in the game window
    for i in range(3):
        for j in range(3):
            x1 = i * 200
            y1 = j * 200
            button_tile = ttk.Button(game_window)
            button_tile.place(x=x1, y=y1, width=200, height=200)
            button_tile.configure(command=lambda row=i, col=j, btn=button_tile, parent=game_window: button_click(row, col, btn, parent))
            board[i][j] = ''


# sets first turn of player to 'O' and next is 'X', so on and on
o_Turn = True

# defines the function what happens when one of the 9 buttons are pressed
def button_click(row, col, button, parent):
    global o_Turn
    Sign = 'X'
    if o_Turn:
        Sign = 'O'
    o_Turn = not o_Turn

    ttk.Label(parent, text=Sign, font=('Times', 60, tkFont.BOLD)).place(x=(row*200)+75, y=(col*200)+55)

    board[row][col] = Sign
    button.destroy()

    if check_winner(Sign):
        game_active = False
        winner_window = tk.Toplevel(parent)
        winner_window.title("Winner")
        ttk.Label(winner_window, text=f"Player {Sign} wins!", font=('Times New Roman', 20)).pack(padx=20, pady=20)

        # finds remaining button widgets in the parent widget (the game board)
        for widget in parent.winfo_children():
            # checks whether or not widget is a ttk.Button
            if isinstance(widget, ttk.Button):
                # destroys remaining buttons
                widget.destroy()

def check_winner(player):
    # checks for a tie
    if all([all(row) for row in board]):
        # all squares are filled and there is no winner, so the game is tied
        tie_window = tk.Toplevel(win)
        tie_window.title("Tie")
        ttk.Label(tie_window, text="The game is tied!", font=('Times New Roman', 20)).pack(padx=20, pady=20)
        return True

    # Check rows
    for i in range(3):
        if board[i][0] == board[i][1] == board[i][2] == player:
            return True

    # Check columns
    for j in range(3):
        if board[0][j] == board[1][j] == board[2][j] == player:
            return True

    # Check diagonals
    if board[0][0] == board[1][1] == board[2][2] == player:
        return True
    if board[0][2] == board[1][1] == board[2][0] == player:
        return True

    return False

# defines the function to open the user guide
def open_user_guide():
    # creates a top-level window (appears above all other windows when pressed)
    guide_window = tk.Toplevel(win)
    guide_window.title("User Guide")
    guide_window.geometry("800x300")
    guide_window.resizable(False, False)

    frame = ttk.Frame(guide_window)
    frame.pack(expand=True, fill='both', padx=20, pady=20)

    guide_label = ttk.Label(frame, text="User Guide", font=('Times New Roman', 20, 'bold'))
    guide_label.pack(pady=(0, 10), anchor='center')

    instructions_label = ttk.Label(frame, text="""
    Hi there! And welcome to my Tic Tac Toe game, made with tkinter.
    
    Here's how to play Tic-Tac-Toe! As you can see, the game is played on a grid that is 3 squares by 3 squares.

    1. The player that starts is X, and the other person is O. You and another player take turns placing X or O into a square.
    2. The first player to get all 3 marks in a row (horizontally, vertically or diagonally) wins the game.
    3. When all the squares are filled and no one has won, the game ends in a tie. AND THAT'S IT! Have fun playing!

    And press reset to play again!""",
    font=('Times New Roman', 12))
    instructions_label.pack(anchor='w')




win = tk.Tk()
win.geometry("1000x600")
bg = tk.PhotoImage(file="tictac.png")
backgroundimage = tk.Label(win, image=bg)
backgroundimage.place(x=0, y=0)
win.minsize(500, 300)
win.resizable(False, False)
win.title("Tic-Tac-Toe")
tmrn = tkFont.Font(family='Times New Roman', size=15, weight='bold')
style = ttk.Style()
style.configure('tmrn.TButton', font=('Times New Roman', 15, 'bold'), padding=(200, 10))
title_label = ttk.Label(win, text="Tic-Tac-Toe Game!", font=('Times New Roman', 40, 'bold'), foreground='black')
title_label.pack(side=tk.TOP, pady=20)
play_button = ttk.Button(win, text="Play", command=play_game, style='tmrn.TButton')
play_button.place(relx=0.5, rely=0.6, anchor=tk.CENTER)
guide_button = ttk.Button(win, text="User Guide", command=open_user_guide, style='tmrn.TButton')
guide_button.place(relx=0.5, rely=0.7, anchor=tk.CENTER)

win.mainloop()

I've implemented most of the features in my tic-tac-toe game, however, I have one issue that pops up when I tie a game and that is that when a game is tied, the "The game is tied!" window does pop up, however, the "Player O has won!" window also pops up, and I don't know why. I've tried doing this in the def check_winner() function but I keep getting this error. Thanks!

0

There are 0 best solutions below