-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.gd
336 lines (261 loc) · 9.49 KB
/
Game.gd
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
329
330
331
332
333
334
335
336
extends Node
var player_turn = "black"
func _get_player_turn():
return player_turn
func check_win_across_top(player_color):
# Check three spaces for player color
# top left
# top middle
# top right
var top_left = $Board.find_node('top_left').occupied_by
var top_middle = $Board.find_node('top_middle').occupied_by
var top_right = $Board.find_node('top_right').occupied_by
if (top_left == player_color and top_middle == player_color and top_right == player_color):
return true
else:
return false
func check_win_across_center(player_color):
# Check three spaces for player color
# center left
# center middle
# center right
var center_left = $Board.find_node('center_left').occupied_by
var center_middle = $Board.find_node('center_middle').occupied_by
var center_right = $Board.find_node('center_right').occupied_by
if (center_left == player_color and center_middle == player_color and center_right == player_color):
return true
else:
return false
func check_win_across_bottom(player_color):
# Check three spaces for player color
# bottom left
# bottom middle
# bottom right
var bottom_left = $Board.find_node('bottom_left').occupied_by
var bottom_middle = $Board.find_node('bottom_middle').occupied_by
var bottom_right = $Board.find_node('bottom_right').occupied_by
if (bottom_left == player_color and bottom_middle == player_color and bottom_right == player_color):
return true
else:
return false
func check_win_vertical_left(player_color):
# Check three spaces for player color
# top left
# center left
# bottom left
var top_left = $Board.find_node('top_left').occupied_by
var center_left = $Board.find_node('center_left').occupied_by
var bottom_left = $Board.find_node('bottom_left').occupied_by
if (top_left == player_color and center_left == player_color and bottom_left == player_color):
return true
else:
return false
func check_win_vertical_middle(player_color):
# Check three spaces for player color
# top middle
# center middle
# bottom middle
var top_middle = $Board.find_node('top_middle').occupied_by
var center_middle = $Board.find_node('center_middle').occupied_by
var bottom_middle = $Board.find_node('bottom_middle').occupied_by
if (top_middle == player_color and center_middle == player_color and bottom_middle == player_color):
return true
else:
return false
func check_win_vertical_right(player_color):
# Check three spaces for player color
# top right
# center right
# bottom right
var top_right = $Board.find_node('top_right').occupied_by
var center_right = $Board.find_node('center_right').occupied_by
var bottom_right = $Board.find_node('bottom_right').occupied_by
if (top_right == player_color and center_right == player_color and bottom_right == player_color):
return true
else:
return false
func check_win_diagonal_up(player_color):
# Check three spaces for player color
# bottom left
# center middle
# top right
var bottom_left = $Board.find_node('bottom_left').occupied_by
var center_middle = $Board.find_node('center_middle').occupied_by
var top_right = $Board.find_node('top_right').occupied_by
if (bottom_left == player_color and center_middle == player_color and top_right == player_color):
return true
else:
return false
func check_win_diagonal_down(player_color):
# Check three spaces for player color
# top left
# center middle
# bottom right
var top_left = $Board.find_node('top_left').occupied_by
var center_middle = $Board.find_node('center_middle').occupied_by
var bottom_right = $Board.find_node('bottom_right').occupied_by
if (top_left == player_color and center_middle == player_color and bottom_right == player_color):
return true
else:
return false
func check_winning_move_top_left(player_color):
# Possible winning combinations are
# across top
# vertical left
# diagonal down
var player_win = false
var across_top_win = check_win_across_top(player_color)
var vertical_left_win = check_win_vertical_left(player_color)
var diagonal_down_win = check_win_diagonal_down(player_color)
if (across_top_win or vertical_left_win or diagonal_down_win):
player_win = true
return player_win
func check_winning_move_top_middle(player_color):
# Possible winning combinations are
# across top
# vertical middle
var player_win = false
var across_top_win = check_win_across_top(player_color)
var vertical_middle_win = check_win_vertical_middle(player_color)
if (across_top_win or vertical_middle_win):
player_win = true
return player_win
func check_winning_move_top_right(player_color):
# Possible winning combinations are
# across top
# vertical right
# diagonal up
var player_win = false
var across_top_win = check_win_across_top(player_color)
var vertical_right_win = check_win_vertical_right(player_color)
var diagonal_up_win = check_win_diagonal_up(player_color)
if (across_top_win or vertical_right_win or diagonal_up_win):
player_win = true
return player_win
func check_winning_move_center_left(player_color):
# Possible winning combinations are
# across center
# vertical left
var player_win = false
var across_center_win = check_win_across_center(player_color)
var vertical_left_win = check_win_vertical_left(player_color)
if (across_center_win or vertical_left_win):
player_win = true
return player_win
func check_winning_move_center_middle(player_color):
# Possible winning combinations are
# across center
# vertical middle
# diagonal up
# diagonal down
var player_win = false
var across_center_win = check_win_across_center(player_color)
var vertical_middle_win = check_win_vertical_middle(player_color)
var diagonal_up_win = check_win_diagonal_up(player_color)
var diagonal_down_win = check_win_diagonal_down(player_color)
if (across_center_win or vertical_middle_win or diagonal_up_win or diagonal_down_win):
player_win = true
return player_win
func check_winning_move_center_right(player_color):
# Possible winning combinations are
# across center
# vertical right
var player_win = false
var across_center_win = check_win_across_center(player_color)
var vertical_right_win = check_win_vertical_right(player_color)
if (across_center_win or vertical_right_win):
player_win = true
return player_win
func check_winning_move_bottom_left(player_color):
# Possible winning combinations are
# across bottom
# vertical left
# diagonal up
var player_win = false
var across_bottom_win = check_win_across_bottom(player_color)
var vertical_left_win = check_win_vertical_left(player_color)
var diagonal_up_win = check_win_diagonal_up(player_color)
if (across_bottom_win or vertical_left_win or diagonal_up_win):
player_win = true
return player_win
func check_winning_move_bottom_middle(player_color):
# Possible winning combinations are
# across bottom
# vertical middle
var player_win = false
var across_bottom_win = check_win_across_bottom(player_color)
var vertical_middle_win = check_win_vertical_middle(player_color)
if (across_bottom_win or vertical_middle_win):
player_win = true
return player_win
func check_winning_move_bottom_right(player_color):
# Possible winning combinations are
# across bottom
# vertical right
# diagonal down
var player_win = false
var across_bottom_win = check_win_across_bottom(player_color)
var vertical_right_win = check_win_vertical_right(player_color)
var diagonal_down_win = check_win_diagonal_down(player_color)
if (across_bottom_win or vertical_right_win or diagonal_down_win):
player_win = true
return player_win
func check_winning_move(player_color, position):
# There are several possible win conditions
# Narrow the search down based on position
if position == 'top_left':
var player_win = check_winning_move_top_left(player_color)
if player_win:
print('Player ', player_color, ' wins!')
pass
elif position == 'top_middle':
var player_win = check_winning_move_top_middle(player_color)
if player_win:
print('Player ', player_color, ' wins!')
pass
elif position == 'top_right':
var player_win = check_winning_move_top_right(player_color)
if player_win:
print('Player ', player_color, ' wins!')
pass
elif position == 'center_left':
var player_win = check_winning_move_center_left(player_color)
if player_win:
print('Player ', player_color, ' wins!')
pass
elif position == 'center_middle':
var player_win = check_winning_move_center_middle(player_color)
if player_win:
print('Player ', player_color, ' wins!')
pass
elif position == 'center_right':
var player_win = check_winning_move_center_right(player_color)
if player_win:
print('Player ', player_color, ' wins!')
pass
elif position == 'bottom_left':
var player_win = check_winning_move_bottom_left(player_color)
if player_win:
print('Player ', player_color, ' wins!')
pass
elif position == 'bottom_middle':
var player_win = check_winning_move_bottom_middle(player_color)
if player_win:
print('Player ', player_color, ' wins!')
pass
elif position == 'bottom_right':
var player_win = check_winning_move_bottom_right(player_color)
if player_win:
print('Player ', player_color, ' wins!')
pass
func change_player_turn():
# Check current player turn
# and change to next player
if player_turn == "black":
player_turn = "white"
elif player_turn == "white":
player_turn = "black"
func _on_Board_player_moved(player_color, position):
check_winning_move(player_color, position)
# When a player moves, it is the other player's turn
change_player_turn()