-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
49 lines (42 loc) · 1014 Bytes
/
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
load 'board.rb'
require 'pry'
class Game
attr_accessor :board, :current_turn
attr_reader :display, :p1, :p2
def initialize
@board = Board.new
@current_turn = :W
@p1 = Player.new(:W, @board)
@p2 = Player.new(:B, @board)
end
def play
until game_over?
puts "\n"
puts "It is #{@current_turn}'s turn"
self.board.display
dup_board = self.board.duplicate
successful_move = false
begin
@current_turn == :W ? self.p1.move(current_turn) : self.p2.move(current_turn)
successful_move = true
rescue Exception => e
puts e.message
exit if $exit_now
p1.board = dup_board
p2.board = dup_board
self.board = dup_board
end
next_turn if successful_move
self.board.check_for_kings
end
end
def next_turn
@current_turn = @current_turn == :W ? :B : :W
end
def game_over?
board.blacks.empty? || board.whites.empty?
end
def display
self.board.display
end
end