-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiece.rb
More file actions
414 lines (343 loc) · 7.66 KB
/
piece.rb
File metadata and controls
414 lines (343 loc) · 7.66 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
module Slideable
def moves_from(pos, diffs, board)
moves = []
diffs.each do |direction|
moves += moves_in_direction(pos, direction, board)
end
moves
end
def moves_in_direction(pos, direction, board)
curr_pos = pos.dup
drow, dcol = direction
moves = []
while Board.is_valid?(curr_pos)
case board.color_at(curr_pos)
when nil
moves << curr_pos.dup
when self.get_color
break unless curr_pos == pos
else
#p "adding because else-case, then breaking"
moves << curr_pos.dup
break
end
curr_pos[0] += drow
curr_pos[1] += dcol
end
# drops our piece's actual position
moves
end
end
module Steppable
def moves_from(pos, diffs, board)
moves = []
diffs.each do |direction|
new_pos = [pos[0] + direction[0], pos[1] + direction[1]]
moves << new_pos if Board.is_valid?(new_pos) && board.color_at(new_pos) != self.get_color
end
moves
end
end
module Pawnable
def moves_from(pos, diffs, board, moved)
moves = []
diffs.each do |move_type, diff|
new_pos = [pos[0] + diff[0], pos[1] + diff[1]]
next unless Board.is_valid?(new_pos)
case move_type
when :single_forward
moves << new_pos unless board.color_at(new_pos)
when :double_forward
single_diff = diffs[:single_forward]
single_forward = [pos[0] + single_diff[0], pos[1] + single_diff[1]]
cannot_move = moved || board.color_at(single_forward) || board.color_at(new_pos)
moves << new_pos unless cannot_move
when :left_capture, :right_capture
moves << new_pos if board.color_at(new_pos) == Board.opposite_color(self.get_color)
end
end
moves
end
end
module Castleable
def extra_moves(board)
moves = []
if can_castle?(board, @color, :kingside)
moves << (@color == :black ? [0, 6] : [7, 6])
end
if can_castle?(board, @color, :queenside)
moves << (@color == :black ? [0, 2] : [7, 2])
end
moves
end
def can_castle?(board, color, side)
king = board.get_piece(color, King)
rook_pos = []
rook_pos << (color == :white ? 7 : 0)
rook_pos << (side == :kingside ? 7 : 0)
rook = board[rook_pos]
squares_to_check = board.positions_between(rook_pos,king.get_position)
are_empty = squares_to_check.none? { |pos| board[pos].to_valid_piece }
opponent_pieces = board.get_pieces(Board.opposite_color(color)).reject { |piece| piece.is_a?(King)}
valid_moves_for_opponent = opponent_pieces.map { |piece| piece.moves }.flatten
are_safe = (squares_to_check & valid_moves_for_opponent).empty?
return false if king.moved? || !rook.is_a?(Rook) || rook.moved?
are_empty && are_safe
end
end
module Passantable
def extra_moves(board)
moves = []
curr_pos = get_position
left = [curr_pos[0], curr_pos[1] - 1]
right = [curr_pos[0], curr_pos[1] + 1]
en_passant_left = @color == :white ? [curr_pos[0] - 1, curr_pos[1] - 1] : [curr_pos[0] + 1, curr_pos[1] - 1]
en_passant_right = @color == :white ? [curr_pos[0] - 1, curr_pos[1] + 1] : [curr_pos[0] + 1, curr_pos[1] + 1]
moves << en_passant_left if can_en_passant?(board, left, @color)
moves << en_passant_right if can_en_passant?(board, right, @color)
moves
end
def can_en_passant?(board, pos, color)
return false unless Board.is_valid?(pos)
return false unless @board.color_at(pos) == Board.opposite_color(color)
enemy_pawn = @board.piece_at(pos)
return false unless enemy_pawn.is_a?(Pawn)
enemy_pawn.double_moved?
end
end
class Piece
attr_reader :color
def initialize(color,position,board)
@color = color
@position = position
@board = board
@icon = nil
end
def get_icon
@icon
end
def get_color
@color
end
def inspect
"#{color} #{self.class}"
end
def get_position
@position
end
def set_position(position)
@position = position
end
def moves
raise "No piece type"
end
def get_board
@board
end
def is_valid_move?(pos)
moves.include?(pos)
end
def dup
Piece.new(@color, @position, @board)
end
def to_valid_piece
self.class == EmptySquare ? nil : self
end
end
class Pawn < Piece
include Pawnable
include Passantable
def initialize(color, position, board)
super
@icon = color == :black ? "\u2659" : "\u265F"
@moved = false
if color == :black
@diffs = {
:single_forward => [1,0],
:double_forward => [2,0],
:left_capture => [1,-1],
:right_capture => [1,1]
}
else
@diffs = {
:single_forward => [-1,0],
:double_forward => [-2,0],
:left_capture => [-1,-1],
:right_capture => [-1,1]
}
end
end
def set_moved(moved)
@moved = moved
end
def set_double_moved(moved)
@double_moved = moved
end
def dup
pawn = Pawn.new(@color, @position, @board)
pawn.set_moved(@moved)
pawn.set_double_moved(@double_moved)
pawn
end
def moves
moves_from(@position, @diffs, @board, @moved) + extra_moves(@board)
end
def set_position(pos)
old_pos = get_position
super
@moved = true
@double_moved = (old_pos[0] - pos[0]).abs == 2
end
def double_moved?
@double_moved
end
end
class Rook < Piece
include Slideable
DIFFS = [
[1,0],
[-1,0],
[0,1],
[0,-1]
]
def initialize(color, position, board)
super
@icon = color == :black ? "\u2656" : "\u265C"
@moved = false
end
def set_moved(moved)
@moved = moved
end
def moves
moves_from(@position, DIFFS, @board)
end
def moved?
@moved
end
def dup
rook = Rook.new(@color, @position, @board)
rook.set_moved(@moved)
rook
end
def set_position(pos)
super
@moved = true
end
end
class Bishop < Piece
include Slideable
DIFFS = [
[1,1],
[1,-1],
[-1,1],
[-1,-1]
]
def initialize(color, position, board)
super
@icon = color == :black ? "\u2657" : "\u265D"
end
def moves
moves_from(@position, DIFFS, @board)
end
def dup
Bishop.new(@color, @position, @board)
end
end
class Knight < Piece
include Steppable
DIFFS = [
[1,2],
[1,-2],
[-1,2],
[-1,-2],
[-2,1],
[-2,-1],
[2,1],
[2,-1]
]
def initialize(color, position, board)
super
@icon = color == :black ? "\u2658" : "\u265E"
end
def moves
moves_from(@position, DIFFS, @board)
end
def dup
Knight.new(@color, @position, @board)
end
end
class Queen < Piece
include Slideable
DIFFS = [
[1,0],
[-1,0],
[0,1],
[0,-1],
[1,1],
[1,-1],
[-1,1],
[-1,-1]
]
def initialize(color, position, board)
super
@icon = color == :black ? "\u2655" : "\u265B"
end
def moves
moves_from(@position, DIFFS, @board)
end
def dup
Queen.new(@color, @position, @board)
end
end
class King < Piece
include Castleable
include Steppable
DIFFS = [
[1,0],
[-1,0],
[0,1],
[0,-1],
[1,1],
[1,-1],
[-1,1],
[-1,-1]
]
def initialize(color, position, board)
super
@icon = color == :black ? "\u2654" : "\u265A"
@moved = false
end
def moves
moves_from(@position, DIFFS, @board) + extra_moves(@board)
end
def set_moved(moved)
@moved = moved
end
def moved?
@moved
end
def dup
king = King.new(@color, @position, @board)
king.set_moved(@moved)
king
end
def set_position(position)
super
@moved = true
end
end
class EmptySquare < Piece
def initialize(color, position, board)
super
@icon = " "
end
def moves
[]
end
def inspect
"."
end
def dup
EmptySquare.new(@color, @position, @board)
end
end