-
Notifications
You must be signed in to change notification settings - Fork 0
/
pawn.rb
executable file
·43 lines (39 loc) · 1.06 KB
/
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
# encoding: utf-8
class Pawn < Piece
def initialize(pos, color, board, has_moved = false)
super
if @color == :white
@step_vector = [0, 1]
@slide_vector = [0, 2]
@attack_vectors = [[-1, 1], [1, 1]]
else
@step_vector = [0, -1]
@slide_vector = [0, -2]
@attack_vectors = [[-1, -1], [1, -1]]
end
end
def moves
moves = []
step_pos = vector_add(@pos, @step_vector)
if @board.on_board?(step_pos) && @board[step_pos].nil?
moves << step_pos
slide_pos = vector_add(@pos, @slide_vector)
if @has_moved == false && @board.on_board?(slide_pos) && @board[slide_pos].nil?
moves << slide_pos
end
end
@attack_vectors.each do |attack_vector|
attack_pos = vector_add(@pos, attack_vector)
if (@board.on_board?(attack_pos) &&
@board[attack_pos].nil? == false &&
@board[attack_pos].color != @color)
moves << attack_pos
end
end
moves
end
def to_s
return '♙' if @color == :white
return '♟' if @color == :black
end
end