bot: various improvements
This commit is contained in:
parent
ff70b0dbd4
commit
4a5fbe88d3
@ -17,10 +17,8 @@ AISnake::AISnake() {
|
|||||||
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
||||||
{
|
{
|
||||||
sf::Vector2f directionDelta;
|
sf::Vector2f directionDelta;
|
||||||
if (!source) {
|
if (!source)
|
||||||
std::cout << "[ERROR - AI] Source was borked, bailing" << std::endl;
|
|
||||||
return kUp;
|
return kUp;
|
||||||
}
|
|
||||||
while (*source == path.top() && !path.empty()) { path.pop(); }
|
while (*source == path.top() && !path.empty()) { path.pop(); }
|
||||||
if (path.empty()) { path.push(GetAnyOpenPath(*source)); }
|
if (path.empty()) { path.push(GetAnyOpenPath(*source)); }
|
||||||
directionDelta = *source - path.top();
|
directionDelta = *source - path.top();
|
||||||
@ -51,7 +49,6 @@ void AISnake::AdjustProbability(double amount)
|
|||||||
probabilityBFS += amount;
|
probabilityBFS += amount;
|
||||||
if (probabilityBFS > 1.0) { probabilityBFS = 1.0; }
|
if (probabilityBFS > 1.0) { probabilityBFS = 1.0; }
|
||||||
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
|
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
|
||||||
std::cout << "[Info - AI] New BFS probability: " << probabilityBFS << std::endl;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,8 +61,8 @@ void AISnake::AddIteration(const int size)
|
|||||||
if (average > size) { AdjustProbability(adjustmentAmount); }
|
if (average > size) { AdjustProbability(adjustmentAmount); }
|
||||||
else { AdjustProbability(-adjustmentAmount); }
|
else { AdjustProbability(-adjustmentAmount); }
|
||||||
}
|
}
|
||||||
std::cout << "[Info - AI] Current average: " << average << std::endl;
|
std::cout << "[LOG - AI] Current average: " << average << std::endl;
|
||||||
std::cout << "[Info - AI] Previous iteration size: " << size << std::endl;
|
std::cout << "[LOG - AI] Previous iteration size: " << size << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::ResetPath(void) {
|
void AISnake::ResetPath(void) {
|
||||||
@ -94,7 +91,6 @@ void AISnake::GetNewPath(const sf::Vector2f& source)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::BFS(const sf::Vector2f& source) {
|
void AISnake::BFS(const sf::Vector2f& source) {
|
||||||
std::cout << "[Info - AI] Rolled BFS" << std::endl;
|
|
||||||
std::queue<sf::Vector2f> search;
|
std::queue<sf::Vector2f> search;
|
||||||
search.push(source);
|
search.push(source);
|
||||||
while (!search.empty()) {
|
while (!search.empty()) {
|
||||||
@ -114,15 +110,10 @@ void AISnake::BFS(const sf::Vector2f& source) {
|
|||||||
GameSpace* space = &g_pEngine->gameBoard.at(nearby.y).at(nearby.x);
|
GameSpace* space = &g_pEngine->gameBoard.at(nearby.y).at(nearby.x);
|
||||||
if (space->m_bFood) {
|
if (space->m_bFood) {
|
||||||
botPathUnsanitized.push(nearby);
|
botPathUnsanitized.push(nearby);
|
||||||
std::cout << "[TRACE - BFS] Path successfully found food" << std::endl;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (nearby.x < 1 || nearby.y < 1
|
if (nearby.x < 1 || nearby.y < 1)
|
||||||
|| nearby.x > g_pEngine->GetGameBoundaries().x - 1
|
|
||||||
|| nearby.y > g_pEngine->GetGameBoundaries().y - 1) {
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
}
|
|
||||||
if (space->m_bVisited)
|
if (space->m_bVisited)
|
||||||
continue;
|
continue;
|
||||||
if (space->m_bSnake)
|
if (space->m_bSnake)
|
||||||
@ -134,12 +125,10 @@ void AISnake::BFS(const sf::Vector2f& source) {
|
|||||||
}
|
}
|
||||||
g_pEngine->gameBoard.at(currentLocation.y).at(currentLocation.x).m_bVisited = true;
|
g_pEngine->gameBoard.at(currentLocation.y).at(currentLocation.x).m_bVisited = true;
|
||||||
}
|
}
|
||||||
std::cout << "[ERROR - BFS] Failed to get a path to food" << std::endl;
|
|
||||||
pathFailed = true;
|
pathFailed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::DFS(const sf::Vector2f& source) {
|
void AISnake::DFS(const sf::Vector2f& source) {
|
||||||
std::cout << "[Info - AI] Rolled DFS" << std::endl;
|
|
||||||
std::stack<sf::Vector2f> search;
|
std::stack<sf::Vector2f> search;
|
||||||
search.push(source);
|
search.push(source);
|
||||||
while (!search.empty()) {
|
while (!search.empty()) {
|
||||||
@ -159,15 +148,10 @@ void AISnake::DFS(const sf::Vector2f& source) {
|
|||||||
GameSpace* space = &g_pEngine->gameBoard.at(nearby.y).at(nearby.x);
|
GameSpace* space = &g_pEngine->gameBoard.at(nearby.y).at(nearby.x);
|
||||||
if (space->m_bFood) {
|
if (space->m_bFood) {
|
||||||
botPathUnsanitized.push(nearby);
|
botPathUnsanitized.push(nearby);
|
||||||
std::cout << "[TRACE - DFS] Path successfully found food" << std::endl;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (nearby.x < 1 || nearby.y < 1
|
if (nearby.x < 1 || nearby.x > g_pEngine->GetGameBoundaries().x - 2)
|
||||||
|| nearby.x > g_pEngine->GetGameBoundaries().x - 1
|
|
||||||
|| nearby.y > g_pEngine->GetGameBoundaries().y - 1) {
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
}
|
|
||||||
if (space->m_bVisited)
|
if (space->m_bVisited)
|
||||||
continue;
|
continue;
|
||||||
if (space->m_bSnake)
|
if (space->m_bSnake)
|
||||||
@ -179,7 +163,6 @@ void AISnake::DFS(const sf::Vector2f& source) {
|
|||||||
}
|
}
|
||||||
g_pEngine->gameBoard.at(currentLocation.y).at(currentLocation.x).m_bVisited = true;
|
g_pEngine->gameBoard.at(currentLocation.y).at(currentLocation.x).m_bVisited = true;
|
||||||
}
|
}
|
||||||
std::cout << "[ERROR - DFS] Failed to get a path to food" << std::endl;
|
|
||||||
pathFailed = true;
|
pathFailed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,10 +177,9 @@ sf::Vector2f AISnake::GetAnyOpenPath(const sf::Vector2f& source) {
|
|||||||
|
|
||||||
for (auto path : paths) {
|
for (auto path : paths) {
|
||||||
try {
|
try {
|
||||||
if (g_pEngine->gameBoard.at(path.y).at(path.x).m_bSnake) {
|
|
||||||
bail = path;
|
bail = path;
|
||||||
|
if (g_pEngine->gameBoard.at(path.y).at(path.x).m_bSnake)
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
return path;
|
return path;
|
||||||
} catch (const std::out_of_range& error) {
|
} catch (const std::out_of_range& error) {
|
||||||
continue; // Out of bounds
|
continue; // Out of bounds
|
||||||
@ -211,7 +193,6 @@ void AISnake::UnvisitBoard(void) {
|
|||||||
for (std::vector<GameSpace>& i : g_pEngine->gameBoard)
|
for (std::vector<GameSpace>& i : g_pEngine->gameBoard)
|
||||||
for (GameSpace& j : i)
|
for (GameSpace& j : i)
|
||||||
j.m_bVisited = false;
|
j.m_bVisited = false;
|
||||||
std::cout << "[TRACE - AI] Unvisited board" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::UpdateAverage(const int size) {
|
void AISnake::UpdateAverage(const int size) {
|
||||||
@ -235,7 +216,6 @@ void AISnake::TrimPath(void) {
|
|||||||
}
|
}
|
||||||
botPathUnsanitized.pop();
|
botPathUnsanitized.pop();
|
||||||
}
|
}
|
||||||
std::cout << "[TRACE - AI] Trimmed path" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::EmptyPath(void) {
|
void AISnake::EmptyPath(void) {
|
||||||
|
@ -25,6 +25,8 @@ void GameEngine::Reset()
|
|||||||
{
|
{
|
||||||
if (!state.m_bIsBotControlled)
|
if (!state.m_bIsBotControlled)
|
||||||
graphics.CheckContinue();
|
graphics.CheckContinue();
|
||||||
|
else
|
||||||
|
bot.AddIteration(player.body.size());
|
||||||
player.Reset();
|
player.Reset();
|
||||||
PrepareGameBoard();
|
PrepareGameBoard();
|
||||||
state.m_bIsGameOver = false;
|
state.m_bIsGameOver = false;
|
||||||
@ -34,7 +36,6 @@ void GameEngine::Reset()
|
|||||||
if (state.m_bNoDisplay)
|
if (state.m_bNoDisplay)
|
||||||
graphics.SetShowGame(false);
|
graphics.SetShowGame(false);
|
||||||
graphics.SetShowGame((bot.amountPlayed + 1) % 50 == 0);
|
graphics.SetShowGame((bot.amountPlayed + 1) % 50 == 0);
|
||||||
bot.AddIteration(player.body.size());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,6 @@ struct Food
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
sf::Vector2f location;
|
sf::Vector2f location;
|
||||||
char* food;
|
|
||||||
void GenerateNewFood(sf::Vector2f boundaries);
|
void GenerateNewFood(sf::Vector2f boundaries);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user