-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.gd
283 lines (249 loc) · 8.7 KB
/
board.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
extends Sprite
var Piece = preload("res://piece.tscn")
var Hexagon = preload("res://hexagon.tscn")
const Invalid = Vector2(42, 42)
var current_selection = Invalid # currently selected position, or Invalid
var step = 0 # 0 -> White to move, ..., 2 -> Black, ..., n -> colors[n%3]
var step_list = [] # note that a step is a third of a move
var escape_time = false
var liberation_move = [Invalid, Invalid] # from, to
# TODO: build singleton "Chess" holding colors and pawn types
enum {
White = 0, Grey = 1, Black = 2
}
func _ready():
# setup clickable hexagons
var board_hexagons = Moves.disk(Vector2(), 7)
for h in board_hexagons:
var hex = Hexagon.instance()
hex.place(h)
hex.connect("hexagon_clicked", self, "on_hexagon_clicked", [h])
hex.add_to_group("hexagons")
add_child(hex)
reset() # setup pieces
print("Shortcuts:")
print(" - 'S' saves the game to a file")
print(" - 'L' loads the saved game `replay.trx` (if it exists)")
print(" - 'U' takes back the last move")
print(" - 'R' resets the board")
func _input(event):
if event is InputEventKey and event.pressed:
if event.scancode == KEY_S:
var date = OS.get_datetime()
var fname = str(date["day"])+"-"
fname += str(date["month"])+"-"
fname += str(date["year"])+"-"
fname += str(date["minute"])+".trx"
if Loader.save_game(fname, step_list):
print("Game saved to file `%s`." % fname)
else:
print("Failed to save the game to `%s`.", fname)
if event.scancode == KEY_R:
print("New game.")
reset()
if event.scancode == KEY_U:
print("Undoing step ", step, " (move ", int(step/3)+1, ")")
undo_step()
if event.scancode == KEY_L:
var steps = Loader.load_game("replay.trx")
if not steps.empty():
print("Loading `replay.trx`.")
replay(steps)
else:
print("Failed to load `replay.trx`.")
func reset():
var piece = Piece.instance() # used only to access piece types
var start_hexs = [Vector2(-5, 5), Vector2(-6, 6), Vector2(-7, 7)]
var types_by_line = [
[ piece.Pawn, piece.Pawn, piece.Pawn,
piece.Pawn, piece.Pawn, piece.Pawn ],
[ piece.Pawn, piece.Bishop, piece.Lanceman, piece.Bishop,
piece.Lanceman, piece.Bishop, piece.Pawn ],
[ piece.Canon, piece.Rook, piece.Knight, piece.King,
piece.Queen, piece.Knight, piece.Rook, piece.Canon ]
]
for p in get_tree().get_nodes_in_group("pieces"):
p.queue_free()
for p in get_tree().get_nodes_in_group("jailed_pieces"):
p.queue_free()
current_selection = Invalid
step = 0
step_list = []
escape_time = false
liberation_move = [Invalid, Invalid]
# TODO: reset highlighting
for rot in [0, 120, 240]:
for i in range(3):
var color = [piece.White, piece.Grey, piece.Black][rot / 120]
var types = types_by_line[i]
var start = Moves.rotate(start_hexs[i], rot)
var dir = Moves.rotate(Moves.axial_direction(Moves.Lines.E), rot)
var line = Moves.line_from(start, dir, types.size())
for j in range(types.size()):
add_piece_at(line[j], types[j], color)
func replay(step_sequence):
reset()
yield(get_tree(), "idle_frame") # wait for the new generation of pawns
for move in step_sequence:
piece_move(move[0], move[1])
if escape_time:
liberate_piece(move[2])
func undo_step():
if step_list.empty():
return
step_list.pop_back()
replay(step_list)
func color_to_move():
return [White, Grey, Black][step % 3]
func piece_move(h1, h2):
"""Moves piece at h1 to h2, eating an enemy piece if necessary.
This functions also handles jailing eaten pieces and recording moves."""
var jails = [ # the index is the color
[Vector2(7, 2), Moves.axial_direction(Moves.Lines.SSW)],
[Vector2(9, -7), Moves.axial_direction(Moves.Lines.SSE)],
[Vector2(-9, 0), Moves.axial_direction(Moves.Lines.NNE)] ]
var liberating_lines = [
Moves.line_from(Vector2(0, -7), Moves.axial_direction(Moves.Lines.E)),
Moves.line_from(Vector2(-7, 0), Moves.axial_direction(Moves.Lines.SSE)),
Moves.line_from(Vector2(0, 7), Moves.axial_direction(Moves.Lines.NNE)),
]
var eater = piece_at(h1)
var eaten = piece_at(h2)
if eaten:
var jail_name = "jail"+str(eaten.color)
var place_in_jail = get_tree().get_nodes_in_group(jail_name).size()
var h = jails[eaten.color][0] + place_in_jail*jails[eaten.color][1]
var hexagon = Hexagon.instance()
hexagon.place(h)
hexagon.add_to_group("jail_hexagons")
hexagon.connect("hexagon_clicked", self, "on_jailed_clicked", [hexagon])
add_child(hexagon)
eaten.add_to_group(jail_name)
eaten.add_to_group("jailed_pieces")
eaten.remove_from_group("pieces")
eaten.move(h)
eater.move(h2)
# check for possible liberation: needs at least one non-pawn jailed piece
if eater.type == eater.Pawn and eater.hex_pos in liberating_lines[eater.color]:
for jailed in get_tree().get_nodes_in_group("jailed_pieces"):
if jailed.color == color_to_move() and jailed.type != jailed.Pawn:
escape_time = true
liberation_move[0] = h1
liberation_move[1] = h2
return
step += 1
step_list.append([h1, h2])
func add_piece_at(h, type, color):
var piece = Piece.instance()
piece.place(h, type, color)
piece.add_to_group("pieces")
add_child(piece)
func piece_at(h):
for piece in get_tree().get_nodes_in_group("pieces"):
if piece.hex_pos == h:
return piece
return null
func highlight_hexagon(h, enable):
for hex in get_tree().get_nodes_in_group("hexagons"):
if hex.hex_pos == h:
hex.set_highlight(enable)
return
func highlight_possible_moves(h, enable=true):
var piece = piece_at(h)
if not piece:
return
var possible_moves = piece.possible_moves()
for move in possible_moves:
highlight_hexagon(move, enable)
func is_in_check(color):
var king_h = Vector2()
for piece in get_tree().get_nodes_in_group("pieces"):
if piece.type == piece.King and piece.color == color:
king_h = piece.hex_pos
for piece in get_tree().get_nodes_in_group("pieces"):
if piece.color != color and king_h in piece.possible_moves(false):
return true
return false
func move_checks_color(h1, h2, color):
"""Returns whether the h1->h2 move would put `color` in check."""
var moving_piece = piece_at(h1)
var other_piece = piece_at(h2)
moving_piece.move(h2)
if other_piece:
other_piece.remove_from_group("pieces")
var res = is_in_check(color)
moving_piece.move(h1)
if other_piece:
other_piece.add_to_group("pieces")
return res
func is_in_checkmate(color):
# `color` is in checkmate if no piece can move to save their attacked king
if not is_in_check(color):
return false
for piece in get_tree().get_nodes_in_group("pieces"):
if piece.color == color and not piece.possible_moves().empty():
return false
return true
func on_hexagon_clicked(hex_pos):
if escape_time:
return
var piece = piece_at(hex_pos)
var previous = piece_at(current_selection)
var playing_color = color_to_move()
# warning: organigram strongly recommanded before modifying
# warning: order of operations (highlighting, moves) matters here
# TODO: simplify the (highlighting, selected) coupling
if piece:
if piece.color != color_to_move():
if previous != null:
if previous.can_move(piece.hex_pos):
highlight_possible_moves(previous.hex_pos, false)
current_selection = Invalid
piece_move(previous.hex_pos, piece.hex_pos)
highlight_possible_moves(previous.hex_pos, false)
current_selection = Invalid
else:
if previous != null:
if previous.hex_pos == piece.hex_pos:
highlight_possible_moves(previous.hex_pos, false)
current_selection = Invalid
else:
highlight_possible_moves(previous.hex_pos, false)
highlight_possible_moves(piece.hex_pos)
current_selection = piece.hex_pos
else:
highlight_possible_moves(piece.hex_pos)
current_selection = piece.hex_pos
else: # empty hexagon clicked
if previous != null:
# remove highlight _before_ losing old position
highlight_possible_moves(previous.hex_pos, false)
current_selection = Invalid
if previous.can_move(hex_pos):
piece_move(previous.hex_pos, hex_pos)
var possibly_checked = [White, Grey, Black]
possibly_checked.erase(playing_color)
for color in possibly_checked:
if is_in_checkmate(color):
print("Color ", color, " has lost. Winner is ", playing_color)
func on_jailed_clicked(hexagon):
if not escape_time:
return
liberate_piece(hexagon.hex_pos)
hexagon.remove_from_group("jail_hexagons")
hexagon.queue_free()
func liberate_piece(h):
for piece in get_tree().get_nodes_in_group("jailed_pieces"):
if piece.hex_pos == h and piece.color == color_to_move():
if piece.type == piece.Pawn:
return
step += 1
step_list.append([liberation_move[0], liberation_move[1], h])
piece_at(liberation_move[1]).queue_free()
piece.remove_from_group("jailed_pieces")
piece.remove_from_group("jail"+str(piece.color))
piece.add_to_group("pieces")
piece.move(liberation_move[1])
escape_time = false
liberation_move = [Invalid, Invalid]
return