-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
72 lines (60 loc) · 1.54 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'colorize'
require_relative 'piece'
require_relative 'board'
require_relative 'player'
require_relative 'errors'
class Game
attr_accessor :board, :player1, :player2
def self.start_new_game
end
def initialize
@board = Board.make_starting_board
@player1 = HumanPlayer.new(:red)
@player2 = HumanPlayer.new(:black)
end
def play
player = @player1
until won? || draw?
begin
puts "#{player.color.capitalize}'s turn."
@board.render
piece = @board[player.get_piece]
raise InvalidPieceError if piece.nil? || piece.color != player.color
target = player.get_target
piece.perform_move(target)
rescue InvalidPieceError => e
puts "Invalid Piece Selection! Try again."
retry
rescue InvalidMoveError => e
puts "Invalid Move! Try again."
retry
rescue
puts "What was that again?"
retry
end
player = toggle_player(player)
end
puts won? ? "#{winner.capitalize} wins!" : "Draw."
end
def toggle_player(player)
player == @player1 ? @player2 : @player1
end
def won?
!winner.nil?
end
def winner
flat_board = @board.grid.flatten.compact
return :red if flat_board.all? {|t| t.color == :red }
return :black if flat_board.all? {|t| t.color == :black }
end
def draw?
end
end
if $PROGRAM_NAME == __FILE__
game = Game.new
game.play
end
test = Board.make_starting_board
test.place_piece(:red, [4,2])
test[[1,5]] = nil
test[[5,1]].perform_move([[3,3],[1,5]])