diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index e780c69..866c16c 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -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} ) \ No newline at end of file diff --git a/game/board.cpp b/game/board.cpp index 6560498..3b257fa 100644 --- a/game/board.cpp +++ b/game/board.cpp @@ -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; } \ No newline at end of file diff --git a/game/board.h b/game/board.h index 53d2e50..109d62b 100644 --- a/game/board.h +++ b/game/board.h @@ -7,11 +7,11 @@ #include // std::numeric_limits #include // 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::max(); -constexpr unit_c square_empty = 32; // 32 is ascii empty +constexpr unitType_c square_invalid = std::numeric_limits::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_ \ No newline at end of file diff --git a/game/unit.h b/game/unit.h index 26b097b..398adc3 100644 --- a/game/unit.h +++ b/game/unit.h @@ -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 diff --git a/game/unitv.cpp b/game/unitv.cpp index 0a84da0..752a60f 100644 --- a/game/unitv.cpp +++ b/game/unitv.cpp @@ -1,15 +1,30 @@ #include "unitv.h" -// V unit -class CUnitV -: public CUnit<'V'> +#include // for std::map + +CUnitV::CUnitV() +: dir(dir_t::S) { -public: - CUnitV() = default; - virtual ~CUnitV() = default; -private: +} - // V also has a direction - char dir; -}; \ No newline at end of file +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; +} \ No newline at end of file diff --git a/game/unitv.h b/game/unitv.h index 261f9aa..baa0911 100644 --- a/game/unitv.h +++ b/game/unitv.h @@ -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 \ No newline at end of file +#endif //_UNITV_H_ \ No newline at end of file diff --git a/maths/basetypes.h b/maths/basetypes.h index 004400b..a0d88c2 100644 --- a/maths/basetypes.h +++ b/maths/basetypes.h @@ -4,4 +4,12 @@ typedef short coord_t; typedef unsigned short ucoord_t; +enum dir_t : char +{ + N = 'N', + S = 'S', + E = 'E', + W = 'W', +}; + #endif //_BASETYPES_H_ \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp index 85142e8..082eb4b 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -2,6 +2,7 @@ #include // std::cout +#include "unitv.h" // Namespace for testing functions namespace tests @@ -19,7 +20,6 @@ namespace tests } } - // Test the board data class void test_CBoard() { @@ -42,4 +42,9 @@ namespace tests int main() { tests::test_CBoard(); + + std::cout<<"Testing units"<