Added scoring and chance-based algorithm picking #3
@ -36,17 +36,33 @@ namespace snakeplusplus
|
||||
return lastKnownDirection;
|
||||
}
|
||||
|
||||
void AISnake::UpdateProbability(int snakeSize)
|
||||
{
|
||||
probabilityBFS = 1 - ((double) snakeSize) / 1000;
|
||||
return;
|
||||
}
|
||||
|
||||
// Gets a new path for the bot to follow
|
||||
// Uses DFS algorithm
|
||||
void AISnake::GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
||||
{
|
||||
// Search for food
|
||||
/*
|
||||
BFS(gameBoard, source, boundaries);
|
||||
if (gameBoard[botPathUnsanitized.top().y][botPathUnsanitized.top().x] != 'X') {
|
||||
while (!botPathUnsanitized.empty()) { botPathUnsanitized.pop(); }
|
||||
DFS(gameBoard, source, boundaries);
|
||||
while (botPathUnsanitized.size() > 15) { botPathUnsanitized.pop(); }
|
||||
}
|
||||
*/
|
||||
// Probability-based approach for fun
|
||||
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
|
||||
if (roll <= probabilityBFS)
|
||||
{
|
||||
BFS(gameBoard, source, boundaries);
|
||||
} else {
|
||||
DFS(gameBoard, source, boundaries);
|
||||
}
|
||||
// Create path for food
|
||||
path.push(botPathUnsanitized.top());
|
||||
botPathUnsanitized.pop();
|
||||
@ -136,7 +152,7 @@ namespace snakeplusplus
|
||||
}
|
||||
for (sf::Vector2f newLocation : localLocations) {
|
||||
try {
|
||||
if (newLocation.x < 2 || newLocation.y < 2
|
||||
if (newLocation.x < 1 || newLocation.y < 1
|
||||
|| newLocation.x > boundaries.x - 2
|
||||
|| newLocation.y > boundaries.y - 2) {
|
||||
continue;
|
||||
|
@ -14,7 +14,9 @@ namespace snakeplusplus
|
||||
AISnake();
|
||||
void GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize);
|
||||
PlayerDirection GetInput(const sf::Vector2f* source);
|
||||
void UpdateProbability(int snakeSize);
|
||||
private:
|
||||
double probabilityBFS = 1.000;
|
||||
std::stack<sf::Vector2f> botPathUnsanitized;
|
||||
void BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
||||
void DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
||||
|
@ -44,6 +44,7 @@ namespace snakeplusplus
|
||||
PlaceNewSnakePart(MovePlayer());
|
||||
RegenerateFood();
|
||||
currentScore = player.body.size() * 100;
|
||||
bot.UpdateProbability(player.body.size());
|
||||
graphics.DisplayGameState(gameBoard, currentScore);
|
||||
}
|
||||
return;
|
||||
|
@ -31,7 +31,7 @@ namespace snakeplusplus
|
||||
sf::RectangleShape drawObject;
|
||||
sf::Event event;
|
||||
bool isWindowAlive;
|
||||
sf::Time delay = sf::milliseconds(15);
|
||||
sf::Time delay = sf::milliseconds(10);
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user