Separated generation into common file
This commit is contained in:
parent
441c52ab0f
commit
d7982b200f
@ -5,6 +5,7 @@ add_executable(snakeplusplus
|
||||
./gamestate.cpp
|
||||
./snake.cpp
|
||||
./playerinterface.cpp
|
||||
./common.cpp
|
||||
)
|
||||
|
||||
target_include_directories(snakeplusplus PUBLIC ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
21
src/common.cpp
Executable file
21
src/common.cpp
Executable 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;
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -9,6 +9,7 @@ namespace snakeplusplus
|
||||
{
|
||||
GameEngine::GameEngine()
|
||||
{
|
||||
snakeplusplus::InitializeGenerator();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,12 @@
|
||||
// Snake.cpp
|
||||
#include <queue>
|
||||
#include <random>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <queue>
|
||||
#include <random>
|
||||
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user