-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.rb
215 lines (173 loc) · 4.74 KB
/
chess.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require_relative 'lib/board'
require_relative 'lib/piece'
require_relative 'lib/stepping_piece'
require_relative 'lib/sliding_piece'
require_relative 'lib/pieces/king'
require_relative 'lib/pieces/queen'
require_relative 'lib/pieces/rook'
require_relative 'lib/pieces/bishop'
require_relative 'lib/pieces/knight'
require_relative 'lib/pieces/pawn'
require 'colorize'
require 'yaml'
PROMOTIONS = {
"queen" => Queen,
"rook" => Rook,
"bishop" => Bishop,
"knight" => Knight
}
class Game
def initialize(player1, player2)
self.board = Board.new
self.players = {:white => player1, :black => player2}
self.turn = :white
end
def play
system "clear" or system "cls"
puts
board.display
until board.checkmate?(turn) || board.stalemate?(turn)
move = do_move
sleep(0.3)
show_move(move)
self.turn = (turn == :white) ? :black : :white
puts "Check!\a" if board.in_check?(turn)
end
board.display
if board.checkmate?(turn)
winner = (turn == :white) ? :black : :white
puts "Checkmate! The winner is #{winner}."
elsif board.stalemate?(turn)
puts "The game is a draw."
end
end
private
attr_accessor :board, :players, :turn
def save_game
puts "Please enter filename:"
fname = gets.chomp
File.open(fname, 'w') do |f|
f.puts self.to_yaml
end
puts "Game saved to file: \"#{fname}\"."
end
def show_move(move)
locs = HumanPlayer.translate_coords(move.split(/\s+/))
system "clear" or system "cls"
board.display(locs)
puts "#{turn.to_s.capitalize}\'s move was #{move.chomp(' ')}."
end
def do_move
begin
move = players[turn].play_turn(board,turn)
if move == "save"
save_game
return
end
rescue MyChessError => e
system "clear" or system "cls"
puts "#{e.message}"
board.display
retry
end
move
end
end
class HumanPlayer
def play_turn(board, color)
puts "#{color.to_s.capitalize} to move. Input your move."
input = gets.chomp
return "save" if input.downcase == "save"
coords = input.split(/\s+/)
if coords.length != 2 || coords.any? { |coord| coord !~ /^[a-hA-H]{1}\d{1}$/ }
raise MyChessError.new("Bad input!")
end
move = HumanPlayer.translate_coords(coords)
board.move(move[0],move[1],color)
check_promotion(board, color)
input
end
def self.translate_coords(coords)
new_coords = []
coords.each do |coord|
new_coords_one = coord[0].downcase.bytes[0] - 97
new_coords_two = 8 - coord[1].to_i
new_coords << [new_coords_two, new_coords_one]
end
new_coords
end
private
def check_promotion(board, color)
if board.pawn_promotion
begin
puts "What piece would you like to promote to?"
piece = gets.chomp.downcase
board.promote_pawn(piece, color)
rescue MyChessError => e
puts "#{e.message}"
board.display(:white, :default, :light_white)
retry
end
end
end
end
class ComputerPlayer
PIECE_VALUES = { Queen => 9,
Rook => 5,
Knight => 3,
Bishop => 3,
Pawn => 1}
def play_turn(board, color)
best_move = []
best_val = 0
our_pieces = board.pieces(color)
enemy_pieces = board.pieces(color == :black ? :white : :black)
enemy_positions = enemy_pieces.map { |piece| piece.pos }
our_pieces.each do |piece|
piece_moves = piece.valid_moves
cap_moves = piece_moves.select { |move| enemy_positions.include?(move) }
next if cap_moves.empty?
best = cap_moves.max_by { |move| PIECE_VALUES[board[move].class] }
val = PIECE_VALUES[board[best].class]
if val > best_val
best_move = [piece.pos,best]
best_val = val
end
end
if best_val.zero?
piece = our_pieces.reject do |piece|
piece.valid_moves.count.zero?
end.sample
move = piece.valid_moves.sample
best_move = [piece.pos,move]
end
board.move(best_move[0],best_move[1],color)
check_promotion(board, color)
ComputerPlayer.translate_coords(best_move)
end
def self.translate_coords(coords)
new_coords = ""
coords.each do |coord|
new_coords_one = (coord[1] + 97).chr
new_coords_two = (8 - coord[0]).to_s
new_coords << new_coords_one << new_coords_two << " "
end
new_coords
end
private
def check_promotion(board, color)
board.promote_pawn(Queen, color) if board.pawn_promotion
end
end
class MyChessError < StandardError
end
if __FILE__ == $PROGRAM_NAME
if ARGV.count == 0
g = Game.new(HumanPlayer.new, ComputerPlayer.new)
g.play
else
fname = ARGV.shift
g = YAML::load(File.read(fname))
g.play
end
end