Implement some more base types, including basic units. This should be much closer to final interface

This commit is contained in:
Marc Di Luzio 2014-12-16 13:12:54 +00:00
parent 51cdda821d
commit 64f9638352
9 changed files with 174 additions and 74 deletions

View file

@ -1,7 +1,5 @@
#include "board.h"
#include <iostream> // std::cout
// ----------------------------------------------
// Default constructor for the board
@ -10,29 +8,22 @@ CBoard::CBoard( unsigned int c, unsigned int r )
, rows ( r )
, total ( rows * cols )
{
board = new square_t[total];
fill(square_invalid);
board.resize(total,square_empty);
}
CBoard::~CBoard()
{
delete[] board;
}
// Clear the board
void CBoard::clear()
// constructor
CBoard::CBoard( unsigned int c, unsigned int r, vunit_t&& b )
: cols ( c )
, rows ( r )
, total ( rows * cols )
, board ( std::move(b) )
{
fill(square_empty);
}
// Fill the board
void CBoard::fill(square_t v)
{
std::fill(board,board+total,v);
board.resize(total,square_empty);
}
// print get a slot on the board
square_t CBoard::get( unsigned int c, unsigned int r ) const
unit_t CBoard::get( const unsigned int c, const unsigned int r ) const
{
if ( (r >= rows) || (c >= cols) )
return square_invalid;
@ -40,29 +31,13 @@ square_t CBoard::get( unsigned int c, unsigned int r ) const
return board[r*c];
}
// print a board
void CBoard::debug_print() const
// Get a square on the board
unit_t CBoard::set( const unsigned int c, const unsigned int r , const unit_t n )
{
for ( unsigned int r = 0; r < rows; r++ )
{
for ( unsigned int c = 0; c < cols; c++ )
{
std::cout<<(char)board[r*c];
}
std::cout<<std::endl;
}
}
if ( (r >= rows) || (c >= cols) )
return square_invalid;
// Test the board data class
void tests::test_CBoard()
{
CBoard board = CBoard(20,10);
std::cout<<"Blank board"<<std::endl;
board.clear();
board.debug_print();
std::cout<<"Filled board"<<std::endl;
board.fill(48);
board.debug_print();
unit_t old = board[r*c];
board[r*c] = n;
return old;
}

View file

@ -2,46 +2,53 @@
#define _BOARD_H_
#include "basetypes.h"
#include "unit.h"
#include <limits> // std::numeric_limits
#include <vector> // std::vector
typedef std::vector< unit_t > vunit_t;
// Invalid value for the board square
constexpr unit_t square_invalid = std::numeric_limits<unit_t>::max();
constexpr unit_t square_empty = 32; // 32 is ascii empty
// Class to store simple data about a board
class CBoard
{
public:
// Invalid value for the board square
static const square_t square_invalid = std::numeric_limits<square_t>::max();
static const square_t square_empty = 32; // 32 is ascii empty
// Default constructor
CBoard( unsigned int c, unsigned int r );
~CBoard();
// Print the board
void debug_print() const;
// clear the board
void clear();
// fill the board
void fill(square_t v);
// Get a square on the board
square_t get( unsigned int c, unsigned int r ) const;
private:
const unsigned int cols; // Number of columns
const unsigned int rows; // Number of rows
const unsigned int total; // Total number of pieces
square_t* board; // Board data storage
// constructor
CBoard( unsigned int c, unsigned int r );
// constructor
CBoard( unsigned int c, unsigned int r, vunit_t&& b );
// Default destructor
~CBoard() = default;
// clear the board
inline void clear() { fill(square_empty); }
// fill the board
inline void fill(unit_t v) { std::fill(board.begin(),board.end(),v); };
// Get a square on the board
unit_t get( const unsigned int c, const unsigned int r ) const;
// Get the full board
inline const vunit_t& get() const { return board; };
// Get a square on the board
unit_t set( const unsigned int c, const unsigned int r , const unit_t n );
private:
vunit_t board; // Board data storage
};
// Namespace for testing functions
namespace tests
{
void test_CBoard();
};
#endif //_BOARD_H_

View file

@ -1,4 +1,17 @@
#ifndef _GAME_H_
#define _GAME_H_
#include "board.h"
class CTTRTSGame
{
public:
CTTRTSGame();
~CTTRTSGame() = default;
private:
};
#endif //_GAME_H_

26
game/unit.h Normal file
View file

@ -0,0 +1,26 @@
#ifndef _UNIT_H_
#define _UNIT_H_
#include <string>
#include "vector2.h"
// Type for the unit type-id
typedef char unit_t;
// Base unit type
template < unit_t unit_type >
class CUnit
{
public:
CUnit() = default;
virtual ~CUnit() = default;
private:
// All units must have position
uvector2 pos;
};
#endif //_UNIT_H_

15
game/unitv.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "unitv.h"
// V unit
class CUnitV
: public CUnit<'V'>
{
public:
CUnitV() = default;
virtual ~CUnitV() = default;
private:
// V also has a direction
char dir;
};

20
game/unitv.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef _UNITV_H_
#define _UNITV_H_
// V unit
class CUnitV
: public CUnit<'V'>
{
public:
CUnitV() = default;
virtual ~CUnitV() = default;
virtual std::string&& getDescriptor();
private:
// V also has a direction
char dir;
};
#endif

View file

@ -1,7 +1,7 @@
#ifndef _BASETYPES_H_
#define _BASETYPES_H_
// Type for the board square
typedef short square_t;
typedef short coord_t;
typedef unsigned short ucoord_t;
#endif //_BASETYPES_H_

View file

@ -5,8 +5,14 @@
struct vector2
{
square_t x;
square_t y;
coord_t x;
coord_t y;
};
struct uvector2
{
ucoord_t x;
ucoord_t y;
};
#endif //_VECTOR2_H_

View file

@ -1,5 +1,43 @@
#include "board.h"
#include <iostream> // std::cout
// 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);
std::cout<<"Blank board"<<std::endl;
board.clear();
debug_print(board);
std::cout<<"Filled board"<<std::endl;
board.fill(48);
debug_print(board);
}
};
// Main program entry point
int main()
{