diff --git a/game/game.cpp b/game/game.cpp index d5eb170..253f0a4 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -218,6 +218,9 @@ int CTTRTSGame::SimulateToNextTurn() pair.order = COrder(); } + // Increment the current turn + turn++; + return error; } @@ -351,4 +354,27 @@ Team CTTRTSGame::CheckForWin() const } return winningTeam; +} + +// Get the game information as a string +std::string CTTRTSGame::GetStateAsString() const +{ + // Print out the header + char header[64]; + snprintf(header, 512, GAME_HEADER_FORMATTER , turn, dimensions.x, dimensions.y ); + + // Gather unit information + std::string units; + for ( const OrderUnitPair& pair : m_OrderUnitPairs ) + { + units += GetStringFromUnit(pair.unit); + units += '\n'; + } + + // Append the header and units + std::string state(header); + state += '\n'; + state += units; + + return state; } \ No newline at end of file diff --git a/game/game.h b/game/game.h index fbc9855..7527768 100644 --- a/game/game.h +++ b/game/game.h @@ -36,6 +36,7 @@ public: CTTRTSGame( ucoord_t c, ucoord_t r ) : dimensions( c,r ) + , turn (0) { } @@ -79,6 +80,9 @@ public: // Note: this function will return invalid a draw was reached // best practice would be to call with GetNumUnits() == 0 Team CheckForWin() const; + + // Get the game information as a string + std::string GetStateAsString() const; private: @@ -102,6 +106,11 @@ private: // Dimensions of the game uvector2 dimensions; + + // Int to store the current turn + unsigned int turn; }; +#define GAME_HEADER_FORMATTER "===== GAME TURN =====\nSIZE:[%u,%u]\nTURN:%u" + #endif //_GAME_H_ diff --git a/game/order.h b/game/order.h index cb1cf80..b35719b 100644 --- a/game/order.h +++ b/game/order.h @@ -45,7 +45,7 @@ inline bool COrder::operator== ( const COrder& rhs ) const // Typedef a vector of orders typedef std::vector COrderVector; -#define ORDER_FORMATTER "ORDER co:%c un:%u" +#define ORDER_FORMATTER "ORDER:%c id:%u" // string <--> order conversion functions std::string GetStringFromOrder(const COrder& order ); diff --git a/game/unit.h b/game/unit.h index 9c1ee0d..61162e3 100644 --- a/game/unit.h +++ b/game/unit.h @@ -100,7 +100,7 @@ inline bool CUnit::valid() const CUnit GetUnitFromVis( unitVis_c vis ); // Unit <--> string conversion functions -#define UNIT_FORMATTER "UNIT id:%u tm:%u pl:%u vs:%c dr:%c ps:%u,%u" +#define UNIT_FORMATTER "UNIT:%u tm:%u pl:%u vs:%c dr:%c ps:[%u,%u]" std::string GetStringFromUnit(const CUnit& unit ); CUnit GetUnitFromString(const std::string& unit ); diff --git a/test/test.cpp b/test/test.cpp index 8ea3136..8637e03 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -180,6 +180,8 @@ const char* tests() if ( game.CheckForWin() != Team::Blue ) return "Game failed to recognise a win for the right Team"; + + std::cout<