Skip to content

Commit

Permalink
Added support for built-in default opening book.
Browse files Browse the repository at this point in the history
  • Loading branch information
romstad committed Apr 28, 2021
1 parent dbdb4d6 commit 529b11b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Tord Romstad"]
version = "0.6.0"

[deps]
Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DefaultApplication = "3f0dd361-4fe0-5fc6-8523-80b14ec94d85"
Expand Down
34 changes: 23 additions & 11 deletions src/book.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Book

using Artifacts
using ..Chess, ..Chess.PGN, Dates, Printf, StatsBase

export BookEntry
Expand Down Expand Up @@ -530,15 +531,20 @@ end


"""
findbookentries(b::Board, bookfilename::String)
findbookentries(key::UInt64, bookfilename::String)
findbookentries(b::Board, bookfilename = nothing)
findbookentries(key::UInt64, bookfilename = nothing)
Returns all book entries for the given board or key.
If `bookfilename` is nothing, use the default built-in opening book.
The return value is a (possibly empty) `Vector{BookEntry}`, sorted by
descending scores.
"""
function findbookentries(key::UInt64, bookfilename::String)::Vector{BookEntry}
function findbookentries(key::UInt64, bookfilename=nothing)::Vector{BookEntry}
if isnothing(bookfilename)
bookfilename = joinpath(artifact"book", "default-book.obk")
end
result = Vector{BookEntry}()
open(bookfilename, "r") do f
compact = read(f, UInt8) == 1
Expand All @@ -559,18 +565,20 @@ function findbookentries(key::UInt64, bookfilename::String)::Vector{BookEntry}
sort(result, by = e -> -e.score)
end

function findbookentries(b::Board, bookfilename::String)::Vector{BookEntry}
function findbookentries(b::Board, bookfilename=nothing)::Vector{BookEntry}
findbookentries(b.key, bookfilename)
end


"""
printbookentries(b::Board, bookfilename::String)
printbookentries(b::Board, bookfilename = nothing)
Pretty-print the move entries for the provided board.
If `bookfilename` is nothing, use the default built-in opening book.
"""
function printbookentries(b::Board, bookfilename::String)
function printbookentries(b::Board, bookfilename=nothing)
entries = findbookentries(b, bookfilename)
scoresum = sum(map(e -> e.score, entries))
for e entries
Expand All @@ -592,8 +600,12 @@ end


"""
pickbookmove(b::Board, bookfilename::String;
minscore = 0, mingamecount = 1)
pickbookmove(
b::Board;
bookfile = nothing,
minscore = 0,
mingamecount = 1
)
Picks a book move for the board `b`, returning `nothing` when out of book.
Expand All @@ -602,14 +614,14 @@ The move is selected with probabilities given by the `score` slots in the
to exclude moves with low score or low play counts.
"""
function pickbookmove(
b::Board,
bookfilename::String;
b::Board;
bookfile = nothing,
minscore = 0,
mingamecount = 1,
)::Union{Move,Nothing}
entries = filter(
e -> e.wins + e.draws + e.losses >= mingamecount && e.score >= minscore,
findbookentries(b, bookfilename),
findbookentries(b, bookfile),
)
if length(entries) == 0
nothing
Expand Down

0 comments on commit 529b11b

Please sign in to comment.