Add more logging

This commit is contained in:
Alex Clink 2021-10-15 22:18:59 -04:00
parent fa4486569e
commit b87b9e0a18
3 changed files with 43 additions and 10 deletions

View file

@ -8,10 +8,6 @@ options = {} of String => String | Nil
OptionParser.parse do |parser| OptionParser.parse do |parser|
parser.banner = "Usage: lx_chess [fen]" parser.banner = "Usage: lx_chess [fen]"
parser.on("--fen=FEN", "use fen string for board") do |fen_string|
options["fen_string"] = fen_string
end
parser.on("-h", "--help", "Show this help") do parser.on("-h", "--help", "Show this help") do
puts parser puts parser
exit exit
@ -22,14 +18,26 @@ OptionParser.parse do |parser|
exit exit
end end
parser.separator "\nGame options"
# parser.on("-e", "--eval", "Evaluate a given position") do
# options["evaluate"] = "true"
# end
parser.on("-f FEN", "--fen=FEN", "use fen string for board") do |fen_string|
options["fen_string"] = fen_string
end
parser.on("--board-theme=COLOR", "Set the board theme") do |color| parser.on("--board-theme=COLOR", "Set the board theme") do |color|
options["theme"] = color options["theme"] = color
end end
parser.on("--open=PGN", "open a pgn file") do |path| parser.on("-o PGN", "--open=PGN", "open a pgn file") do |path|
options["pgn_path"] = path options["pgn_path"] = path
end end
parser.separator "\nPlayer options"
parser.on("--player-white=PLAYER", "set the type of player (human|computer)") do |player_type| parser.on("--player-white=PLAYER", "set the type of player (human|computer)") do |player_type|
case player_type case player_type
when /c(omputer)?/i when /c(omputer)?/i
@ -44,6 +52,8 @@ OptionParser.parse do |parser|
end end
end end
parser.separator "\nDebugging options"
parser.on("--log=FILE", "log to file") do |file_Path| parser.on("--log=FILE", "log to file") do |file_Path|
options["log_file"] = file_Path options["log_file"] = file_Path
end end

View file

@ -86,15 +86,15 @@ module LxChess
# Generate an array of moves (from => to) which result in an even or best score # Generate an array of moves (from => to) which result in an even or best score
# TODO: promotion, pruning # TODO: promotion, pruning
def best_moves(game : Game, turn : Int8? = nil, depth = 0, root_tree : MoveTree? = nil) # : Array(Array(Int16)) def best_moves(game : Game, turn : Int8? = nil, depth = 0, root_tree : MoveTree? = nil) # : Array(Array(Int16))
turn = turn || game.turn Log.debug { "=================================== Best moves ===================================" }
Log.debug { "Evaluating best moves for turn: #{turn}, depth: #{depth}..." } turn = turn || game.turn
if root_tree.nil? if root_tree.nil?
root_tree = MoveTree.new score: board_score(game, turn), turn: turn root_tree = MoveTree.new score: board_score(game, turn), turn: turn
end end
Log.debug { "Current Board score: #{root_tree.score}, turn: #{turn}" } Log.debug { "Evaluating best moves for turn: #{turn}, depth: #{depth}, current score: #{root_tree.score} ..." }
move_sets(game, turn).each do |set| move_sets(game, turn).each do |set|
origin = set.piece.index origin = set.piece.index
@ -109,17 +109,27 @@ module LxChess
end end
end end
Log.debug { "Considered #{root_tree.branches.size} moves at depth #{depth}" } Log.debug { "\n" + root_tree.to_s(IO::Memory.new).to_s }
Log.debug { "Found #{root_tree.branches.size} moves" }
Log.debug do
current_bests = root_tree.best_branches.map { |b| [b[0], b[1]].map { |c| game.board.cord(c) }.join(" => ") }.join(", ")
"current best branches: #{current_bests}"
end
if depth < 2 if depth < 2
root_tree.branches.each do |branch| root_tree.branches.each do |branch|
from, to, tree = branch from, to, tree = branch
Log.debug { "Considering #{game.board.cord(from)} => #{game.board.cord(to)} ..." } Log.debug do
"Considering branch: #{game.board.cord(from)} => #{game.board.cord(to)} ..."
end
game.tmp_move(from: from, to: to) do game.tmp_move(from: from, to: to) do
best_moves(game, turn, depth + 1, tree) best_moves(game, turn, depth + 1, tree)
if tree.score > root_tree.score if tree.score > root_tree.score
Log.debug { "Found better branch: #{game.board.cord(from)} => #{game.board.cord(to)} : #{root_tree.score} => #{tree.score}" }
root_tree.score = tree.score root_tree.score = tree.score
root_tree.best_branches.truncate(0, 0) root_tree.best_branches.truncate(0, 0)
end end

View file

@ -33,5 +33,18 @@ module LxChess
@branches << other @branches << other
end end
def to_s(io = STDOUT, depth = 0)
if parent
io << parent.to_s(io)
io << "\n"
depth += 1
end
io << "Tree: score - " << @score << "\n"
@branches.each do |(from, to, tree)|
io << " " << Board.cord(from) << " => " << Board.cord(to) << " : " << tree.score << "\n"
end
io
end
end end
end end