-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
63 lines (54 loc) · 1.32 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
require_relative 'board'
class Game
def initialize
@board = Board.new
@turn = :black
play
end
private
attr_accessor :turn
def get_input
begin
puts "#{turn.capitalize}, please enter a move. Example format: '26 35 46'"
input = gets.chomp
unless input =~ /^[0-9]{2}[\s[0-9]{2}]+/
raise InvalidMoveError.new "Formatting issue."
end
rescue InvalidMoveError => e
puts e.message
retry
end
input.split.map do |pos|
[Integer(pos[0]), Integer(pos[1])]
end
end
def over?
@board.grid.flatten.compact.none? { |piece| piece.color == turn }
end
def play
until over?
puts @board
play_turn
@turn = (turn == :black ? :white : :black)
end
@turn = (turn == :black ? :white : :black)
puts @board
puts "Game over! #{turn.capitalize} wins!"
end
def play_turn
begin
move_seq = get_input
if @board[move_seq.first].color != turn
raise InvalidMoveError.new "Cannot move opponent's piece."
elsif @board.empty?(move_seq.first)
raise InvalidMoveError.new "No piece here."
end
@board.perform_moves(move_seq)
rescue InvalidMoveError => e
puts "You've made some sort of invalid move. Please try again."
puts "Error: #{e.message}"
retry
end
end
end
Game.new