Rename CBoardData, refactor a few things and add a maths library folder

This commit is contained in:
Marc Di Luzio 2014-12-16 13:12:53 +00:00
parent e8429ae4a2
commit 743c9a2859
11 changed files with 150 additions and 116 deletions

View file

@ -29,5 +29,8 @@ Wrapper for user interface for the terminal, this only really needs three stages
* Run the game simulation to it's conclusion * Run the game simulation to it's conclusion
* Display the game result * Display the game result
### maths
simple maths library for 2D calculations and types
### player ### player
Custom player AI code, this should contain examples and test code to help newcomers begin their journey Custom player AI code, this should contain examples and test code to help newcomers begin their journey

View file

@ -3,12 +3,17 @@ cmake_minimum_required(VERSION 2.8.7)
# game project # game project
project( game ) project( game )
include_directories(
../maths
)
# Set to use c++11, because we're cool like that # Set to use c++11, because we're cool like that
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11" ) set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11" )
# Add the sources # Add the sources
set( SOURCES set( SOURCES
game.cpp game.cpp
board.cpp
) )
add_library( game ${SOURCES} ) add_library( game ${SOURCES} )

View file

@ -1,7 +1,7 @@
ttrts Game Design ttrts Game Design
================= =================
The game takes place in a series of simultanious turns on an arbitrarilly sized 2D grid. The game takes place in a series of simultanious turns on an arbitrarilly sized 2D board.
Each player is in control of a set number of starting units, each turn recieves data on the status of the board. Each player is in control of a set number of starting units, each turn recieves data on the status of the board.
@ -28,8 +28,8 @@ X is your basic slow melee unit. It has very basic data and controls, basically
##### properties ##### properties
| property | type | description | | property | type | description |
|:---------|:--------|:----------------------------------| |:---------|:--------|:----------------------------------|
| pos | char[2] | x,y position on the grid | | pos | char[2] | x,y position on the board |
| dir | char | compass direction unit is facing | | dir | char[2] | compass direction unit is facing |
##### commands ##### commands
| command | description | | command | description |
@ -40,10 +40,10 @@ X is your basic slow melee unit. It has very basic data and controls, basically
| A | deal 1 damage to unit directly in front | | A | deal 1 damage to unit directly in front |
-------------------------------------------------------- --------------------------------------------------------
The Grid The Board
-------- --------
As an example, let's start with a basic starting `[10,5]` grid As an example, let's start with a basic starting `[10,5]` board
```` ````
0000000000 0000000000
0000000000 0000000000

68
game/board.cpp Normal file
View file

@ -0,0 +1,68 @@
#include "board.h"
#include <iostream> // std::cout
// ----------------------------------------------
// Default constructor for the board
CBoard::CBoard( unsigned int c, unsigned int r )
: cols ( c )
, rows ( r )
, total ( rows * cols )
{
board = new square_t[total];
fill(square_invalid);
}
CBoard::~CBoard()
{
delete[] board;
}
// Clear the board
void CBoard::clear()
{
fill(square_empty);
}
// Fill the board
void CBoard::fill(square_t v)
{
std::fill(board,board+total,v);
}
// print get a slot on the board
square_t CBoard::get( unsigned int c, unsigned int r ) const
{
if ( (r >= rows) || (c >= cols) )
return square_invalid;
return board[r*c];
}
// print a board
void CBoard::debug_print() const
{
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;
}
}
// 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();
}

47
game/board.h Normal file
View file

@ -0,0 +1,47 @@
#ifndef _BOARD_H_
#define _BOARD_H_
#include "basetypes.h"
#include <limits> // std::numeric_limits
// 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
};
// Namespace for testing functions
namespace tests
{
void test_CBoard();
};
#endif //_BOARD_H_

View file

@ -1,65 +1 @@
#include "game.h" #include "game.h"
// ----------------------------------------------
// Default constructor for the board
CBoardData::CBoardData( unsigned int c, unsigned int r )
: cols ( c )
, rows ( r )
, total ( rows * cols )
{
board = new square_t[total];
fill(square_invalid);
}
CBoardData::~CBoardData()
{
delete[] board;
}
// Clear the board
void CBoardData::clear()
{
fill(square_empty);
}
// Fill the board
void CBoardData::fill(square_t v)
{
std::fill(board,board+total,v);
}
// print get a slot on the board
CBoardData::square_t CBoardData::get( unsigned int c, unsigned int r ) const
{
if ( (r >= rows) || (c >= cols) )
return square_invalid;
return board[r*c];
}
// print a board
void CBoardData::print() const
{
for ( unsigned int r = 0; r < rows; r++ )
{
for ( unsigned int c = 0; c < cols; c++ )
{
std::cout<<board[r*c];
}
std::cout<<std::endl;
}
}
// Test the board data class
void tests::test_CBoardData()
{
CBoardData board = CBoardData(20,10);
std::cout<<"Blank board"<<std::endl;
board.clear();
board.print();
std::cout<<"Filled board"<<std::endl;
board.fill(48);
board.print();
}

View file

@ -1,49 +1,4 @@
#ifndef _GAME_H_ #ifndef _GAME_H_
#define _GAME_H_ #define _GAME_H_
#include <iostream> // std::cout
#include <limits> // std::numeric_limits
// Class to store simple data about a board
class CBoardData
{
public:
// Type for the board square
typedef char square_t
;
// 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
CBoardData( unsigned int c, unsigned int r );
~CBoardData();
// Print the board
void 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
};
// Namespace for testing functions
namespace tests
{
void test_CBoardData();
};
#endif //_GAME_H_ #endif //_GAME_H_

7
maths/basetypes.h Normal file
View file

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

12
maths/vector2.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef _VECTOR2_H_
#define _VECTOR2_H_
#include "basetypes.h"
struct vector2
{
square_t x;
square_t y;
};
#endif //_VECTOR2_H_

View file

@ -5,6 +5,7 @@ project( ttrts-test )
include_directories( include_directories(
../game ../game
../maths
) )
set( SOURCES set( SOURCES

View file

@ -1,7 +1,7 @@
#include "game.h" #include "board.h"
// Main program entry point // Main program entry point
int main() int main()
{ {
tests::test_CBoardData(); tests::test_CBoard();
}; };