-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkers.rb
66 lines (56 loc) · 1.4 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
require './piece'
require './board'
require './player'
require 'colorize'
class Game
def initialize
@board = Board.new
@black_player = Player.new(:black)
@white_player = Player.new(:white)
@current_player = @white_player
play
end
def swap_player
@current_player = @current_player == @white_player ? @black_player : @white_player
end
def play
until @board.game_over? || @board.draw?
begin
@board.display
color = @current_player.color
moves = @current_player.gets_move
raise "Please select a valid piece" unless @board.valid_piece(moves[0].dup, color)
if @board.jump_available?(color)
new_start = moves[1].dup
@board.get_spot?(moves[0]).perform_moves([moves[1]])
keep_moving(new_start)
else
@board.get_spot?(moves[0]).perform_moves([moves[1]])
end
swap_player
rescue => e
puts e.message
retry
end
puts "Draw!" if @board.draw?
puts "Game Over" if @board.game_over?
end
end
def keep_moving(pos)
begin
@board.display
if @board.get_spot?(pos).jump.count > 0
puts "You can still jump from #{pos}"
new_spot = @current_player.move
@board.get_spot?(pos).perform_moves([new_spot.dup])
keep_moving(new_spot)
end
rescue
retry
end
end
end
if __FILE__ == $PROGRAM_NAME
puts "Let's play checkers!"
Game.new
end