Compare commits

...

31 Commits

Author SHA1 Message Date
Trianta
677466d99e core: formatting cleanup 2024-08-21 02:59:30 -05:00
Trianta
01f24e4ebb auto: remove unnecessary pointer passing 2024-08-21 02:29:50 -05:00
Trianta
aba9a9cc35 ai: remove food greed and adjust probability weights manually a bit for testing 2024-08-14 04:01:14 -05:00
Trianta
a5902171ec ai: added basic decision making based on sensors, needs weights and weight adjustment 2024-08-14 03:35:04 -05:00
f055ab4078 Merge pull request 'refactor: merge final cleanups before working on AI' (#6) from refactor into master
Reviewed-on: Trianta/snakeplusplus#6
2024-08-10 15:05:56 -05:00
Trianta
6fc3580102 options: add help option and rename bot argument for later 2024-08-10 14:59:32 -05:00
Trianta
4a5fbe88d3 bot: various improvements 2024-08-03 21:18:57 -05:00
Trianta
ff70b0dbd4 bot: fix pathfinding breaking 2024-08-03 20:42:34 -05:00
Trianta
35bbc152df core: implicitly set bot usage when no gui 2024-08-03 18:41:36 -05:00
Trianta
64c5c5d20e core: fix arguments check crash 2024-08-03 18:40:38 -05:00
Trianta
6e7ae19e8b core: fix not closing when window exits and add bot gameplay to be an argument 2024-08-03 18:34:14 -05:00
Trianta
722970eb12 core: fix various bugs and simplify things 2024-08-03 01:26:10 -05:00
Trianta
ccf5843e61 core: move various booleans to single bits in struct 2024-08-02 21:23:49 -05:00
Trianta
b9b771bc1c refactor: convert engine to a singleton 2024-08-02 19:50:48 -05:00
Trianta
0ff3ef3f62 refactor: remove all code from being in namespace 2024-08-02 19:45:07 -05:00
Trianta
d5c797d460 git: update gitignore for clangd stuff 2024-08-02 19:39:35 -05:00
Trianta
e5698ec96b cmake: update standard to C++23 2024-08-02 19:38:26 -05:00
eefc2bf60b Merge pull request 'add misc features to game' (#5) from iterative into master
Reviewed-on: Trianta/snakeplusplus#5
2024-08-02 19:36:14 -05:00
Trianta
03e2bf2748 git: update gitignore for cmake stuff 2024-08-02 19:33:45 -05:00
Trianta
22da054dab controls: added adjusting game speed with - and + 2024-05-20 01:20:05 -05:00
Trianta
b18ce89970 bot: added ability to skip display delay between specific generations 2024-05-20 01:05:59 -05:00
05f78d67fd Add iterative approach to deciding DFS and BFS 2024-02-01 12:50:10 -06:00
30ffcc4bb3 Merge pull request 'Minor code style fix' (#4) from dev into master
Reviewed-on: Trianta/snakeplusplus#4
2024-02-01 11:25:33 -06:00
a6c60ac3c4 Merge branch 'dev' 2024-02-01 11:25:43 -06:00
1498110048 Merge branch 'master' into dev 2024-02-01 11:23:52 -06:00
Trianta
e2d0c5dac7 Mini style adjustment 2023-11-05 19:04:44 -06:00
4f7cacfc44 Merge pull request 'Added scoring and chance-based algorithm picking' (#3) from dev into master
Reviewed-on: Trianta/snakeplusplus#3
2023-10-24 00:55:30 -05:00
Trianta
5dce9b677d Added scoring and chance-based algorithm picking 2023-10-24 00:54:30 -05:00
Trianta
da6ceebb55 Added displaying score 2023-10-23 21:33:47 -05:00
014aaffc0a Merge pull request 'Update dev from master' (#2) from master into dev
Reviewed-on: Trianta/snakeplusplus#2
2023-10-18 09:42:26 -05:00
9bb4b8ebca Increased survivability with DFS when no path 2023-10-16 12:35:35 -05:00
13 changed files with 879 additions and 566 deletions
Vendored
+15 -2
View File
@@ -33,7 +33,20 @@
*.json *.json
*.ps1 *.ps1
# ---> CMake
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
build
# Extras # Extras
.vs* .vs*
build .cache
bin
+1 -1
View File
@@ -4,7 +4,7 @@ project(
snakeplusplus snakeplusplus
LANGUAGES CXX) LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11 CACHE STRING "The C++ standard to use") set(CMAKE_CXX_STANDARD 23 CACHE STRING "The C++ standard to use")
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
+328 -134
View File
@@ -1,149 +1,343 @@
#include "botinterface.hpp" #include "botinterface.hpp"
#include "common.hpp" #include "common.hpp"
#include "gamestate.hpp"
#include <array> #include <array>
#include <cstdlib> #include <cstdlib>
#include <iostream>
#include <queue> #include <queue>
#include <stdexcept> #include <stdexcept>
#include <SFML/System/Vector2.hpp>
namespace snakeplusplus PlayerDirection lastKnownDirection = kNone;
{
PlayerDirection lastKnownDirection = kNone;
AISnake::AISnake() { AISnake::AISnake(void) {
; ;
} }
PlayerDirection AISnake::GetInput(const sf::Vector2f* source) PlayerDirection AISnake::GetInput(void) {
{ sf::Vector2f source(g_pEngine->GetHeadLocation());
sf::Vector2f directionDelta; //if (g_pEngine->state.m_bSmart && path.empty())
if (*source == path.top()) { path.pop(); } // return CurrentBestDecision();
if (path.empty()) { return kUp; } // Snake is trapped sf::Vector2f directionDelta;
directionDelta = *source - path.top();
while (source == path.top() && !path.empty())
path.pop(); path.pop();
if ((directionDelta.y == 1) if (path.empty())
&& (lastKnownDirection != kDown)) path.push(GetAnyOpenPath());
{ lastKnownDirection = kUp; }
else if ((directionDelta.y == -1)
&& (lastKnownDirection != kUp))
{ lastKnownDirection = kDown; }
else if ((directionDelta.x == 1)
&& (lastKnownDirection != kRight))
{ lastKnownDirection = kLeft; }
else if ((directionDelta.x == -1)
&& (lastKnownDirection != kLeft))
{ lastKnownDirection = kRight; }
return lastKnownDirection;
}
// Gets a new path for the bot to follow directionDelta = source - path.top();
// Uses DFS algorithm path.pop();
void AISnake::GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
{
// Search for food
if (snakeSize < 135) {
BFS(gameBoard, source, boundaries);
} else {
DFS(gameBoard, source, boundaries);
}
// Create path for food
path.push(botPathUnsanitized.top());
botPathUnsanitized.pop();
while (!botPathUnsanitized.empty()) {
sf::Vector2f deltaVector = botPathUnsanitized.top() - path.top();
int delta = abs(deltaVector.x) + abs(deltaVector.y);
if (delta == 1) {
path.push(botPathUnsanitized.top());
}
botPathUnsanitized.pop();
}
}
void AISnake::BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) { if ((directionDelta.y == 1) && (lastKnownDirection != kDown))
std::queue<sf::Vector2f> search; lastKnownDirection = kUp;
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false)); else if ((directionDelta.y == -1) && (lastKnownDirection != kUp))
bool foodFound = false; lastKnownDirection = kDown;
search.push(source); else if ((directionDelta.x == 1) && (lastKnownDirection != kRight))
while (!search.empty()) { lastKnownDirection = kLeft;
sf::Vector2f currentLocation = search.front(); else if ((directionDelta.x == -1) && (lastKnownDirection != kLeft))
search.pop(); lastKnownDirection = kRight;
if (foodFound) { break; } return lastKnownDirection;
if (visited.at(currentLocation.y).at(currentLocation.x)) { continue; } }
if (gameBoard.at(currentLocation.y).at(currentLocation.x) == 'X') {
foodFound = true;
}
botPathUnsanitized.push(currentLocation);
std::array<sf::Vector2f, 4> localLocations;
localLocations.fill(currentLocation);
localLocations[0].y += 1;
localLocations[1].x += 1;
localLocations[2].y -= 1;
localLocations[3].x -= 1;
for (auto i : localLocations) {
try {
if (gameBoard.at(i.y).at(i.x) == 'X') {
botPathUnsanitized.push(i);
foodFound = true;
}
} catch (const std::out_of_range& error) {
continue; // Out of bounds
}
}
for (sf::Vector2f newLocation : localLocations) {
try {
if ((!visited.at(newLocation.y).at(newLocation.x))
&& (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) {
search.push(newLocation);
}
} catch (const std::out_of_range& error) {
continue; // Out of bounds
}
}
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::UpdateProbability(int snakeSize) {
std::stack<sf::Vector2f> search; probabilityBFS = 1 - ((double) snakeSize) / 1000;
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false)); return;
bool foodFound = false; }
search.push(source);
while (!search.empty()) { void AISnake::AdjustProbability(double amount) {
sf::Vector2f currentLocation = search.top(); probabilityBFS += amount;
search.pop(); if (probabilityBFS > 1.0)
if (foodFound) { break; } probabilityBFS = 1.0;
if (visited.at(currentLocation.y).at(currentLocation.x)) { continue; } if (probabilityBFS < 0.0)
if (gameBoard.at(currentLocation.y).at(currentLocation.x) == 'X') { probabilityBFS = 0.0;
foodFound = true; return;
} }
botPathUnsanitized.push(currentLocation);
std::array<sf::Vector2f, 4> localLocations; void AISnake::AddIteration(const int size) {
localLocations.fill(currentLocation); if (size > 40) {
localLocations[0].y += 1; UpdateAverage(size);
localLocations[1].x += 1; double adjustmentAmount = 0.002;
localLocations[2].y -= 1; if (average > size)
localLocations[3].x -= 1; AdjustProbability(adjustmentAmount);
for (auto i : localLocations) { else
try { AdjustProbability(-adjustmentAmount);
if (gameBoard.at(i.y).at(i.x) == 'X') { }
botPathUnsanitized.push(i); std::cout << "[LOG - AI] Current average: " << average << std::endl;
foodFound = true; std::cout << "[LOG - AI] Previous iteration size: " << size << std::endl;
} }
} catch (const std::out_of_range& error) {
continue; // Out of bounds void AISnake::ResetPath(void) {
} while (!path.empty())
} path.pop();
for (sf::Vector2f newLocation : localLocations) { }
try {
if ((!visited.at(newLocation.y).at(newLocation.x)) // Gets a new path for the bot to follow
&& (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) { // Uses DFS algorithm
search.push(newLocation); void AISnake::GetNewPath(void) {
} // Search for food
} catch (const std::out_of_range& error) { // Probability-based approach for fun
continue; // Out of bounds double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
} if (roll <= probabilityBFS)
} BFS();
visited.at(currentLocation.y).at(currentLocation.x) = true; else
} DFS();
UnvisitBoard();
if (pathFailed) {
pathFailed = false;
EmptyPath();
} else {
TrimPath();
if (path.empty())
path.push(GetAnyOpenPath());
}
}
void AISnake::BFS(void) {
std::queue<sf::Vector2f> search;
search.push(g_pEngine->GetHeadLocation());
while (!search.empty()) {
sf::Vector2f currentLocation = search.front();
search.pop();
if (g_pEngine->gameBoard.at(currentLocation.y).at(currentLocation.x).m_bVisited)
continue;
botPathUnsanitized.push(currentLocation);
std::array<sf::Vector2f, 4> localLocations;
localLocations.fill(currentLocation);
localLocations[0].y += 1;
localLocations[1].x += 1;
localLocations[2].y -= 1;
localLocations[3].x -= 1;
for (sf::Vector2f nearby : localLocations) {
try {
GameSpace* space = &g_pEngine->gameBoard.at(nearby.y).at(nearby.x);
if (space->m_bFood) {
botPathUnsanitized.push(nearby);
return;
}
if (nearby.x < 1 || nearby.x > g_pEngine->GetGameBoundaries().x - 2)
continue;
if (space->m_bVisited)
continue;
if (space->m_bSnake)
continue;
search.push(nearby);
} catch (const std::out_of_range& error) {
continue; // Out of bounds
}
}
g_pEngine->gameBoard.at(currentLocation.y).at(currentLocation.x).m_bVisited = true;
}
pathFailed = true;
}
void AISnake::DFS(void) {
std::stack<sf::Vector2f> search;
search.push(g_pEngine->GetHeadLocation());
while (!search.empty()) {
sf::Vector2f currentLocation = search.top();
search.pop();
if (g_pEngine->gameBoard.at(currentLocation.y).at(currentLocation.x).m_bVisited)
continue;
botPathUnsanitized.push(currentLocation);
std::array<sf::Vector2f, 4> localLocations;
localLocations.fill(currentLocation);
localLocations.at(0).y += 1;
localLocations.at(1).x += 1;
localLocations.at(2).y -= 1;
localLocations.at(3).x -= 1;
for (sf::Vector2f nearby : localLocations) {
try {
GameSpace* space = &g_pEngine->gameBoard.at(nearby.y).at(nearby.x);
if (space->m_bFood) {
botPathUnsanitized.push(nearby);
return;
}
if (nearby.x < 1 || nearby.x > g_pEngine->GetGameBoundaries().x - 2)
continue;
if (space->m_bVisited)
continue;
if (space->m_bSnake)
continue;
search.push(nearby);
} catch (const std::out_of_range& error) {
continue; // Out of bounds
}
}
g_pEngine->gameBoard.at(currentLocation.y).at(currentLocation.x).m_bVisited = true;
}
pathFailed = true;
}
sf::Vector2f AISnake::GetAnyOpenPath(void) {
sf::Vector2f bail;
std::array<sf::Vector2f, 4> paths;
paths.fill(g_pEngine->GetHeadLocation());
paths[0].x -= 1;
paths[1].x += 1;
paths[2].y -= 1;
paths[3].y += 1;
for (auto path : paths) {
try {
bail = path;
if (g_pEngine->gameBoard.at(path.y).at(path.x).m_bSnake)
continue;
return path;
} catch (const std::out_of_range& error) {
continue; // Out of bounds
}
}
return bail; // Snake is trapped, give up and die
}
void AISnake::UnvisitBoard(void) {
for (std::vector<GameSpace>& i : g_pEngine->gameBoard)
for (GameSpace& j : i)
j.m_bVisited = false;
}
void AISnake::UpdateAverage(const int size) {
totalLength += size;
amountPlayed += 1;
average = (double)totalLength / amountPlayed;
}
void AISnake::TrimPath(void) {
bool reachedSnake = false;
path.push(botPathUnsanitized.top()); // Push food location
while (!botPathUnsanitized.empty()) {
if (!reachedSnake) {
sf::Vector2f location = botPathUnsanitized.top();
if (g_pEngine->gameBoard[location.y][location.x].m_bSnake)
reachedSnake = true;
sf::Vector2f deltaVector = location - path.top();
int delta = abs(deltaVector.x) + abs(deltaVector.y);
if (delta == 1)
path.push(location);
}
botPathUnsanitized.pop();
}
}
void AISnake::EmptyPath(void) {
while (!botPathUnsanitized.empty())
botPathUnsanitized.pop();
}
// Main method for using AI snake
PlayerDirection AISnake::CurrentBestDecision(void) {
// Reset probabilities
probabilityUp = 0.25;
probabilityDown = 0.25;
probabilityLeft = 0.25;
probabilityRight = 0.25;
// Calculate options
CheckLocalFreedom();
CheckFoodDirection();
// Deny impossible movement
RemoveImpossibleChoice();
// Make decision
if (probabilityUp > probabilityDown) {
if (probabilityUp < probabilityLeft)
return kLeft;
if (probabilityUp < probabilityRight)
return kRight;
return kUp;
} else {
if (probabilityDown < probabilityLeft)
return kLeft;
if (probabilityDown < probabilityRight)
return kRight;
return kDown;
}
}
// Improves probability based on amount of local open spaces
// TODO: Add weights
void AISnake::CheckLocalFreedom(void) {
std::array<sf::Vector2f, 4> choices;
std::array<double, 4> chances;
chances.fill(0);
choices.fill(g_pEngine->GetHeadLocation());
choices[0].y += 1;
choices[1].x += 1;
choices[2].y -= 1;
choices[3].x -= 1;
for (int i = 0; i < 4; ++i) {
try {
if (g_pEngine->gameBoard.at(choices[i].y).at(choices[i].x).m_bSnake) {
chances[i] = 0;
continue;
}
if (g_pEngine->gameBoard.at(choices[i].y).at(choices[i].x).m_bFood) {
chances[0] = 0;
chances[1] = 0;
chances[2] = 0;
chances[3] = 0;
chances[i] = 1;
break;
}
} catch (const std::out_of_range& error) {
chances[i] = 0;
continue;
}
double openSpaces = 0;
for (int j = -1; j < 2; ++j) {
for (int k = -1; k < 2; ++k) {
try {
if (!g_pEngine->gameBoard.at(choices[i].y + j).at(choices[i].x + k).m_bSnake)
++openSpaces;
} catch (const std::out_of_range& error) {
continue; // Out of bounds
}
}
}
chances[i] = openSpaces * 0.11111111111;
}
probabilityDown *= chances[0] * 4;
probabilityRight *= chances[1] * 4;
probabilityUp *= chances[2] * 4;
probabilityLeft *= chances[3] * 4;
}
// Improves probability that direction of food is best option
// TODO: Add weights
void AISnake::CheckFoodDirection(void) {
sf::Vector2f delta = g_pEngine->GetHeadLocation() - g_pEngine->GetFoodLocation();
if (delta.x > 0)
probabilityLeft *= 1.5;
if (delta.x < 0)
probabilityRight *= 1.5;
if (delta.y > 0)
probabilityUp *= 1.5;
if (delta.y < 0)
probabilityDown *= 1.5;
}
void AISnake::RemoveImpossibleChoice(void) {
if (g_pEngine->GetPlayerSize() == 1)
return; // Player can go any direction
PlayerDirection currentDirection = g_pEngine->GetCurrentDirection();
switch (currentDirection) {
case (kUp):
probabilityDown = 0;
break;
case (kDown):
probabilityUp = 0;
break;
case (kLeft):
probabilityRight = 0;
break;
case (kRight):
probabilityLeft = 0;
break;
default:
std::cout << "[ERR - AI] Impossibility defaulted somehow??" << std::endl;
break;
} }
} }
+39 -15
View File
@@ -3,22 +3,46 @@
#include "common.hpp" #include "common.hpp"
#include <stack> #include <stack>
#include <vector>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
namespace snakeplusplus class AISnake {
{ public:
class AISnake { std::stack<sf::Vector2f> path;
public: AISnake(void);
std::stack<sf::Vector2f> path; void GetNewPath(void);
AISnake(); PlayerDirection GetInput(void);
void GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize); void UpdateProbability(int snakeSize);
PlayerDirection GetInput(const sf::Vector2f* source); void AdjustProbability(double amount);
private: void AddIteration(const int size);
std::stack<sf::Vector2f> botPathUnsanitized; void ResetPath(void);
void BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries); int amountPlayed = 0;
void DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries); private:
}; int totalLength = 0;
} double average = 0;
double probabilityBFS = 0.200;
bool pathFailed = false;
// Generic search algorithms
std::stack<sf::Vector2f> botPathUnsanitized;
void BFS(void);
void DFS(void);
sf::Vector2f GetAnyOpenPath(void);
void UnvisitBoard(void);
void UpdateAverage(const int size);
void TrimPath(void);
void EmptyPath(void);
// Unsupervised learning
// Make decisions about current state of board
double probabilityUp = 0.25;
double probabilityDown = 0.25;
double probabilityLeft = 0.25;
double probabilityRight = 0.25;
PlayerDirection CurrentBestDecision(void);
void CheckLocalFreedom(void);
void CheckFoodDirection(void);
void RemoveImpossibleChoice(void);
};
#endif #endif
+21 -15
View File
@@ -2,20 +2,26 @@
#include <random> #include <random>
#include "common.hpp" #include "common.hpp"
namespace snakeplusplus std::default_random_engine generator;
{
std::default_random_engine generator;
void InitializeGenerator(void)
{
generator.seed(std::random_device{}());
}
// Returns a newly generated number void InitializeGenerator(void) {
int GenerateRandomNumber(int generationLimit) generator.seed(std::random_device{}());
{ }
int generatedNumber;
std::uniform_int_distribution<> distribution(0, generationLimit - 1); // Returns a newly generated number
generatedNumber = distribution(snakeplusplus::generator); int GenerateRandomNumber(int generationLimit) {
return generatedNumber; int generatedNumber;
} std::uniform_int_distribution<> distribution(0, generationLimit - 1);
generatedNumber = distribution(generator);
return generatedNumber;
}
GameSpace::GameSpace(void) {
Reset();
}
void GameSpace::Reset(void) {
m_bFood = 0;
m_bSnake = 0;
m_bVisited = 0;
} }
+28 -20
View File
@@ -1,20 +1,28 @@
#ifndef COMMON_HPP #ifndef COMMON_HPP
#define COMMON_HPP #define COMMON_HPP
namespace snakeplusplus void InitializeGenerator(void);
{ int GenerateRandomNumber(int generationLimit);
void InitializeGenerator(void);
int GenerateRandomNumber(int generationLimit); enum PlayerDirection {
kNone = 0,
enum PlayerDirection kLeft = 1,
{ kUp = 2,
kNone = 0, kDown = 3,
kLeft = 1, kRight = 4
kUp = 2, };
kDown = 3,
kRight = 4 struct GameSpace {
}; GameSpace(void);
unsigned char m_bFood : 1 = 0;
} unsigned char m_bSnake : 1 = 0;
unsigned char m_bVisited : 1 = 0; // Used for BFS/DFS
#endif unsigned char _3 : 1 = 0;
unsigned char _4 : 1 = 0;
unsigned char _5 : 1 = 0;
unsigned char _6 : 1 = 0;
unsigned char _7 : 1 = 0;
void Reset(void);
};
#endif
+167 -140
View File
@@ -6,150 +6,177 @@
#include "playerinterface.hpp" #include "playerinterface.hpp"
#include "gamestate.hpp" #include "gamestate.hpp"
namespace snakeplusplus GameEngine::GameEngine(void) {
{ InitializeGenerator();
GameEngine::GameEngine() }
{
InitializeGenerator();
return;
}
void GameEngine::Start() void GameEngine::Start(void) {
{ PrepareGameBoard();
PrepareGameBoard(); if (!state.m_bNoDisplay)
graphics.StartGameWindow(); graphics.StartGameWindow();
Loop(); Loop();
}
void GameEngine::Reset(void) {
if (!state.m_bIsBotControlled)
graphics.CheckContinue();
else
bot.AddIteration(player.body.size());
player.Reset();
PrepareGameBoard();
state.m_bIsGameOver = false;
if (!state.m_bIsBotControlled)
return; return;
if (!state.m_bSmart) {
while (!bot.path.empty())
bot.path.pop();
} }
if (state.m_bNoDisplay)
graphics.SetShowGame(false);
if (state.m_bSkipIterations)
graphics.SetShowGame((bot.amountPlayed + 1) % 50 == 0);
// TODO: Replace with value to force this effect
graphics.SetShowGame(true);
}
void GameEngine::Reset() void GameEngine::Loop(void) {
{ int currentScore = 0;
graphics.CheckContinue(isBotControlled); while (graphics.IsOpen() || state.m_bNoDisplay) {
player.Reset(); if (state.m_bIsGameOver)
if (isBotControlled) { Reset();
while (!bot.path.empty()) { bot.path.pop(); } UpdatePlayerSpeed();
} PlaceNewSnakePart(MovePlayer());
PrepareGameBoard(); RegenerateFood();
isGameOver = false; currentScore = player.body.size() * 100;
return; if (!state.m_bNoDisplay)
} graphics.DisplayGameState(gameBoard, currentScore);
void GameEngine::Loop(void)
{
while (graphics.IsOpen())
{
if (isGameOver) { Reset(); }
UpdatePlayerSpeed();
PlaceNewSnakePart(MovePlayer());
RegenerateFood();
graphics.DisplayGameState(gameBoard);
}
return;
}
sf::Vector2f GameEngine::MovePlayer(void)
{
sf::Vector2f newHeadPosition;
newHeadPosition.x = player.headLocation.x + player.speed.x;
newHeadPosition.y = player.headLocation.y + player.speed.y;
return newHeadPosition;
}
sf::Vector2f GameEngine::GetGameBoundaries(void)
{
return graphics.gameBoundaries;
}
void GameEngine::PlaceNewSnakePart(sf::Vector2f location)
{
if (!player.speed.x && !player.speed.y) { return; }
try
{
char* locationState;
locationState = &gameBoard.at(location.y).at(location.x);
if (*locationState == 'O' && (player.body.size() > 1))
isGameOver = true; // Game should end (Snake touching snake)
*locationState = 'O';
player.body.push(locationState);
player.headLocation = location;
if (playerFood.location != location)
player.Pop();
} catch (const std::out_of_range& error) {
isGameOver = true; // Snake ran into edge
}
return;
}
// Generates new food until not colliding with player
void GameEngine::RegenerateFood(void)
{
sf::Vector2f newLocation = playerFood.location;
bool isUpdated = false;
while (gameBoard.at(newLocation.y).at(newLocation.x) == 'O')
{
isUpdated = true;
playerFood.GenerateNewFood(GetGameBoundaries());
newLocation = playerFood.location;
}
if (isUpdated) {
gameBoard.at(newLocation.y).at(newLocation.x) = 'X';
}
return;
}
void GameEngine::PrepareGameBoard(void)
{
gameBoard.clear();
sf::Vector2f boardDimensions = GetGameBoundaries();
gameBoard.resize(boardDimensions.y, std::vector<char> (boardDimensions.x, ' '));
// Snake setup
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
{
char* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
player.body.push(locationState);
*locationState = 'O';
}
// Food setup
playerFood.GenerateNewFood(boardDimensions);
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
return;
}
void GameEngine::UpdatePlayerSpeed(void)
{
PlayerDirection controller;
if (isBotControlled) {
if (bot.path.empty()) {
bot.GetNewPath(gameBoard, player.headLocation, GetGameBoundaries(), player.body.size());
}
controller = bot.GetInput(&player.headLocation);
}
else { controller = GetPlayerInput(); }
switch (controller) {
case kUp:
if (player.speed.y == kUnitSpeed) { break; }
player.speed.x = 0;
player.speed.y = -kUnitSpeed;
break;
case kLeft:
if (player.speed.x == kUnitSpeed) { break; }
player.speed.x = -kUnitSpeed;
player.speed.y = 0;
break;
case kRight:
if (player.speed.x == -kUnitSpeed) { break; }
player.speed.x = kUnitSpeed;
player.speed.y = 0;
break;
case kDown:
if (player.speed.y == -kUnitSpeed) { break; }
player.speed.x = 0;
player.speed.y = kUnitSpeed;
break;
default:
break;
}
return;
} }
} }
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) {
return graphics.gameBoundaries;
}
PlayerDirection GameEngine::GetCurrentDirection(void) {
if (player.speed.x) {
if (player.speed.x > 0)
return kRight;
else
return kLeft;
} else {
if (player.speed.y > 0)
return kDown;
else
return kUp;
}
}
int GameEngine::GetPlayerSize(void) {
return player.body.size();
}
sf::Vector2f GameEngine::GetHeadLocation(void) {
return player.headLocation;
}
sf::Vector2f GameEngine::GetFoodLocation(void) {
return playerFood.location;
}
void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
if (!player.speed.x && !player.speed.y)
return;
try {
GameSpace* locationState = &gameBoard.at(location.y).at(location.x);
if (locationState->m_bSnake && (player.body.size() > 1))
state.m_bIsGameOver = true; // Game should end (Snake touching snake)
locationState->m_bSnake = true;
player.body.push(locationState);
player.headLocation = location;
if (playerFood.location != location)
player.Pop();
else {
locationState->m_bFood = false;
if (state.m_bIsBotControlled)
bot.ResetPath();
}
} catch (const std::out_of_range& error) {
state.m_bIsGameOver = true; // Snake ran into edge
}
}
// Generates new food until not colliding with player
void GameEngine::RegenerateFood(void) {
// Generate a new food location if the current one is occupied
while (gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bSnake)
playerFood.GenerateNewFood(GetGameBoundaries());
// Update the game board with the new food location
gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bFood = 1;
}
void GameEngine::PrepareGameBoard(void) {
// Create empty game board
gameBoard.clear();
sf::Vector2f boardDimensions = GetGameBoundaries();
gameBoard.resize(boardDimensions.y, std::vector<GameSpace>(boardDimensions.x));
// Snake setup
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
GameSpace* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
player.body.push(locationState);
locationState->m_bSnake = true;
// Food setup
playerFood.GenerateNewFood(boardDimensions);
gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bFood = true;
return;
}
void GameEngine::UpdatePlayerSpeed(void) {
PlayerDirection controller;
if (state.m_bIsBotControlled) {
if (bot.path.empty())
bot.GetNewPath();
controller = bot.GetInput();
} else
controller = GetPlayerInput();
switch (controller) {
case kUp:
if (player.speed.y == kUnitSpeed)
break;
player.speed.x = 0;
player.speed.y = -kUnitSpeed;
break;
case kLeft:
if (player.speed.x == kUnitSpeed)
break;
player.speed.x = -kUnitSpeed;
player.speed.y = 0;
break;
case kRight:
if (player.speed.x == -kUnitSpeed)
break;
player.speed.x = kUnitSpeed;
player.speed.y = 0;
break;
case kDown:
if (player.speed.y == -kUnitSpeed)
break;
player.speed.x = 0;
player.speed.y = kUnitSpeed;
break;
default:
break;
}
return;
}
+38 -27
View File
@@ -3,37 +3,48 @@
#define GAMESTATE_HPP #define GAMESTATE_HPP
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <memory>
#include "botinterface.hpp" #include "botinterface.hpp"
#include "common.hpp"
#include "snake.hpp" #include "snake.hpp"
#include "playerinterface.hpp" #include "playerinterface.hpp"
namespace snakeplusplus const int kUnitSpeed = 1;
{
const int kUnitSpeed = 1;
class GameEngine class GameEngine {
{ public:
public: GameEngine(void);
GameEngine(); void Start(void);
void Start(void); void Reset(void);
void Reset(void); sf::Vector2f GetGameBoundaries(void);
sf::Vector2f GetGameBoundaries(void); struct GameState {
private: unsigned char m_bIsGameOver : 1 = 0;
std::vector< std::vector<char> > gameBoard; unsigned char m_bIsBotControlled : 1 = 0;
PlayerOutput graphics; unsigned char m_bNoDisplay : 1 = 0;
Snake player; unsigned char m_bSmart : 1 = 0;
Food playerFood; unsigned char m_bSkipIterations : 1 = 0;
AISnake bot; unsigned char _5 : 1 = 0;
bool isGameOver = 0; unsigned char _6 : 1 = 0;
bool isBotControlled = 1; unsigned char _7 : 1 = 0;
void DisplayEndScreen(void); } state;
void Loop(void); std::vector< std::vector<GameSpace> > gameBoard;
sf::Vector2f MovePlayer(void); PlayerDirection GetCurrentDirection(void);
void PlaceNewSnakePart(sf::Vector2f location); int GetPlayerSize(void);
void RegenerateFood(void); sf::Vector2f GetHeadLocation(void);
void PrepareGameBoard(void); sf::Vector2f GetFoodLocation(void);
void UpdatePlayerSpeed(); private:
}; PlayerOutput graphics;
} Snake player;
Food playerFood;
AISnake bot;
void Loop(void);
sf::Vector2f MovePlayer(void);
void PlaceNewSnakePart(sf::Vector2f location);
void RegenerateFood(void);
void PrepareGameBoard(void);
void UpdatePlayerSpeed(void);
};
inline std::unique_ptr<GameEngine> g_pEngine;
#endif #endif
+48 -8
View File
@@ -1,8 +1,48 @@
#include "gamestate.hpp" #include "gamestate.hpp"
#include <memory>
int main(void) #include <string>
{ #include <vector>
snakeplusplus::GameEngine game; #include <iostream>
game.Start();
return 0; void Help(void) {
} std::cout << "Usage: snakeplusplus [OPTIONS]" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << "\t--server\tRun snake in server mode (also sets --bot)" << std::endl;
std::cout << "\t--auto\t\tControl snake using a bot or AI" << std::endl;
std::cout << "\t-h, --help\tPrint this help message and exit" << std::endl;
std::cout << std::endl;
std::cout << "Autoplay options (requires --auto):" << std::endl;
std::cout << "\t--dumb\t\tPlays using basic search algorithms BFS and DFS" << std::endl;
std::cout << "\t--smart\t\tTrains an algorithm using unsupervised learning" << std::endl;
std::cout << std::endl;
}
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);
g_pEngine = std::make_unique<GameEngine>();
for (int i = 1; i < args.size(); ++i) {
if (args[i].compare("--server") == 0) {
g_pEngine->state.m_bNoDisplay = true;
g_pEngine->state.m_bIsBotControlled = true;
std::cout << "[LOG - Main] Disabling display" << std::endl;
} else if (args[i].compare("--auto") == 0) {
g_pEngine->state.m_bIsBotControlled = true;
std::cout << "[LOG - Main] Bot control enabled" << std::endl;
} else if (args[i].compare("--smart") == 0) {
g_pEngine->state.m_bSmart = true;
std::cout << "[LOG - Main] Using AI" << std::endl;
} else if (args[i].compare("--skip") == 0) {
g_pEngine->state.m_bSkipIterations = true;
std::cout << "[LOG - Main] Only showing every 50 epochs" << std::endl;
} else if (args[i].compare("-h") == 0 || args[i].compare("--help") == 0) {
Help();
return 0;
} else {
std::cout << "[LOG - Main] Argument `" << args[i] << "` unrecognized, printing help and exiting..."<< std::endl;
Help();
return 1;
}
}
g_pEngine->Start();
return 0;
}
+136 -134
View File
@@ -2,144 +2,146 @@
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp> #include <SFML/Window/Keyboard.hpp>
namespace snakeplusplus PlayerDirection GetPlayerInput(void) {
{ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ||
PlayerDirection GetPlayerInput(void) (sf::Keyboard::isKeyPressed(sf::Keyboard::A)))
{ return kLeft;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) ||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::A)) (sf::Keyboard::isKeyPressed(sf::Keyboard::W)))
return kLeft; return kUp;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) ||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::W)) (sf::Keyboard::isKeyPressed(sf::Keyboard::S)))
return kUp; return kDown;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) ||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::S)) (sf::Keyboard::isKeyPressed(sf::Keyboard::D)))
return kDown; return kRight;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) return kNone;
|| sf::Keyboard::isKeyPressed(sf::Keyboard::D)) }
return kRight;
return kNone;
}
bool PlayerOutput::IsOpen(void) bool PlayerOutput::IsOpen(void) {
{ return isWindowAlive;
return gameWindow.isOpen(); }
}
PlayerOutput::PlayerOutput(void) PlayerOutput::PlayerOutput(void) {
{ float kWidth = 1025;
float kWidth = 1025; float kHeight = 725;
float kHeight = 725; float kBoardWidth = kWidth / kGridSize;
float kBoardWidth = kWidth / kGridSize; float kBoardHeight = kHeight / kGridSize;
float kBoardHeight = kHeight / kGridSize; gameBoundaries = sf::Vector2f(kBoardWidth, kBoardHeight);
gameBoundaries = sf::Vector2f(kBoardWidth, kBoardHeight); gameVideoSettings = sf::VideoMode(kWidth, kHeight);
gameVideoSettings = sf::VideoMode(kWidth, kHeight); drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize)); return;
return; }
}
void PlayerOutput::CheckContinue(bool isBotControlled) void PlayerOutput::CheckContinue(void) {
{ DisplayEndScreen();
if (isBotControlled) { return; } while (true) {
DisplayEndScreen(); gameWindow.pollEvent(event);
while (true) if ((event.type == sf::Event::Closed) ||
{ (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) {
gameWindow.pollEvent(event); gameWindow.close();
if ((event.type == sf::Event::Closed) isWindowAlive = false;
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) return;
{
gameWindow.close();
return;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) { return; }
sf::sleep(delay);
} }
} if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
return;
void PlayerOutput::DisplayEndScreen(void)
{
gameWindow.clear();
sf::Vector2f textPosition(gameBoundaries);
textPosition.x = textPosition.x / 2;
textPosition.y = textPosition.y / 2;
sf::Font font;
font.loadFromFile("Arial.ttf");
sf::Text gameOverText("Game Over\nPress 'Enter' to play again", font);
gameOverText.setPosition(textPosition);
gameWindow.draw(gameOverText);
gameWindow.display();
return;
}
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard)
{
CheckWindowEvents();
char* letterOnBoard;
for (float y = 0; y < gameBoundaries.y; y++)
{
for (float x = 0; x < gameBoundaries.x; x++)
{
letterOnBoard = &gameBoard.at(y).at(x);
switch (*letterOnBoard)
{
case 'O':
DrawSnake(sf::Vector2f(x, y));
break;
case 'X':
DrawFood(sf::Vector2f(x,y));
break;
default:
DrawEmpty(sf::Vector2f(x,y));
break;
}
}
}
gameWindow.display();
sf::sleep(delay); sf::sleep(delay);
return;
}
void PlayerOutput::StartGameWindow(void)
{
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
isWindowAlive = true;
return;
}
void PlayerOutput::CheckWindowEvents(void)
{
while (gameWindow.pollEvent(event))
{
if ((event.type == sf::Event::Closed)
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
gameWindow.close();
}
}
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)
{
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Red);
gameWindow.draw(drawObject);
return;
}
void PlayerOutput::DrawSnake(sf::Vector2f location)
{
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Green);
gameWindow.draw(drawObject);
return;
} }
} }
void PlayerOutput::DisplayEndScreen(void) {
gameWindow.clear();
sf::Vector2f textPosition(gameBoundaries);
textPosition.x = textPosition.x / 2;
textPosition.y = textPosition.y / 2;
sf::Font font;
font.loadFromFile("Arial.ttf");
sf::Text gameOverText("Game Over\nPress 'Enter' to play again", font);
gameOverText.setPosition(textPosition);
gameWindow.draw(gameOverText);
gameWindow.display();
return;
}
void PlayerOutput::DisplayScore(int score) {
sf::Vector2f textPosition(gameBoundaries);
textPosition.x = textPosition.x / 2;
textPosition.y = textPosition.y / 2;
sf::Font font;
font.loadFromFile("Arial.ttf");
std::string text = "Score: " + std::to_string(score);
sf::Text ScoreText(text, font);
ScoreText.setPosition(textPosition);
gameWindow.draw(ScoreText);
}
void PlayerOutput::DisplayGameState(std::vector< std::vector<GameSpace> >& gameBoard, int score) {
CheckWindowEvents();
if (delay == sf::milliseconds(0)) { return; }
char* letterOnBoard;
for (float y = 0; y < gameBoundaries.y; y++) {
for (float x = 0; x < gameBoundaries.x; x++) {
if (gameBoard.at(y).at(x).m_bSnake)
DrawSnake(sf::Vector2f(x, y));
else if (gameBoard.at(y).at(x).m_bFood)
DrawFood(sf::Vector2f(x,y));
else
DrawEmpty(sf::Vector2f(x,y));
}
}
DisplayScore(score);
gameWindow.display();
sf::sleep(delay);
return;
}
void PlayerOutput::StartGameWindow(void) {
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
isWindowAlive = true;
}
void PlayerOutput::SetShowGame(bool isShowing) {
if (isShowing)
delay = sf::milliseconds(5);
else
delay = sf::milliseconds(0);
}
void PlayerOutput::CheckWindowEvents(void) {
while (gameWindow.pollEvent(event)) {
if ((event.type == sf::Event::Closed) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) {
gameWindow.close();
isWindowAlive = false;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Equal)) {
if (delay > sf::milliseconds(16)) { continue; }
delay += sf::milliseconds(1);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Hyphen)) {
if (delay == sf::milliseconds(0)) { continue; }
delay -= sf::milliseconds(1);
}
}
}
void PlayerOutput::DrawEmpty(sf::Vector2f location) {
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Black);
gameWindow.draw(drawObject);
}
void PlayerOutput::DrawFood(sf::Vector2f location) {
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Red);
gameWindow.draw(drawObject);
}
void PlayerOutput::DrawSnake(sf::Vector2f location) {
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Green);
gameWindow.draw(drawObject);
}
+24 -26
View File
@@ -6,32 +6,30 @@
const int kGridSize = 25; const int kGridSize = 25;
namespace snakeplusplus PlayerDirection GetPlayerInput(void);
{
PlayerDirection GetPlayerInput(void);
class PlayerOutput class PlayerOutput {
{ public:
public: sf::Vector2f gameBoundaries;
sf::Vector2f gameBoundaries; PlayerOutput(void);
PlayerOutput(void); bool IsOpen(void);
bool IsOpen(void); void CheckContinue(void);
void CheckContinue(bool isBotControlled); void DisplayGameState(std::vector< std::vector<GameSpace> >& gameBoard, int score);
void DisplayGameState(std::vector< std::vector<char> >& gameBoard); void DisplayScore(int score);
void StartGameWindow(void); void StartGameWindow(void);
private: void SetShowGame(bool isShowing);
void CheckWindowEvents(void); private:
void DisplayEndScreen(void); void CheckWindowEvents(void);
void DrawEmpty(sf::Vector2f location); void DisplayEndScreen(void);
void DrawFood(sf::Vector2f location); void DrawEmpty(sf::Vector2f location);
void DrawSnake(sf::Vector2f location); void DrawFood(sf::Vector2f location);
sf::RenderWindow gameWindow; void DrawSnake(sf::Vector2f location);
sf::VideoMode gameVideoSettings; sf::RenderWindow gameWindow;
sf::RectangleShape drawObject; sf::VideoMode gameVideoSettings;
sf::Event event; sf::RectangleShape drawObject;
bool isWindowAlive; sf::Event event;
sf::Time delay = sf::milliseconds(15); bool isWindowAlive = false;
}; sf::Time delay = sf::milliseconds(12);
} };
#endif #endif
+20 -25
View File
@@ -3,29 +3,24 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "common.hpp" #include "common.hpp"
#include "snake.hpp" #include "snake.hpp"
namespace snakeplusplus void Snake::Pop(void) {
{ body.front()->m_bSnake = false;
void Snake::Pop(void) body.pop();
{ return;
*(body.front()) = ' '; }
body.pop();
return; void Snake::Reset(void) {
} while (!body.empty())
Pop();
void Snake::Reset(void) speed.x = 0;
{ speed.y = 0;
while (!body.empty()) Pop(); return;
speed.x = 0; }
speed.y = 0;
return; // Returns a new food object for the snakeFood
} void Food::GenerateNewFood(sf::Vector2f boundaries) {
location.x = GenerateRandomNumber(boundaries.x);
// Returns a new food object for the snakeFood location.y = GenerateRandomNumber(boundaries.y);
void Food::GenerateNewFood(sf::Vector2f boundaries) return;
{
location.x = GenerateRandomNumber(boundaries.x);
location.y = GenerateRandomNumber(boundaries.y);
return;
}
} }
+14 -19
View File
@@ -4,26 +4,21 @@
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <queue> #include <queue>
#include "common.hpp"
namespace snakeplusplus struct Snake {
{ public:
struct Snake sf::Vector2f headLocation;
{ sf::Vector2f speed;
public: std::queue<GameSpace*> body;
sf::Vector2f headLocation; void Pop(void);
sf::Vector2f speed; void Reset(void);
std::queue<char*> body; };
void Pop(void);
void Reset(void);
};
struct Food struct Food {
{ public:
public: sf::Vector2f location;
sf::Vector2f location; void GenerateNewFood(sf::Vector2f boundaries);
char* food; };
void GenerateNewFood(sf::Vector2f boundaries);
};
}
#endif #endif