-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
60 lines (47 loc) · 1.17 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
require_relative 'board'
class Game
attr_reader :board, :current_player, :players
def initialize
@board = Board.new(true)
@players = {
:white => HumanPlayer.new(:white),
:black => HumanPlayer.new(:black)
}
@current_player = :white
end
def play
until board.checkmate?(current_player)
players[current_player].play_turn(board)
@current_player = (current_player == :white) ? :black : :white
end
board.display_grid
puts "\nCHECKMATE! \n\n#{current_player.to_s} won!"
end
end
class HumanPlayer
attr_reader :color
def initialize(color)
@color = color
end
def play_turn(board)
board.display_grid
puts "\n#{color.upcase}'s Turn \n\n"
begin
start_pos = get_position("Please choose a piece to move, format: y-axis, x-axis")
end_pos = get_position("To which position?")
board.move(start_pos, end_pos, color)
rescue MovePieceError => error
puts error.message
retry
end
end
private
def get_position(prompt)
puts prompt
gets.chomp.split(",").map { |coord| Integer(coord) }
end
end
if $PROGRAM_NAME == __FILE__
new_game = Game.new
new_game.play
end