diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 02db7fb..04900db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ add_executable(snakeplusplus ./gamestate.cpp ./snake.cpp ./playerinterface.cpp + ./common.cpp ) target_include_directories(snakeplusplus PUBLIC ${CMAKE_CURRENT_LIST_DIR}) diff --git a/src/common.cpp b/src/common.cpp new file mode 100755 index 0000000..62088ee --- /dev/null +++ b/src/common.cpp @@ -0,0 +1,21 @@ +// common.cpp +#include +#include "common.hpp" + +namespace snakeplusplus +{ + std::default_random_engine generator; + void InitializeGenerator(void) + { + generator.seed(std::random_device{}()); + } + + // Returns a newly generated number + int GenerateRandomNumber(int generationLimit) + { + int generatedNumber; + std::uniform_int_distribution<> distribution(0, generationLimit - 1); + generatedNumber = distribution(snakeplusplus::generator); + return generatedNumber; + } +} diff --git a/src/common.hpp b/src/common.hpp index ef440c9..82f60e2 100755 --- a/src/common.hpp +++ b/src/common.hpp @@ -1,13 +1,20 @@ #ifndef COMMON_HPP #define COMMON_HPP -enum PlayerDirection +namespace snakeplusplus { - kNone = 0, - kLeft = 1, - kUp = 2, - kDown = 3, - kRight = 4 -}; + void InitializeGenerator(void); + int GenerateRandomNumber(int generationLimit); + + enum PlayerDirection + { + kNone = 0, + kLeft = 1, + kUp = 2, + kDown = 3, + kRight = 4 + }; + +} #endif diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 51d4e99..14b3ec9 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -9,6 +9,7 @@ namespace snakeplusplus { GameEngine::GameEngine() { + snakeplusplus::InitializeGenerator(); return; } diff --git a/src/snake.cpp b/src/snake.cpp index d1908e4..f6c635b 100755 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -1,11 +1,12 @@ // Snake.cpp #include -#include #include +#include "common.hpp" #include "snake.hpp" namespace snakeplusplus { + void Snake::Pop(void) { *(body.front()) = ' '; @@ -21,26 +22,11 @@ namespace snakeplusplus return; } - Food::Food(void) - { - generator.seed(std::random_device{}()); - return; - } - // Returns a new food object for the snakeFood void Food::GenerateNewFood(sf::Vector2f boundaries) { - location.x = GenerateRandomNumber(boundaries.x); - location.y = GenerateRandomNumber(boundaries.y); + location.x = snakeplusplus::GenerateRandomNumber(boundaries.x); + location.y = snakeplusplus::GenerateRandomNumber(boundaries.y); return; } - - // Returns a newly generated number - int Food::GenerateRandomNumber(int generationLimit) - { - int generatedNumber; - std::uniform_int_distribution<> distribution(0, generationLimit - 1); - generatedNumber = distribution(generator); - return generatedNumber; - } } diff --git a/src/snake.hpp b/src/snake.hpp index 6508e3f..80cec53 100755 --- a/src/snake.hpp +++ b/src/snake.hpp @@ -4,7 +4,6 @@ #include #include -#include namespace snakeplusplus { @@ -21,13 +20,9 @@ namespace snakeplusplus struct Food { public: - Food(void); sf::Vector2f location; char* food; void GenerateNewFood(sf::Vector2f boundaries); - private: - std::default_random_engine generator; - int GenerateRandomNumber(int generationLimit); }; }