More skeleton code for simulating a turn

Use enum classes for safe enums
This commit is contained in:
Marc Di Luzio 2014-12-16 13:13:01 +00:00
parent b43248e89f
commit e310acfaf8
9 changed files with 983 additions and 25 deletions

View file

@ -98,7 +98,7 @@ int CTTRTSGame::AddUnits( CUnitVector&& units )
}
// Verify any order
int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order )
int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order ) const
{
// Grab the unit ID
const unit_id_t unitID = order.unit;
@ -118,17 +118,68 @@ int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order )
return unitFound;
}
// Get unit by unit ID
const CUnit& CTTRTSGame::GetUnitByID( unit_id_t id ) const
{
CUnitVector::const_iterator it;
for ( it = m_allUnits.begin(); it != m_allUnits.end(); it++ )
{
// Attempt the unit add
if ( (*it).getID() )
return *it;
}
// Return an invalid unit
static CUnit invalid_unit;
return invalid_unit;
}
// Verify an order unit pair
int CTTRTSGame::VerifyOrderUnitPair( const OrderUnitPair& pair ) const
{
switch ( pair.order.order )
{
case order_c::F:
{
// Verify new unit position will be on the board
}
break;
case order_c::L:
case order_c::R:
case order_c::A:
// Nothing needed here, orders can always be carried out
break;
}
return 0;
}
// Simulate all movements
int CTTRTSGame::SimulateMovements()
{
// Grab all movement orders
COrderVector movements;
OrderUnitPairVector movements;
// Grab all movement orders
for ( COrderVector::const_iterator it = m_orders.begin(); it != m_orders.end(); it++ )
{
if( isMovementOrder(*it) )
movements.push_back(*it);
{
const OrderUnitPair pair = { *it, GetUnitByID((*it).unit) };
movements.push_back(pair);
}
}
// Remove all orders with straight up impossible movements
for ( OrderUnitPairVector::const_iterator it = movements.begin(); it != movements.end(); it++ )
{
if( VerifyOrderUnitPair(*it) )
{
}
}
// Calculate movements
return 0;

View file

@ -9,6 +9,15 @@
typedef std::vector< CUnit > CUnitVector;
// Type for order and unit pairs
struct OrderUnitPair
{
const COrder& order;
const CUnit& unit;
};
typedef std::vector< OrderUnitPair > OrderUnitPairVector;
class CTTRTSGame
{
public:
@ -44,6 +53,9 @@ public:
// Get unit by index as above (not unit ID)
inline const CUnit& GetUnitByIndex( unsigned int i ) const { return m_allUnits[i]; }
// Get unit by unit ID
const CUnit& GetUnitByID( unit_id_t id ) const;
// Get the number of order
inline unsigned int GetNumOrders() const { return m_orders.size(); }
@ -51,7 +63,7 @@ public:
inline const COrder& GetOrdersByIndex( unsigned int i ) const { return m_orders[i]; }
// Get dimentions
inline const uvector2& GetDimentions() const { return dimentions; };
inline const uvector2& GetDimentions() const { return dimentions; }
private:
@ -61,11 +73,13 @@ private:
// Simulate all actions
int SimulateActions();
// Verify any order
int VerifyOrder( player_id_t player, const COrder& order );
int VerifyOrderUnitPair( const OrderUnitPair& pair ) const;
// Verify any order
int VerifyUnit( const CUnit& unit );
int VerifyOrder( player_id_t player, const COrder& order ) const;
// Verify any order
int VerifyUnit( const CUnit& unit ) const;
// Vector to store points to all units
CUnitVector m_allUnits;
@ -77,4 +91,4 @@ private:
uvector2 dimentions;
};
#endif //_GAME_H_
#endif //_GAME_H_

View file

@ -8,7 +8,7 @@ std::string GetStringFromOrder(const COrder& order )
std::string ret;
ret += std::to_string(order.unit);
ret += ORDER_DELIMITER;
ret += order.order;
ret += (char)order.order;
return ret;
}
@ -30,7 +30,7 @@ COrder GetOrderFromString( const std::string& _order )
order.erase(0, pos + 1);
// Next single char is the order
ret.order = order[0];
ret.order = (order_c)order[0];
}
return ret;
@ -56,4 +56,4 @@ bool isActionOrder( const COrder& order )
return true;
}
return false;
}
}

View file

@ -9,20 +9,26 @@
#define ORDER_DELIMITER ' '
// Type for all orders ( as a char )
typedef char order_c;
enum class order_c : char
{
F = 'F',
L = 'L',
R = 'R',
A = 'A'
};
// Movement orders
static const order_c sk_movementOrders[] =
{
'F', // Forward
order_c::F, // Forward
};
// Action orders
static const order_c sk_actionOrders[] =
{
'L', // Left
'R', // Right
'A', // Attack
order_c::L, // Left
order_c::R, // Right
order_c::A, // Attack
};
// Container for an order
@ -56,4 +62,4 @@ COrder GetOrderFromString( const std::string& order );
bool isMovementOrder( const COrder& order );
bool isActionOrder( const COrder& order );
#endif //_ORDERS_H_
#endif //_ORDERS_H_