From 4733383d4ac69d4091fd5984af24221875ad8a5f Mon Sep 17 00:00:00 2001 From: Trimutex Date: Sat, 19 Aug 2023 23:32:53 -0500 Subject: [PATCH] Logic cleanup --- src/botinterface.cpp | 4 ++-- src/botinterface.hpp | 2 +- src/gamestate.cpp | 27 +++++++++++++-------------- src/gamestate.hpp | 2 ++ src/playerinterface.cpp | 5 ++--- src/playerinterface.hpp | 5 +++-- src/snake.cpp | 4 ++-- 7 files changed, 25 insertions(+), 24 deletions(-) mode change 100644 => 100755 src/botinterface.cpp mode change 100644 => 100755 src/botinterface.hpp diff --git a/src/botinterface.cpp b/src/botinterface.cpp old mode 100644 new mode 100755 index fe9fc28..6b4ed21 --- a/src/botinterface.cpp +++ b/src/botinterface.cpp @@ -5,10 +5,10 @@ namespace snakeplusplus { PlayerDirection lastKnownDirection = kNone; - PlayerDirection GetBotInput(sf::Vector2f snakeHeadLocation, sf::Vector2f foodLocation) + PlayerDirection GetBotInput(const sf::Vector2f* snakeHeadLocation, const sf::Vector2f* foodLocation) { sf::Vector2f directionDelta; - directionDelta = snakeHeadLocation - foodLocation; + directionDelta = *snakeHeadLocation - *foodLocation; if ((directionDelta.y > 0) && (lastKnownDirection != kDown)) { lastKnownDirection = kUp; } diff --git a/src/botinterface.hpp b/src/botinterface.hpp old mode 100644 new mode 100755 index cd2f14e..3f98ab7 --- a/src/botinterface.hpp +++ b/src/botinterface.hpp @@ -6,7 +6,7 @@ namespace snakeplusplus { - PlayerDirection GetBotInput(sf::Vector2f snakeHeadLocation, sf::Vector2f foodLocation); + PlayerDirection GetBotInput(const sf::Vector2f* snakeHeadLocation, const sf::Vector2f* foodLocation); } #endif diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 035cf65..af3899d 100755 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -10,7 +10,7 @@ namespace snakeplusplus { GameEngine::GameEngine() { - snakeplusplus::InitializeGenerator(); + InitializeGenerator(); return; } @@ -24,10 +24,10 @@ namespace snakeplusplus void GameEngine::Reset() { - graphics.CheckContinue(); + graphics.CheckContinue(isBotControlled); player.Reset(); PrepareGameBoard(); - isGameOver = 0; + isGameOver = false; return; } @@ -88,8 +88,7 @@ namespace snakeplusplus playerFood.GenerateNewFood(GetGameBoundaries()); newLocation = playerFood.location; } - if (isUpdated) - gameBoard.at(newLocation.y).at(newLocation.x) = 'X'; + if (isUpdated) { gameBoard.at(newLocation.y).at(newLocation.x) = 'X'; } return; } @@ -115,28 +114,28 @@ namespace snakeplusplus void GameEngine::UpdatePlayerSpeed(void) { PlayerDirection controller; - if (isBotControlled) { controller = GetBotInput(player.headLocation, playerFood.location); } + if (isBotControlled) { controller = GetBotInput(&player.headLocation, &playerFood.location); } else { controller = GetPlayerInput(); } switch (controller) { case kUp: - if (player.speed.y == 1) { break; } + if (player.speed.y == kUnitSpeed) { break; } player.speed.x = 0; - player.speed.y = -1; + player.speed.y = -kUnitSpeed; break; case kLeft: - if (player.speed.x == 1) { break; } - player.speed.x = -1; + if (player.speed.x == kUnitSpeed) { break; } + player.speed.x = -kUnitSpeed; player.speed.y = 0; break; case kRight: - if (player.speed.x == -1) { break; } - player.speed.x = 1; + if (player.speed.x == -kUnitSpeed) { break; } + player.speed.x = kUnitSpeed; player.speed.y = 0; break; case kDown: - if (player.speed.y == -1) { break; } + if (player.speed.y == -kUnitSpeed) { break; } player.speed.x = 0; - player.speed.y = 1; + player.speed.y = kUnitSpeed; break; default: break; diff --git a/src/gamestate.hpp b/src/gamestate.hpp index baf8b80..7665477 100755 --- a/src/gamestate.hpp +++ b/src/gamestate.hpp @@ -8,6 +8,8 @@ namespace snakeplusplus { + const int kUnitSpeed = 1; + class GameEngine { public: diff --git a/src/playerinterface.cpp b/src/playerinterface.cpp index d63706c..ac01982 100755 --- a/src/playerinterface.cpp +++ b/src/playerinterface.cpp @@ -38,9 +38,9 @@ namespace snakeplusplus return; } - void PlayerOutput::CheckContinue(void) + void PlayerOutput::CheckContinue(bool isBotControlled) { - sf::Event event; + if (isBotControlled) { return; } DisplayEndScreen(); while (true) { @@ -108,7 +108,6 @@ namespace snakeplusplus void PlayerOutput::CheckWindowEvents(void) { - sf::Event event; while (gameWindow.pollEvent(event)) { if ((event.type == sf::Event::Closed) diff --git a/src/playerinterface.hpp b/src/playerinterface.hpp index a4de760..f397a14 100755 --- a/src/playerinterface.hpp +++ b/src/playerinterface.hpp @@ -16,7 +16,7 @@ namespace snakeplusplus sf::Vector2f gameBoundaries; PlayerOutput(void); bool IsOpen(void); - void CheckContinue(void); + void CheckContinue(bool isBotControlled); void DisplayGameState(std::vector< std::vector >& gameBoard); void StartGameWindow(void); private: @@ -28,8 +28,9 @@ namespace snakeplusplus sf::RenderWindow gameWindow; sf::VideoMode gameVideoSettings; sf::RectangleShape drawObject; + sf::Event event; bool isWindowAlive; - sf::Time delay = sf::milliseconds(20); + sf::Time delay = sf::milliseconds(50); }; } diff --git a/src/snake.cpp b/src/snake.cpp index 4dfc7f4..c9602a5 100755 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -24,8 +24,8 @@ namespace snakeplusplus // Returns a new food object for the snakeFood void Food::GenerateNewFood(sf::Vector2f boundaries) { - location.x = snakeplusplus::GenerateRandomNumber(boundaries.x); - location.y = snakeplusplus::GenerateRandomNumber(boundaries.y); + location.x = GenerateRandomNumber(boundaries.x); + location.y = GenerateRandomNumber(boundaries.y); return; } }