-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.rb
528 lines (482 loc) · 13.8 KB
/
pieces.rb
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
class Piece
attr_accessor :color
attr_accessor :name
attr_accessor :icon
attr_accessor :square
attr_accessor :legal_moves
attr_accessor :moved
attr_accessor :checks
attr_accessor :check_blocks
attr_accessor :pinned
def initialize(name, color, square)
@name = name
@color = color
@square = square
@legal_moves = []
@moved = false
@checks = 0
@check_blocks = []
@pinned = {}
end
def reset
@legal_moves = []
@moved = false
@checks = 0
@check_blocks = []
@pinned = {}
end
def set_posn(x, y)
@icon.x = x + 5
@icon.y = y + 5
end
def move_to_square(square)
x_offset = 320
y_offset = 40
x_pos = ((square % 8) * 80) + x_offset
y_pos = ((square / 8.floor) * 80) + y_offset
set_posn(x_pos, y_pos)
end
def get_other_piece_info(piece)
if piece[0] == @color[0]
result = "own"
elsif piece[1] == "k"
result = "enemy_king"
else
result = "enemy"
end
end
def on_edge(square) # corners considered as separate cases, as have 2 'edges'
if square < 8
edge = 'N'
elsif square > 55
edge = 'S'
elsif square % 8 == 0
edge = 'W'
elsif (square + 1) % 8 == 0
edge = 'E'
else
edge = 'none'
end
end
def orthogonal_step(square, direction)
corners = {0 => 'NW', 7 => 'NE', 56 => 'SW', 63 => 'SE'}
if corners.key?(square) == true &&
(direction == corners[square][0] || direction == corners[square][1])
square = nil
else
edge = on_edge(square)
if direction == 'N' && edge != 'N'
square -= 8
elsif direction == 'S' && edge != 'S'
square += 8
elsif direction == 'E' && edge != 'E'
square += 1
elsif direction == 'W' && edge != 'W'
square -= 1
else
square = nil
end
end
square
end
def diagonal_step(square, direction)
corners = {0 => 'SE', 7 => 'SW', 56 => 'NE', 63 => 'NW'}
if corners.key?(square) == true && direction != corners[square]
square = nil
else
edge = on_edge(square)
if direction == 'NE' && edge != 'N' && edge != 'E'
square -= 7
elsif direction == 'SE' && edge != 'S' && edge != 'E'
square += 9
elsif direction == 'SW' && edge != 'S' && edge != 'W'
square += 7
elsif direction == 'NW' && edge != 'N' && edge != 'W'
square -= 9
else
square = nil
end
end
square
end
def common(array_a, array_b)
common = []
array_a.each do |a|
common << a if array_b.any? {|b| b == a}
end
common
end
def filter_pins
if @pinned != {}
pin_moves = @pinned[@name]
@legal_moves = common(@legal_moves, pin_moves)
end
end
def filter_checks_and_pins
if @check_blocks != []
@legal_moves = common(@legal_moves, @check_blocks)
end
filter_pins
end
end
class Sliding_Piece < Piece
def find_sliding_paths(posn, path_type)
moves = []
@disambiguate = []
if path_type == 'orthogonal'
directions = ['N', 'S', 'E', 'W']
else
directions = ['NE', 'SE', 'SW', 'NW']
end
directions.each do |direction|
square, start_square = @square, @square
loop do
if path_type == 'orthogonal'
new_square = orthogonal_step(square, direction)
else
new_square = diagonal_step(square, direction)
end
break if new_square == nil
if posn[new_square] != "---"
piece_type = get_other_piece_info(posn[new_square])
if piece_type == "own" || piece_type == "enemy_king"
if piece_type == "own" && posn[new_square][1] == posn[start_square][1]
@disambiguate << new_square
end
break
else
moves << (new_square)
break
end
end
moves << (new_square)
square = new_square
end
end
moves
end
end
class Pawn < Piece
attr_accessor :ep_square
attr_accessor :ep_take_sq
def initialize(name, color, square)
super
@icon = Image.new("img/#{@color[0]}_pawn.png", height: 70, width: 70)
@icon.z = -1
@ep_square = -1 # >= 0, == valid en-passant move square
@ep_take_sq = -1 # >= 0, == valid en-passant capture square
end
def find_moves(posn, moves = [])
@legal_moves = []
if @checks > 1
@legal_moves
else
if @color == 'white'
directions = ['N', 'NE', 'NW']
else
directions = ['S', 'SE', 'SW']
end
new_square = @square
new_square = orthogonal_step(new_square, directions[0])
@legal_moves << (new_square) if posn[new_square] == '---'
if @moved == false && @legal_moves.length > 0
new_square = orthogonal_step(new_square, directions[0])
@legal_moves << (new_square) if posn[new_square] == '---'
end
2.times do |i|
new_square = diagonal_step(@square, directions[i + 1])
if new_square != nil && posn[new_square] != '---'
piece_type = get_other_piece_info(posn[new_square])
if piece_type != "own" && piece_type != "enemy_king"
@legal_moves << (new_square)
end
end
end
opp_pawn = '' # look for en-passant opportunities (only on 4th/5th rank)
if @color == 'white' && @square / 8.floor == 3
opp_pawn = 'bp'
elsif @color == 'black' && @square / 8.floor == 4
opp_pawn = 'wp'
end
if opp_pawn != '' # get number(s) of E & W squares (if not off-board)
directions = ['E', 'W']
directions.each do |e|
new_square = orthogonal_step(@square, e)
if new_square != nil && (moves[-1][1] - moves[-1][2]).abs == 16
if posn[new_square][0..1] == opp_pawn && posn[new_square] == moves[-1][0]
@ep_take_sq = new_square
if @color == 'white'
@ep_square = new_square - 8
@legal_moves << @ep_square
else
@ep_square = new_square + 8
@legal_moves << @ep_square
end
end
end
end
end
if @check_blocks != []
if @check_blocks[0] == @ep_take_sq
@legal_moves = [@ep_square]
else
@legal_moves = common(@legal_moves, @check_blocks)
end
end
filter_pins
end
end
end
class Rook < Sliding_Piece
attr_accessor :disambiguate
def initialize(name, color, square)
super
@icon = Image.new("img/#{@color[0]}_rook.png", height: 70, width: 70)
@icon.z = -1
@disambiguate = []
end
def find_moves(posn, moves = [])
if @checks > 1
@legal_moves = []
else
@legal_moves = find_sliding_paths(posn, 'orthogonal')
filter_checks_and_pins
end
end
end
class Knight < Piece
attr_accessor :disambiguate
def initialize(name, color, square)
super
@icon = Image.new("img/#{@color[0]}_knight.png", height: 70, width: 70)
@icon.z = -1
@disambiguate = []
end
def find_moves(posn, moves = [])
if @checks > 1
@legal_moves = []
else
@disambiguate = []
@legal_moves = []
directions = ['NE', 'NW', 'EN', 'ES', 'SE', 'SW', 'WN', 'WS']
directions.each do |direction|
square = @square
new_square = nil
new_square = orthogonal_step(square, direction[0])
new_square = orthogonal_step(new_square, direction[0]) if new_square != nil
new_square = orthogonal_step(new_square, direction[1]) if new_square != nil
if new_square != nil
if posn[new_square] != "---"
piece_type = get_other_piece_info(posn[new_square])
if piece_type != "own" && piece_type != "enemy_king"
@legal_moves << (new_square)
elsif piece_type == "own" && posn[new_square][1] == 'n'
@disambiguate << new_square
end
else
@legal_moves << (new_square)
end
end
end
filter_checks_and_pins
end
end
end
class Bishop < Sliding_Piece
attr_accessor :disambiguate
def initialize(name, color, square)
super
@icon = Image.new("img/#{@color[0]}_bishop.png", height: 70, width: 70)
@icon.z = -1
@disambiguate = []
end
def find_moves(posn, moves = [])
if @checks > 1
@legal_moves = []
else
@legal_moves = find_sliding_paths(posn, 'diagonal')
filter_checks_and_pins
end
end
end
class Queen < Sliding_Piece
attr_accessor :disambiguate
def initialize(name, color, square)
super
@icon = Image.new("img/#{@color[0]}_queen.png", height: 70, width: 70)
@icon.z = -1
@disambiguate = []
end
def find_moves(posn, moves = [])
if @checks > 1
@legal_moves = []
else
dis_list = []
orthogonal_moves = find_sliding_paths(posn, 'orthogonal')
dis_list = @disambiguate
diagonal_moves = find_sliding_paths(posn, 'diagonal')
dis_list += @disambiguate
@disambiguate = dis_list
@legal_moves = orthogonal_moves + diagonal_moves
filter_checks_and_pins
end
end
end
class King < Piece
attr_accessor :long
attr_accessor :short
def initialize(name, color, square)
super
@icon = Image.new("img/#{@color[0]}_king.png", height: 70, width: 70)
@icon.z = -1
@long = true
@short = true
end
def find_moves(game_pieces, posn, moves = [])
def castling_square(near_sq, castle_sq, game_pieces, posn)
if @legal_moves.any? {|e| e == near_sq} && posn[castle_sq] == '---'
@legal_moves << castle_sq if is_in_check(game_pieces, posn, castle_sq) == false
end
@legal_moves
end
def neighbor_squares(posn, square, type)
directions = [['N', 'S', 'E', 'W'], ['NE', 'SE', 'SW', 'NW']]
ok_squares = []
found = false
2.times do |i|
directions[i].each do |direction|
new_square = nil
if i == 0
new_square = orthogonal_step(square, direction)
else
new_square = diagonal_step(square, direction)
end
if new_square != nil
if posn[new_square] != "---"
piece_type = get_other_piece_info(posn[new_square])
if piece_type != type
ok_squares << (new_square)
elsif piece_type == type
found = true
end
else
ok_squares << (new_square)
end
end
end
end
return ok_squares, found
end
if @checks < 2 # def already run before @dbl_check set true
@legal_moves = []
@legal_moves, found = neighbor_squares(posn, @square, 'own')
enemy_king = []
@legal_moves.each do |square|
enemy_king, found = neighbor_squares(posn, square, 'enemy_king')
@legal_moves -= [square] if found == true
end
end
@legal_moves.each do |move|
checks = 0
is_check = is_in_check(game_pieces, posn, move)
@legal_moves = @legal_moves - [move] if is_check == true
end
if @checks == 0 && @moved == false # add castling square(s) if legal move(s)
if @long == true
if @color == 'white'
castling_square(59, 58, game_pieces, posn)
else
castling_square(3, 2, game_pieces, posn)
end
end
if @short == true
if @color == 'white'
castling_square(61, 62, game_pieces, posn)
else
castling_square(5, 6, game_pieces, posn)
end
end
end
end
def is_in_check(game_pieces, posn, move)
checks, check_blocks, pinned = checks_and_pins(game_pieces, posn, move)
if checks == 0
false
else
true
end
end
def checks_and_pins(game_pieces, posn, square = -1)
n_of_checks = 0
check_blocks = []
pinned = {}
if square == -1
king_sq = posn.find_index("#{@color[0]}k0")
else
king_sq = square
end
if @color == 'white'
enemy = 'b'
pawn_dirs = ['NE', 'NW']
else
enemy = 'w'
pawn_dirs = ['SE', 'SW']
end
if posn.any? {|e| e.include?("#{enemy}n")}
knight = game_pieces.detect {|e| e.name == "#{enemy}n0"}
sq_store = knight.square
knight.square = king_sq
knight.find_moves(posn)
if knight.disambiguate != []
n_of_checks = 1
check_blocks << knight.disambiguate[0]
end
knight.square = sq_store
end
directions = ['N', 'S', 'E', 'W', 'NE', 'SE', 'SW', 'NW']
8.times do |i|
path = []
square = king_sq
pc1 = nil
count = 0
loop do
if i < 4
square = orthogonal_step(square, directions[i])
else
square = diagonal_step(square, directions[i])
end
path << square if square != nil
break if square == nil
if posn[square] != '---' && posn[square] != @name
piece = posn[square]
if (pc1 == nil && i < 4 &&
(piece[0..1] == "#{enemy}r" || piece[0..1] == "#{enemy}q")) ||
(pc1 == nil && i > 3 &&
(piece[0..1] == "#{enemy}b" || piece[0..1] == "#{enemy}q" ||
(piece[0..1] == "#{enemy}p" && count == 0 &&
pawn_dirs.any? {|e| e == directions[i]})))
n_of_checks += 1
path[0..-1].each {|e| check_blocks << e}
break
elsif pc1 == nil && piece[0] == enemy
break
elsif pc1 == nil # could be pinned piece
pc1 = piece
elsif pc1 != nil && piece[0] != enemy
break
elsif i < 4 && (piece[0..1] == "#{enemy}r" || piece[0..1] == "#{enemy}q") ||
i > 3 && (piece[0..1] == "#{enemy}b" || piece[0..1] == "#{enemy}q")
pinned["#{pc1}"] = []
path[0..count].each {|e| pinned["#{pc1}"] << e}
break
else
break
end
end
count += 1
end
break if n_of_checks > 1
end
return n_of_checks, check_blocks, pinned
end
end