-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.rb
156 lines (125 loc) · 3.95 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class Game
attr_reader :board, :colors, :white, :black
def initialize(white, black, position = nil)
@board = Board.new(position)
@white = white
@black = black
@fifty_move_count = 0
@previous_positions = Hash.new(0)
@previous_positions[board.extract_positions] += 1
@colors = { white => :white,
black => :black }
end
def play
until over?(:white) || over?(:black)
[white, black].each do |player|
board.display
begin
puts "It is #{colors[player]}'s turn!"
move = player.play_turn
make_move(move, colors[player])
rescue MoveError => err
puts err.message
puts "Please select a different move."
retry
end
reset_en_passant(player)
@previous_positions[board.extract_positions] += 1
break if over?(other_color(colors[player]))
end
end
board.display
winner ? (puts "The winner is #{winner}.") : (puts draw_message)
end
private
def make_move(move, color)
start_pos, end_pos = move
piece = board[start_pos]
player = @colors.key(color)
if board[start_pos] && board[start_pos].color != color
raise MoveError.new "Trying to move opponent's piece!"
end
prev_captures = board.captured[other_color(color)].count
board.move(start_pos, end_pos)
adjust_fifty_move_count(piece, board, prev_captures, color)
check_and_manage_promotion(end_pos, player, color)
end
def other_color(color)
color == :black ? :white : :black
end
def reset_en_passant(player)
color = @colors[player]
pawns = board.pieces(color).select { |piece| piece.is_a?(Pawn) }
pawns.each { |pawn| pawn.reset_en_passant }
end
def over?(color)
board.checkmate?(:white) || board.checkmate?(:black) || draw(color)
end
def winner
if board.checkmate?(:white)
:black
elsif board.checkmate?(:black)
:white
else
nil
end
end
def check_and_manage_promotion(end_pos, player, color)
if board[end_pos].is_a?(Pawn) && board[end_pos].promotes?
board.display
piece = player.promote(piece).new(end_pos, color, board)
board[end_pos] = piece
end
end
def draw(color)
stalemate?(color) || fifty_moves? || threefold_repetition? || insufficient_material?
end
def draw_message
if stalemate?(:white) || stalemate?(:black)
"The game has ended in a draw due to stalemate"
elsif fifty_moves?
"The game has ended in a draw due to the fifty rule move."
elsif threefold_repetition?
"The game has ended in a draw due to threefold repetition."
elsif insufficient_material?
"The game has ended in a draw due to insufficient material."
end
end
def stalemate?(color)
board.pieces(color).all? { |piece| piece.valid_moves.empty? }
end
def fifty_moves?
@fifty_move_count >= 50
end
def adjust_fifty_move_count(piece, board, prev_captures, color)
new_captures = board.captured[other_color(color)].count
if (piece.is_a?(Pawn) || new_captures != prev_captures)
@fifty_move_count = 0
else
@fifty_move_count += 1
end
end
def threefold_repetition?
@previous_positions.values.any? { |position_count| position_count >= 3 }
end
def insufficient_material?
return false if bishops_sufficient?
insufficient = [[:King],
[:Bishop, :King],
[:King, :Knight]]
white_pieces = board.pieces(:black).map { |piece| piece.class.name.to_sym }
white_pieces.sort!
black_pieces = board.pieces(:black).map { |piece| piece.class.name.to_sym }
black_pieces.sort!
(insufficient.include?(white_pieces) &&
insufficient.include?(black_pieces))
end
def bishops_sufficient?
pieces = board.pieces
bishops = pieces.select { |piece| piece.is_a?(Bishop) }
(pieces.count == bishops.count + 2) &&
(bishops.any? do |bishop|
bishop.square_color != bishops.first.square_color
end)
end
end