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

View File

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

View File

@ -157,9 +157,9 @@ void GameEngine::UpdatePlayerSpeed(void)
PlayerDirection controller;
if (state.m_bIsBotControlled) {
if (bot.path.empty()) {
bot.GetNewPath(player.headLocation);
bot.GetNewPath();
}
controller = bot.GetInput(&player.headLocation);
controller = bot.GetInput();
}
else { controller = GetPlayerInput(); }
switch (controller) {