auto: fix crashing on edges and getting stuck
This commit is contained in:
parent
c6197e84dc
commit
63cbe41086
@ -15,17 +15,27 @@ AISnake::AISnake(void) {
|
|||||||
|
|
||||||
PlayerDirection AISnake::GetInput(void) {
|
PlayerDirection AISnake::GetInput(void) {
|
||||||
sf::Vector2f source(g_pEngine->GetHeadLocation());
|
sf::Vector2f source(g_pEngine->GetHeadLocation());
|
||||||
//if (g_pEngine->state.m_bSmart && path.empty())
|
|
||||||
// return CurrentBestDecision();
|
|
||||||
sf::Vector2f directionDelta;
|
sf::Vector2f directionDelta;
|
||||||
|
|
||||||
while (source == path.top() && !path.empty())
|
while (!path.empty() && source == path.top())
|
||||||
path.pop();
|
path.pop();
|
||||||
if (path.empty())
|
if (path.empty()) {
|
||||||
path.push(GetAnyOpenPath());
|
if (g_pEngine->state.m_bSmart) {
|
||||||
|
return CurrentBestDecision();
|
||||||
|
} else
|
||||||
|
path.push(GetAnyOpenPath());
|
||||||
|
}
|
||||||
|
|
||||||
directionDelta = source - path.top();
|
try {
|
||||||
path.pop();
|
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))
|
if ((directionDelta.y == 1) && (lastKnownDirection != kDown))
|
||||||
lastKnownDirection = kUp;
|
lastKnownDirection = kUp;
|
||||||
@ -115,8 +125,6 @@ void AISnake::BFS(void) {
|
|||||||
botPathUnsanitized.push(nearby);
|
botPathUnsanitized.push(nearby);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (nearby.x < 1 || nearby.x > g_pEngine->GetGameBoundaries().x - 2)
|
|
||||||
continue;
|
|
||||||
if (space->m_bVisited)
|
if (space->m_bVisited)
|
||||||
continue;
|
continue;
|
||||||
if (space->m_bSnake)
|
if (space->m_bSnake)
|
||||||
@ -153,8 +161,6 @@ void AISnake::DFS(void) {
|
|||||||
botPathUnsanitized.push(nearby);
|
botPathUnsanitized.push(nearby);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (nearby.x < 1 || nearby.x > g_pEngine->GetGameBoundaries().x - 2)
|
|
||||||
continue;
|
|
||||||
if (space->m_bVisited)
|
if (space->m_bVisited)
|
||||||
continue;
|
continue;
|
||||||
if (space->m_bSnake)
|
if (space->m_bSnake)
|
||||||
@ -180,6 +186,8 @@ sf::Vector2f AISnake::GetAnyOpenPath(void) {
|
|||||||
|
|
||||||
for (auto path : paths) {
|
for (auto path : paths) {
|
||||||
try {
|
try {
|
||||||
|
if (path == -g_pEngine->GetCurrentDirectionRaw())
|
||||||
|
continue; // Impossible action
|
||||||
bail = path;
|
bail = path;
|
||||||
if (g_pEngine->gameBoard.at(path.y).at(path.x).m_bSnake)
|
if (g_pEngine->gameBoard.at(path.y).at(path.x).m_bSnake)
|
||||||
continue;
|
continue;
|
||||||
|
@ -19,7 +19,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
int totalLength = 0;
|
int totalLength = 0;
|
||||||
double average = 0;
|
double average = 0;
|
||||||
double thresholdDFS = 0.200;
|
double thresholdDFS = 0.900;
|
||||||
bool pathFailed = false;
|
bool pathFailed = false;
|
||||||
|
|
||||||
// Generic search algorithms
|
// Generic search algorithms
|
||||||
|
@ -78,6 +78,10 @@ PlayerDirection GameEngine::GetCurrentDirection(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sf::Vector2f GameEngine::GetCurrentDirectionRaw(void) {
|
||||||
|
return player.speed;
|
||||||
|
}
|
||||||
|
|
||||||
int GameEngine::GetPlayerSize(void) {
|
int GameEngine::GetPlayerSize(void) {
|
||||||
return player.body.size();
|
return player.body.size();
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ public:
|
|||||||
} state;
|
} state;
|
||||||
std::vector< std::vector<GameSpace> > gameBoard;
|
std::vector< std::vector<GameSpace> > gameBoard;
|
||||||
PlayerDirection GetCurrentDirection(void);
|
PlayerDirection GetCurrentDirection(void);
|
||||||
|
sf::Vector2f GetCurrentDirectionRaw(void);
|
||||||
int GetPlayerSize(void);
|
int GetPlayerSize(void);
|
||||||
sf::Vector2f GetHeadLocation(void);
|
sf::Vector2f GetHeadLocation(void);
|
||||||
sf::Vector2f GetFoodLocation(void);
|
sf::Vector2f GetFoodLocation(void);
|
||||||
|
Loading…
Reference in New Issue
Block a user