-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameGUI.py
328 lines (278 loc) · 11.5 KB
/
GameGUI.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
from __future__ import annotations
import customtkinter as ctk
from Games import GameState
from GamePlayingAgent import Player, Agent, Human
import threading
import time
class AIPlayingGUI():
event = threading.Event()
def __init__(self, gamestate: GameState = None, playerOne: Player = None,
playerTwo: Player = None):
self.gamestate = gamestate
self.playerOne = playerOne
self.playerTwo = playerTwo
self.gameMutex = False
self.loading = False
self.slowTime = False
self.playerOneMax = True
self.__initGUI()
def __initGUI(self):
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
self.windowWidth = 800
self.windowHeight = 800
self.root = ctk.CTk()
self.root.resizable(False, False)
self.root.title("Game AI Player")
self.root.geometry(f"{self.windowWidth}x{self.windowHeight}")
self.frame = ctk.CTkFrame(master=self.root)
self.frame.pack(pady=20, padx=60, fill="both", expand=True)
self.buttonPanel = ctk.CTkFrame(master=self.root)
self.buttonPanel.pack(pady=10, padx=60, fill="both", expand=True,
anchor='s')
self.statusText = ctk.CTkLabel(master=self.buttonPanel,
text="")
self.statusText.pack(anchor='n')
self.playerOneFrame = ctk.CTkFrame(master=self.buttonPanel,
width=200,
height=200)
self.playerOneLabel = ctk.CTkLabel(master=self.playerOneFrame,
text="Player X",
width=200)
self.playerOneLabel.pack()
options = ['Baseline', 'Minimax', 'Alpha Beta Pruning', 'Dynamic']
if type(self.playerOne) == Agent:
self.ai1SelectedMethod = ctk.StringVar(master=self.playerOneFrame,
value="Baseline")
self.ai1MethodCombo = ctk.CTkComboBox(
master=self.playerOneFrame,
values=options,
width=200,
variable=self.ai1SelectedMethod,
state="readonly")
self.ai1MethodCombo.pack()
def setAIOneDepth(slider):
self.playerOne.depth = int(slider)
self.ai1DepthLabel.configure(text="Depth: = "
f"{int(slider)}")
self.ai1DepthSlider = ctk.CTkSlider(
master=self.playerOneFrame,
from_=1,
to=10,
number_of_steps=9,
command=setAIOneDepth)
self.ai1DepthLabel = ctk.CTkLabel(
master=self.playerOneFrame,
text="Depth: = "
f"{int(self.ai1DepthSlider.get())}")
self.ai1DepthSlider.pack()
self.ai1DepthLabel.pack()
self.playerOneFrame.pack(pady=10, side='top', anchor='w')
self.playerOneFrame.pack(padx=10, side='left', anchor='w')
self.playerTwoFrame = ctk.CTkFrame(master=self.buttonPanel,
width=200,
height=200)
self.playerTwoLabel = ctk.CTkLabel(master=self.playerTwoFrame,
text="Player O",
width=200)
self.playerTwoLabel.pack()
if type(self.playerTwo) == Agent:
self.ai2SelectedMethod = ctk.StringVar(
master=self.playerTwoFrame,
value="Baseline")
self.ai2MethodCombo = ctk.CTkComboBox(
master=self.playerTwoFrame,
values=options,
width=200,
variable=self.ai2SelectedMethod,
state="readonly")
self.ai2MethodCombo.pack()
def setAITwoDepth(slider):
self.playerTwo.depth = int(slider)
self.ai2DepthLabel.configure(text="Depth: = "
f"{int(slider)}")
self.ai2DepthSlider = ctk.CTkSlider(
master=self.playerTwoFrame,
from_=1,
to=10,
number_of_steps=9,
command=setAITwoDepth)
self.ai2DepthLabel = ctk.CTkLabel(
master=self.playerTwoFrame,
text="Depth: = "
f"{int(self.ai2DepthSlider.get())}")
self.ai2DepthSlider.pack()
self.ai2DepthLabel.pack()
self.playerTwoFrame.pack(pady=10, side='top', anchor='e')
self.playerTwoFrame.pack(padx=10, side='right', anchor='e')
self.startingPlayer = ctk.StringVar(value='X')
# Human player augmentation Hook
self._humanPlayerOptionsGUIHook()
def changeStartingPlayer():
if self.gameMutex is False:
self.gamestate.move = self.startingPlayer.get()
self.startMoveAIOne = ctk.CTkRadioButton(
master=self.playerOneFrame,
variable=self.startingPlayer,
value='X',
text="Starting",
command=changeStartingPlayer)
self.startMoveAITwo = ctk.CTkRadioButton(
master=self.playerTwoFrame,
variable=self.startingPlayer,
value='O',
text="Starting",
command=changeStartingPlayer)
self.startMoveAIOne.pack()
self.startMoveAITwo.pack()
self.startButton = ctk.CTkButton(
master=self.buttonPanel,
text="Start!",
width=80,
height=80,
command=self.play)
self.startButton.pack(pady=5)
self.resetButton = ctk.CTkButton(
master=self.buttonPanel,
text="Reset",
width=80,
command=self.reset)
self.resetButton.pack(pady=5)
def slowTime():
self.slowTime = self.slowTimeCheckBox.get()
self.slowTimeCheckBox = ctk.CTkCheckBox(master=self.buttonPanel,
width=80,
text="Slow Time",
command=slowTime)
self.slowTimeCheckBox.pack()
def hasHumanPlayer(self):
return type(self.playerOne) == Human or type(self.playerTwo) == Human
def display(self):
pass
def _humanPlayerOptionsGUIHook(self):
pass
def _play(self):
self.gameMutex = True
def getMode():
methods = ['baseline', 'minimax', 'alphabeta', 'dynamic']
aiOneMode = None
if type(self.playerOne) == Agent:
aiOneModeStr = self.ai1SelectedMethod.get()
if aiOneModeStr == 'Baseline':
aiOneMode = methods[0]
elif aiOneModeStr == 'Minimax':
aiOneMode = methods[1]
elif aiOneModeStr == 'Alpha Beta Pruning':
aiOneMode = methods[2]
else:
aiOneMode = methods[3]
aiTwoMode = None
if type(self.playerTwo) == Agent:
aiTwoModeStr = self.ai2SelectedMethod.get()
if aiTwoModeStr == 'Baseline':
aiTwoMode = methods[0]
elif aiTwoModeStr == 'Minimax':
aiTwoMode = methods[1]
elif aiTwoModeStr == 'Alpha Beta Pruning':
aiTwoMode = methods[2]
else:
aiTwoMode = methods[3]
return (aiOneMode, aiTwoMode,)
def playersTurn(player: str):
def threadResult(result: dict, player: str):
"""
Acquire move via threading to prevent lagging GUI.
Unfortunately Python doesn't have true threading so
it still lags. But at least the GUI is responsive
"""
# This was the weirdest bit of code I have written
result['result'] = \
self.playerOne.getNextMove(
state=self.gamestate,
depth=self.playerOne.depth,
mode=getMode()[0]
) if player == "X" else \
self.playerTwo.getNextMove(
state=self.gamestate,
depth=self.playerTwo.depth,
mode=getMode()[1]
)
result = {'result': self.gamestate}
child = self.gamestate
if self.gamestate.isTerminal():
return self.gamestate
if player == "X" and type(self.playerOne) == Agent:
self.gamestate.move = 'O'
t = threading.Thread(
target=threadResult,
args=[result, player]
)
t.start()
t.join()
AIPlayingGUI.event.clear()
child = result['result']
elif player == "O" and type(self.playerTwo) == Agent:
self.gamestate.move = 'X'
t = threading.Thread(
target=threadResult,
args=[result, player]
)
t.start()
t.join()
AIPlayingGUI.event.clear()
child = result['result']
elif type(self.playerOne) == Human or \
type(self.playerTwo) == Human:
# Human player handler. Refer to descendant game GUI class
# for way to handle
if player == "X":
self.playerOne.turn = True
elif player == "O":
self.playerTwo.turn = True
# time.sleep(0.5)
return child
while not self.gamestate.isTerminal():
if self.slowTime or self.hasHumanPlayer():
if self.gamestate.move == 'X':
self.statusText.configure(text="Loading...\nPlayer X turn")
else:
self.statusText.configure(text="Loading...\nPlayer O turn")
if self.gamestate.move == 'X': # AI 1
if type(self.playerOne) == Human:
AIPlayingGUI.event.set()
child = playersTurn("X")
AIPlayingGUI.event.wait()
else:
child = playersTurn("X")
self.gamestate.board = child.board
else: # AI 2
if type(self.playerTwo) == Human:
AIPlayingGUI.event.set()
child = playersTurn("O")
AIPlayingGUI.event.wait()
else:
child = playersTurn("O")
self.gamestate.board = child.board
self.display()
if self.slowTime:
time.sleep(1)
self.display()
self.gameMutex = False
winner = self.gamestate.getWinner()
text = "Tie!"
if winner is not None:
text = f"{winner} has won!"
self.statusText.configure(text=text)
def play(self):
if self.gameMutex is False:
t = threading.Thread(target=self._play)
t.daemon = True
t.start()
self.loading = True
self.statusText.configure(text="Loading...")
def reset(self):
pass
def start(self):
self.root.mainloop()
def stop(self):
self.root.quit()