Skip to content

implement == and hash for game and state #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/patch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ Base.show(io::IO, g::CxxWrap.StdLib.SharedPtrAllocated{Game}) = print(io, to_str
Base.show(io::IO, s::CxxWrap.StdLib.UniquePtrAllocated{State}) = print(io, to_string(s))
Base.show(io::IO, gp::Union{GameParameterAllocated, GameParameterDereferenced}) = print(io, to_repr_string(gp))

function Base.hash(s::CxxWrap.CxxWrapCore.SmartPointer{T}, h::UInt) where {T<:Union{Game,State}}
hash(to_string(s), h)
end

function Base.:(==)(s::CxxWrap.CxxWrapCore.SmartPointer{T}, ss::CxxWrap.CxxWrapCore.SmartPointer{T}) where {T<:Union{Game, State}}
to_string(s) == to_string(ss)
end

GameParameter(x::Int) = GameParameter(Ref(Int32(x)))

Base.copy(s::CxxWrap.StdLib.UniquePtrAllocated{State}) = deepcopy(s)
Expand Down
6 changes: 6 additions & 0 deletions test/games_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ end

@testset "play kuhn_poker" begin
game = load_game("kuhn_poker")
@test game == load_game("kuhn_poker")
@test hash(game) == hash(load_game("kuhn_poker"))

state = new_initial_state(game)
@test game == load_game("kuhn_poker")
@test hash(game) == hash(load_game("kuhn_poker"))

@test is_chance_node(state) == true
@test chance_outcomes(state) == [0 => 1/3, 1 => 1/3, 2 => 1/3]

Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ function evaluate_bots(
apply_action(state, action)
elseif is_simultaneous_node(state)
chosen_actions = [
legal_actions(state, pid)[pid+1] ? step(bot, state) : INVALID_ACTION
legal_actions(state, pid)[pid+1] ? OpenSpiel.step(bot, state) : INVALID_ACTION
for (pid, bot) in enumerate(bots)
] # in julia, index starts with 1
apply_action(state, chosen_actions)
else
apply_action(state, step(bots[current_player(state) + 1], state))
apply_action(state, OpenSpiel.step(bots[current_player(state) + 1], state))
end
end
returns(state)
Expand Down