core: fix various bugs and simplify things
This commit is contained in:
parent
ccf5843e61
commit
722970eb12
@ -1,5 +1,6 @@
|
|||||||
#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 <iostream>
|
||||||
@ -15,9 +16,14 @@ AISnake::AISnake() {
|
|||||||
|
|
||||||
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
||||||
{
|
{
|
||||||
|
// TODO: Figure out why bot is suddenly going rogue
|
||||||
sf::Vector2f directionDelta;
|
sf::Vector2f directionDelta;
|
||||||
if (*source == path.top()) { path.pop(); }
|
if (!source) {
|
||||||
if (path.empty()) { return kUp; } // Snake is trapped
|
std::cout << "[ERROR - AI] Source was borked, bailing" << std::endl;
|
||||||
|
return kUp;
|
||||||
|
}
|
||||||
|
while (*source == path.top() && !path.empty()) { path.pop(); }
|
||||||
|
if (path.empty()) { path.push(GetAnyOpenPath(*source)); }
|
||||||
directionDelta = *source - path.top();
|
directionDelta = *source - path.top();
|
||||||
path.pop();
|
path.pop();
|
||||||
if ((directionDelta.y == 1)
|
if ((directionDelta.y == 1)
|
||||||
@ -46,30 +52,158 @@ void AISnake::AdjustProbability(double amount)
|
|||||||
probabilityBFS += amount;
|
probabilityBFS += amount;
|
||||||
if (probabilityBFS > 1.0) { probabilityBFS = 1.0; }
|
if (probabilityBFS > 1.0) { probabilityBFS = 1.0; }
|
||||||
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
|
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
|
||||||
std::cout << "[Info - AISnake] New BFS probability: " << probabilityBFS << std::endl;
|
std::cout << "[Info - AI] New BFS probability: " << probabilityBFS << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AISnake::AddIteration(const int size)
|
||||||
|
{
|
||||||
|
if (size > 40)
|
||||||
|
{
|
||||||
|
UpdateAverage(size);
|
||||||
|
double adjustmentAmount = 0.002;
|
||||||
|
if (average > size) { AdjustProbability(adjustmentAmount); }
|
||||||
|
else { AdjustProbability(-adjustmentAmount); }
|
||||||
|
}
|
||||||
|
std::cout << "[Info - AI] Current average: " << average << std::endl;
|
||||||
|
std::cout << "[Info - AI] Previous iteration size: " << size << std::endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void AISnake::ResetPath(void) {
|
||||||
|
while (!path.empty()) { path.pop(); }
|
||||||
|
}
|
||||||
|
|
||||||
// Gets a new path for the bot to follow
|
// Gets a new path for the bot to follow
|
||||||
// Uses DFS algorithm
|
// Uses DFS algorithm
|
||||||
void AISnake::GetNewPath(const std::vector< std::vector<GameSpace> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
void AISnake::GetNewPath(const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
||||||
{
|
{
|
||||||
// Search for food
|
// Search for food
|
||||||
/*
|
|
||||||
BFS(gameBoard, source, boundaries);
|
|
||||||
if (gameBoard[botPathUnsanitized.top().y][botPathUnsanitized.top().x] != 'X') {
|
|
||||||
while (!botPathUnsanitized.empty()) { botPathUnsanitized.pop(); }
|
|
||||||
DFS(gameBoard, source, boundaries);
|
|
||||||
while (botPathUnsanitized.size() > 15) { botPathUnsanitized.pop(); }
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
// Probability-based approach for fun
|
// Probability-based approach for fun
|
||||||
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
|
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
|
||||||
if (roll <= probabilityBFS) { BFS(gameBoard, source, boundaries); }
|
if (roll <= probabilityBFS) { BFS(source, boundaries); }
|
||||||
else { DFS(gameBoard, source, boundaries); }
|
else { DFS(source, boundaries); }
|
||||||
|
UnvisitBoard();
|
||||||
// Create path for food
|
// Create path for food
|
||||||
path.push(botPathUnsanitized.top());
|
path.push(botPathUnsanitized.top());
|
||||||
botPathUnsanitized.pop();
|
botPathUnsanitized.pop();
|
||||||
|
TrimPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AISnake::BFS(const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
||||||
|
std::queue<sf::Vector2f> search;
|
||||||
|
bool foodFound = false;
|
||||||
|
search.push(source);
|
||||||
|
while (!search.empty()) {
|
||||||
|
sf::Vector2f currentLocation = search.front();
|
||||||
|
search.pop();
|
||||||
|
if (foodFound) { break; }
|
||||||
|
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 {
|
||||||
|
if (g_pEngine->gameBoard.at(nearby.y).at(nearby.x).m_bFood) {
|
||||||
|
botPathUnsanitized.push(nearby);
|
||||||
|
foodFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (g_pEngine->gameBoard.at(nearby.y).at(nearby.x).m_bVisited)
|
||||||
|
continue;
|
||||||
|
if (g_pEngine->gameBoard.at(nearby.y).at(nearby.x).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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void AISnake::DFS(const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
||||||
|
std::stack<sf::Vector2f> search;
|
||||||
|
bool foodFound = false;
|
||||||
|
search.push(source);
|
||||||
|
while (!search.empty()) {
|
||||||
|
sf::Vector2f currentLocation = search.top();
|
||||||
|
search.pop();
|
||||||
|
if (foodFound) { break; }
|
||||||
|
if (g_pEngine->gameBoard.at(currentLocation.y).at(currentLocation.x).m_bVisited) { continue; }
|
||||||
|
if (g_pEngine->gameBoard.at(currentLocation.y).at(currentLocation.x).m_bFood) { foodFound = true; }
|
||||||
|
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 {
|
||||||
|
if (g_pEngine->gameBoard.at(nearby.y).at(nearby.x).m_bFood) {
|
||||||
|
botPathUnsanitized.push(nearby);
|
||||||
|
foodFound = true;
|
||||||
|
std::cout << "[TRACE - AI] Found food, breaking..." << std::endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (g_pEngine->gameBoard.at(nearby.y).at(nearby.x).m_bVisited)
|
||||||
|
continue;
|
||||||
|
if (g_pEngine->gameBoard.at(nearby.y).at(nearby.x).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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sf::Vector2f AISnake::GetAnyOpenPath(const sf::Vector2f& source) {
|
||||||
|
sf::Vector2f bail;
|
||||||
|
sf::Vector2f paths[4];
|
||||||
|
paths[0] = source;
|
||||||
|
paths[0].x -= 1;
|
||||||
|
paths[1] = source;
|
||||||
|
paths[1].x += 1;
|
||||||
|
paths[2] = source;
|
||||||
|
paths[2].y -= 1;
|
||||||
|
paths[3] = source;
|
||||||
|
paths[3].y += 1;
|
||||||
|
|
||||||
|
for (auto path : paths) {
|
||||||
|
try {
|
||||||
|
if (g_pEngine->gameBoard.at(path.y).at(path.x).m_bSnake) {
|
||||||
|
bail = path;
|
||||||
|
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 (auto i : g_pEngine->gameBoard)
|
||||||
|
for (auto j : i)
|
||||||
|
j.m_bVisited = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AISnake::UpdateAverage(const int size) {
|
||||||
|
totalLength += size;
|
||||||
|
amountPlayed += 1;
|
||||||
|
average = (double)totalLength / amountPlayed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AISnake::TrimPath(void) {
|
||||||
while (!botPathUnsanitized.empty()) {
|
while (!botPathUnsanitized.empty()) {
|
||||||
sf::Vector2f deltaVector = botPathUnsanitized.top() - path.top();
|
sf::Vector2f deltaVector = botPathUnsanitized.top() - path.top();
|
||||||
int delta = abs(deltaVector.x) + abs(deltaVector.y);
|
int delta = abs(deltaVector.x) + abs(deltaVector.y);
|
||||||
@ -79,99 +213,3 @@ void AISnake::GetNewPath(const std::vector< std::vector<GameSpace> >& gameBoard,
|
|||||||
botPathUnsanitized.pop();
|
botPathUnsanitized.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::BFS(const std::vector< std::vector<GameSpace> >& 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;
|
|
||||||
search.push(source);
|
|
||||||
while (!search.empty()) {
|
|
||||||
sf::Vector2f currentLocation = search.front();
|
|
||||||
search.pop();
|
|
||||||
if (foodFound) { break; }
|
|
||||||
if (visited.at(currentLocation.y).at(currentLocation.x)) { continue; }
|
|
||||||
if (gameBoard.at(currentLocation.y).at(currentLocation.x).m_bFood) {
|
|
||||||
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).m_bFood) {
|
|
||||||
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).m_bFood)
|
|
||||||
&& (gameBoard.at(newLocation.y).at(newLocation.x).m_bSnake)) {
|
|
||||||
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<GameSpace> >& 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;
|
|
||||||
search.push(source);
|
|
||||||
while (!search.empty()) {
|
|
||||||
sf::Vector2f currentLocation = search.top();
|
|
||||||
search.pop();
|
|
||||||
if (foodFound) { break; }
|
|
||||||
if (visited[currentLocation.y][currentLocation.x]) { continue; }
|
|
||||||
if (gameBoard[currentLocation.y][currentLocation.x].m_bFood) {
|
|
||||||
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).m_bFood) {
|
|
||||||
botPathUnsanitized.push(i);
|
|
||||||
foodFound = true;
|
|
||||||
}
|
|
||||||
} catch (const std::out_of_range& error) {
|
|
||||||
continue; // Out of bounds
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (sf::Vector2f newLocation : localLocations) {
|
|
||||||
try {
|
|
||||||
if (newLocation.x < 1 || newLocation.y < 1
|
|
||||||
|| newLocation.x > boundaries.x - 2
|
|
||||||
|| newLocation.y > boundaries.y - 2) {
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
if ((!visited.at(newLocation.y).at(newLocation.x))
|
|
||||||
&& !(gameBoard.at(newLocation.y).at(newLocation.x).m_bFood)
|
|
||||||
&& !(gameBoard.at(newLocation.y).at(newLocation.x).m_bSnake)) {
|
|
||||||
search.push(newLocation);
|
|
||||||
}
|
|
||||||
} catch (const std::out_of_range& error) {
|
|
||||||
continue; // Out of bounds
|
|
||||||
}
|
|
||||||
}
|
|
||||||
visited.at(currentLocation.y).at(currentLocation.x) = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -3,22 +3,30 @@
|
|||||||
|
|
||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <vector>
|
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
class AISnake {
|
class AISnake {
|
||||||
public:
|
public:
|
||||||
std::stack<sf::Vector2f> path;
|
std::stack<sf::Vector2f> path;
|
||||||
AISnake();
|
AISnake();
|
||||||
void GetNewPath(const std::vector< std::vector<GameSpace> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize);
|
void GetNewPath(const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize);
|
||||||
PlayerDirection GetInput(const sf::Vector2f* source);
|
PlayerDirection GetInput(const sf::Vector2f* source);
|
||||||
void UpdateProbability(int snakeSize);
|
void UpdateProbability(int snakeSize);
|
||||||
void AdjustProbability(double amount);
|
void AdjustProbability(double amount);
|
||||||
|
void AddIteration(const int size);
|
||||||
|
void ResetPath(void);
|
||||||
|
int amountPlayed = 0;
|
||||||
private:
|
private:
|
||||||
|
int totalLength = 0;
|
||||||
|
double average = 0;
|
||||||
double probabilityBFS = 0.500;
|
double probabilityBFS = 0.500;
|
||||||
std::stack<sf::Vector2f> botPathUnsanitized;
|
std::stack<sf::Vector2f> botPathUnsanitized;
|
||||||
void BFS(const std::vector< std::vector<GameSpace> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
void BFS(const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
||||||
void DFS(const std::vector< std::vector<GameSpace> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
void DFS(const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
||||||
|
sf::Vector2f GetAnyOpenPath(const sf::Vector2f& source);
|
||||||
|
void UnvisitBoard(void);
|
||||||
|
void UpdateAverage(const int size);
|
||||||
|
void TrimPath(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -18,6 +18,11 @@ int GenerateRandomNumber(int generationLimit)
|
|||||||
}
|
}
|
||||||
|
|
||||||
GameSpace::GameSpace(void) {
|
GameSpace::GameSpace(void) {
|
||||||
|
Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameSpace::Reset(void) {
|
||||||
m_bFood = 0;
|
m_bFood = 0;
|
||||||
m_bSnake = 0;
|
m_bSnake = 0;
|
||||||
|
m_bVisited = 0;
|
||||||
}
|
}
|
||||||
|
@ -17,12 +17,13 @@ struct GameSpace {
|
|||||||
GameSpace();
|
GameSpace();
|
||||||
unsigned char m_bFood : 1 = 0;
|
unsigned char m_bFood : 1 = 0;
|
||||||
unsigned char m_bSnake : 1 = 0;
|
unsigned char m_bSnake : 1 = 0;
|
||||||
unsigned char _2 : 1 = 0;
|
unsigned char m_bVisited : 1 = 0; // Used for BFS/DFS
|
||||||
unsigned char _3 : 1 = 0;
|
unsigned char _3 : 1 = 0;
|
||||||
unsigned char _4 : 1 = 0;
|
unsigned char _4 : 1 = 0;
|
||||||
unsigned char _5 : 1 = 0;
|
unsigned char _5 : 1 = 0;
|
||||||
unsigned char _6 : 1 = 0;
|
unsigned char _6 : 1 = 0;
|
||||||
unsigned char _7 : 1 = 0;
|
unsigned char _7 : 1 = 0;
|
||||||
|
void Reset(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
// GameState.cpp
|
// GameState.cpp
|
||||||
#include <iostream>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include "botinterface.hpp"
|
#include "botinterface.hpp"
|
||||||
@ -24,7 +23,8 @@ void GameEngine::Start()
|
|||||||
|
|
||||||
void GameEngine::Reset()
|
void GameEngine::Reset()
|
||||||
{
|
{
|
||||||
AddIteration();
|
graphics.CheckContinue(state.m_bIsBotControlled);
|
||||||
|
bot.AddIteration(player.body.size());
|
||||||
player.Reset();
|
player.Reset();
|
||||||
if (state.m_bIsBotControlled) { while (!bot.path.empty()) { bot.path.pop(); } }
|
if (state.m_bIsBotControlled) { while (!bot.path.empty()) { bot.path.pop(); } }
|
||||||
PrepareGameBoard();
|
PrepareGameBoard();
|
||||||
@ -32,25 +32,10 @@ void GameEngine::Reset()
|
|||||||
if (state.m_bNoDisplay)
|
if (state.m_bNoDisplay)
|
||||||
graphics.SetShowGame(false);
|
graphics.SetShowGame(false);
|
||||||
else
|
else
|
||||||
graphics.SetShowGame((amountPlayed + 1) % 50 == 0);
|
graphics.SetShowGame((bot.amountPlayed + 1) % 50 == 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::AddIteration(void)
|
|
||||||
{
|
|
||||||
graphics.CheckContinue(state.m_bIsBotControlled);
|
|
||||||
if (player.body.size() > 40)
|
|
||||||
{
|
|
||||||
UpdateAverage();
|
|
||||||
double adjustmentAmount = 0.002;
|
|
||||||
if (average > player.body.size()) { bot.AdjustProbability(adjustmentAmount); }
|
|
||||||
else { bot.AdjustProbability(-adjustmentAmount); }
|
|
||||||
}
|
|
||||||
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;
|
int currentScore = 0;
|
||||||
@ -61,7 +46,6 @@ void GameEngine::Loop(void)
|
|||||||
PlaceNewSnakePart(MovePlayer());
|
PlaceNewSnakePart(MovePlayer());
|
||||||
RegenerateFood();
|
RegenerateFood();
|
||||||
currentScore = player.body.size() * 100;
|
currentScore = player.body.size() * 100;
|
||||||
//bot.UpdateProbability(player.body.size());
|
|
||||||
if (!state.m_bNoDisplay)
|
if (!state.m_bNoDisplay)
|
||||||
graphics.DisplayGameState(gameBoard, currentScore);
|
graphics.DisplayGameState(gameBoard, currentScore);
|
||||||
}
|
}
|
||||||
@ -91,8 +75,11 @@ void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
|
|||||||
player.headLocation = location;
|
player.headLocation = location;
|
||||||
if (playerFood.location != location)
|
if (playerFood.location != location)
|
||||||
player.Pop();
|
player.Pop();
|
||||||
else
|
else {
|
||||||
locationState->m_bFood = false;
|
locationState->m_bFood = false;
|
||||||
|
if (state.m_bIsBotControlled)
|
||||||
|
bot.ResetPath();
|
||||||
|
}
|
||||||
} catch (const std::out_of_range& error) {
|
} catch (const std::out_of_range& error) {
|
||||||
state.m_bIsGameOver = true; // Snake ran into edge
|
state.m_bIsGameOver = true; // Snake ran into edge
|
||||||
}
|
}
|
||||||
@ -104,12 +91,12 @@ void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
|
|||||||
void GameEngine::RegenerateFood()
|
void GameEngine::RegenerateFood()
|
||||||
{
|
{
|
||||||
// Generate a new food location if the current one is occupied
|
// Generate a new food location if the current one is occupied
|
||||||
while (gameBoard[playerFood.location.y][playerFood.location.x].m_bSnake) {
|
while (gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bSnake) {
|
||||||
playerFood.GenerateNewFood(GetGameBoundaries());
|
playerFood.GenerateNewFood(GetGameBoundaries());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the game board with the new food location
|
// Update the game board with the new food location
|
||||||
gameBoard[playerFood.location.y][playerFood.location.x].m_bFood = 1;
|
gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bFood = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -122,13 +109,13 @@ void GameEngine::PrepareGameBoard(void)
|
|||||||
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
|
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
|
||||||
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
|
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
|
||||||
{
|
{
|
||||||
GameSpace* locationState = &gameBoard[player.headLocation.y][player.headLocation.x];
|
GameSpace* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
|
||||||
player.body.push(locationState);
|
player.body.push(locationState);
|
||||||
locationState->m_bSnake = true;
|
locationState->m_bSnake = true;
|
||||||
}
|
}
|
||||||
// Food setup
|
// Food setup
|
||||||
playerFood.GenerateNewFood(boardDimensions);
|
playerFood.GenerateNewFood(boardDimensions);
|
||||||
gameBoard[playerFood.location.y][playerFood.location.x].m_bFood = true;
|
gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bFood = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +124,7 @@ void GameEngine::UpdatePlayerSpeed(void)
|
|||||||
PlayerDirection controller;
|
PlayerDirection controller;
|
||||||
if (state.m_bIsBotControlled) {
|
if (state.m_bIsBotControlled) {
|
||||||
if (bot.path.empty()) {
|
if (bot.path.empty()) {
|
||||||
bot.GetNewPath(gameBoard, player.headLocation, GetGameBoundaries(), player.body.size());
|
bot.GetNewPath(player.headLocation, GetGameBoundaries(), player.body.size());
|
||||||
}
|
}
|
||||||
controller = bot.GetInput(&player.headLocation);
|
controller = bot.GetInput(&player.headLocation);
|
||||||
}
|
}
|
||||||
@ -168,9 +155,3 @@ void GameEngine::UpdatePlayerSpeed(void)
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::UpdateAverage() {
|
|
||||||
totalLength += player.body.size();
|
|
||||||
amountPlayed += 1;
|
|
||||||
average = (double)totalLength / amountPlayed;
|
|
||||||
}
|
|
||||||
|
@ -16,7 +16,6 @@ public:
|
|||||||
GameEngine();
|
GameEngine();
|
||||||
void Start(void);
|
void Start(void);
|
||||||
void Reset(void);
|
void Reset(void);
|
||||||
void AddIteration(void);
|
|
||||||
sf::Vector2f GetGameBoundaries(void);
|
sf::Vector2f GetGameBoundaries(void);
|
||||||
struct GameState {
|
struct GameState {
|
||||||
unsigned char m_bIsGameOver : 1 = 0;
|
unsigned char m_bIsGameOver : 1 = 0;
|
||||||
@ -28,8 +27,8 @@ public:
|
|||||||
unsigned char _6 : 1 = 0;
|
unsigned char _6 : 1 = 0;
|
||||||
unsigned char _7 : 1 = 0;
|
unsigned char _7 : 1 = 0;
|
||||||
} state;
|
} state;
|
||||||
private:
|
|
||||||
std::vector< std::vector<GameSpace> > gameBoard;
|
std::vector< std::vector<GameSpace> > gameBoard;
|
||||||
|
private:
|
||||||
PlayerOutput graphics;
|
PlayerOutput graphics;
|
||||||
Snake player;
|
Snake player;
|
||||||
Food playerFood;
|
Food playerFood;
|
||||||
@ -40,10 +39,6 @@ private:
|
|||||||
void RegenerateFood(void);
|
void RegenerateFood(void);
|
||||||
void PrepareGameBoard(void);
|
void PrepareGameBoard(void);
|
||||||
void UpdatePlayerSpeed();
|
void UpdatePlayerSpeed();
|
||||||
void UpdateAverage();
|
|
||||||
int totalLength = 0;
|
|
||||||
int amountPlayed = 0;
|
|
||||||
double average = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline std::unique_ptr<GameEngine> g_pEngine;
|
inline std::unique_ptr<GameEngine> g_pEngine;
|
||||||
|
@ -10,6 +10,7 @@ int main(int argc, char* argv[])
|
|||||||
for (auto it = args.begin(); it != args.end(); it++) {
|
for (auto it = args.begin(); it != args.end(); it++) {
|
||||||
if (it->compare("--no-gui") == 0) {
|
if (it->compare("--no-gui") == 0) {
|
||||||
g_pEngine->state.m_bNoDisplay = true;
|
g_pEngine->state.m_bNoDisplay = true;
|
||||||
|
std::cout << "[LOG - Main] Disabling display" << std::endl;
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "[ERROR] Argument option not found, exiting..." << std::endl;
|
std::cerr << "[ERROR] Argument option not found, exiting..." << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "playerinterface.hpp"
|
#include "playerinterface.hpp"
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <SFML/Window/Keyboard.hpp>
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
PlayerDirection GetPlayerInput(void)
|
PlayerDirection GetPlayerInput(void)
|
||||||
{
|
{
|
||||||
@ -92,9 +93,9 @@ void PlayerOutput::DisplayGameState(std::vector< std::vector<GameSpace> >& gameB
|
|||||||
{
|
{
|
||||||
for (float x = 0; x < gameBoundaries.x; x++)
|
for (float x = 0; x < gameBoundaries.x; x++)
|
||||||
{
|
{
|
||||||
if (gameBoard[y][x].m_bSnake)
|
if (gameBoard.at(y).at(x).m_bSnake)
|
||||||
DrawSnake(sf::Vector2f(x, y));
|
DrawSnake(sf::Vector2f(x, y));
|
||||||
else if (gameBoard[y][x].m_bFood)
|
else if (gameBoard.at(y).at(x).m_bFood)
|
||||||
DrawFood(sf::Vector2f(x,y));
|
DrawFood(sf::Vector2f(x,y));
|
||||||
else
|
else
|
||||||
DrawEmpty(sf::Vector2f(x,y));
|
DrawEmpty(sf::Vector2f(x,y));
|
||||||
|
Loading…
Reference in New Issue
Block a user