|
| 1 | +import random |
| 2 | +from tkinter import * |
| 3 | + |
| 4 | + |
| 5 | +def next_turn(row, column): |
| 6 | + global player |
| 7 | + |
| 8 | + if buttons[row][column]['text'] == "" and check_winner() is False: |
| 9 | + |
| 10 | + if player == players[0]: # if its not player 1 turn then it will be else |
| 11 | + |
| 12 | + buttons[row][column]['text'] = player |
| 13 | + |
| 14 | + if check_winner() is False: |
| 15 | + player = players[1] |
| 16 | + label.config(text=(players[1] + " turn")) |
| 17 | + |
| 18 | + elif check_winner() is True: |
| 19 | + label.config(text=(players[0] + " wins")) |
| 20 | + |
| 21 | + elif check_winner() == "Tie": |
| 22 | + label.config(text=("Tie!")) |
| 23 | + else: |
| 24 | + |
| 25 | + buttons[row][column]['text'] = player |
| 26 | + if check_winner() is False: |
| 27 | + player = players[0] |
| 28 | + label.config(text=(players[0] + " turn")) |
| 29 | + |
| 30 | + elif check_winner() is True: |
| 31 | + label.config(text=(players[1] + " wins")) |
| 32 | + |
| 33 | + elif check_winner() == "Tie": |
| 34 | + label.config(text="Tie!") |
| 35 | + |
| 36 | + |
| 37 | +def check_winner(): |
| 38 | + for row in range(3): |
| 39 | + if buttons[row][0]['text'] == buttons[row][1]['text'] == buttons[row][2]['text'] != "": |
| 40 | + buttons[row][0].config(bg="light green") |
| 41 | + buttons[row][1].config(bg="light green") |
| 42 | + buttons[row][2].config(bg="light green") |
| 43 | + return True |
| 44 | + |
| 45 | + for column in range(3): |
| 46 | + if buttons[0][column]['text'] == buttons[1][column]['text'] == buttons[2][column]['text'] != "": |
| 47 | + buttons[0][column].config(bg="light green") |
| 48 | + buttons[1][column].config(bg="light green") |
| 49 | + buttons[2][column].config(bg="light green") |
| 50 | + return True |
| 51 | + |
| 52 | + if buttons[0][0]['text'] == buttons[1][1]['text'] == buttons[2][2]['text'] != "": |
| 53 | + buttons[0][0].config(bg="light green") |
| 54 | + buttons[1][1].config(bg="light green") |
| 55 | + buttons[2][2].config(bg="light green") |
| 56 | + return True |
| 57 | + |
| 58 | + elif buttons[0][2]['text'] == buttons[1][1]['text'] == buttons[2][0]['text'] != "": |
| 59 | + buttons[0][2].config(bg="light green") |
| 60 | + buttons[1][1].config(bg="light green") |
| 61 | + buttons[2][0].config(bg="light green") |
| 62 | + return True |
| 63 | + |
| 64 | + elif empty_spaces() is False: |
| 65 | + for row in range(3): |
| 66 | + for column in range(3): |
| 67 | + buttons[row][column].config(bg="yellow") |
| 68 | + return "Tie" |
| 69 | + |
| 70 | + else: |
| 71 | + return False |
| 72 | + |
| 73 | + |
| 74 | +def empty_spaces(): |
| 75 | + spaces = 9 |
| 76 | + for row in range(3): |
| 77 | + for column in range(3): |
| 78 | + if buttons[row][column]['text'] != "": |
| 79 | + spaces -= 1 |
| 80 | + if spaces == 0: |
| 81 | + return False |
| 82 | + else: |
| 83 | + return True |
| 84 | + |
| 85 | + |
| 86 | +def new_game(): |
| 87 | + global player |
| 88 | + |
| 89 | + player = random.choice(players) |
| 90 | + label.config(text=player + " turn") |
| 91 | + for row in range(3): |
| 92 | + for column in range(3): |
| 93 | + buttons[row][column].config(text="", bg="#F0F0F0") |
| 94 | + |
| 95 | + |
| 96 | +windows = Tk() |
| 97 | +players = ["x", "o"] |
| 98 | +player = random.choice(players) |
| 99 | +buttons = [[0, 0, 0], |
| 100 | + [0, 0, 0], |
| 101 | + [0, 0, 0]] |
| 102 | + |
| 103 | +label = Label(text=player + " turn", font=('bell mt', 40)) |
| 104 | +label.pack(side="top") |
| 105 | + |
| 106 | +reset_button = Button(text="Restart", font=('bell mt', 20), command=new_game) |
| 107 | +reset_button.pack(side="top") |
| 108 | + |
| 109 | +frame = Frame(windows) |
| 110 | +frame.pack() |
| 111 | + |
| 112 | +for row in range(3): |
| 113 | + for column in range(3): |
| 114 | + buttons[row][column] = Button(frame, text="", width=5, height=2, font=('ink free', 25), |
| 115 | + command=lambda row=row, column=column: next_turn(row, column)) |
| 116 | + buttons[row][column].grid(row=row, column=column) |
| 117 | + |
| 118 | +windows.mainloop() |
0 commit comments