Add new game string outputer and use it.

modify some of the other outputters to look better
This commit is contained in:
Marc Di Luzio 2014-12-16 13:13:02 +00:00
parent 24ac058475
commit 002c1e5927
5 changed files with 39 additions and 2 deletions

View file

@ -218,6 +218,9 @@ int CTTRTSGame::SimulateToNextTurn()
pair.order = COrder();
}
// Increment the current turn
turn++;
return error;
}
@ -352,3 +355,26 @@ 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;
}

View file

@ -36,6 +36,7 @@ public:
CTTRTSGame( ucoord_t c, ucoord_t r )
: dimensions( c,r )
, turn (0)
{
}
@ -80,6 +81,9 @@ public:
// best practice would be to call with GetNumUnits() == 0
Team CheckForWin() const;
// Get the game information as a string
std::string GetStateAsString() const;
private:
// Verify any order - non-zero is error
@ -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_

View file

@ -45,7 +45,7 @@ inline bool COrder::operator== ( const COrder& rhs ) const
// Typedef a vector of orders
typedef std::vector<COrder> 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 );

View file

@ -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 );

View file

@ -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<<game.GetStateAsString()<<std::endl;
}
return 0;