From 743c9a28592d6da4ef466e827be25acadc322aae Mon Sep 17 00:00:00 2001
From: Marc Di Luzio <mdiluzio@feralinteractive.com>
Date: Tue, 16 Dec 2014 13:12:53 +0000
Subject: [PATCH] Rename CBoardData, refactor a few things and add a maths
 library folder

---
 README.md           |  3 ++
 game/CMakeLists.txt |  5 ++++
 game/README.md      | 10 +++----
 game/board.cpp      | 68 +++++++++++++++++++++++++++++++++++++++++++++
 game/board.h        | 47 +++++++++++++++++++++++++++++++
 game/game.cpp       | 64 ------------------------------------------
 game/game.h         | 45 ------------------------------
 maths/basetypes.h   |  7 +++++
 maths/vector2.h     | 12 ++++++++
 test/CMakeLists.txt |  1 +
 test/test.cpp       |  4 +--
 11 files changed, 150 insertions(+), 116 deletions(-)
 create mode 100644 game/board.cpp
 create mode 100644 game/board.h
 create mode 100644 maths/basetypes.h
 create mode 100644 maths/vector2.h

diff --git a/README.md b/README.md
index 58619d8..67fdcaa 100644
--- a/README.md
+++ b/README.md
@@ -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
 * Display the game result
 
+### maths
+simple maths library for 2D calculations and types
+
 ### player
 Custom player AI code, this should contain examples and test code to help newcomers begin their journey
\ No newline at end of file
diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt
index 6ef148f..e780c69 100644
--- a/game/CMakeLists.txt
+++ b/game/CMakeLists.txt
@@ -3,12 +3,17 @@ cmake_minimum_required(VERSION 2.8.7)
 # game project
 project( game )
 
+include_directories(
+	../maths
+)
+
 # 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
+	board.cpp
 )
 
 add_library( game ${SOURCES} )
\ No newline at end of file
diff --git a/game/README.md b/game/README.md
index 6f95afb..7c4e70f 100644
--- a/game/README.md
+++ b/game/README.md
@@ -1,7 +1,7 @@
 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.
 
@@ -28,8 +28,8 @@ X is your basic slow melee unit. It has very basic data and controls, basically
 ##### properties
 | property | type    | description                       |
 |:---------|:--------|:----------------------------------|
-| pos      | char[2] | x,y position on the grid          |
-| dir      | char    | compass direction unit is facing  |
+| pos      | char[2] | x,y position on the board         |
+| dir      | char[2] | compass direction unit is facing  |
 
 ##### commands
 | 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     |
 
 --------------------------------------------------------
-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
diff --git a/game/board.cpp b/game/board.cpp
new file mode 100644
index 0000000..54b4a95
--- /dev/null
+++ b/game/board.cpp
@@ -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();
+}
\ No newline at end of file
diff --git a/game/board.h b/game/board.h
new file mode 100644
index 0000000..aaf610a
--- /dev/null
+++ b/game/board.h
@@ -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_
\ No newline at end of file
diff --git a/game/game.cpp b/game/game.cpp
index 624985e..7bb61ba 100644
--- a/game/game.cpp
+++ b/game/game.cpp
@@ -1,65 +1 @@
 #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();
-}
\ No newline at end of file
diff --git a/game/game.h b/game/game.h
index a2e6af8..fd6dbf3 100644
--- a/game/game.h
+++ b/game/game.h
@@ -1,49 +1,4 @@
 #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_
\ No newline at end of file
diff --git a/maths/basetypes.h b/maths/basetypes.h
new file mode 100644
index 0000000..bacfeb1
--- /dev/null
+++ b/maths/basetypes.h
@@ -0,0 +1,7 @@
+#ifndef _BASETYPES_H_
+#define _BASETYPES_H_
+
+// Type for the board square
+typedef short square_t;
+
+#endif //_BASETYPES_H_
\ No newline at end of file
diff --git a/maths/vector2.h b/maths/vector2.h
new file mode 100644
index 0000000..d375fee
--- /dev/null
+++ b/maths/vector2.h
@@ -0,0 +1,12 @@
+#ifndef _VECTOR2_H_
+#define _VECTOR2_H_
+
+#include "basetypes.h"
+
+struct vector2
+{
+	square_t x;
+	square_t y;
+};
+
+#endif //_VECTOR2_H_
\ No newline at end of file
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 85597f3..ceca484 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -5,6 +5,7 @@ project( ttrts-test )
 
 include_directories( 
 	../game 
+	../maths
 )
 
 set( SOURCES 
diff --git a/test/test.cpp b/test/test.cpp
index 671a826..e7b2a28 100644
--- a/test/test.cpp
+++ b/test/test.cpp
@@ -1,7 +1,7 @@
-#include "game.h"
+#include "board.h"
 
 // Main program entry point
 int main()
 {
-	tests::test_CBoardData();
+	tests::test_CBoard();
 };
\ No newline at end of file