-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkers.rb
344 lines (273 loc) · 7.01 KB
/
checkers.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# encoding: UTF-8
require 'set'
require 'colorize'
class CheckersPiece
SYMBOLS = { :white => ["⛀","⛁"], :black => ["⛂" , "⛃"] }
attr_accessor :position
attr_reader :color, :board
def initialize(position, color, board, king = false)
@position, @color, @board = position, color, board
@king = king
end
def king?
@king
end
def to_s
king? ? SYMBOLS[@color][1] : SYMBOLS[@color][0]
end
def perform_moves(move_sequence)
unless @board.valid_move_seq?(move_sequence)
raise InvalidMoveError.new, "Illegal move sequence"
end
perform_moves!(move_sequence)
end
def perform_moves!(move_sequence)
validate_move_sequence(move_sequence)
turn_over, just_jumped = false, false
move_sequence[1..-1].each do |move|
raise InvalidMoveError.new, "Can't move twice" if turn_over
if just_jumped && slide?(move)
raise InvalidMoveError.new, "Can't jump then slide"
end
perform_move(move)
turn_over = true if slide?(move)
just_jumped = true if jump?(move)
promote if should_promote?
end
end
private
def promote
@king = true
end
def should_promote?
@position[0] == king_row
end
def king_row
@color == :white ? 7 : 0
end
def direction
@color == :white ? 1 : -1
end
def perform_move(move)
if slide?(move)
perform_slide(move)
elsif jump?(move)
perform_jump(move)
else
raise InvalidMoveError.new, "That move is neither a slide nor a jump"
end
end
def slide?(move)
y1, x1 = @position
y2, x2 = move
(y2 - y1).abs == 1 && (x2 - x1).abs == 1
end
def jump?(move)
y1, x1 = @position
y2, x2 = move
(y2 - y1).abs == 2 && (x2 - x1).abs == 2
end
def validate_move_sequence(move_sequence)
if move_sequence[0] != @position
raise InvalidMoveError.new, "Illegal start position"
end
if move_sequence.length < 2
raise InvalidMoveError.new, "Need a start and end location"
end
end
def perform_slide(pos)
unless slide_moves.include?(pos)
raise InvalidMoveError.new, "Can't slide there"
end
@position = pos
end
def perform_jump(pos)
unless jump_moves.include?(pos)
raise InvalidMoveError.new, "Can't jump there"
end
y1, x1 = @position
y2, x2 = pos
kill_pos = [ (y1 + y2) / 2, (x1 + x2) / 2]
@board.kill(@board[kill_pos])
@position = pos
end
def slide_moves
y, x = @position
slides = avail_slide_pos
slides.select do |slide|
@board[slide].nil? && @board.on_board?(slide)
end
end
def jump_moves
avail_jump_pos.select do |pos|
mid = @board[calc_mid_point(pos)]
mid && mid.color != @color && @board[pos].nil?
end
end
def calc_mid_point(jump_move)
y1, x1 = @position
y2, x2 = jump_move
[(y1 + y2) / 2, (x1 + x2) / 2]
end
def avail_jump_pos
y, x = @position
moves = [[y + direction * 2, x + 2], [y + direction * 2, x - 2]]
if king?
moves += [[y - direction * 2, x + 2], [y - direction * 2, x - 2]]
end
moves
end
def avail_slide_pos
y, x = @position
moves = [[y + direction, x + 1], [y + direction, x - 1]]
if king?
moves += [[y - direction, x + 1], [y - direction, x - 1]]
end
moves
end
end
class CheckersBoard
attr_accessor :pieces
def initialize(spawn = true)
spawn_pieces if spawn
end
def [](position)
@pieces.find { |piece| piece.position == position }
end
def game_over?
[:white, :black].any? do |color|
@pieces.select{ |piece| piece.color == color } == []
end
end
def display_board
display = build_colorized_board_array
puts ("a".."h").inject(" "){|sum, letter| sum + " #{letter} "}
display.each_with_index do |row, i|
puts " #{i+1} #{row.join}"
end
nil
end
def kill(piece)
@pieces.delete(piece)
end
def on_board?(position)
position.all? { |coord| coord.between?(0,7) }
end
def dup
dup_board = CheckersBoard.new(spawn = false)
dup_board.pieces = Set.new
@pieces.each do |piece|
dup_board.pieces << CheckersPiece.new(piece.position, piece.color,
dup_board, king = piece.king?)
end
dup_board
end
def valid_move_seq?(move_sequence)
temp_board = self.dup
begin
temp_board[move_sequence[0]].perform_moves!(move_sequence)
rescue InvalidMoveError => e
puts e.message
false
else
true
end
end
private
def build_colorized_board_array
colors = [:red, :white]
(0..7).map do |row|
(0..7).map do |column|
color = colors[(column + row) % 2]
piece_there = self[[row, column]]
if piece_there
" #{piece_there.to_s} ".colorize(:background => color)
else
" ".colorize(:background => color)
end
end
end
end
def spawn_pieces
@pieces = Set.new
[0, 1, 2, 5, 6, 7].each do |row|
(0..7).each do |column|
if (column + row) % 2 == 1 && row.between?(0, 2)
@pieces << CheckersPiece.new([row, column], :white, self)
end
if (column + row) % 2 == 1 && row.between?(5, 7)
@pieces << CheckersPiece.new([row, column], :black, self)
end
end
end
end
end
class CheckersGame
def initialize
@board = CheckersBoard.new
@players = { :white => CheckersPlayer.new(:white),
:black => CheckersPlayer.new(:black) }
@turn_color = :white
end
def play
until @board.game_over?
play_turn
end
switch_turn #switches turns again because it switches after the move
@board.display_board #one last time to see the board
puts "#{@turn_color.to_s.capitalize} Player Wins!"
end
private
def play_turn
begin
@board.display_board
puts "#{@turn_color.to_s.capitalize}'s Turn"
move_sequence = @players[@turn_color].make_move
validate_move_sequence(move_sequence)
@board[move_sequence[0]].perform_moves(move_sequence)
switch_turn
rescue InvalidMoveError => e
puts e.message
retry
end
end
def validate_move_sequence(move_sequence)
unless @board[move_sequence[0]]
raise InvalidMoveError.new "no piece there"
end
if @board[move_sequence[0]].color != @turn_color
raise InvalidMoveError.new "not your piece"
end
end
def switch_turn
@turn_color = @turn_color == :white ? :black : :white
end
end
class CheckersPlayer
def initialize(color)
@color = color
end
def make_move
puts "Please enter your move as a series of board spaces."
moves = gets.chomp
parse_input(moves)
end
private
def parse_input(moves)
unless moves =~ /^[a-h][1-8],([a-h][1-8],?)+$/
raise InvalidMoveError.new "Invalid Input"
end
moves = moves.split(",")
moves.map { |move| parse_coord(move)}
end
def parse_coord(move)
chars = move.split(//)
[chars[1].to_i - 1, chars[0].ord - 97]
end
end
class InvalidMoveError < StandardError
end
if __FILE__ == $PROGRAM_NAME
game = CheckersGame.new
game.play
end