Compare commits

...

4 Commits

Author SHA1 Message Date
Trianta
a650970cc5 output: add todos for later 2024-08-21 04:34:03 -05:00
Trianta
63cbe41086 auto: fix crashing on edges and getting stuck 2024-08-21 04:32:30 -05:00
Trianta
c6197e84dc auto: default to an increased speed when auto playing 2024-08-21 03:17:23 -05:00
Trianta
2cb54b68c4 auto: better naming for BFS/DFS roll 2024-08-21 03:15:32 -05:00
5 changed files with 36 additions and 19 deletions
+24 -16
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())
if (path.empty()) {
if (g_pEngine->state.m_bSmart) {
return CurrentBestDecision();
} else
path.push(GetAnyOpenPath());
}
directionDelta = source - path.top();
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;
@@ -39,16 +49,16 @@ PlayerDirection AISnake::GetInput(void) {
}
void AISnake::UpdateProbability(int snakeSize) {
probabilityBFS = 1 - ((double) snakeSize) / 1000;
thresholdDFS = 1 - ((double) snakeSize) / 1000;
return;
}
void AISnake::AdjustProbability(double amount) {
probabilityBFS += amount;
if (probabilityBFS > 1.0)
probabilityBFS = 1.0;
if (probabilityBFS < 0.0)
probabilityBFS = 0.0;
thresholdDFS += amount;
if (thresholdDFS > 1.0)
thresholdDFS = 1.0;
if (thresholdDFS < 0.0)
thresholdDFS = 0.0;
return;
}
@@ -76,7 +86,7 @@ void AISnake::GetNewPath(void) {
// Search for food
// Probability-based approach for fun
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
if (roll <= probabilityBFS)
if (roll <= thresholdDFS)
BFS();
else
DFS();
@@ -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;
+1 -1
View File
@@ -19,7 +19,7 @@ public:
private:
int totalLength = 0;
double average = 0;
double probabilityBFS = 0.200;
double thresholdDFS = 0.900;
bool pathFailed = false;
// Generic search algorithms
+6
View File
@@ -14,6 +14,8 @@ void GameEngine::Start(void) {
PrepareGameBoard();
if (!state.m_bNoDisplay)
graphics.StartGameWindow();
if (state.m_bIsBotControlled)
graphics.SetShowGame(true);
Loop();
}
@@ -76,6 +78,10 @@ PlayerDirection GameEngine::GetCurrentDirection(void) {
}
}
sf::Vector2f GameEngine::GetCurrentDirectionRaw(void) {
return player.speed;
}
int GameEngine::GetPlayerSize(void) {
return player.body.size();
}
+1
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);
+2
View File
@@ -22,6 +22,8 @@ bool PlayerOutput::IsOpen(void) {
return isWindowAlive;
}
// TODO: Add board size limits
// Add gaps and a border that accommodate the limits
PlayerOutput::PlayerOutput(void) {
float kWidth = 1025;
float kHeight = 725;