From 8ddb8ae9164d52c2e390563ff3e0b0cb5513457a Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Tue, 16 Dec 2014 13:12:50 +0000 Subject: [PATCH] Some initial game board code, really simple --- game/CMakeLists.txt | 14 ++++++++++ game/game.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++ game/game.h | 49 ++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 game/CMakeLists.txt create mode 100644 game/game.cpp create mode 100644 game/game.h diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt new file mode 100644 index 0000000..6ef148f --- /dev/null +++ b/game/CMakeLists.txt @@ -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} ) \ No newline at end of file diff --git a/game/game.cpp b/game/game.cpp new file mode 100644 index 0000000..624985e --- /dev/null +++ b/game/game.cpp @@ -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< // std::cout +#include // 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::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_ \ No newline at end of file