auto: fix crashing on edges and getting stuck

This commit is contained in:
Trianta 2024-08-21 04:32:30 -05:00
parent c6197e84dc
commit 63cbe41086
4 changed files with 25 additions and 12 deletions

View File

@ -15,17 +15,27 @@ AISnake::AISnake(void) {
PlayerDirection AISnake::GetInput(void) {
sf::Vector2f source(g_pEngine->GetHeadLocation());
//if (g_pEngine->state.m_bSmart && path.empty())
// return CurrentBestDecision();
sf::Vector2f directionDelta;
while (source == path.top() && !path.empty())
while (!path.empty() && source == path.top())
path.pop();
if (path.empty())
path.push(GetAnyOpenPath());
if (path.empty()) {
if (g_pEngine->state.m_bSmart) {
return CurrentBestDecision();
} else
path.push(GetAnyOpenPath());
}
directionDelta = source - path.top();
path.pop();
try {
sf::Vector2f next = path.top();
if (g_pEngine->gameBoard.at(next.y).at(next.x).m_bSnake)
next = GetAnyOpenPath();
directionDelta = source - next;
path.pop();
} catch (const std::out_of_range& error) {
directionDelta = source - GetAnyOpenPath(); // Out of bounds
EmptyPath();
}
if ((directionDelta.y == 1) && (lastKnownDirection != kDown))
lastKnownDirection = kUp;
@ -115,8 +125,6 @@ void AISnake::BFS(void) {
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)
@ -153,8 +161,6 @@ void AISnake::DFS(void) {
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)
@ -180,6 +186,8 @@ sf::Vector2f AISnake::GetAnyOpenPath(void) {
for (auto path : paths) {
try {
if (path == -g_pEngine->GetCurrentDirectionRaw())
continue; // Impossible action
bail = path;
if (g_pEngine->gameBoard.at(path.y).at(path.x).m_bSnake)
continue;

View File

@ -19,7 +19,7 @@ public:
private:
int totalLength = 0;
double average = 0;
double thresholdDFS = 0.200;
double thresholdDFS = 0.900;
bool pathFailed = false;
// Generic search algorithms

View File

@ -78,6 +78,10 @@ PlayerDirection GameEngine::GetCurrentDirection(void) {
}
}
sf::Vector2f GameEngine::GetCurrentDirectionRaw(void) {
return player.speed;
}
int GameEngine::GetPlayerSize(void) {
return player.body.size();
}

View File

@ -29,6 +29,7 @@ public:
} state;
std::vector< std::vector<GameSpace> > gameBoard;
PlayerDirection GetCurrentDirection(void);
sf::Vector2f GetCurrentDirectionRaw(void);
int GetPlayerSize(void);
sf::Vector2f GetHeadLocation(void);
sf::Vector2f GetFoodLocation(void);