Merge pull request 'Added scoring and chance-based algorithm picking' (#3) from dev into master
Reviewed-on: #3
This commit is contained in:
commit
4f7cacfc44
@ -36,17 +36,33 @@ namespace snakeplusplus
|
|||||||
return lastKnownDirection;
|
return lastKnownDirection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AISnake::UpdateProbability(int snakeSize)
|
||||||
|
{
|
||||||
|
probabilityBFS = 1 - ((double) snakeSize) / 1000;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Gets a new path for the bot to follow
|
// Gets a new path for the bot to follow
|
||||||
// Uses DFS algorithm
|
// Uses DFS algorithm
|
||||||
void AISnake::GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
void AISnake::GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
||||||
{
|
{
|
||||||
// Search for food
|
// Search for food
|
||||||
|
/*
|
||||||
BFS(gameBoard, source, boundaries);
|
BFS(gameBoard, source, boundaries);
|
||||||
if (gameBoard[botPathUnsanitized.top().y][botPathUnsanitized.top().x] != 'X') {
|
if (gameBoard[botPathUnsanitized.top().y][botPathUnsanitized.top().x] != 'X') {
|
||||||
while (!botPathUnsanitized.empty()) { botPathUnsanitized.pop(); }
|
while (!botPathUnsanitized.empty()) { botPathUnsanitized.pop(); }
|
||||||
DFS(gameBoard, source, boundaries);
|
DFS(gameBoard, source, boundaries);
|
||||||
while (botPathUnsanitized.size() > 15) { botPathUnsanitized.pop(); }
|
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
|
// Create path for food
|
||||||
path.push(botPathUnsanitized.top());
|
path.push(botPathUnsanitized.top());
|
||||||
botPathUnsanitized.pop();
|
botPathUnsanitized.pop();
|
||||||
@ -136,6 +152,12 @@ namespace snakeplusplus
|
|||||||
}
|
}
|
||||||
for (sf::Vector2f newLocation : localLocations) {
|
for (sf::Vector2f newLocation : localLocations) {
|
||||||
try {
|
try {
|
||||||
|
if (newLocation.x < 1 || newLocation.y < 1
|
||||||
|
|| newLocation.x > boundaries.x - 2
|
||||||
|
|| newLocation.y > boundaries.y - 2) {
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}
|
||||||
if ((!visited.at(newLocation.y).at(newLocation.x))
|
if ((!visited.at(newLocation.y).at(newLocation.x))
|
||||||
&& (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) {
|
&& (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) {
|
||||||
search.push(newLocation);
|
search.push(newLocation);
|
||||||
|
@ -14,7 +14,9 @@ namespace snakeplusplus
|
|||||||
AISnake();
|
AISnake();
|
||||||
void GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize);
|
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);
|
PlayerDirection GetInput(const sf::Vector2f* source);
|
||||||
|
void UpdateProbability(int snakeSize);
|
||||||
private:
|
private:
|
||||||
|
double probabilityBFS = 1.000;
|
||||||
std::stack<sf::Vector2f> botPathUnsanitized;
|
std::stack<sf::Vector2f> botPathUnsanitized;
|
||||||
void BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
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);
|
void DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
||||||
|
@ -36,13 +36,16 @@ namespace snakeplusplus
|
|||||||
|
|
||||||
void GameEngine::Loop(void)
|
void GameEngine::Loop(void)
|
||||||
{
|
{
|
||||||
|
int currentScore = 0;
|
||||||
while (graphics.IsOpen())
|
while (graphics.IsOpen())
|
||||||
{
|
{
|
||||||
if (isGameOver) { Reset(); }
|
if (isGameOver) { Reset(); }
|
||||||
UpdatePlayerSpeed();
|
UpdatePlayerSpeed();
|
||||||
PlaceNewSnakePart(MovePlayer());
|
PlaceNewSnakePart(MovePlayer());
|
||||||
RegenerateFood();
|
RegenerateFood();
|
||||||
graphics.DisplayGameState(gameBoard);
|
currentScore = player.body.size() * 100;
|
||||||
|
bot.UpdateProbability(player.body.size());
|
||||||
|
graphics.DisplayGameState(gameBoard, currentScore);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,20 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard)
|
void PlayerOutput::DisplayScore(int score) {
|
||||||
|
sf::Vector2f textPosition(gameBoundaries);
|
||||||
|
textPosition.x = textPosition.x / 2;
|
||||||
|
textPosition.y = textPosition.y / 2;
|
||||||
|
sf::Font font;
|
||||||
|
font.loadFromFile("Arial.ttf");
|
||||||
|
std::string text = "Score: " + std::to_string(score);
|
||||||
|
sf::Text ScoreText(text, font);
|
||||||
|
ScoreText.setPosition(textPosition);
|
||||||
|
gameWindow.draw(ScoreText);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score)
|
||||||
{
|
{
|
||||||
CheckWindowEvents();
|
CheckWindowEvents();
|
||||||
char* letterOnBoard;
|
char* letterOnBoard;
|
||||||
@ -94,6 +107,7 @@ namespace snakeplusplus
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
DisplayScore(score);
|
||||||
gameWindow.display();
|
gameWindow.display();
|
||||||
sf::sleep(delay);
|
sf::sleep(delay);
|
||||||
return;
|
return;
|
||||||
|
@ -17,7 +17,8 @@ namespace snakeplusplus
|
|||||||
PlayerOutput(void);
|
PlayerOutput(void);
|
||||||
bool IsOpen(void);
|
bool IsOpen(void);
|
||||||
void CheckContinue(bool isBotControlled);
|
void CheckContinue(bool isBotControlled);
|
||||||
void DisplayGameState(std::vector< std::vector<char> >& gameBoard);
|
void DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score);
|
||||||
|
void DisplayScore(int score);
|
||||||
void StartGameWindow(void);
|
void StartGameWindow(void);
|
||||||
private:
|
private:
|
||||||
void CheckWindowEvents(void);
|
void CheckWindowEvents(void);
|
||||||
@ -30,7 +31,7 @@ namespace snakeplusplus
|
|||||||
sf::RectangleShape drawObject;
|
sf::RectangleShape drawObject;
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
bool isWindowAlive;
|
bool isWindowAlive;
|
||||||
sf::Time delay = sf::milliseconds(15);
|
sf::Time delay = sf::milliseconds(10);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user