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
game.cpp
board.cpp
unitv.cpp
)
add_library( game ${SOURCES} )

View file

@ -13,7 +13,7 @@ CBoard::CBoard( unsigned int c, unsigned int r )
// 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 )
, rows ( r )
, 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
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) )
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
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) )
return square_invalid;
unit_c old = board[r*c];
unitType_c old = board[r*c];
board[r*c] = n;
return old;
}

View file

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

View file

@ -6,10 +6,13 @@
#include "vector2.h"
// 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
template < unit_c unit_cype >
template < unitType_c unit_type >
class CUnit
{
public:
@ -17,6 +20,8 @@ public:
CUnit() = default;
virtual ~CUnit() = default;
virtual unitVis_c getVisual() const = 0;
private:
// All units must have position

View file

@ -1,15 +1,30 @@
#include "unitv.h"
// V unit
class CUnitV
: public CUnit<'V'>
#include <map> // for std::map
CUnitV::CUnitV()
: dir(dir_t::S)
{
public:
CUnitV() = default;
virtual ~CUnitV() = default;
private:
}
// V also has a direction
char dir;
};
unitVis_c CUnitV::getVisual() const
{
// 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_
#define _UNITV_H_
#include "unit.h"
// V unit
class CUnitV
: public CUnit<'V'>
{
public:
CUnitV() = default;
CUnitV();
virtual ~CUnitV() = default;
virtual unitVis_c getVisual() const;
private:
// V also has a direction
char dir;
dir_t dir;
};
#endif
#endif //_UNITV_H_