auto: remove unnecessary pointer passing

This commit is contained in:
Trianta 2024-08-21 02:29:50 -05:00
parent aba9a9cc35
commit 01f24e4ebb
3 changed files with 30 additions and 32 deletions

View File

@ -6,24 +6,22 @@
#include <iostream> #include <iostream>
#include <queue> #include <queue>
#include <stdexcept> #include <stdexcept>
#include <SFML/System/Vector2.hpp>
PlayerDirection lastKnownDirection = kNone; PlayerDirection lastKnownDirection = kNone;
AISnake::AISnake() { AISnake::AISnake(void) {
; ;
} }
PlayerDirection AISnake::GetInput(const sf::Vector2f* source) PlayerDirection AISnake::GetInput(void)
{ {
if (g_pEngine->state.m_bSmart) sf::Vector2f source(g_pEngine->GetHeadLocation());
return CurrentBestDecision(); //if (g_pEngine->state.m_bSmart && path.empty())
// return CurrentBestDecision();
sf::Vector2f directionDelta; sf::Vector2f directionDelta;
if (!source) while (source == path.top() && !path.empty()) { path.pop(); }
return kUp; if (path.empty()) { path.push(GetAnyOpenPath()); }
while (*source == path.top() && !path.empty()) { path.pop(); } directionDelta = source - path.top();
if (path.empty()) { path.push(GetAnyOpenPath(*source)); }
directionDelta = *source - path.top();
path.pop(); path.pop();
if ((directionDelta.y == 1) if ((directionDelta.y == 1)
&& (lastKnownDirection != kDown)) && (lastKnownDirection != kDown))
@ -73,28 +71,27 @@ void AISnake::ResetPath(void) {
// 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 sf::Vector2f& source) void AISnake::GetNewPath(void)
{ {
// Search for food // Search for food
// 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(source); } if (roll <= probabilityBFS) { BFS(); }
else { DFS(source); } else { DFS(); }
UnvisitBoard(); UnvisitBoard();
if (pathFailed) { if (pathFailed) {
pathFailed = false; pathFailed = false;
EmptyPath(); EmptyPath();
path.push(GetAnyOpenPath(source));
} else { } else {
TrimPath(); TrimPath();
if (path.empty()) if (path.empty())
path.push(GetAnyOpenPath(source)); path.push(GetAnyOpenPath());
} }
} }
void AISnake::BFS(const sf::Vector2f& source) { void AISnake::BFS(void) {
std::queue<sf::Vector2f> search; std::queue<sf::Vector2f> search;
search.push(source); search.push(g_pEngine->GetHeadLocation());
while (!search.empty()) { while (!search.empty()) {
sf::Vector2f currentLocation = search.front(); sf::Vector2f currentLocation = search.front();
search.pop(); search.pop();
@ -114,7 +111,7 @@ void AISnake::BFS(const sf::Vector2f& source) {
botPathUnsanitized.push(nearby); botPathUnsanitized.push(nearby);
return; return;
} }
if (nearby.x < 1 || nearby.y < 1) if (nearby.x < 1 || nearby.x > g_pEngine->GetGameBoundaries().x - 2)
continue; continue;
if (space->m_bVisited) if (space->m_bVisited)
continue; continue;
@ -130,9 +127,9 @@ void AISnake::BFS(const sf::Vector2f& source) {
pathFailed = true; pathFailed = true;
} }
void AISnake::DFS(const sf::Vector2f& source) { void AISnake::DFS(void) {
std::stack<sf::Vector2f> search; std::stack<sf::Vector2f> search;
search.push(source); search.push(g_pEngine->GetHeadLocation());
while (!search.empty()) { while (!search.empty()) {
sf::Vector2f currentLocation = search.top(); sf::Vector2f currentLocation = search.top();
search.pop(); search.pop();
@ -168,10 +165,10 @@ void AISnake::DFS(const sf::Vector2f& source) {
pathFailed = true; pathFailed = true;
} }
sf::Vector2f AISnake::GetAnyOpenPath(const sf::Vector2f& source) { sf::Vector2f AISnake::GetAnyOpenPath(void) {
sf::Vector2f bail; sf::Vector2f bail;
std::array<sf::Vector2f, 4> paths; std::array<sf::Vector2f, 4> paths;
paths.fill(source); paths.fill(g_pEngine->GetHeadLocation());
paths[0].x -= 1; paths[0].x -= 1;
paths[1].x += 1; paths[1].x += 1;
paths[2].y -= 1; paths[2].y -= 1;
@ -282,7 +279,8 @@ void AISnake::CheckLocalFreedom(void) {
break; break;
} }
} catch (const std::out_of_range& error) { } catch (const std::out_of_range& error) {
continue; // Out of bounds chances[i] = 0;
continue;
} }
double openSpaces = 0; double openSpaces = 0;
for (int j = -1; j < 2; ++j) { for (int j = -1; j < 2; ++j) {

View File

@ -8,9 +8,9 @@
class AISnake { class AISnake {
public: public:
std::stack<sf::Vector2f> path; std::stack<sf::Vector2f> path;
AISnake(); AISnake(void);
void GetNewPath(const sf::Vector2f& source); void GetNewPath(void);
PlayerDirection GetInput(const sf::Vector2f* source); PlayerDirection GetInput(void);
void UpdateProbability(int snakeSize); void UpdateProbability(int snakeSize);
void AdjustProbability(double amount); void AdjustProbability(double amount);
void AddIteration(const int size); void AddIteration(const int size);
@ -19,14 +19,14 @@ public:
private: private:
int totalLength = 0; int totalLength = 0;
double average = 0; double average = 0;
double probabilityBFS = 0.800; double probabilityBFS = 0.200;
bool pathFailed = false; bool pathFailed = false;
// Generic search algorithms // Generic search algorithms
std::stack<sf::Vector2f> botPathUnsanitized; std::stack<sf::Vector2f> botPathUnsanitized;
void BFS(const sf::Vector2f& source); void BFS(void);
void DFS(const sf::Vector2f& source); void DFS(void);
sf::Vector2f GetAnyOpenPath(const sf::Vector2f& source); sf::Vector2f GetAnyOpenPath(void);
void UnvisitBoard(void); void UnvisitBoard(void);
void UpdateAverage(const int size); void UpdateAverage(const int size);
void TrimPath(void); void TrimPath(void);

View File

@ -157,9 +157,9 @@ 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(player.headLocation); bot.GetNewPath();
} }
controller = bot.GetInput(&player.headLocation); controller = bot.GetInput();
} }
else { controller = GetPlayerInput(); } else { controller = GetPlayerInput(); }
switch (controller) { switch (controller) {