-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
50 lines (40 loc) · 941 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
50
require_relative "display"
require_relative "player"
require_relative "human_player"
require_relative "board"
class Game
def initialize
@board = Board.new
@display = Display.new(@board)
@white = HumanPlayer.new(:w, @display)
@black = HumanPlayer.new(:b, @display)
@current_player = @white
end
def play
while !@board.checkmate?(:w) && !@board.checkmate?(:b)
begin
@display.render
notify_players
start_pos, end_pos = @current_player.make_move(@board)
@board.move_piece(@current_player.color, start_pos, end_pos)
rescue
retry
end
swap_turn!
end
end_game
end
def end_game
@display.render
print "\nCheckmate."
end
private
def notify_players
print "\nEnter a valid move using the cursor keys."
end
def swap_turn!
@current_player = @current_player == @white ? @black : @white
end
end
g = Game.new
g.play