-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiece.rb
More file actions
210 lines (178 loc) · 3.88 KB
/
piece.rb
File metadata and controls
210 lines (178 loc) · 3.88 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
# encoding: utf-8
require './board'
require './game'
class Piece
attr_accessor :pos
attr_reader :color, :board, :symbol
DIAGONAL = [
[-1, -1],
[1, 1],
[-1, 1],
[1, -1]
]
ORTHOGONAL = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1]
]
def initialize(pos, color, board)
@pos, @color, @board = pos, color, board
end
def moves
end
def dup(new_board)
# self.class.new()
Piece.create(symbol, pos, color, new_board)
end
def inspect
puts "#{self.class}"
end
def self.create(type_piece, pos, color, board)
case type_piece
when :rook
Rook.new(pos, color, board)
when :knight
Knight.new(pos, color, board)
when :bishop
Bishop.new(pos, color, board)
when :queen
Queen.new(pos, color, board)
when :king
King.new(pos, color, board)
else
Pawn.new(pos, color, board)
end
end
def valid_moves
moves.reject do |move|
move_into_check?(move)
end
end
def move_into_check?(end_pos)
new_board = board.dup
new_board.move!(pos, end_pos)
new_board.in_check?(self.color)
end
end
class SlidingPieces < Piece
# def get_move(offset)
# [offset[0] + pos[0], offset[1] + pos[1]]
# end
def moves(offsets)
possible_moves = []
offsets.each do |offset|
row, col = pos
move = [offset[0] + row, offset[1] + col]
while board.on_board?(move)
break if board[move] && board[move].color == color
possible_moves << move
break if board[move] && board[move].color != color
move = [move[0] + offset[0], move[1] + offset[1]]
end
end
possible_moves
end
end
class SteppingPieces < Piece
def moves(offsets)
possible_moves = []
row = pos[0]
col = pos[1]
offsets.each do |offset|
move = [row + offset[0], col + offset[1]]
if board.on_board?(move) && (board[move].nil? || board[move].color != color)
possible_moves << move
end
end
possible_moves
end
end
class Queen < SlidingPieces
def initialize (pos, color, board)
super(pos, color, board)
@symbol = :queen
end
def moves
super(DIAGONAL + ORTHOGONAL)
end
end
class Rook < SlidingPieces
def initialize (pos, color, board)
super(pos, color, board)
@symbol = :rook
end
def moves
super(ORTHOGONAL)
end
end
class Bishop < SlidingPieces
def initialize (pos, color, board)
super(pos, color, board)
@symbol = :bishop
end
def moves
super(DIAGONAL)
end
end
class Knight < SteppingPieces
OFFSETS = [
[2, 1],
[1, 2],
[-2, 1],
[-1, 2],
[-2, -1],
[-1, -2],
[2, -1],
[1, -2],
]
def initialize (pos, color, board)
super(pos, color, board)
@symbol = :knight
end
def moves
super(OFFSETS)
end
end
class King < SteppingPieces
def initialize (pos, color, board)
super(pos, color, board)
@symbol = :king
end
def moves
super(DIAGONAL + ORTHOGONAL)
end
end
class Pawn < SteppingPieces
DIAGONAL = {
:black => [[1, -1], [1, 1]],
:white => [[-1, -1], [-1, 1]]
}
def initialize (pos, color, board)
super(pos, color, board)
@symbol = :pawn
end
def moves
possible_moves = []
starting_row = (color == :black ? 1 : 6)
direction = (color == :black ? 1 : -1)
if pos[0] == starting_row
possible_moves << [pos[0] + direction, pos[1]]
possible_moves << [pos[0] + (direction * 2), pos[1]]
else
possible_moves << [pos[0] + direction, pos[1]]
end
possible_moves.select! { |move| board.on_board?(move) }
possible_moves.select! { |move| board.empty?(move) }
DIAGONAL[color].each do |offset|
row, col = offset
move = [pos[0] + row, pos[1] + col]
next unless board.on_board?(move)
test_pos = board[move]
if test_pos && test_pos.color != color
possible_moves << test_pos.pos
end
end
possible_moves
end
end