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

new method to return board representation as a string #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/python/src/gamestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ namespace stratega
.def("set_game_type", &SGA::GameState::setGameType, py::arg("gt"), "Sets the type of game (enum type GameType)")
.def("get_game_type", &SGA::GameState::getGameType, "Returns the type of the game, of GameType")

.def("get_board_state", &SGA::GameState::getBoardState, "Returns a string representation of the board")

//Print information
.def("print_state_info", &SGA::GameState::printStateInfo, "Print all the entities of the current state")
.def("print_board", py::overload_cast<int>(&SGA::GameState::printBoard, py::const_), "Print view of the map of the current state")
Expand Down
5 changes: 5 additions & 0 deletions src/stratega/include/Stratega/Representation/GameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ namespace SGA
/// </summary>
void printStateInfo() const;

/// <summary>
/// Return board state as a string
/// </summary>
std::string getBoardState() const;

/// <summary>
// /Print view of the map of the current state
/// </summary>
Expand Down
30 changes: 29 additions & 1 deletion src/stratega/src/Representation/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,36 @@ namespace SGA
}


/* PRINTS */
std::string GameState::getBoardState() const
{
std::string map;
for (size_t y = 0; y < board.getHeight(); ++y)
{
for (size_t x = 0; x < board.getWidth(); ++x)
{
map += gameInfo->getTileType(board.get(static_cast<int>(x), static_cast<int>(y)).getTileTypeID()).getSymbol();
map += " ";

}
map += "\n";
}

for (auto& entity : entities)
{
auto& pos = entity.getPosition();
const char symbol = gameInfo->getEntityType(entity.getEntityTypeID()).getSymbol();
const char ownerID = std::to_string(entity.getOwnerID())[0];
const int entityMapIndex = static_cast<int>((pos.y * static_cast<double>(board.getWidth()) + pos.x) * 3 + pos.y);

map[entityMapIndex] = symbol;

if (!entity.isNeutral())
map[static_cast<int>(static_cast<int>(entityMapIndex) + 1)] = ownerID;
}
return map;
}

/* PRINTS */

void GameState::printStateInfo() const
{
Expand Down