-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.rb
217 lines (180 loc) · 3.81 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
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
require 'colorize'
require 'debugger'
class Piece
attr_accessor :position, :board, :color, :moved
ORTHOGONAL = [
[[-1, 0]],
[[0, 1]],
[[1, 0]],
[[0, -1]]
]
DIAGONAL = [
[[-1, -1]],
[[-1, 1]],
[[1, -1]],
[[1, 1]]
]
def initialize(position, board = nil, color = nil)
@position = position
@board = board
@color = color
@moved = false
end
def valid_moves(modifiers)
moves = []
modifiers.each do |direction|
direction.each do |modifier|
new_x = modifier.first + @position.first
new_y = modifier.last + @position.last
new_move = [new_x, new_y]
break if board.off_board?(new_move) || board.my_piece?(new_move, @color)
moves << new_move
break if board[new_move] #will return nil if no piece
end
end
moves
end
def moves
raise "Stop being an asshole, make a real piece"
end
def dup(board)
self.class.new(@position.dup, board, @color)
end
def to_show
raise "Don't call me from piece, dude"
end
end
class SlidingPiece < Piece
def move_dirs(directions)
move_mods = []
directions.each do |modifiers|
move_mods << generate_mods(*modifiers)
end
move_mods
end
def generate_mods(modifier)
coord_mods = []
1.upto(7) do |multiplier|
new_x = modifier.first * multiplier
new_y = modifier.last * multiplier
coord_mods << [new_x, new_y]
end
coord_mods
end
end
class King < Piece
DISPLAY = {:white => "\u265a".light_white, :black => "\u265a".black}
def moves
valid_moves
end
def valid_moves
super(Piece::ORTHOGONAL + Piece::DIAGONAL)
end
def to_show
DISPLAY[@color]
end
end
class Knight < Piece
DISPLAY = {:white => "\u265e".light_white, :black => "\u265e".black}
KNIGHT_MOVES = [
[[1, 2]],
[[2, 1]],
[[1, -2]],
[[2, -1]],
[[-1, 2]],
[[-2, 1]],
[[-1, -2]],
[[-2, -1]]
]
def moves
valid_moves
end
def valid_moves
super(KNIGHT_MOVES)
end
def to_show
DISPLAY[@color]
end
end
class Bishop < SlidingPiece
DISPLAY = {:white => "\u265d".light_white, :black => "\u265d".black}
def moves
valid_moves
end
def valid_moves
super(move_dirs(Piece::DIAGONAL))
end
def to_show
DISPLAY[@color]
end
end
class Rook < SlidingPiece
DISPLAY = {:white => "\u265c".light_white, :black => "\u265c".black}
def moves
valid_moves
end
def valid_moves
super(move_dirs(Piece::ORTHOGONAL))
end
def to_show
DISPLAY[@color]
end
end
class Queen < SlidingPiece
DISPLAY = {:white => "\u265b".light_white, :black => "\u265b".black}
def moves
valid_moves
end
def valid_moves
super(move_dirs(Piece::ORTHOGONAL + Piece::DIAGONAL))
end
def to_show
DISPLAY[@color]
end
end
class Pawn < Piece
DISPLAY = {:white => "\u265f".light_white, :black => "\u265f".black}
PAWN_MOVES =
{
:white =>
[
[[0, -1], [0, -2]],
[[-1, -1]],
[[+1, -1]]
],
:black =>
[
[[0, 1], [0, 2]],
[[1, 1]],
[[-1, 1]]
]
}
def moves
valid_moves
end
def valid_moves
moves = []
PAWN_MOVES[@color].each do |direction|
direction.each do |modifier|
new_x = modifier.first + self.position.first
new_y = modifier.last + self.position.last
new_move = [new_x, new_y]
if modifier.first == 0
break if board.off_board?(new_move)
break if board[new_move]
else
break if board.off_board?(new_move)
break if !board[new_move]
break if board.my_piece?(new_move, @color)
end
moves << new_move
break if @color == :black && @position.last != 1
break if @color == :white && @position.last != 6
end
end
moves
end
def to_show
DISPLAY[@color]
end
end