diff --git a/CMakeLists.txt b/CMakeLists.txt index 8668298..6606710 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,15 +5,16 @@ set(CMAKE_BUILD_TYPE Debug) set(BUILD_TESTS ON) cmake_policy(SET CMP0015 NEW) -set(states activeGameState.cpp inGameAppState.cpp guiAppState.cpp) -set(util util.cpp binds.h) -set(gui tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp) -set(core gameManager.cpp main.cpp defConfigs.cpp) -set(ships vessel.cpp destroyer.cpp cruiser.cpp aircraftCarrier.cpp submarine.cpp) -set(projectiles projectile.cpp guidedMissile.cpp depthCharge.cpp missile.cpp shell.cpp torpedo.cpp) -set(aircraft jet.cpp demoJet.cpp missileJet.cpp) -set(fx explosion.cpp) -set(content player.cpp unitDataManager.cpp unit.cpp map.cpp ${ships} ${projectiles} ${aircraft} ${fx}) +set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp) +set(UTIL util.cpp binds.h) +set(GUI tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp) +set(CORE gameManager.cpp defConfigs.cpp) +set(SHIPS vessel.cpp destroyer.cpp cruiser.cpp aircraftCarrier.cpp submarine.cpp) +set(PROJECTILES projectile.cpp guidedMissile.cpp depthCharge.cpp missile.cpp shell.cpp torpedo.cpp) +set(AIRCRAFT jet.cpp demoJet.cpp missileJet.cpp) +set(FX explosion.cpp) +set(CONTENT player.cpp unitDataManager.cpp unit.cpp pathfinder.cpp map.cpp ${SHIPS} ${PROJECTILES} ${AIRCRAFT} ${FX}) +set(GAME_SRC ${STATES} ${GUI} ${CORE} ${CONTENT} ${UTIL}) set(SFML_DIR external/SFML) set(VB01_DIR external/vb01) @@ -23,7 +24,7 @@ set(GAME_BASE_DIR external/gameBase) set(GLFW_DIR ${VB01_DIR}/external/glfw/) set(GAME_NAME battleship) -add_executable(${GAME_NAME} ${states} ${gui} ${core} ${content} ${util}) +add_executable(${GAME_NAME} main.cpp ${GAME_SRC}) target_include_directories(${GAME_NAME} PUBLIC ${FREETYPE_DIR}/include) @@ -67,8 +68,12 @@ if(BUILD_TESTS) link_directories(C:/Program\ Files\ \(x86\)/cppunit/lib) endif() - set(TEST_SRC testMain.cpp ${GAME_SRC}) + set(TEST_SRC testMain.cpp pathfinderTest.cpp ${GAME_SRC}) + set(TEST_NAME battleshipTests) - add_executable(battleshipTests ${TEST_SRC}) - target_link_libraries(battleshipTests ${DEPS} cppunit) + add_executable(${TEST_NAME} ${TEST_SRC}) + target_include_directories(${TEST_NAME} PUBLIC ${VB01_DIR}) + target_include_directories(${TEST_NAME} PUBLIC ${VB01_GUI_DIR}) + target_include_directories(${TEST_NAME} PUBLIC ${GAME_BASE_DIR}) + target_link_libraries(${TEST_NAME} ${DEPS} cppunit) endif() diff --git a/map.cpp b/map.cpp index 9992a2b..53ab60b 100755 --- a/map.cpp +++ b/map.cpp @@ -125,9 +125,12 @@ namespace battleship{ for(int i = 0; i < numCellsByDim[0]; i++){ cells[i] = new GraphNode*[numCellsByDim[1]]; - for(int j = 0; j < numCellsByDim[1]; j++){ + for(int j = 0; j < numCellsByDim[1]; j++) cells[i][j] = new GraphNode[numCellsByDim[2]]; + } + for(int i = 0; i < numCellsByDim[0]; i++){ + for(int j = 0; j < numCellsByDim[1]; j++){ for(int k = 0; k < numCellsByDim[2]; k++){ GraphNode::Type type = GraphNode::SEA; @@ -138,6 +141,32 @@ namespace battleship{ } } } + + int numCells = numCellsByDim[0] * numCellsByDim[1] * numCellsByDim[2]; + weights = new int*[numCells]; + + for(int i = 0; i < numCells; i++){ + weights[i] = new int[numCells]; + int x1 = i / numCellsByDim[2] / numCellsByDim[1]; + int y1 = i / numCellsByDim[2] % numCellsByDim[1]; + int z1 = i % numCellsByDim[2]; + + for(int j = 0; j < numCells; j++){ + int x2 = j / numCellsByDim[2] / numCellsByDim[1]; + int y2 = j / numCellsByDim[2] % numCellsByDim[1]; + int z2 = j % numCellsByDim[2]; + int weight; + + if(abs(x1 - x2) == 1 || abs(y1 - y2) == 1 || abs(z1 - z2) == 1) + weight = 1; + else if(x1 == x2 && y1 == y2 && z1 == z2) + weight = 0; + else + weight = -1; + + weights[i][j] = weight; + } + } } void Map::unload() {} diff --git a/map.h b/map.h index 930c3d7..5da0b39 100755 --- a/map.h +++ b/map.h @@ -31,6 +31,7 @@ namespace battleship{ std::string mapName; float cellSize = 1; GraphNode*** cells = nullptr; + int **weights = nullptr; void loadSkybox(gameBase::LuaManager*); void loadTerrain(gameBase::LuaManager*); diff --git a/pathfinder.cpp b/pathfinder.cpp new file mode 100644 index 0000000..fac7242 --- /dev/null +++ b/pathfinder.cpp @@ -0,0 +1,28 @@ +#include "pathfinder.h" + +namespace battleship{ + using namespace std; + + static Pathfinder *pathfinder = nullptr; + + Pathfinder* Pathfinder::getSingleton(){ + if(!pathfinder) + pathfinder = new Pathfinder(); + + return pathfinder; + } + + Pathfinder::Pathfinder(){ + } + + vector Pathfinder::findPath(int **weights, int size, int source, int sink){ + int pathWeights[size]; + pathWeights[0] = 0; + + for(int i = 1; i < size; i++) + pathWeights[i] = -1; + + vector path; + return path; + } +} diff --git a/pathfinder.h b/pathfinder.h new file mode 100644 index 0000000..cc3d79c --- /dev/null +++ b/pathfinder.h @@ -0,0 +1,16 @@ +#ifndef PATHFINDER_H +#define PATHFINDER_H + +#include + +namespace battleship{ + class Pathfinder{ + public: + static Pathfinder* getSingleton(); + std::vector findPath(int**, int, int, int); + private: + Pathfinder(); + }; +} + +#endif diff --git a/pathfinderTest.cpp b/pathfinderTest.cpp new file mode 100644 index 0000000..979167a --- /dev/null +++ b/pathfinderTest.cpp @@ -0,0 +1,46 @@ +#include "pathfinderTest.h" +#include "pathfinder.h" + +#include + +namespace battleship{ + using namespace std; + + void PathfinderTest::testFindPath(){ + int size = 7; + int w[size][size] = { + {0, 2, 4, -1, -1, -1, -1}, + {-1, 0, 1, 9, 13, -1, -1}, + {-1, 2, 0, -1, 4, 5, -1}, + {-1, -1, -1, 0, -1, -1, 1}, + {1, -1, -1, 1, 0, -1, 3}, + {-1, -1, -1, -1, 9, 0, 2}, + {-1, -1, -1, -1, -1, -1, 0} + }; + int **weights = new int*[size]; + + for(int i = 0; i < size; i++){ + weights[i] = new int[size]; + + for(int j = 0; j < size; j++) + weights[i][j] = w[i][j]; + } + + vector path = pathfinder->findPath(weights, size, 0, size - 1); + CPPUNIT_ASSERT(path.size() == 6); + + int sumPathWeights = 0; + + for(int p : path) + sumPathWeights += p; + + CPPUNIT_ASSERT(sumPathWeights == 9); + } + + void PathfinderTest::setUp(){ + pathfinder = Pathfinder::getSingleton(); + } + + void PathfinderTest::tearDown(){ + } +} diff --git a/pathfinderTest.h b/pathfinderTest.h new file mode 100644 index 0000000..2cc4f09 --- /dev/null +++ b/pathfinderTest.h @@ -0,0 +1,25 @@ +#ifndef PATHFINDER_TEST_H +#define PATHFINDER_TEST_H + +#include +#include + +namespace battleship{ + class Pathfinder; + + class PathfinderTest : public CppUnit::TestFixture{ + CPPUNIT_TEST_SUITE(PathfinderTest); + CPPUNIT_TEST(testFindPath); + CPPUNIT_TEST_SUITE_END(); + + public: + PathfinderTest(){} + void testFindPath(); + void setUp(); + void tearDown(); + private: + Pathfinder *pathfinder = nullptr; + }; +} + +#endif diff --git a/testMain.cpp b/testMain.cpp index d8a2db1..1e7755e 100644 --- a/testMain.cpp +++ b/testMain.cpp @@ -3,10 +3,14 @@ #include #include +#include "pathfinderTest.h" + using namespace CppUnit; +using namespace battleship; int main(){ TextUi::TestRunner runner; + runner.addTest(PathfinderTest::suite()); runner.run(); return 0; }