Finalise implementation of reading in a game from a string

This commit is contained in:
Marc Di Luzio 2014-12-16 13:13:02 +00:00
parent 002c1e5927
commit a75e6e290d
8 changed files with 178 additions and 125 deletions

View file

@ -5,6 +5,9 @@
#include "gametypes.h"
#include "order.h"
#define GAME_HEADER_FORMATTER "===== %s =====\nSIZE:[%u,%u]\nTURN:%u"
#define GAME_HEADER_DELIMITER "~~~~\n"
// Type for order and unit pairs
struct OrderUnitPair
{
@ -20,61 +23,41 @@ struct OrderUnitPair
, order ( o )
{}
// Move asignment operator
inline OrderUnitPair& operator=( OrderUnitPair&& rhs ) { this->unit = std::move(rhs.unit);this->order = rhs.order;rhs.order = COrder(); return *this; }
// Move assignment operator
inline OrderUnitPair& operator=( OrderUnitPair&& rhs )
{
this->unit = std::move(rhs.unit);
this->order = std::move(rhs.order);
return *this;
}
CUnit unit; // The unit
COrder order; // Order for this unit from this turn
CUnit unit; // The unit
COrder order; // Order for this unit from this turn
};
// Typedef for a vector of these unit pairs
typedef std::vector< OrderUnitPair > OrderUnitPairVector;
// Full TTRTS Game class
// Stores information about the game
// Can convert from a string or to a string
class CTTRTSGame
{
public:
CTTRTSGame( ucoord_t c, ucoord_t r )
: dimensions( c,r )
, turn (0)
{
// Get the game information as a string
static CTTRTSGame CreateFromString( const std::string& input );
}
// Constructors
CTTRTSGame( ucoord_t c, ucoord_t r );
CTTRTSGame(CTTRTSGame&& game);
// Default dtor
~CTTRTSGame() = default;
// move asignment operator
CTTRTSGame& operator=(CTTRTSGame&& game);
// Issue orders to the game, returns non-zero if orders are incorrect
int IssueOrders( player_id_t player, const std::string& orders );
int IssueOrders( player_id_t player, const COrderVector& orders );
// Issue a single order, returns non-zero for rejection
int IssueOrder( player_id_t player, const COrder& order );
// Simulate and progress to the next turn
// Returns non-zero if simulation failed
int SimulateToNextTurn();
// Add a unit, nonzero return value indicates error
int AddUnit( CUnit&& unit );
// Add a units, nonzero return value indicates error
int AddUnits( CUnitVector&& units );
// Get the number of units
inline unsigned int GetNumUnits() const { return m_OrderUnitPairs.size(); }
// Get unit by index as above (not unit ID)
inline const CUnit& GetUnitByIndex( unsigned int i ) const { return m_OrderUnitPairs[i].unit; }
// Get unit by unit ID
const CUnit& GetUnitByIDConst( unit_id_t id ) const;
// Get orders by index as above
inline const COrder& GetOrdersByIndex( unsigned int i ) const { return m_OrderUnitPairs[i].order; }
// Get dimensions
inline const uvector2 &GetDimensions() const { return dimensions; }
// Simulate and progress to the next turn
// Returns non-zero if simulation failed
int SimulateToNextTurn();
// Check for a win, returns invalid for no win state reached
// Note: this function will return invalid a draw was reached
@ -83,13 +66,40 @@ public:
// Get the game information as a string
std::string GetStateAsString() const;
// Issue orders to the game, returns non-zero if orders are incorrect
int IssueOrders( player_id_t player, const std::string& orders );
int IssueOrders( player_id_t player, const COrderVector& orders );
int IssueOrder( player_id_t player, const COrder& order );
// Add a units to the game, nonzero return value indicates error
int AddUnit( CUnit&& unit );
int AddUnits( CUnitVector&& units );
// Get the number of units
inline unsigned int GetNumUnits() const { return m_OrderUnitPairs.size(); }
// Get unit and orderby index as above (not unit ID)
inline const CUnit& GetUnitByIndex( unsigned int i ) const { return m_OrderUnitPairs[i].unit; }
inline const COrder& GetOrdersByIndex( unsigned int i ) const { return m_OrderUnitPairs[i].order; }
// Get a unit by it's ID
const CUnit& GetUnitByIDConst( unit_id_t id ) const;
// Get dimensions
inline const uvector2 &GetDimensions() const { return dimensions; }
// Set the game name
// NOTE: Names with spaces not allowed
inline std::string SetName( const std::string& in ) { return (name = in); }
// Set the turn of the game
inline int SetTurn( int in ) { return (turn = in); }
private:
// Verify any order - non-zero is error
// Verify any order or position - non-zero is error
int VerifyOrder( player_id_t player, const COrder& order ) const;
// Verify Position - non-zero is error
int VerifyPos( uvector2 vec ) const;
// Get a units new position after an order
@ -98,19 +108,11 @@ private:
// Get unit by unit ID
CUnit& GetUnitByID( unit_id_t id );
// Vector to store points to all units
OrderUnitPairVector m_OrderUnitPairs;
// List of dead units
CUnitVector m_deadUnits;
// Dimensions of the game
uvector2 dimensions;
// Int to store the current turn
unsigned int turn;
std::string name; // Game Name
unsigned int turn; // Int to store the current turn
uvector2 dimensions; // Dimensions of the game
OrderUnitPairVector m_OrderUnitPairs; // Vector to store all units and orders
};
#define GAME_HEADER_FORMATTER "===== GAME TURN =====\nSIZE:[%u,%u]\nTURN:%u"
#endif //_GAME_H_