-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess.rb
More file actions
339 lines (293 loc) · 8.95 KB
/
chess.rb
File metadata and controls
339 lines (293 loc) · 8.95 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative 'lib/types'
require_relative 'lib/board'
require_relative 'lib/move_generator'
require_relative 'lib/fen_parser'
require_relative 'lib/ai'
require_relative 'lib/perft'
module Chess
class ChessEngine
def initialize
@board = Board.new
@move_generator = MoveGenerator.new(@board)
@fen_parser = FenParser.new(@board)
@ai = AI.new(@board, @move_generator)
@perft = Perft.new(@board, @move_generator)
@move_history = []
end
def start
puts @board.display
flush_output
loop do
print "\n> " if $stdin.tty?
flush_output
input = gets&.strip
break if input.nil? || input.empty?
process_command(input)
end
end
private
def flush_output
$stdout.flush
end
def process_command(command)
parts = command.split
cmd = parts[0]&.downcase
case cmd
when 'move'
handle_move(parts[1])
when 'undo'
handle_undo
when 'new'
handle_new_game
when 'ai'
handle_ai_move(parts[1]&.to_i || 3)
when 'fen'
handle_fen(parts[1..-1]&.join(' '))
when 'export'
handle_export
when 'eval'
handle_eval
when 'hash'
handle_hash
when 'draws'
handle_draws
when 'history'
handle_history
when 'status'
handle_status
when 'perft'
handle_perft(parts[1]&.to_i || 4)
when 'help'
handle_help
when 'quit', 'exit'
puts 'Goodbye!'
flush_output
exit
else
puts 'ERROR: Invalid command. Type "help" for available commands.'
flush_output
end
rescue StandardError => e
puts "ERROR: #{e.message}"
flush_output
end
def handle_move(move_str)
unless move_str
puts 'ERROR: Invalid move format'
flush_output
return
end
move = Move.from_algebraic(move_str)
unless move
puts 'ERROR: Invalid move format'
flush_output
return
end
# Auto-promote to Queen if not specified and moving to promotion rank
piece = @board.piece_at(move.from_row, move.from_col)
if piece&.type == :pawn && move.promotion.nil?
if (piece.color == :white && move.to_row == 0) || (piece.color == :black && move.to_row == 7)
move.promotion = :queen
end
end
# Validate move is legal
legal_moves = @move_generator.generate_legal_moves
legal_move = legal_moves.find do |legal|
legal.from_row == move.from_row &&
legal.from_col == move.from_col &&
legal.to_row == move.to_row &&
legal.to_col == move.to_col &&
legal.promotion == move.promotion
end
unless legal_move
puts 'ERROR: Illegal move'
flush_output
return
end
# Make the move
@move_history << legal_move
@board.make_move(legal_move)
puts "OK: #{move_str}"
puts @board.display
flush_output
# Check for game end
check_game_end
end
def handle_undo
if @move_history.empty?
puts 'ERROR: No moves to undo'
flush_output
return
end
last_move = @move_history.pop
@board.undo_move(last_move)
puts 'OK: Move undone'
puts @board.display
flush_output
end
def handle_new_game
@board = Board.new
@move_generator = MoveGenerator.new(@board)
@fen_parser = FenParser.new(@board)
@ai = AI.new(@board, @move_generator)
@perft = Perft.new(@board, @move_generator)
@move_history.clear
puts 'OK: New game started'
puts @board.display
flush_output
end
def handle_ai_move(depth)
unless depth.between?(1, 5)
puts 'ERROR: AI depth must be 1-5'
flush_output
return
end
result = @ai.find_best_move(depth)
unless result
puts 'ERROR: No legal moves available'
flush_output
return
end
move = result[:move]
@move_history << move
@board.make_move(move)
puts "AI: #{move.to_algebraic} (depth=#{result[:depth]}, eval=#{result[:score]}, time=#{result[:time_ms]}ms)"
puts @board.display
flush_output
check_game_end
end
def handle_fen(fen_string)
unless fen_string
puts 'ERROR: Invalid FEN string'
flush_output
return
end
if @fen_parser.parse(fen_string)
@move_history.clear
puts 'OK: FEN loaded'
puts @board.display
flush_output
else
puts 'ERROR: Invalid FEN string'
flush_output
end
end
def handle_export
fen = @fen_parser.export
puts "FEN: #{fen}"
flush_output
end
def handle_eval
# Simple evaluation display
material_balance = calculate_material_balance
puts "Evaluation: #{material_balance} (from White's perspective)"
flush_output
end
def handle_hash
puts "Hash: #{@board.zobrist_hash.to_s(16).rjust(16, '0')}"
flush_output
end
def handle_draws
require_relative 'lib/draw_detection'
repetition = DrawDetection.draw_by_repetition?(@board)
fifty_moves = DrawDetection.draw_by_fifty_moves?(@board)
puts "Repetition: #{repetition}, 50-move rule: #{fifty_moves}, 50-move clock: #{@board.halfmove_clock}"
flush_output
end
def handle_history
puts "Position History (#{@board.position_history.length + 1} positions):"
@board.position_history.each_with_index do |h, i|
puts " #{i}: #{h.to_s(16).rjust(16, '0')}"
end
puts " #{@board.position_history.length}: #{@board.zobrist_hash.to_s(16).rjust(16, '0')} (current)"
flush_output
end
def handle_status
current_color = @board.current_turn
if @move_generator.in_checkmate?(current_color)
winner = current_color == :white ? 'Black' : 'White'
puts "CHECKMATE: #{winner} wins"
elsif @move_generator.in_stalemate?(current_color)
puts 'STALEMATE: Draw'
else
require_relative 'lib/draw_detection'
if DrawDetection.draw?(@board)
reason = DrawDetection.draw_by_repetition?(@board) ? "repetition" : "50-move rule"
puts "DRAW: by #{reason}"
else
puts "OK: ongoing"
end
end
flush_output
end
def handle_perft(depth)
unless depth.between?(1, 6)
puts 'ERROR: Perft depth must be 1-6'
flush_output
return
end
result = @perft.calculate(depth)
puts "Perft #{depth}: #{result[:nodes]} nodes in #{result[:time_ms]}ms"
flush_output
end
def handle_help
puts <<~HELP
Available commands:
move <from><to>[promotion] - Execute a move (e.g., move e2e4, move e7e8Q)
undo - Undo the last move
new - Start a new game
ai <depth> - AI makes a move (depth 1-5)
fen <string> - Load position from FEN notation
export - Export current position as FEN
eval - Display position evaluation
hash - Show Zobrist hash of current position
draws - Show draw detection status
history - Show position hash history
perft <depth> - Performance test (move count)
help - Display this help message
quit - Exit the program
HELP
flush_output
end
def check_game_end
current_color = @board.current_turn
if @move_generator.in_checkmate?(current_color)
winner = current_color == :white ? 'Black' : 'White'
puts "CHECKMATE: #{winner} wins"
flush_output
elsif @move_generator.in_stalemate?(current_color)
puts 'STALEMATE: Draw'
flush_output
else
require_relative 'lib/draw_detection'
if DrawDetection.draw?(@board)
reason = DrawDetection.draw_by_repetition?(@board) ? "repetition" : "50-move rule"
puts "DRAW: by #{reason}"
flush_output
end
end
end
def calculate_material_balance
white_material = 0
black_material = 0
(0..7).each do |row|
(0..7).each do |col|
piece = @board.piece_at(row, col)
next unless piece
if piece.color == :white
white_material += piece.value
else
black_material += piece.value
end
end
end
white_material - black_material
end
end
end
# Start the chess engine if this file is run directly
if __FILE__ == $PROGRAM_NAME
Chess::ChessEngine.new.start
end