-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
55 lines (46 loc) · 1.09 KB
/
game.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
require 'colorize'
require_relative 'checkers.rb'
require_relative 'players.rb'
class Game
def initialize(first_player = nil, second_player = nil, size = 8)
@players = first_player || HumanPlayer.new, second_player || HumanPlayer.new
@game_board = Board.new(size)
@players.first.set_color(:white)
@players.last.set_color(:black)
nil
end
def run
play
end_game
end
def play
until over?
begin
turn = @players.first.get_moves(@game_board)
@game_board[turn.first].perform_moves(turn.last)
rescue StandardError => error
p error.message
retry
end
@players.reverse!
end
end
def over?
draw? || won?
end
def won?
@game_board.all_pieces(@players.first.color).count == 0 ||
!@game_board.has_moves?(@players.first.color)
end
def draw?
!@game_board.has_moves?(@players.first.color) &&
!@game_board.has_moves?(@players.last.color)
end
def end_game
if won?
p "Congratulations #{@players.last.color.to_s.capitalize}! You won!"
else
p "You both suck!"
end
end
end