Framework for movement and action orders, plus some better organisation of the test code

This commit is contained in:
Marc Di Luzio 2014-12-16 13:13:00 +00:00
parent 008739dee6
commit 72b35dbc06
5 changed files with 115 additions and 44 deletions

View file

@ -122,11 +122,31 @@ int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order )
// Simulate all movements // Simulate all movements
int CTTRTSGame::SimulateMovements() int CTTRTSGame::SimulateMovements()
{ {
// Grab all movement orders
COrderVector movements;
for ( COrderVector::const_iterator it = m_orders.begin(); it != m_orders.end(); it++ )
{
if( isMovementOrder(*it) )
movements.push_back(*it);
}
// Calculate movements
return 0; return 0;
} }
// Simulate all actions // Simulate all actions
int CTTRTSGame::SimulateActions() int CTTRTSGame::SimulateActions()
{ {
// Grab all action orders
COrderVector actions;
for ( COrderVector::const_iterator it = m_orders.begin(); it != m_orders.end(); it++ )
{
if( isActionOrder(*it) )
actions.push_back(*it);
}
// Calculate actions
return 0; return 0;
} }

View file

@ -1,7 +1,9 @@
#include "orders.h" #include "orders.h"
#include "mathtypes.h"
// Convert an order to a string // Convert an order to a string
std::string GetStringFromOrder( COrder& order ) std::string GetStringFromOrder(const COrder& order )
{ {
std::string ret; std::string ret;
ret += std::to_string(order.unit); ret += std::to_string(order.unit);
@ -12,8 +14,9 @@ std::string GetStringFromOrder( COrder& order )
} }
// Convert a string to an order // Convert a string to an order
COrder GetOrderFromString( std::string order ) COrder GetOrderFromString( const std::string& _order )
{ {
std::string order = _order;
COrder ret; COrder ret;
int pos = order.find(ORDER_DELIMITER); int pos = order.find(ORDER_DELIMITER);
@ -31,4 +34,26 @@ COrder GetOrderFromString( std::string order )
} }
return ret; return ret;
}
bool isMovementOrder( const COrder& order )
{
const order_c c = order.order;
for ( unsigned int i = 0; i < _countof(sk_movementOrders); i++ )
{
if ( c == sk_movementOrders[i] )
return true;
}
return false;
}
bool isActionOrder( const COrder& order )
{
const order_c c = order.order;
for ( unsigned int i = 0; i < _countof(sk_actionOrders); i++ )
{
if ( c == sk_actionOrders[i] )
return true;
}
return false;
} }

View file

@ -11,6 +11,20 @@
// Type for all orders ( as a char ) // Type for all orders ( as a char )
typedef char order_c; typedef char order_c;
// Movement orders
static const order_c sk_movementOrders[] =
{
'F', // Forward
};
// Action orders
static const order_c sk_actionOrders[] =
{
'L', // Left
'R', // Right
'A', // Attack
};
// Container for an order // Container for an order
struct COrder struct COrder
{ {
@ -36,7 +50,10 @@ typedef std::vector<COrder> COrderVector;
// Order strings stored as simply "[unit id] [order char]" // Order strings stored as simply "[unit id] [order char]"
// string <--> order conversion functions // string <--> order conversion functions
std::string GetStringFromOrder( COrder& order ); std::string GetStringFromOrder(const COrder& order );
COrder GetOrderFromString( std::string order ); COrder GetOrderFromString( const std::string& order );
bool isMovementOrder( const COrder& order );
bool isActionOrder( const COrder& order );
#endif //_ORDERS_H_ #endif //_ORDERS_H_

View file

@ -3,6 +3,11 @@
#include <limits> // std::numeric_limits #include <limits> // std::numeric_limits
#include "stdlib.h" // for size_t
template<class T, size_t N>
constexpr size_t _countof(T (&)[N]) { return N; }
// Coordinate types // Coordinate types
typedef short coord_t; typedef short coord_t;
typedef unsigned short ucoord_t; typedef unsigned short ucoord_t;

View file

@ -4,69 +4,73 @@
#include "board.h" #include "board.h"
#include "orders.h" #include "orders.h"
const char* tests()
// Namespace for testing functions
namespace tests
{ {
// print a board
void debug_print( CBoard& b )
{
for ( unsigned int r = 0; r < b.rows; r++ )
{
for ( unsigned int c = 0; c < b.cols; c++ )
{
std::cout<<(char)(b.get(c,r));
}
std::cout<<std::endl;
}
}
// Test the board data class
void test_CBoard()
{ {
CBoard board = CBoard(10,5); CBoard board = CBoard(10,5);
std::cout<<"Blank board"<<std::endl;
board.clear(); board.clear();
debug_print(board);
std::cout<<"Filled board"<<std::endl;
board.fill(48); board.fill(48);
debug_print(board);
} }
};
// Main program entry point
int main()
{
tests::test_CBoard();
{ {
std::cout<<"Basic V unit construction"<<std::endl;
CUnitV myV; CUnitV myV;
std::cout<<myV.getVisual()<<std::endl; if( myV.getVisual() != 'v' && myV.getVisual() != '<' && myV.getVisual() != '^' && myV.getVisual() != '>' )
return "failed to properly create V unit";
} }
{ {
std::cout<<"CUnit factory V unit construction"<<std::endl;
std::unique_ptr<CUnit> myV = CUnit::getUnitFromVis('v'); std::unique_ptr<CUnit> myV = CUnit::getUnitFromVis('v');
if( myV->getVisual() != 'v' ) if( myV->getVisual() != 'v' )
std::cout<<"ERROR, failed to properly create V unit"<<std::endl; return "failed to properly create V unit";
} }
{ {
std::cout<<"COrder construction and conversion"<<std::endl;
COrder order; COrder order;
order.order = 'F'; order.order = 'F';
order.unit = 10; order.unit = 10;
std::string order_string = GetStringFromOrder(order); std::string order_string = GetStringFromOrder(order);
std::cout<<order_string<<std::endl;
COrder order2 = GetOrderFromString(order_string); COrder order2 = GetOrderFromString(order_string);
if ( order2 != order ) if ( order2 != order )
std::cout<<"ERROR, failed order string conversion test"<<std::endl; return "failed order string conversion test";
} }
{
COrder order;
order.order = 'F';
if (!isMovementOrder(order) )
return "Failed to detect a movement order";
if (isActionOrder(order) )
return "Wrongly detected an action order";
}
{
COrder order;
order.order = 'L';
if (! isActionOrder(order) )
return "Failed to detect a action order";
if (isMovementOrder(order) )
return "Wrongly detected an movement order";
}
return nullptr;
}
// Main program entry point
int main()
{
std::cout<<"Running tests"<<std::endl;
const char* res = tests();
if( res )
{
std::cout<<"Tests failed - "<<res<<std::endl;
return -1;
}
std::cout<<"Tests succeeded"<<std::endl;
return 0;
}; };