-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
41 lines (35 loc) · 944 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
require_relative 'board'
require_relative 'display'
require_relative 'player'
class Game
def initialize
@board = Board.new
@display = Display.new(@board)
@players = {
:white => Player.new(:white, @display),
:black => Player.new(:black, @display)
}
@current_player = :white
end
def play
until @board.checkmate?(@current_player)
begin
start_pos, end_pos = @players[@current_player].make_move
@board.move_piece(@current_player, start_pos, end_pos)
rescue StandardError => e
puts "#{e.message} Try again."
sleep 2.5
retry
end
swap_turn!
@board.in_check?(@current_player) ? @display.in_check(true) : @display.in_check(false)
end
@display.render
puts "GG. #{@current_player} loses."
end
private
def swap_turn!
@current_player == :white ? @current_player = :black : @current_player = :white
end
end
Game.new.play