Skip to content
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

Add *abrupt* phantom tic-tac-toe #1306

Merged
merged 4 commits into from
Apr 7, 2025
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
36 changes: 28 additions & 8 deletions open_spiel/games/phantom_ttt/phantom_ttt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const GameType kGameType{
/*provides_observation_string=*/true,
/*provides_observation_tensor=*/true,
/*parameter_specification=*/
{{"obstype", GameParameter(std::string(kDefaultObsType))}}};
{{"obstype", GameParameter(std::string(kDefaultObsType))},
{"gameversion", GameParameter(std::string(kDefaultGameVersion))}}};

const GameType kImperfectRecallGameType{
/*short_name=*/"phantom_ttt_ir",
Expand All @@ -69,7 +70,8 @@ const GameType kImperfectRecallGameType{
/*provides_observation_string=*/false,
/*provides_observation_tensor=*/false,
/*parameter_specification=*/
{{"obstype", GameParameter(std::string(kDefaultObsType))}}};
{{"obstype", GameParameter(std::string(kDefaultObsType))},
{"gameversion", GameParameter(std::string(kDefaultGameVersion))}}};

std::shared_ptr<const Game> Factory(const GameParameters& params) {
return std::shared_ptr<const Game>(new PhantomTTTGame(params, kGameType));
Expand All @@ -93,8 +95,8 @@ ImperfectRecallPTTTGame::ImperfectRecallPTTTGame(const GameParameters& params)
: PhantomTTTGame(params, kImperfectRecallGameType) {}

PhantomTTTState::PhantomTTTState(std::shared_ptr<const Game> game,
ObservationType obs_type)
: State(game), state_(game), obs_type_(obs_type) {
GameVersion game_version, ObservationType obs_type)
: State(game), state_(game), game_version_(game_version), obs_type_(obs_type) {
std::fill(begin(x_view_), end(x_view_), CellState::kEmpty);
std::fill(begin(o_view_), end(o_view_), CellState::kEmpty);
if (obs_type_ == ObservationType::kRevealNumTurns) {
Expand All @@ -114,10 +116,19 @@ void PhantomTTTState::DoApplyAction(Action move) {
Player cur_player = CurrentPlayer();
auto& cur_view = cur_player == 0 ? x_view_ : o_view_;

// Two cases: either there is a mark already there, or not.
if (state_.BoardAt(move) == CellState::kEmpty) {
// No mark on board, so play this normally.
state_.ApplyAction(move);
// Either occupied or not
if (game_version_ == GameVersion::kClassicalPhantomTicTacToe) {
if (state_.BoardAt(move) == CellState::kEmpty) {
state_.ApplyAction(move);
}
} else {
SPIEL_CHECK_EQ(game_version_, GameVersion::kAbruptPhantomTicTacToe);
if (state_.BoardAt(move) == CellState::kEmpty) {
state_.ApplyAction(move);
} else {
// switch the current player
state_.ChangePlayer();
}
}

// Update current player's view, and action sequence.
Expand Down Expand Up @@ -311,6 +322,15 @@ PhantomTTTGame::PhantomTTTGame(const GameParameters& params, GameType game_type)
} else {
SpielFatalError(absl::StrCat("Unrecognized observation type: ", obs_type));
}

std::string game_version = ParameterValue<std::string>("gameversion");
if (game_version == "classical") {
game_version_ = GameVersion::kClassicalPhantomTicTacToe;
} else if (game_version == "abrupt") {
game_version_ = GameVersion::kAbruptPhantomTicTacToe;
} else {
SpielFatalError(absl::StrCat("Unrecognized game version: ", game_version));
}
}

std::vector<int> PhantomTTTGame::InformationStateTensorShape() const {
Expand Down
33 changes: 27 additions & 6 deletions open_spiel/games/phantom_ttt/phantom_ttt.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,28 @@
//
// Parameters:
/// "obstype", string, "reveal-nothing" (default) or "reveal-numturns"
/// "gameversion", string, "classical" (default) or "abrupt"

namespace open_spiel {
namespace phantom_ttt {

inline constexpr const char* kDefaultObsType = "reveal-nothing";
inline constexpr const char* kDefaultGameVersion = "classical";

enum class ObservationType {
kRevealNothing,
kRevealNumTurns,
};

enum class GameVersion {
kAbruptPhantomTicTacToe,
kClassicalPhantomTicTacToe,
};

// State of an in-play game.
class PhantomTTTState : public State {
public:
PhantomTTTState(std::shared_ptr<const Game> game, ObservationType obs_type);
PhantomTTTState(std::shared_ptr<const Game> game, GameVersion game_version, ObservationType obs_type);

// Forward to underlying game state
Player CurrentPlayer() const override { return state_.CurrentPlayer(); }
Expand Down Expand Up @@ -84,6 +91,7 @@ class PhantomTTTState : public State {

tic_tac_toe::TicTacToeState state_;
ObservationType obs_type_;
GameVersion game_version_;
int bits_per_action_;
int longest_sequence_;

Expand All @@ -99,7 +107,7 @@ class PhantomTTTGame : public Game {
PhantomTTTGame(const GameParameters& params, GameType game_type);
std::unique_ptr<State> NewInitialState() const override {
return std::unique_ptr<State>(
new PhantomTTTState(shared_from_this(), obs_type_));
new PhantomTTTState(shared_from_this(), game_version_, obs_type_));
}
int NumDistinctActions() const override {
return game_->NumDistinctActions();
Expand All @@ -121,10 +129,12 @@ class PhantomTTTGame : public Game {
int MaxGameLength() const override { return tic_tac_toe::kNumCells * 2 - 1; }

ObservationType obs_type() const { return obs_type_; }
GameVersion game_version() const { return game_version_; }

private:
std::shared_ptr<const tic_tac_toe::TicTacToeGame> game_;
ObservationType obs_type_;
GameVersion game_version_;
int bits_per_action_;
int longest_sequence_;
};
Expand All @@ -133,9 +143,8 @@ class PhantomTTTGame : public Game {
// http://mlanctot.info/files/papers/12icml-ir.pdf
class ImperfectRecallPTTTState : public PhantomTTTState {
public:
ImperfectRecallPTTTState(std::shared_ptr<const Game> game,
ObservationType obs_type)
: PhantomTTTState(game, obs_type) {}
ImperfectRecallPTTTState(std::shared_ptr<const Game> game, GameVersion game_version, ObservationType obs_type)
: PhantomTTTState(game, game_version, obs_type) {}
std::string InformationStateString(Player player) const override {
SPIEL_CHECK_GE(player, 0);
SPIEL_CHECK_LT(player, num_players_);
Expand All @@ -151,7 +160,7 @@ class ImperfectRecallPTTTGame : public PhantomTTTGame {
explicit ImperfectRecallPTTTGame(const GameParameters& params);
std::unique_ptr<State> NewInitialState() const override {
return std::unique_ptr<State>(
new ImperfectRecallPTTTState(shared_from_this(), obs_type()));
new ImperfectRecallPTTTState(shared_from_this(), game_version(), obs_type()));
}
};

Expand All @@ -167,6 +176,18 @@ inline std::ostream& operator<<(std::ostream& stream,
}
}

inline std::ostream& operator<<(std::ostream& stream,
const GameVersion& game_version) {
switch (game_version) {
case GameVersion::kClassicalPhantomTicTacToe:
return stream << "Classical Phantom Tic Tac Toe";
case GameVersion::kAbruptPhantomTicTacToe:
return stream << "Abrupt Phantom Tic Tac Toe";
default:
SpielFatalError("Unknown game version");
}
}

} // namespace phantom_ttt
} // namespace open_spiel

Expand Down
1 change: 1 addition & 0 deletions open_spiel/games/tic_tac_toe/tic_tac_toe.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class TicTacToeState : public State {
return board_[row * kNumCols + column];
}
Player outcome() const { return outcome_; }
void ChangePlayer() { current_player_ = current_player_ == 0 ? 1 : 0; }

// Only used by Ultimate Tic-Tac-Toe.
void SetCurrentPlayer(Player player) { current_player_ = player; }
Expand Down
4 changes: 2 additions & 2 deletions open_spiel/integration_tests/playthroughs/phantom_ttt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GameType.information = Information.IMPERFECT_INFORMATION
GameType.long_name = "Phantom Tic Tac Toe"
GameType.max_num_players = 2
GameType.min_num_players = 2
GameType.parameter_specification = ["obstype"]
GameType.parameter_specification = ["gameversion", "obstype"]
GameType.provides_information_state_string = True
GameType.provides_information_state_tensor = True
GameType.provides_observation_string = True
Expand All @@ -19,7 +19,7 @@ GameType.utility = Utility.ZERO_SUM
NumDistinctActions() = 9
PolicyTensorShape() = [9]
MaxChanceOutcomes() = 0
GetParameters() = {obstype=reveal-nothing}
GetParameters() = {gameversion=classical,obstype=reveal-nothing}
NumPlayers() = 2
MinUtility() = -1.0
MaxUtility() = 1.0
Expand Down
4 changes: 2 additions & 2 deletions open_spiel/integration_tests/playthroughs/phantom_ttt_ir.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GameType.information = Information.IMPERFECT_INFORMATION
GameType.long_name = "Phantom Tic Tac Toe with Imperfect Recall"
GameType.max_num_players = 2
GameType.min_num_players = 2
GameType.parameter_specification = ["obstype"]
GameType.parameter_specification = ["gameversion", "obstype"]
GameType.provides_information_state_string = True
GameType.provides_information_state_tensor = False
GameType.provides_observation_string = False
Expand All @@ -19,7 +19,7 @@ GameType.utility = Utility.ZERO_SUM
NumDistinctActions() = 9
PolicyTensorShape() = [9]
MaxChanceOutcomes() = 0
GetParameters() = {obstype=reveal-nothing}
GetParameters() = {gameversion=classical,obstype=reveal-nothing}
NumPlayers() = 2
MinUtility() = -1.0
MaxUtility() = 1.0
Expand Down
Loading