Logic cleanup

This commit is contained in:
Trimutex 2023-08-19 23:32:53 -05:00
parent d2e1424e7d
commit 4733383d4a
7 changed files with 25 additions and 24 deletions

4
src/botinterface.cpp Normal file → Executable file
View File

@ -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; }

2
src/botinterface.hpp Normal file → Executable file
View File

@ -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

View File

@ -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;

View File

@ -8,6 +8,8 @@
namespace snakeplusplus
{
const int kUnitSpeed = 1;
class GameEngine
{
public:

View File

@ -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)

View File

@ -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<char> >& 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);
};
}

View File

@ -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;
}
}