Skip to content

Commit

Permalink
Merge pull request #1278 from waterhorse1:master
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 677793409
Change-Id: Id802f87f995c7ae59b35e638e0c6043313b0fc0d
  • Loading branch information
lanctot committed Sep 23, 2024
2 parents d1bddb3 + aa68d4a commit 08543bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion open_spiel/games/breakthrough/breakthrough.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,19 +391,22 @@ int BreakthroughGame::NumDistinctActions() const {

std::string BreakthroughState::Serialize() const {
std::string str = "";
// Serialize the board state.
for (int r = 0; r < rows_; r++) {
for (int c = 0; c < cols_; c++) {
absl::StrAppend(&str, CellToString(board(r, c)));
}
}
// Append current player information.
absl::StrAppend(&str, std::to_string(cur_player_));
return str;
}

std::unique_ptr<State> BreakthroughGame::DeserializeState(
const std::string& str) const {
std::unique_ptr<State> state = NewInitialState();

if (str.length() != rows_ * cols_) {
if (str.length() != rows_ * cols_ + 1) {
SpielFatalError("Incorrect number of characters in string.");
return std::unique_ptr<State>();
}
Expand Down Expand Up @@ -434,6 +437,8 @@ std::unique_ptr<State> BreakthroughGame::DeserializeState(
}
}

// -'0' to get the int value.
bstate->Set_cur_player(str.at(i) - '0');
return state;
}

Expand Down
6 changes: 6 additions & 0 deletions open_spiel/games/breakthrough/breakthrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class BreakthroughState : public State {
bool InBounds(int r, int c) const;
void SetBoard(int r, int c, CellState cs) { board_[r * cols_ + c] = cs; }
void SetPieces(int idx, int value) { pieces_[idx] = value; }
void Set_cur_player(int player) { cur_player_ = player; }
CellState board(int row, int col) const { return board_[row * cols_ + col]; }
int pieces(int idx) const { return pieces_[idx]; }
int rows() const { return rows_; }
Expand Down Expand Up @@ -97,6 +98,11 @@ class BreakthroughGame : public Game {
return std::unique_ptr<State>(
new BreakthroughState(shared_from_this(), rows_, cols_));
}
std::unique_ptr<State> NewInitialState(const std::string& str)
const override {
return DeserializeState(str);
}

int NumPlayers() const override { return kNumPlayers; }
double MinUtility() const override { return -1; }
absl::optional<double> UtilitySum() const override { return 0; }
Expand Down

0 comments on commit 08543bd

Please sign in to comment.