-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
167 lines (128 loc) · 3.72 KB
/
runner.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import gui
import sys
import time
import tictactoe as ttt
game = gui.GUI()
board = ttt.initial_state()
ai_turn = False
user = None
tiles = []
def menu() -> None:
try:
run()
except KeyboardInterrupt:
sys.exit("Game exited.")
def run() -> None:
while True:
for event in game.events():
if game.quit(event):
sys.exit()
game.background(gui.BLACK)
draw_home() if user is None else draw_game()
game.show()
def draw_home() -> None:
title = gui.Title("Play Tic-Tac-Toe")
title.align((gui.HORIZONTAL, 50))
title.draw(game.screen)
# Draw buttons
left = gui.WIDTH / 8
top = gui.HEIGHT / 2
width = gui.WIDTH / 4
height = 50
button_x = gui.Button("Play as X", left, top, width, height)
button_x.onclick(set_user)
button_x.draw(game.screen)
left = button_x.left * 5
button_o = gui.Button("Play as O", left, top, width, height)
button_o.onclick(set_user)
button_o.draw(game.screen)
if game.mouse_pressed():
button_x.handle_click(ttt.X)
button_o.handle_click(ttt.O)
def draw_game() -> None:
draw_board()
player = ttt.player(board)
game_over = ttt.terminal(board)
status = game_status(game_over, player)
title = gui.Title(status)
title.align((gui.HORIZONTAL, 30))
title.draw(game.screen)
check_ai_turn(game_over, player)
if game_over:
draw_reset_game_button()
if game.mouse_pressed():
check_user_turn(game_over, player)
def draw_board() -> None:
global tiles
tile_size = 80
origin = [(gui.WIDTH / 2) - (1.5 * tile_size),
(gui.HEIGHT / 2) - (1.5 * tile_size)]
for i in range(3):
row = []
for j in range(3):
button = gui.Button(
text=board[i][j],
left=origin[0] + j * tile_size,
top=origin[1] + i * tile_size,
width=tile_size,
height=tile_size,
color=gui.WHITE
)
button.onclick(user_move)
button.draw(game.screen, width=3)
row.append(button)
tiles.append(row)
def game_status(game_over: bool, player: str) -> str:
if game_over:
winner = ttt.winner(board)
if winner is None:
return "Game Over: Tie."
else:
return f"Game Over: {winner} wins."
elif user == player:
return f"Play as {user}"
else:
return "Computer thinking..."
def check_ai_turn(game_over: bool, player: str) -> None:
global ai_turn, board
if not game_over and user != player:
if ai_turn:
time.sleep(0.5)
move = ttt.minimax(board)
board = ttt.result(board, move)
ai_turn = False
else:
ai_turn = True
def check_user_turn(game_over: bool, player: str) -> None:
if not game_over and user == player:
for i in range(3):
for j in range(3):
button = tiles[i][j]
button.handle_click(i, j)
def draw_reset_game_button():
button = gui.Button(
text="Play Again",
left=gui.WIDTH / 3,
top=gui.HEIGHT - 65,
width=gui.WIDTH / 3,
height=50
)
button.onclick(reset_game)
button.draw(game.screen)
if game.mouse_pressed():
button.handle_click()
def set_user(player: str) -> None:
global user
user = player
def user_move(i: int, j: int) -> None:
global board
if board[i][j] == ttt.EMPTY:
board = ttt.result(board, (i, j))
def reset_game() -> None:
time.sleep(0.2)
global board, ai_turn, user
board = ttt.initial_state()
ai_turn = False
user = None
if __name__ == "__main__":
menu()