refactor: remove all code from being in namespace
This commit is contained in:
parent
d5c797d460
commit
0ff3ef3f62
@ -7,16 +7,14 @@
|
||||
#include <stdexcept>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
namespace snakeplusplus
|
||||
{
|
||||
PlayerDirection lastKnownDirection = kNone;
|
||||
PlayerDirection lastKnownDirection = kNone;
|
||||
|
||||
AISnake::AISnake() {
|
||||
AISnake::AISnake() {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
||||
{
|
||||
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
||||
{
|
||||
sf::Vector2f directionDelta;
|
||||
if (*source == path.top()) { path.pop(); }
|
||||
if (path.empty()) { return kUp; } // Snake is trapped
|
||||
@ -35,27 +33,27 @@ namespace snakeplusplus
|
||||
&& (lastKnownDirection != kLeft))
|
||||
{ lastKnownDirection = kRight; }
|
||||
return lastKnownDirection;
|
||||
}
|
||||
}
|
||||
|
||||
void AISnake::UpdateProbability(int snakeSize)
|
||||
{
|
||||
void AISnake::UpdateProbability(int snakeSize)
|
||||
{
|
||||
probabilityBFS = 1 - ((double) snakeSize) / 1000;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void AISnake::AdjustProbability(double amount)
|
||||
{
|
||||
void AISnake::AdjustProbability(double amount)
|
||||
{
|
||||
probabilityBFS += amount;
|
||||
if (probabilityBFS > 1.0) { probabilityBFS = 1.0; }
|
||||
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
|
||||
std::cout << "[Info - AISnake] New BFS probability: " << probabilityBFS << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Gets a new path for the bot to follow
|
||||
// Uses DFS algorithm
|
||||
void AISnake::GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
||||
{
|
||||
// Gets a new path for the bot to follow
|
||||
// Uses DFS algorithm
|
||||
void AISnake::GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
||||
{
|
||||
// Search for food
|
||||
/*
|
||||
BFS(gameBoard, source, boundaries);
|
||||
@ -80,9 +78,9 @@ namespace snakeplusplus
|
||||
}
|
||||
botPathUnsanitized.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AISnake::BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
||||
void AISnake::BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
||||
std::queue<sf::Vector2f> search;
|
||||
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false));
|
||||
bool foodFound = false;
|
||||
@ -124,9 +122,9 @@ namespace snakeplusplus
|
||||
}
|
||||
visited.at(currentLocation.y).at(currentLocation.x) = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AISnake::DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
||||
void AISnake::DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
||||
std::stack<sf::Vector2f> search;
|
||||
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false));
|
||||
bool foodFound = false;
|
||||
@ -174,5 +172,4 @@ namespace snakeplusplus
|
||||
}
|
||||
visited.at(currentLocation.y).at(currentLocation.x) = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,22 +6,19 @@
|
||||
#include <vector>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
namespace snakeplusplus
|
||||
{
|
||||
class AISnake {
|
||||
public:
|
||||
class AISnake {
|
||||
public:
|
||||
std::stack<sf::Vector2f> path;
|
||||
AISnake();
|
||||
void GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize);
|
||||
PlayerDirection GetInput(const sf::Vector2f* source);
|
||||
void UpdateProbability(int snakeSize);
|
||||
void AdjustProbability(double amount);
|
||||
private:
|
||||
private:
|
||||
double probabilityBFS = 0.500;
|
||||
std::stack<sf::Vector2f> botPathUnsanitized;
|
||||
void BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
||||
void DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -2,20 +2,17 @@
|
||||
#include <random>
|
||||
#include "common.hpp"
|
||||
|
||||
namespace snakeplusplus
|
||||
std::default_random_engine generator;
|
||||
void InitializeGenerator(void)
|
||||
{
|
||||
std::default_random_engine generator;
|
||||
void InitializeGenerator(void)
|
||||
{
|
||||
generator.seed(std::random_device{}());
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a newly generated number
|
||||
int GenerateRandomNumber(int generationLimit)
|
||||
{
|
||||
// Returns a newly generated number
|
||||
int GenerateRandomNumber(int generationLimit)
|
||||
{
|
||||
int generatedNumber;
|
||||
std::uniform_int_distribution<> distribution(0, generationLimit - 1);
|
||||
generatedNumber = distribution(snakeplusplus::generator);
|
||||
generatedNumber = distribution(generator);
|
||||
return generatedNumber;
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,16 @@
|
||||
#ifndef COMMON_HPP
|
||||
#define COMMON_HPP
|
||||
|
||||
namespace snakeplusplus
|
||||
{
|
||||
void InitializeGenerator(void);
|
||||
int GenerateRandomNumber(int generationLimit);
|
||||
void InitializeGenerator(void);
|
||||
int GenerateRandomNumber(int generationLimit);
|
||||
|
||||
enum PlayerDirection
|
||||
{
|
||||
enum PlayerDirection
|
||||
{
|
||||
kNone = 0,
|
||||
kLeft = 1,
|
||||
kUp = 2,
|
||||
kDown = 3,
|
||||
kRight = 4
|
||||
};
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -7,24 +7,22 @@
|
||||
#include "playerinterface.hpp"
|
||||
#include "gamestate.hpp"
|
||||
|
||||
namespace snakeplusplus
|
||||
GameEngine::GameEngine()
|
||||
{
|
||||
GameEngine::GameEngine()
|
||||
{
|
||||
InitializeGenerator();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void GameEngine::Start()
|
||||
{
|
||||
void GameEngine::Start()
|
||||
{
|
||||
PrepareGameBoard();
|
||||
graphics.StartGameWindow();
|
||||
Loop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void GameEngine::Reset()
|
||||
{
|
||||
void GameEngine::Reset()
|
||||
{
|
||||
AddIteration();
|
||||
player.Reset();
|
||||
if (isBotControlled) { while (!bot.path.empty()) { bot.path.pop(); } }
|
||||
@ -32,10 +30,10 @@ namespace snakeplusplus
|
||||
isGameOver = false;
|
||||
graphics.SetShowGame((amountPlayed + 1) % 50 == 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void GameEngine::AddIteration(void)
|
||||
{
|
||||
void GameEngine::AddIteration(void)
|
||||
{
|
||||
graphics.CheckContinue(isBotControlled);
|
||||
if (player.body.size() > 40)
|
||||
{
|
||||
@ -47,10 +45,10 @@ namespace snakeplusplus
|
||||
std::cout << "[Info - GameEngine] Current average: " << average << std::endl;
|
||||
std::cout << "[Info - GameEngine] Previous iteration size: " << player.body.size() << std::endl;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void GameEngine::Loop(void)
|
||||
{
|
||||
void GameEngine::Loop(void)
|
||||
{
|
||||
int currentScore = 0;
|
||||
while (graphics.IsOpen())
|
||||
{
|
||||
@ -63,20 +61,20 @@ namespace snakeplusplus
|
||||
graphics.DisplayGameState(gameBoard, currentScore);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
sf::Vector2f GameEngine::MovePlayer(void)
|
||||
{
|
||||
sf::Vector2f GameEngine::MovePlayer(void)
|
||||
{
|
||||
return sf::Vector2f(player.headLocation.x + player.speed.x, player.headLocation.y + player.speed.y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sf::Vector2f GameEngine::GetGameBoundaries(void)
|
||||
{
|
||||
sf::Vector2f GameEngine::GetGameBoundaries(void)
|
||||
{
|
||||
return graphics.gameBoundaries;
|
||||
}
|
||||
}
|
||||
|
||||
void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
|
||||
void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
|
||||
if (!player.speed.x && !player.speed.y) { return; }
|
||||
try {
|
||||
char* locationState = &gameBoard.at(location.y).at(location.x);
|
||||
@ -92,12 +90,12 @@ namespace snakeplusplus
|
||||
isGameOver = true; // Snake ran into edge
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Generates new food until not colliding with player
|
||||
void GameEngine::RegenerateFood()
|
||||
{
|
||||
// Generates new food until not colliding with player
|
||||
void GameEngine::RegenerateFood()
|
||||
{
|
||||
// Generate a new food location if the current one is occupied
|
||||
while (gameBoard.at(playerFood.location.y).at(playerFood.location.x) == 'O') {
|
||||
playerFood.GenerateNewFood(GetGameBoundaries());
|
||||
@ -105,11 +103,11 @@ namespace snakeplusplus
|
||||
|
||||
// Update the game board with the new food location
|
||||
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GameEngine::PrepareGameBoard(void)
|
||||
{
|
||||
void GameEngine::PrepareGameBoard(void)
|
||||
{
|
||||
gameBoard.clear();
|
||||
sf::Vector2f boardDimensions = GetGameBoundaries();
|
||||
gameBoard.resize(boardDimensions.y, std::vector<char> (boardDimensions.x, ' '));
|
||||
@ -125,10 +123,10 @@ namespace snakeplusplus
|
||||
playerFood.GenerateNewFood(boardDimensions);
|
||||
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void GameEngine::UpdatePlayerSpeed(void)
|
||||
{
|
||||
void GameEngine::UpdatePlayerSpeed(void)
|
||||
{
|
||||
PlayerDirection controller;
|
||||
if (isBotControlled) {
|
||||
if (bot.path.empty()) {
|
||||
@ -162,10 +160,10 @@ namespace snakeplusplus
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
void GameEngine::UpdateAverage() {
|
||||
}
|
||||
|
||||
void GameEngine::UpdateAverage() {
|
||||
totalLength += player.body.size();
|
||||
amountPlayed += 1;
|
||||
average = (double)totalLength / amountPlayed;
|
||||
}
|
||||
}
|
||||
|
@ -3,23 +3,22 @@
|
||||
#define GAMESTATE_HPP
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <memory>
|
||||
#include "botinterface.hpp"
|
||||
#include "snake.hpp"
|
||||
#include "playerinterface.hpp"
|
||||
|
||||
namespace snakeplusplus
|
||||
{
|
||||
const int kUnitSpeed = 1;
|
||||
const int kUnitSpeed = 1;
|
||||
|
||||
class GameEngine
|
||||
{
|
||||
public:
|
||||
class GameEngine
|
||||
{
|
||||
public:
|
||||
GameEngine();
|
||||
void Start(void);
|
||||
void Reset(void);
|
||||
void AddIteration(void);
|
||||
sf::Vector2f GetGameBoundaries(void);
|
||||
private:
|
||||
private:
|
||||
std::vector< std::vector<char> > gameBoard;
|
||||
PlayerOutput graphics;
|
||||
Snake player;
|
||||
@ -38,7 +37,6 @@ namespace snakeplusplus
|
||||
int totalLength = 0;
|
||||
int amountPlayed = 0;
|
||||
double average = 0;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
int main(void)
|
||||
{
|
||||
snakeplusplus::GameEngine game;
|
||||
GameEngine game;
|
||||
game.Start();
|
||||
return 0;
|
||||
}
|
||||
|
@ -2,10 +2,8 @@
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Window/Keyboard.hpp>
|
||||
|
||||
namespace snakeplusplus
|
||||
PlayerDirection GetPlayerInput(void)
|
||||
{
|
||||
PlayerDirection GetPlayerInput(void)
|
||||
{
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)
|
||||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::A))
|
||||
return kLeft;
|
||||
@ -19,15 +17,15 @@ namespace snakeplusplus
|
||||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::D))
|
||||
return kRight;
|
||||
return kNone;
|
||||
}
|
||||
}
|
||||
|
||||
bool PlayerOutput::IsOpen(void)
|
||||
{
|
||||
bool PlayerOutput::IsOpen(void)
|
||||
{
|
||||
return gameWindow.isOpen();
|
||||
}
|
||||
}
|
||||
|
||||
PlayerOutput::PlayerOutput(void)
|
||||
{
|
||||
PlayerOutput::PlayerOutput(void)
|
||||
{
|
||||
float kWidth = 1025;
|
||||
float kHeight = 725;
|
||||
float kBoardWidth = kWidth / kGridSize;
|
||||
@ -36,10 +34,10 @@ namespace snakeplusplus
|
||||
gameVideoSettings = sf::VideoMode(kWidth, kHeight);
|
||||
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerOutput::CheckContinue(bool isBotControlled)
|
||||
{
|
||||
void PlayerOutput::CheckContinue(bool isBotControlled)
|
||||
{
|
||||
if (isBotControlled) { return; }
|
||||
DisplayEndScreen();
|
||||
while (true)
|
||||
@ -54,10 +52,10 @@ namespace snakeplusplus
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) { return; }
|
||||
sf::sleep(delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerOutput::DisplayEndScreen(void)
|
||||
{
|
||||
void PlayerOutput::DisplayEndScreen(void)
|
||||
{
|
||||
gameWindow.clear();
|
||||
sf::Vector2f textPosition(gameBoundaries);
|
||||
textPosition.x = textPosition.x / 2;
|
||||
@ -69,9 +67,9 @@ namespace snakeplusplus
|
||||
gameWindow.draw(gameOverText);
|
||||
gameWindow.display();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerOutput::DisplayScore(int score) {
|
||||
void PlayerOutput::DisplayScore(int score) {
|
||||
sf::Vector2f textPosition(gameBoundaries);
|
||||
textPosition.x = textPosition.x / 2;
|
||||
textPosition.y = textPosition.y / 2;
|
||||
@ -82,10 +80,10 @@ namespace snakeplusplus
|
||||
ScoreText.setPosition(textPosition);
|
||||
gameWindow.draw(ScoreText);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score)
|
||||
{
|
||||
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score)
|
||||
{
|
||||
CheckWindowEvents();
|
||||
if (delay == sf::milliseconds(0)) { return; }
|
||||
char* letterOnBoard;
|
||||
@ -112,23 +110,23 @@ namespace snakeplusplus
|
||||
gameWindow.display();
|
||||
sf::sleep(delay);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerOutput::StartGameWindow(void)
|
||||
{
|
||||
void PlayerOutput::StartGameWindow(void)
|
||||
{
|
||||
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
||||
isWindowAlive = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerOutput::SetShowGame(bool isShowing) {
|
||||
void PlayerOutput::SetShowGame(bool isShowing) {
|
||||
if (isShowing) { delay = sf::milliseconds(2); }
|
||||
else { delay = sf::milliseconds(0); }
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerOutput::CheckWindowEvents(void)
|
||||
{
|
||||
void PlayerOutput::CheckWindowEvents(void)
|
||||
{
|
||||
while (gameWindow.pollEvent(event))
|
||||
{
|
||||
if ((event.type == sf::Event::Closed)
|
||||
@ -143,32 +141,31 @@ namespace snakeplusplus
|
||||
delay -= sf::milliseconds(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerOutput::DrawEmpty(sf::Vector2f location)
|
||||
{
|
||||
void PlayerOutput::DrawEmpty(sf::Vector2f location)
|
||||
{
|
||||
location *= static_cast<float>(kGridSize);
|
||||
drawObject.setPosition(location);
|
||||
drawObject.setFillColor(sf::Color::Black);
|
||||
gameWindow.draw(drawObject);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerOutput::DrawFood(sf::Vector2f location)
|
||||
{
|
||||
void PlayerOutput::DrawFood(sf::Vector2f location)
|
||||
{
|
||||
location *= static_cast<float>(kGridSize);
|
||||
drawObject.setPosition(location);
|
||||
drawObject.setFillColor(sf::Color::Red);
|
||||
gameWindow.draw(drawObject);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerOutput::DrawSnake(sf::Vector2f location)
|
||||
{
|
||||
void PlayerOutput::DrawSnake(sf::Vector2f location)
|
||||
{
|
||||
location *= static_cast<float>(kGridSize);
|
||||
drawObject.setPosition(location);
|
||||
drawObject.setFillColor(sf::Color::Green);
|
||||
gameWindow.draw(drawObject);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -6,13 +6,11 @@
|
||||
|
||||
const int kGridSize = 25;
|
||||
|
||||
namespace snakeplusplus
|
||||
{
|
||||
PlayerDirection GetPlayerInput(void);
|
||||
PlayerDirection GetPlayerInput(void);
|
||||
|
||||
class PlayerOutput
|
||||
{
|
||||
public:
|
||||
class PlayerOutput
|
||||
{
|
||||
public:
|
||||
sf::Vector2f gameBoundaries;
|
||||
PlayerOutput(void);
|
||||
bool IsOpen(void);
|
||||
@ -21,7 +19,7 @@ namespace snakeplusplus
|
||||
void DisplayScore(int score);
|
||||
void StartGameWindow(void);
|
||||
void SetShowGame(bool isShowing);
|
||||
private:
|
||||
private:
|
||||
void CheckWindowEvents(void);
|
||||
void DisplayEndScreen(void);
|
||||
void DrawEmpty(sf::Vector2f location);
|
||||
@ -33,7 +31,6 @@ namespace snakeplusplus
|
||||
sf::Event event;
|
||||
bool isWindowAlive;
|
||||
sf::Time delay = sf::milliseconds(1);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -4,28 +4,25 @@
|
||||
#include "common.hpp"
|
||||
#include "snake.hpp"
|
||||
|
||||
namespace snakeplusplus
|
||||
void Snake::Pop(void)
|
||||
{
|
||||
void Snake::Pop(void)
|
||||
{
|
||||
*(body.front()) = ' ';
|
||||
body.pop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Snake::Reset(void)
|
||||
{
|
||||
void Snake::Reset(void)
|
||||
{
|
||||
while (!body.empty()) Pop();
|
||||
speed.x = 0;
|
||||
speed.y = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a new food object for the snakeFood
|
||||
void Food::GenerateNewFood(sf::Vector2f boundaries)
|
||||
{
|
||||
// Returns a new food object for the snakeFood
|
||||
void Food::GenerateNewFood(sf::Vector2f boundaries)
|
||||
{
|
||||
location.x = GenerateRandomNumber(boundaries.x);
|
||||
location.y = GenerateRandomNumber(boundaries.y);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -5,25 +5,22 @@
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <queue>
|
||||
|
||||
namespace snakeplusplus
|
||||
struct Snake
|
||||
{
|
||||
struct Snake
|
||||
{
|
||||
public:
|
||||
public:
|
||||
sf::Vector2f headLocation;
|
||||
sf::Vector2f speed;
|
||||
std::queue<char*> body;
|
||||
void Pop(void);
|
||||
void Reset(void);
|
||||
};
|
||||
};
|
||||
|
||||
struct Food
|
||||
{
|
||||
public:
|
||||
struct Food
|
||||
{
|
||||
public:
|
||||
sf::Vector2f location;
|
||||
char* food;
|
||||
void GenerateNewFood(sf::Vector2f boundaries);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user