-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.rb
71 lines (54 loc) · 1.55 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
64
65
66
67
68
69
70
71
require_relative 'board'
require_relative 'player'
class Game
attr_reader :board
def initialize(white_player, black_player)
@white, @black = white_player, black_player
@white.color = :white
@black.color = :black
end
def play
playing = @white
@board = Board.new
puts "#{@white.name} will play white and #{@black.name} will play black."
until @board.checkmate?
@board.display
puts "Check!" if board.in_check?(playing.color)
begin
move = playing.choose_move
if move == :long || move == :short
@board.castle(move, playing.color)
else
@board.move(*move, playing.color)
end
rescue NoCastleError
puts "You may not castle with that rook."
retry
rescue NotYoPieceError
puts "Stop cheating. Move your own piece. Come on man."
retry
rescue IllegalMoveError
puts "You can't move there."
retry
rescue MoveToCheckError
if board.in_check?(playing.color)
puts "You are in check! Move out of check."
else
puts "That will put your King in check. Choose again."
end
retry
rescue NoPieceError
puts "There is no piece at that position."
retry
end
@board.check_for_pawn_promotion(playing.color)
playing = ( playing == @white ? @black : @white ) #switches turns
end
if playing == @white
puts "Checkmate! Black wins."
else
puts "Checkmate! White wins."
end
@board.display
end
end