-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.rb
More file actions
49 lines (38 loc) · 928 Bytes
/
game.rb
File metadata and controls
49 lines (38 loc) · 928 Bytes
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
require "./board"
require "./pieces"
require "./players"
class Game
def initialize(player1, player2)
@board = Board.new(true)
player1.color = :w
player2.color = :b
@players = [player1, player2]
end
def play
until @board.over?
@board.render
begin
start_pos, end_pos = @players.first.play_turn
check_color(start_pos,@players.first)
@board.move(start_pos, end_pos)
rescue InvalidMoveError => e
puts e.message
retry
end
@players.rotate!
end
@board.render
puts "Checkmate! #{@players.last.name}, you won!"
end
def check_color(pos,player)
if player.color != @board[pos].color
raise InvalidMoveError, "That's not your piece!"
end
end
end
if __FILE__ == $PROGRAM_NAME
p1 = HumanPlayer.new("Jack")
p2 = HumanPlayer.new("Jim")
game = Game.new(p1, p2)
game.play
end