Some initial game board code, really simple
This commit is contained in:
parent
bf2f79dc8d
commit
8ddb8ae916
3 changed files with 128 additions and 0 deletions
14
game/CMakeLists.txt
Normal file
14
game/CMakeLists.txt
Normal file
|
@ -0,0 +1,14 @@
|
|||
cmake_minimum_required(VERSION 2.8.7)
|
||||
|
||||
# game project
|
||||
project( game )
|
||||
|
||||
# Set to use c++11, because we're cool like that
|
||||
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11" )
|
||||
|
||||
# Add the sources
|
||||
set( SOURCES
|
||||
game.cpp
|
||||
)
|
||||
|
||||
add_library( game ${SOURCES} )
|
65
game/game.cpp
Normal file
65
game/game.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
#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();
|
||||
}
|
49
game/game.h
Normal file
49
game/game.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
#ifndef _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_
|
Loading…
Add table
Add a link
Reference in a new issue