From da6ceebb55344e5bdb54bfe9ec3c4833de516480 Mon Sep 17 00:00:00 2001 From: Trianta <56975502+Trimutex@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:33:47 -0500 Subject: [PATCH 1/2] Added displaying score --- src/botinterface.cpp | 6 ++++++ src/gamestate.cpp | 4 +++- src/playerinterface.cpp | 16 +++++++++++++++- src/playerinterface.hpp | 3 ++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/botinterface.cpp b/src/botinterface.cpp index 4ac0535..535230b 100755 --- a/src/botinterface.cpp +++ b/src/botinterface.cpp @@ -136,6 +136,12 @@ namespace snakeplusplus } for (sf::Vector2f newLocation : localLocations) { try { + if (newLocation.x < 2 || newLocation.y < 2 + || newLocation.x > boundaries.x - 2 + || newLocation.y > boundaries.y - 2) { + continue; + + } if ((!visited.at(newLocation.y).at(newLocation.x)) && (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) { search.push(newLocation); diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 5e37107..8b44d8c 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -36,13 +36,15 @@ namespace snakeplusplus void GameEngine::Loop(void) { + int currentScore = 0; while (graphics.IsOpen()) { if (isGameOver) { Reset(); } UpdatePlayerSpeed(); PlaceNewSnakePart(MovePlayer()); RegenerateFood(); - graphics.DisplayGameState(gameBoard); + currentScore = player.body.size() * 100; + graphics.DisplayGameState(gameBoard, currentScore); } return; } diff --git a/src/playerinterface.cpp b/src/playerinterface.cpp index ac01982..55a7ca3 100755 --- a/src/playerinterface.cpp +++ b/src/playerinterface.cpp @@ -71,7 +71,20 @@ namespace snakeplusplus return; } - void PlayerOutput::DisplayGameState(std::vector< std::vector >& 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 >& gameBoard, int score) { CheckWindowEvents(); char* letterOnBoard; @@ -94,6 +107,7 @@ namespace snakeplusplus } } } + DisplayScore(score); gameWindow.display(); sf::sleep(delay); return; diff --git a/src/playerinterface.hpp b/src/playerinterface.hpp index 861a2b6..f4a824b 100755 --- a/src/playerinterface.hpp +++ b/src/playerinterface.hpp @@ -17,7 +17,8 @@ namespace snakeplusplus PlayerOutput(void); bool IsOpen(void); void CheckContinue(bool isBotControlled); - void DisplayGameState(std::vector< std::vector >& gameBoard); + void DisplayGameState(std::vector< std::vector >& gameBoard, int score); + void DisplayScore(int score); void StartGameWindow(void); private: void CheckWindowEvents(void); -- 2.45.3 From 5dce9b677d9fbc93fdf3d9b53d76ce6e7b254192 Mon Sep 17 00:00:00 2001 From: Trianta <56975502+Trimutex@users.noreply.github.com> Date: Tue, 24 Oct 2023 00:54:30 -0500 Subject: [PATCH 2/2] Added scoring and chance-based algorithm picking --- src/botinterface.cpp | 18 +++++++++++++++++- src/botinterface.hpp | 2 ++ src/gamestate.cpp | 1 + src/playerinterface.hpp | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/botinterface.cpp b/src/botinterface.cpp index 535230b..f35941b 100755 --- a/src/botinterface.cpp +++ b/src/botinterface.cpp @@ -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 >& 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; diff --git a/src/botinterface.hpp b/src/botinterface.hpp index 5664235..8bbbfb0 100755 --- a/src/botinterface.hpp +++ b/src/botinterface.hpp @@ -14,7 +14,9 @@ namespace snakeplusplus AISnake(); void GetNewPath(const std::vector< std::vector >& 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 botPathUnsanitized; void BFS(const std::vector< std::vector >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries); void DFS(const std::vector< std::vector >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries); diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 8b44d8c..d5f3ee1 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -44,6 +44,7 @@ namespace snakeplusplus PlaceNewSnakePart(MovePlayer()); RegenerateFood(); currentScore = player.body.size() * 100; + bot.UpdateProbability(player.body.size()); graphics.DisplayGameState(gameBoard, currentScore); } return; diff --git a/src/playerinterface.hpp b/src/playerinterface.hpp index f4a824b..9098822 100755 --- a/src/playerinterface.hpp +++ b/src/playerinterface.hpp @@ -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); }; } -- 2.45.3