|
1 | 1 | from tkinter import * |
2 | | -import tkinter.messagebox |
| 2 | +import tkinter.messagebox as messagebox |
3 | 3 |
|
4 | 4 | tk = Tk() |
5 | 5 | tk.title("Tic Tac Toe") |
6 | 6 | tk.configure(bg='yellow') |
7 | 7 |
|
| 8 | +# Player names |
8 | 9 | p1 = StringVar() |
9 | 10 | p2 = StringVar() |
10 | 11 |
|
11 | | -player1_name = Entry(textvariable=p1, bd=5, bg='white', width=40) |
| 12 | +player1_name = Entry(tk, textvariable=p1, bd=5, bg='white', width=40) |
12 | 13 | player1_name.grid(row=1, column=1, columnspan=8) |
13 | 14 |
|
14 | 15 | player2_name = Entry(tk, textvariable=p2, bd=5, bg='white', width=40) |
15 | 16 | player2_name.grid(row=2, column=1, columnspan=8) |
16 | 17 |
|
17 | | -bclick = True |
18 | | -flag = 0 |
19 | | -current_player_name = p1.get() if p1.get() else 'X' |
20 | | - |
21 | | - |
22 | | -def disableButton(): |
23 | | - for i in range(3): |
24 | | - for j in range(3): |
25 | | - buttons[i][j].configure(state=DISABLED) |
26 | | - |
27 | | - |
28 | | -def checkForWin(): |
29 | | - for i in range(3): |
30 | | - if buttons[i][0]["text"] == buttons[i][1]["text"] == buttons[i][2]["text"] != " ": |
31 | | - buttons[i][0].config(bg="green") |
32 | | - buttons[i][1].config(bg="green") |
33 | | - buttons[i][2].config(bg="green") |
34 | | - disableButton() |
35 | | - winner_name = p1.get( |
36 | | - ) if buttons[i][0]['text'] == 'X' else p2.get() |
37 | | - if not winner_name: |
38 | | - winner_name = 'Player 1' if buttons[i][0]['text'] == 'X' else 'Player 2' |
39 | | - tkinter.messagebox.showinfo("Tic-Tac-Toe", f"{winner_name} wins!") |
| 18 | +# Score tracking |
| 19 | +score_p1 = 0 |
| 20 | +score_p2 = 0 |
| 21 | + |
| 22 | +current_player = "X" |
| 23 | +moves_count = 0 |
| 24 | + |
| 25 | + |
| 26 | +def update_scoreboard(): |
| 27 | + score_label.config( |
| 28 | + text=f"{p1.get() or 'Player 1'} (X): {score_p1} | {p2.get() or 'Player 2'} (O): {score_p2}" |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def disable_buttons(): |
| 33 | + for row in buttons: |
| 34 | + for button in row: |
| 35 | + button.config(state=DISABLED) |
| 36 | + |
| 37 | + |
| 38 | +def check_winner(): |
| 39 | + global score_p1, score_p2 |
| 40 | + |
| 41 | + win_positions = [ |
| 42 | + [(0, 0), (0, 1), (0, 2)], |
| 43 | + [(1, 0), (1, 1), (1, 2)], |
| 44 | + [(2, 0), (2, 1), (2, 2)], |
| 45 | + [(0, 0), (1, 0), (2, 0)], |
| 46 | + [(0, 1), (1, 1), (2, 1)], |
| 47 | + [(0, 2), (1, 2), (2, 2)], |
| 48 | + [(0, 0), (1, 1), (2, 2)], |
| 49 | + [(0, 2), (1, 1), (2, 0)] |
| 50 | + ] |
| 51 | + |
| 52 | + for combo in win_positions: |
| 53 | + if buttons[combo[0][0]][combo[0][1]]["text"] == \ |
| 54 | + buttons[combo[1][0]][combo[1][1]]["text"] == \ |
| 55 | + buttons[combo[2][0]][combo[2][1]]["text"] != " ": |
| 56 | + |
| 57 | + winner_symbol = buttons[combo[0][0]][combo[0][1]]["text"] |
| 58 | + |
| 59 | + if winner_symbol == "X": |
| 60 | + score_p1 += 1 |
| 61 | + winner_name = p1.get() or "Player 1" |
| 62 | + else: |
| 63 | + score_p2 += 1 |
| 64 | + winner_name = p2.get() or "Player 2" |
| 65 | + |
| 66 | + update_scoreboard() |
| 67 | + disable_buttons() |
| 68 | + messagebox.showinfo("Winner", f"{winner_name} wins!") |
| 69 | + return True |
| 70 | + |
| 71 | + return False |
| 72 | + |
| 73 | + |
| 74 | +def button_click(row, col): |
| 75 | + global current_player, moves_count |
| 76 | + |
| 77 | + if buttons[row][col]["text"] == " ": |
| 78 | + buttons[row][col]["text"] = current_player |
| 79 | + moves_count += 1 |
| 80 | + |
| 81 | + if check_winner(): |
40 | 82 | return |
41 | | - if buttons[0][i]["text"] == buttons[1][i]["text"] == buttons[2][i]["text"] != " ": |
42 | | - buttons[0][i].config(bg="green") |
43 | | - buttons[1][i].config(bg="green") |
44 | | - buttons[2][i].config(bg="green") |
45 | | - disableButton() |
46 | | - winner_name = p1.get( |
47 | | - ) if buttons[0][i]['text'] == 'X' else p2.get() |
48 | | - if not winner_name: |
49 | | - winner_name = 'Player 1' if buttons[0][i]['text'] == 'X' else 'Player 2' |
50 | | - tkinter.messagebox.showinfo("Tic-Tac-Toe", f"{winner_name} wins!") |
| 83 | + |
| 84 | + if moves_count == 9: |
| 85 | + messagebox.showinfo("Tie", "It's a Tie!") |
51 | 86 | return |
52 | | - if buttons[0][0]["text"] == buttons[1][1]["text"] == buttons[2][2]["text"] != " ": |
53 | | - buttons[0][0].config(bg="green") |
54 | | - buttons[1][1].config(bg="green") |
55 | | - buttons[2][2].config(bg="green") |
56 | | - disableButton() |
57 | | - winner_name = p1.get() if buttons[0][0]['text'] == 'X' else p2.get() |
58 | | - if not winner_name: |
59 | | - winner_name = 'Player 1' if buttons[0][0]['text'] == 'X' else 'Player 2' |
60 | | - tkinter.messagebox.showinfo("Tic-Tac-Toe", f"{winner_name} wins!") |
61 | | - return |
62 | | - if buttons[0][2]["text"] == buttons[1][1]["text"] == buttons[2][0]["text"] != " ": |
63 | | - buttons[0][2].config(bg="green") |
64 | | - buttons[1][1].config(bg="green") |
65 | | - buttons[2][0].config(bg="green") |
66 | | - disableButton() |
67 | | - winner_name = p1.get() if buttons[0][2]['text'] == 'X' else p2.get() |
68 | | - if not winner_name: |
69 | | - winner_name = 'Player 1' if buttons[0][2]['text'] == 'X' else 'Player 2' |
70 | | - tkinter.messagebox.showinfo("Tic-Tac-Toe", f"{winner_name} wins!") |
71 | | - return |
72 | | - if flag == 8: |
73 | | - tkinter.messagebox.showinfo("Tic-Tac-Toe", "It's a Tie") |
74 | | - |
75 | | - |
76 | | -def resetGame(): |
77 | | - global bclick, flag, current_player_name |
78 | | - for i in range(3): |
79 | | - for j in range(3): |
80 | | - buttons[i][j]["text"] = " " |
81 | | - buttons[i][j].config(bg='black', state=NORMAL) |
82 | | - bclick = True |
83 | | - flag = 0 |
84 | | - current_player_name = p1.get() if p1.get() else 'X' |
85 | | - |
86 | | - |
87 | | -Label(tk, text="Player 1:", font='Times 20 bold', bg='yellow', |
88 | | - fg='black', height=1, width=8).grid(row=1, column=0) |
89 | | -Label(tk, text="Player 2:", font='Times 20 bold', bg='yellow', |
90 | | - fg='black', height=1, width=8).grid(row=2, column=0) |
91 | | -buttons = [[Button(tk, text=' ', font='Times 20 bold', bg='black', |
92 | | - fg='white', height=4, width=8) for _ in range(3)] for _ in range(3)] |
93 | | - |
94 | | - |
95 | | -def btnClick(button): |
96 | | - global bclick, flag |
97 | | - if button["text"] == " ": |
98 | | - if bclick: |
99 | | - button["text"] = "X" |
100 | | - else: |
101 | | - button["text"] = "O" |
102 | | - bclick = not bclick |
103 | | - flag += 1 |
104 | | - checkForWin() |
| 87 | + |
| 88 | + current_player = "O" if current_player == "X" else "X" |
105 | 89 | else: |
106 | | - tkinter.messagebox.showinfo("Tic-Tac-Toe", "Button already Clicked!") |
| 90 | + messagebox.showinfo("Invalid Move", "Button already clicked!") |
| 91 | + |
| 92 | + |
| 93 | +def reset_game(): |
| 94 | + global current_player, moves_count |
| 95 | + current_player = "X" |
| 96 | + moves_count = 0 |
107 | 97 |
|
| 98 | + for row in buttons: |
| 99 | + for button in row: |
| 100 | + button.config(text=" ", state=NORMAL) |
| 101 | + |
| 102 | + |
| 103 | +Label(tk, text="Player 1:", font='Times 20 bold', bg='yellow').grid(row=1, column=0) |
| 104 | +Label(tk, text="Player 2:", font='Times 20 bold', bg='yellow').grid(row=2, column=0) |
| 105 | + |
| 106 | +buttons = [[Button(tk, text=" ", font='Times 20 bold', bg='black', |
| 107 | + fg='white', height=4, width=8, |
| 108 | + command=lambda r=i, c=j: button_click(r, c)) |
| 109 | + for j in range(3)] for i in range(3)] |
108 | 110 |
|
109 | 111 | for i in range(3): |
110 | 112 | for j in range(3): |
111 | | - buttons[i][j].configure(command=lambda row=i, |
112 | | - col=j: btnClick(buttons[row][col])) |
113 | 113 | buttons[i][j].grid(row=i + 3, column=j) |
114 | | -reset_button = Button(tk, text="Reset Game", font='Times 16 bold', |
115 | | - bg='white', fg='black', command=resetGame) |
116 | | -reset_button.grid(row=6, column=0, columnspan=3) |
| 114 | + |
| 115 | +score_label = Label(tk, text="", font='Times 14 bold', bg='yellow') |
| 116 | +score_label.grid(row=6, column=0, columnspan=3) |
| 117 | + |
| 118 | +update_scoreboard() |
| 119 | + |
| 120 | +reset_btn = Button(tk, text="Reset Game", font='Times 16 bold', |
| 121 | + command=reset_game) |
| 122 | +reset_btn.grid(row=7, column=0, columnspan=3) |
| 123 | + |
117 | 124 | tk.mainloop() |
0 commit comments