Separated generation into common file

This commit is contained in:
Trimutex 2023-08-18 19:09:57 -05:00
parent 441c52ab0f
commit d7982b200f
6 changed files with 41 additions and 30 deletions

View File

@ -5,6 +5,7 @@ add_executable(snakeplusplus
./gamestate.cpp ./gamestate.cpp
./snake.cpp ./snake.cpp
./playerinterface.cpp ./playerinterface.cpp
./common.cpp
) )
target_include_directories(snakeplusplus PUBLIC ${CMAKE_CURRENT_LIST_DIR}) target_include_directories(snakeplusplus PUBLIC ${CMAKE_CURRENT_LIST_DIR})

21
src/common.cpp Executable file
View File

@ -0,0 +1,21 @@
// common.cpp
#include <random>
#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;
}
}

View File

@ -1,6 +1,11 @@
#ifndef COMMON_HPP #ifndef COMMON_HPP
#define COMMON_HPP #define COMMON_HPP
namespace snakeplusplus
{
void InitializeGenerator(void);
int GenerateRandomNumber(int generationLimit);
enum PlayerDirection enum PlayerDirection
{ {
kNone = 0, kNone = 0,
@ -10,4 +15,6 @@ enum PlayerDirection
kRight = 4 kRight = 4
}; };
}
#endif #endif

View File

@ -9,6 +9,7 @@ namespace snakeplusplus
{ {
GameEngine::GameEngine() GameEngine::GameEngine()
{ {
snakeplusplus::InitializeGenerator();
return; return;
} }

View File

@ -1,11 +1,12 @@
// Snake.cpp // Snake.cpp
#include <queue> #include <queue>
#include <random>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "common.hpp"
#include "snake.hpp" #include "snake.hpp"
namespace snakeplusplus namespace snakeplusplus
{ {
void Snake::Pop(void) void Snake::Pop(void)
{ {
*(body.front()) = ' '; *(body.front()) = ' ';
@ -21,26 +22,11 @@ namespace snakeplusplus
return; return;
} }
Food::Food(void)
{
generator.seed(std::random_device{}());
return;
}
// Returns a new food object for the snakeFood // Returns a new food object for the snakeFood
void Food::GenerateNewFood(sf::Vector2f boundaries) void Food::GenerateNewFood(sf::Vector2f boundaries)
{ {
location.x = GenerateRandomNumber(boundaries.x); location.x = snakeplusplus::GenerateRandomNumber(boundaries.x);
location.y = GenerateRandomNumber(boundaries.y); location.y = snakeplusplus::GenerateRandomNumber(boundaries.y);
return; 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;
}
} }

View File

@ -4,7 +4,6 @@
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <queue> #include <queue>
#include <random>
namespace snakeplusplus namespace snakeplusplus
{ {
@ -21,13 +20,9 @@ namespace snakeplusplus
struct Food struct Food
{ {
public: public:
Food(void);
sf::Vector2f location; sf::Vector2f location;
char* food; char* food;
void GenerateNewFood(sf::Vector2f boundaries); void GenerateNewFood(sf::Vector2f boundaries);
private:
std::default_random_engine generator;
int GenerateRandomNumber(int generationLimit);
}; };
} }