-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpieces.rb
More file actions
301 lines (233 loc) · 6.69 KB
/
Copy pathpieces.rb
File metadata and controls
301 lines (233 loc) · 6.69 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
class Piece
attr_accessor :pos, :color, :unicode, :game
def initialize(pos, game)
@pos = pos
@color = get_color
@unicode = ""
@game = game
end
def to_s
unicode
end
def get_color
return [6, 7].include?(pos[0]) ? :white : :black
end
def on_board?(to_pos)
to_pos[0].between?(0, 7) && to_pos[1].between?(0, 7)
end
end
class Pawn < Piece
def initialize(pos, game)
super(pos, game)
@unicode = @color == :white ? "\u2659" : "\u265F"
end
def get_peaceful_coords
coords = []
if color == :white
coords << [pos[0] - 1, pos[1]]
coords << [pos[0] - 2, pos[1]] if pos[0] == 6 && game.board[(pos[0] - 1), pos[1]].nil?
else
coords << [pos[0] + 1, pos[1]]
coords << [pos[0] + 2, pos[1]] if pos[0] == 1 && game.board[(pos[0] + 1), pos[1]].nil?
end
coords.keep_if { |p| on_board?(p) }
end
def get_attack_coords
coords = []
if color == :white
coords << [pos[0] - 1, pos[1] - 1]
coords << [pos[0] - 1, pos[1] + 1]
else
coords << [pos[0] + 1, pos[1] - 1]
coords << [pos[0] + 1, pos[1] + 1]
end
if color == :white
if en_passant?(:right)
coords << [pos[0] - 1, pos[1] + 1]
end
if en_passant?(:left)
coords << [pos[0] - 1, pos[1] - 1]
end
else
if en_passant?(:right)
coords << [pos[0] + 1, pos[1] + 1]
end
if en_passant?(:left)
coords << [pos[0] + 1, pos[1] - 1]
end
end
coords.keep_if { |p| on_board?(p) }
end
def en_passant?(direction)
# skip first turn
return false if game.move_hashes.length < 3
if color == :white?
return false unless pos[0] == 3
else
return false unless pos[0] == 4
end
debugger
return false unless game.move_hashes.last[:piece].is_a?(Pawn)
enemy_start_pos = game.move_hashes.last[:move][0]
enemy_end_pos = game.move_hashes.last[:move][1]
if color == :white?
unless (enemy_start_pos[0] == pos[0] - 1) && (enemy_end_pos[0] == pos[0])
return false
end
else
unless (enemy_start_pos[0] == pos[0] + 1) && (enemy_end_pos[0] == pos[0])
return false
end
end
if direction == :right
unless (enemy_start_pos[1] == pos[1] + 1) && (enemy_end_pos[1] == pos[1] + 1)
return false
end
else
unless (enemy_start_pos[1] == pos[1] - 1) && (enemy_end_pos[1] == pos[1] - 1)
return false
end
end
true
end
end
class Knight < Piece
MOVE_DIFF = [[1,2],[2,1],[-1,2],[2,-1],[1,-2],[-2,1],[-1,-2],[-2,-1]]
def initialize(pos, game)
super(pos, game)
@unicode = @color == :white ? "\u2658" : "\u265E"
end
def get_peaceful_coords
coords = []
MOVE_DIFF.each do |diff|
coords << [pos,diff].transpose.map { |x| x.reduce(:+) }
end
coords.keep_if { |p| on_board?(p) }
end
def get_attack_coords
#because they are identical
get_peaceful_coords
end
end
class MultiMover < Piece
def initialize(pos, game)
super(pos,game)
end
def get_peaceful_coords
coords = []
self.class::MOVE_DIFF.each do |diff|
i = 1
until i == self.class::MOVE_LENGTH
multiplied = [diff[0] * i, diff[1] * i]
trans = [pos,multiplied].transpose.map { |x| x.reduce(:+) }
break if !(on_board?(trans)) || game.board[trans[0], trans[1]]
coords << trans
i += 1
end
end
coords.keep_if { |coord| on_board?(coord) }
end
def get_attack_coords
coords = []
add_to_coords = nil
self.class::MOVE_DIFF.each do |diff|
i = 1
until i == self.class::MOVE_LENGTH
multiplied = [diff[0] * i, diff[1] * i]
trans = [pos,multiplied].transpose.map { |x| x.reduce(:+) } # [2, 2]
add_to_coords = trans
break if !(on_board?(trans)) || game.board[trans[0], trans[1]]
i += 1
end
coords << add_to_coords
end
coords.keep_if { |p| on_board?(p) && p != pos }
end
end
class Bishop < MultiMover
MOVE_DIFF = [[-1, -1], [1, 1], [1, -1], [-1, 1]]
MOVE_LENGTH = 8
def initialize(pos, game)
super(pos, game)
@unicode = @color == :white ? "\u2657" : "\u265D"
end
end
class Rook < MultiMover
MOVE_DIFF = [[1,0], [-1, 0], [0, 1], [0, -1]]
MOVE_LENGTH = 8
def initialize(pos, game)
super(pos, game)
@unicode = @color == :white ? "\u2656" : "\u265C"
end
end
class Queen < MultiMover
MOVE_DIFF = [[1,0], [-1, 0], [0, 1], [0, -1], [-1, -1], [1, 1], [1, -1], [-1, 1]]
MOVE_LENGTH = 8
def initialize(pos, game)
super(pos, game)
@unicode = @color == :white ? "\u2655" : "\u265B"
end
end
class King < MultiMover
MOVE_DIFF = [[1,0], [-1, 0], [0, 1], [0, -1], [-1, -1], [1, 1], [1, -1], [-1, 1]]
MOVE_LENGTH = 2
def initialize(pos, game)
super(pos, game)
@unicode = @color == :white ? "\u2654" : "\u265A"
end
def get_peaceful_coords
coords = super
co = castle_options # :kingside, #queenside #both #neither
case co
when :kingside
y = turn == :white ? 7 : 0
coords << [y, 6]
when :queenside
y = turn == :white ? 7 : 0
coords << [y, 2]
when :both
y = turn == :white ? 7 : 0
coords << [y, 6]
coords << [y, 2]
end
coords
end
def castle_options
#not in check currently
return :neither if game.board.check?
king = game.board.board.flatten.select { |piece| piece && piece.is_a?(King) && piece.color == game.turn}[0]
#king hasnt moved
return :neither if game.move_hashes.any? { |hash| hash.has_value?(king) }
q_rook = game.board.board.flatten.select { |piece| piece && piece.is_a?(Rook) && piece.color == game.turn && piece.pos[1] == 0}[0]
can_castle_queenside = can_castle?(q_rook, king)
k_rook = game.board.board.flatten.select { |piece| piece && piece.is_a?(Rook) &&
piece.color == game.turn && piece.pos[1] == 7}[0]
can_castle_kingside = can_castle?(k_rook, king)
if can_castle_kingside && can_castle_queenside
return :both
elsif can_castle_kingside
return :kingside
elsif can_castle_queenside
return :queenside
else
return :neither
end
end
def can_castle?(rook, king)
return false if game.move_hashes.any? { |hash| hash.has_value?(rook) }
if rook.pos[1] == 0 #if it's queenside
path = [[rook.pos[0], 1], [rook.pos[0], 2], [rook.pos[0], 3]]
else #if it's kingside
path = [[rook.pos[0], 5], [rook.pos[0], 6]]
end
#check for pieces in path
return false if path.any? { |coord| game.board[coord] }
board.board.each do |row|
row.each do |piece|
next if piece.nil? || piece.color == game.turn
return false unless (piece.get_attack_coords & path).empty?
end
end
true
end
end