-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.rb
139 lines (116 loc) · 2.89 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
class Piece
DIAGONAL_DIRS = [[1, 1], [1, -1], [-1, -1], [-1, 1]]
STRAIGHT_DIRS = [[1, 0], [0, -1], [-1, 0], [0, 1]]
attr_accessor :position, :color
#creates the piece in a given position
def initialize color, position
@color = color
@position = position
end
def move_on_board? move
return (0..7).include?(move[0]) && (0..7).include?(move[1])
end
end
#can move until the end of the board in move_dirs
class SlidingPiece < Piece
def possible_moves
[].tap do |poss_moves|
#needs to take move_dirs and vectorize the dirs
move_dirs.each do |dir|
magnitude = 1
loop do
move_vec = dir.map { |el| el * magnitude}
move = [@position[0] + move_vec[0], @position[1] + move_vec[1]]
break unless move_on_board?(move)
poss_moves << move
magnitude += 1
end
end
end
end
end
class Rook < SlidingPiece
attr_accessor :has_moved
def initialize color, position
super(color, position)
@has_moved = false
end
def position= position
@has_moved = true
@position = position
end
def move_dirs
STRAIGHT_DIRS
end
end
class Bishop < SlidingPiece
def move_dirs
DIAGONAL_DIRS
end
end
class Queen < SlidingPiece
def move_dirs
DIAGONAL_DIRS + STRAIGHT_DIRS
end
end
#stepping pieces can move 1
class SteppingPiece < Piece
def possible_moves
move_dirs.map do |dir|
[position[0] + dir[0], position[1] + dir[1]]
end.select { |move| move_on_board?(move) }
end
end
class King < SteppingPiece
attr_accessor :has_moved
def initialize color, position
super(color, position)
@has_moved = false
end
def position= position
@has_moved = true
@position = position
end
def move_dirs
STRAIGHT_DIRS + DIAGONAL_DIRS
end
end
class Knight < SteppingPiece
def move_dirs
[1, 2, -1, -2].permutation(2).to_a.reject do |combo|
combo[0] + combo[1] == 0
end
end
end
class Pawn < Piece
def initialize color, position, board
super(color, position)
@board = board
end
def possible_moves
direction = @color == :white ? 1 : -1
moves = []
basic_move = [@position[0] + direction, @position[1]]
if move_on_board?(basic_move) && @board.get_board_piece(basic_move).nil?
moves << basic_move
first_move = [@position[0] + (direction * 2), @position[1]]
if @board.get_board_piece(first_move).nil? &&
((@color == :white && position[0] == 1) || (@color == :black && position[0] == 6))
moves << first_move
end
end
attacks = [
[@position[0] + direction, @position[1] + 1],
[@position[0] + direction, @position[1] - 1]
]
attacks.select! do |move|
if move_on_board?(move)
attacked_piece = @board.get_board_piece(move)
attacked_piece != nil and attacked_piece.color != @color
else
false
end
end
moves + attacks
end
end