Add more code to units to help distinguish between types and visual values

This commit is contained in:
Marc Di Luzio 2014-12-16 13:12:55 +00:00
parent ea426c70c6
commit 9fc5f33de8
8 changed files with 67 additions and 29 deletions

View file

@ -14,6 +14,7 @@ set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11" )
set( SOURCES set( SOURCES
game.cpp game.cpp
board.cpp board.cpp
unitv.cpp
) )
add_library( game ${SOURCES} ) add_library( game ${SOURCES} )

View file

@ -13,7 +13,7 @@ CBoard::CBoard( unsigned int c, unsigned int r )
// constructor // constructor
CBoard::CBoard( unsigned int c, unsigned int r, vunit_c&& b ) CBoard::CBoard( unsigned int c, unsigned int r, vunitType_c&& b )
: cols ( c ) : cols ( c )
, rows ( r ) , rows ( r )
, total ( rows * cols ) , total ( rows * cols )
@ -23,7 +23,7 @@ CBoard::CBoard( unsigned int c, unsigned int r, vunit_c&& b )
} }
// print get a slot on the board // print get a slot on the board
unit_c CBoard::get( const unsigned int c, const unsigned int r ) const unitType_c CBoard::get( const unsigned int c, const unsigned int r ) const
{ {
if ( (r >= rows) || (c >= cols) ) if ( (r >= rows) || (c >= cols) )
return square_invalid; return square_invalid;
@ -32,12 +32,12 @@ unit_c CBoard::get( const unsigned int c, const unsigned int r ) const
} }
// Get a square on the board // Get a square on the board
unit_c CBoard::set( const unsigned int c, const unsigned int r , const unit_c n ) unitType_c CBoard::set( const unsigned int c, const unsigned int r , const unitType_c n )
{ {
if ( (r >= rows) || (c >= cols) ) if ( (r >= rows) || (c >= cols) )
return square_invalid; return square_invalid;
unit_c old = board[r*c]; unitType_c old = board[r*c];
board[r*c] = n; board[r*c] = n;
return old; return old;
} }

View file

@ -7,11 +7,11 @@
#include <limits> // std::numeric_limits #include <limits> // std::numeric_limits
#include <vector> // std::vector #include <vector> // std::vector
typedef std::vector< unit_c > vunit_c; typedef std::vector< unitType_c > vunitType_c;
// Invalid value for the board square // Invalid value for the board square
constexpr unit_c square_invalid = std::numeric_limits<unit_c>::max(); constexpr unitType_c square_invalid = std::numeric_limits<unitType_c>::max();
constexpr unit_c square_empty = 32; // 32 is ascii empty constexpr unitType_c square_empty = 32; // 32 is ascii empty
// Class to store simple data about a board // Class to store simple data about a board
class CBoard class CBoard
@ -26,7 +26,7 @@ public:
CBoard( unsigned int c, unsigned int r ); CBoard( unsigned int c, unsigned int r );
// constructor // constructor
CBoard( unsigned int c, unsigned int r, vunit_c&& b ); CBoard( unsigned int c, unsigned int r, vunitType_c&& b );
// Default destructor // Default destructor
~CBoard() = default; ~CBoard() = default;
@ -35,20 +35,20 @@ public:
inline void clear() { fill(square_empty); } inline void clear() { fill(square_empty); }
// fill the board // fill the board
inline void fill(unit_c v) { std::fill(board.begin(),board.end(),v); }; inline void fill(unitType_c v) { std::fill(board.begin(),board.end(),v); };
// Get a square on the board // Get a square on the board
unit_c get( const unsigned int c, const unsigned int r ) const; unitType_c get( const unsigned int c, const unsigned int r ) const;
// Get the full board // Get the full board
inline const vunit_c& get() const { return board; }; inline const vunitType_c& get() const { return board; };
// Get a square on the board // Get a square on the board
unit_c set( const unsigned int c, const unsigned int r , const unit_c n ); unitType_c set( const unsigned int c, const unsigned int r , const unitType_c n );
private: private:
vunit_c board; // Board data storage vunitType_c board; // Board data storage
}; };
#endif //_BOARD_H_ #endif //_BOARD_H_

View file

@ -6,10 +6,13 @@
#include "vector2.h" #include "vector2.h"
// Type for the unit type-id // Type for the unit type-id
typedef char unit_c; typedef char unitType_c;
// Typedef for unit visual representations
typedef char unitVis_c;
// Base unit type // Base unit type
template < unit_c unit_cype > template < unitType_c unit_type >
class CUnit class CUnit
{ {
public: public:
@ -17,6 +20,8 @@ public:
CUnit() = default; CUnit() = default;
virtual ~CUnit() = default; virtual ~CUnit() = default;
virtual unitVis_c getVisual() const = 0;
private: private:
// All units must have position // All units must have position

View file

@ -1,15 +1,30 @@
#include "unitv.h" #include "unitv.h"
// V unit #include <map> // for std::map
class CUnitV
: public CUnit<'V'> CUnitV::CUnitV()
: dir(dir_t::S)
{ {
public:
CUnitV() = default;
virtual ~CUnitV() = default;
private: }
// V also has a direction unitVis_c CUnitV::getVisual() const
char dir; {
}; // Map of visual representation of unitv
static const std::map< dir_t, unitVis_c > sk_visMap =
{
{dir_t::N,'^'},
{dir_t::E,'>'},
{dir_t::S,'v'},
{dir_t::W,'<'},
};
std::map< dir_t, char >::const_iterator it = sk_visMap.find(dir);
if( it == sk_visMap.end() )
{
return 0;
}
return it->second;
}

View file

@ -1,18 +1,22 @@
#ifndef _UNITV_H_ #ifndef _UNITV_H_
#define _UNITV_H_ #define _UNITV_H_
#include "unit.h"
// V unit // V unit
class CUnitV class CUnitV
: public CUnit<'V'> : public CUnit<'V'>
{ {
public: public:
CUnitV() = default; CUnitV();
virtual ~CUnitV() = default; virtual ~CUnitV() = default;
virtual unitVis_c getVisual() const;
private: private:
// V also has a direction // V also has a direction
char dir; dir_t dir;
}; };
#endif #endif //_UNITV_H_

View file

@ -4,4 +4,12 @@
typedef short coord_t; typedef short coord_t;
typedef unsigned short ucoord_t; typedef unsigned short ucoord_t;
enum dir_t : char
{
N = 'N',
S = 'S',
E = 'E',
W = 'W',
};
#endif //_BASETYPES_H_ #endif //_BASETYPES_H_

View file

@ -2,6 +2,7 @@
#include <iostream> // std::cout #include <iostream> // std::cout
#include "unitv.h"
// Namespace for testing functions // Namespace for testing functions
namespace tests namespace tests
@ -19,7 +20,6 @@ namespace tests
} }
} }
// Test the board data class // Test the board data class
void test_CBoard() void test_CBoard()
{ {
@ -42,4 +42,9 @@ namespace tests
int main() int main()
{ {
tests::test_CBoard(); tests::test_CBoard();
std::cout<<"Testing units"<<std::endl;
CUnitV myV;
std::cout<<myV.getVisual()<<std::endl;
}; };