-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpawn.rb
49 lines (41 loc) · 926 Bytes
/
pawn.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
require_relative 'piece'
class Pawn < Piece
def move_dirs
case @color
when :w
[DELTAS[:n], DELTAS[:ne], DELTAS[:nw]]
when :b
[DELTAS[:s], DELTAS[:se], DELTAS[:sw]]
end
end
def moves
moves = []
dirs = move_dirs
advance = vector_sum([@pos, dirs[0]])
if @board[advance].nil?
moves << advance
unless moved?
advance = vector_sum([advance, dirs[0]])
moves << advance if @board[advance].nil?
end
end
2.times do |i|
advance = vector_sum([@pos, dirs[i + 1]])
unless @board[advance].nil? || @board[advance].color == self.color
moves << advance
end
end
moves.select { |move| move_within_boundaries?(move) }
end
def to_s
@color == :b ? "\u{265f}" : "\u{2659}"
end
def moved?
case @color
when :w
@pos.first != @board.height - 2
when :b
@pos.first != 1
end
end
end