diff --git a/src/botinterface.cpp b/src/botinterface.cpp index 51832a8..692da66 100755 --- a/src/botinterface.cpp +++ b/src/botinterface.cpp @@ -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; diff --git a/src/botinterface.hpp b/src/botinterface.hpp index bf023db..4c2b6bc 100755 --- a/src/botinterface.hpp +++ b/src/botinterface.hpp @@ -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 diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 7e949f0..7ad4baa 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -78,6 +78,10 @@ PlayerDirection GameEngine::GetCurrentDirection(void) { } } +sf::Vector2f GameEngine::GetCurrentDirectionRaw(void) { + return player.speed; +} + int GameEngine::GetPlayerSize(void) { return player.body.size(); } diff --git a/src/gamestate.hpp b/src/gamestate.hpp index ad4dbe1..1eb4b54 100755 --- a/src/gamestate.hpp +++ b/src/gamestate.hpp @@ -29,6 +29,7 @@ public: } state; std::vector< std::vector > gameBoard; PlayerDirection GetCurrentDirection(void); + sf::Vector2f GetCurrentDirectionRaw(void); int GetPlayerSize(void); sf::Vector2f GetHeadLocation(void); sf::Vector2f GetFoodLocation(void);