-
Notifications
You must be signed in to change notification settings - Fork 1
/
10_tic_tac_toe.py
108 lines (77 loc) · 3.46 KB
/
10_tic_tac_toe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Tic-Tac-Toe
from guizero import App, Box, PushButton, Text
def clear_board():
new_board = [[None, None, None],
[None, None, None],
[None, None, None]]
for x in range(0, 3, 1):
for y in range(0, 3, 1):
button = PushButton(master = board, text = "", grid = [x, y], width = 3, command = choose_square, args = [x, y])
# button.bg = "orange"
new_board[x][y] = button
return new_board
def choose_square(x, y):
board_square[x][y].text = turn
board_square[x][y].disable() # Disabling the button click after clicking once.
toggle_player()
check_win()
def toggle_player():
global turn
if turn == "X":
turn = "O"
else:
turn = "X"
message.value = "It is your turn, " + turn
def check_win():
winner = None
# Vertical lines --> |
if (board_square[0][0].text == board_square[0][1].text == board_square[0][2].text) and (board_square[0][2].text in ["X", "O"]):
winner = board_square[0][0]
# This condition check the text "X" and "O" correctly.
elif (board_square[1][0].text == board_square[1][1].text == board_square[1][2].text) and (board_square[1][2].text in ["X", "O"]):
winner = board_square[1][0]
elif (board_square[2][0].text == board_square[2][1].text == board_square[2][2].text) and board_square[2][2].text in ["X", "O"]:
winner = board_square[2][0]
# Horizontal lines --> -- # This condition and board_square[2][0].text in ["X", "O"] is indeed needed.
elif (board_square[0][0].text == board_square[1][0].text == board_square[2][0].text) and board_square[2][0].text in ["X", "O"]:
winner = board_square[0][0]
elif (board_square[0][1].text == board_square[1][1].text == board_square[2][1].text) and board_square[2][1].text in ["X", "O"]:
winner = board_square[0][1]
elif (board_square[0][2].text == board_square[1][2].text == board_square[2][2].text) and board_square[2][2].text in ["X", "O"]:
winner = board_square[0][2]
# Diagonal lines
elif (board_square[0][0].text == board_square[1][1].text == board_square[2][2].text) and board_square[2][2].text in ["X", "O"]:
winner = board_square[0][0]
elif (board_square[2][0].text == board_square[1][1].text == board_square[0][2].text) and board_square[0][2].text in ["X", "O"]:
winner = board_square[2][0]
if winner is not None:
message.value = winner.text + " Wins!"
elif moves_taken() == 9:
message.value = "It is a Draw! Play again"
def moves_taken():
moves = 0
for row in board_square:
for col in row:
if (col.text == "X") or (col.text == "O"):
moves += 1
return moves
def reset():
global board_square, message
board_square = clear_board()
turn = "X"
app = App(title = "Tic Tac Toe")
big_title = Text(master = app, text = "👉Tic Tac Toe👈")
big_title.size = 16
board = Box(master = app, layout = "grid")
board.bg = "Orange"
board_square = clear_board()
message = Text(master = app, text = "It is your turn, " + turn)
message.text_color = "red"
reset_button = PushButton(master = app, command = reset, text = "Reset")
reset_button.bg = "green"
# message2 = Text(master = app, text = "I am testing the box grid layout")
# board2 = Box(master = app, layout = "grid")
# for i in range(0, 3, 1):
# for j in range(0, 3, 1):
# PushButton(master = board2, text = f"{i=}, {j=}", grid = [i, j], width = 3)
app.display()