Logic cleanup
This commit is contained in:
		
							parent
							
								
									d2e1424e7d
								
							
						
					
					
						commit
						4733383d4a
					
				
							
								
								
									
										4
									
								
								src/botinterface.cpp
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										4
									
								
								src/botinterface.cpp
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							@ -5,10 +5,10 @@
 | 
				
			|||||||
namespace snakeplusplus
 | 
					namespace snakeplusplus
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    PlayerDirection lastKnownDirection = kNone;
 | 
					    PlayerDirection lastKnownDirection = kNone;
 | 
				
			||||||
    PlayerDirection GetBotInput(sf::Vector2f snakeHeadLocation, sf::Vector2f foodLocation)
 | 
					    PlayerDirection GetBotInput(const sf::Vector2f* snakeHeadLocation, const sf::Vector2f* foodLocation)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        sf::Vector2f directionDelta;
 | 
					        sf::Vector2f directionDelta;
 | 
				
			||||||
        directionDelta = snakeHeadLocation - foodLocation;
 | 
					        directionDelta = *snakeHeadLocation - *foodLocation;
 | 
				
			||||||
        if ((directionDelta.y > 0)
 | 
					        if ((directionDelta.y > 0)
 | 
				
			||||||
                && (lastKnownDirection != kDown))
 | 
					                && (lastKnownDirection != kDown))
 | 
				
			||||||
            { lastKnownDirection = kUp; }
 | 
					            { lastKnownDirection = kUp; }
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								src/botinterface.hpp
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										2
									
								
								src/botinterface.hpp
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							@ -6,7 +6,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace snakeplusplus
 | 
					namespace snakeplusplus
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    PlayerDirection GetBotInput(sf::Vector2f snakeHeadLocation, sf::Vector2f foodLocation);
 | 
					    PlayerDirection GetBotInput(const sf::Vector2f* snakeHeadLocation, const sf::Vector2f* foodLocation);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif 
 | 
					#endif 
 | 
				
			||||||
 | 
				
			|||||||
@ -10,7 +10,7 @@ namespace snakeplusplus
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    GameEngine::GameEngine()
 | 
					    GameEngine::GameEngine()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        snakeplusplus::InitializeGenerator();
 | 
					        InitializeGenerator();
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -24,10 +24,10 @@ namespace snakeplusplus
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    void GameEngine::Reset()
 | 
					    void GameEngine::Reset()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        graphics.CheckContinue();
 | 
					        graphics.CheckContinue(isBotControlled);
 | 
				
			||||||
        player.Reset();
 | 
					        player.Reset();
 | 
				
			||||||
        PrepareGameBoard();
 | 
					        PrepareGameBoard();
 | 
				
			||||||
        isGameOver = 0;
 | 
					        isGameOver = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -88,8 +88,7 @@ namespace snakeplusplus
 | 
				
			|||||||
            playerFood.GenerateNewFood(GetGameBoundaries());
 | 
					            playerFood.GenerateNewFood(GetGameBoundaries());
 | 
				
			||||||
            newLocation = playerFood.location;
 | 
					            newLocation = playerFood.location;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (isUpdated)
 | 
					        if (isUpdated) { gameBoard.at(newLocation.y).at(newLocation.x) = 'X'; }
 | 
				
			||||||
            gameBoard.at(newLocation.y).at(newLocation.x) = 'X';
 | 
					 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -115,28 +114,28 @@ namespace snakeplusplus
 | 
				
			|||||||
    void GameEngine::UpdatePlayerSpeed(void)
 | 
					    void GameEngine::UpdatePlayerSpeed(void)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        PlayerDirection controller;
 | 
					        PlayerDirection controller;
 | 
				
			||||||
        if (isBotControlled) { controller = GetBotInput(player.headLocation, playerFood.location); }
 | 
					        if (isBotControlled) { controller = GetBotInput(&player.headLocation, &playerFood.location); }
 | 
				
			||||||
        else { controller = GetPlayerInput(); }
 | 
					        else { controller = GetPlayerInput(); }
 | 
				
			||||||
        switch (controller) { 
 | 
					        switch (controller) { 
 | 
				
			||||||
            case kUp:
 | 
					            case kUp:
 | 
				
			||||||
                if (player.speed.y == 1) { break; }
 | 
					                if (player.speed.y == kUnitSpeed) { break; }
 | 
				
			||||||
                player.speed.x = 0;
 | 
					                player.speed.x = 0;
 | 
				
			||||||
                player.speed.y = -1;
 | 
					                player.speed.y = -kUnitSpeed;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            case kLeft:
 | 
					            case kLeft:
 | 
				
			||||||
                if (player.speed.x == 1) { break; }
 | 
					                if (player.speed.x == kUnitSpeed) { break; }
 | 
				
			||||||
                player.speed.x = -1;
 | 
					                player.speed.x = -kUnitSpeed;
 | 
				
			||||||
                player.speed.y = 0;
 | 
					                player.speed.y = 0;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            case kRight:
 | 
					            case kRight:
 | 
				
			||||||
                if (player.speed.x == -1) { break; }
 | 
					                if (player.speed.x == -kUnitSpeed) { break; }
 | 
				
			||||||
                player.speed.x = 1;
 | 
					                player.speed.x = kUnitSpeed;
 | 
				
			||||||
                player.speed.y = 0;
 | 
					                player.speed.y = 0;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            case kDown:
 | 
					            case kDown:
 | 
				
			||||||
                if (player.speed.y == -1) { break; }
 | 
					                if (player.speed.y == -kUnitSpeed) { break; }
 | 
				
			||||||
                player.speed.x = 0;
 | 
					                player.speed.x = 0;
 | 
				
			||||||
                player.speed.y = 1;
 | 
					                player.speed.y = kUnitSpeed;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
            default:
 | 
					            default:
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
 | 
				
			|||||||
@ -8,6 +8,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace snakeplusplus
 | 
					namespace snakeplusplus
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    const int kUnitSpeed = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class GameEngine
 | 
					    class GameEngine
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
    public:
 | 
					    public:
 | 
				
			||||||
 | 
				
			|||||||
@ -38,9 +38,9 @@ namespace snakeplusplus
 | 
				
			|||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void PlayerOutput::CheckContinue(void)
 | 
					    void PlayerOutput::CheckContinue(bool isBotControlled)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        sf::Event event;
 | 
					        if (isBotControlled) { return; }
 | 
				
			||||||
        DisplayEndScreen();
 | 
					        DisplayEndScreen();
 | 
				
			||||||
        while (true)
 | 
					        while (true)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@ -108,7 +108,6 @@ namespace snakeplusplus
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    void PlayerOutput::CheckWindowEvents(void)
 | 
					    void PlayerOutput::CheckWindowEvents(void)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        sf::Event event;
 | 
					 | 
				
			||||||
        while (gameWindow.pollEvent(event))
 | 
					        while (gameWindow.pollEvent(event))
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if ((event.type == sf::Event::Closed)
 | 
					            if ((event.type == sf::Event::Closed)
 | 
				
			||||||
 | 
				
			|||||||
@ -16,7 +16,7 @@ namespace snakeplusplus
 | 
				
			|||||||
        sf::Vector2f gameBoundaries;
 | 
					        sf::Vector2f gameBoundaries;
 | 
				
			||||||
        PlayerOutput(void);
 | 
					        PlayerOutput(void);
 | 
				
			||||||
        bool IsOpen(void);
 | 
					        bool IsOpen(void);
 | 
				
			||||||
        void CheckContinue(void);
 | 
					        void CheckContinue(bool isBotControlled);
 | 
				
			||||||
        void DisplayGameState(std::vector< std::vector<char> >& gameBoard);
 | 
					        void DisplayGameState(std::vector< std::vector<char> >& gameBoard);
 | 
				
			||||||
        void StartGameWindow(void);
 | 
					        void StartGameWindow(void);
 | 
				
			||||||
    private:
 | 
					    private:
 | 
				
			||||||
@ -28,8 +28,9 @@ namespace snakeplusplus
 | 
				
			|||||||
        sf::RenderWindow gameWindow;
 | 
					        sf::RenderWindow gameWindow;
 | 
				
			||||||
        sf::VideoMode gameVideoSettings;
 | 
					        sf::VideoMode gameVideoSettings;
 | 
				
			||||||
        sf::RectangleShape drawObject;
 | 
					        sf::RectangleShape drawObject;
 | 
				
			||||||
 | 
					        sf::Event event;
 | 
				
			||||||
        bool isWindowAlive;
 | 
					        bool isWindowAlive;
 | 
				
			||||||
        sf::Time delay = sf::milliseconds(20);
 | 
					        sf::Time delay = sf::milliseconds(50);
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -24,8 +24,8 @@ namespace snakeplusplus
 | 
				
			|||||||
    // Returns a new food object for the snakeFood
 | 
					    // Returns a new food object for the snakeFood
 | 
				
			||||||
    void Food::GenerateNewFood(sf::Vector2f boundaries)
 | 
					    void Food::GenerateNewFood(sf::Vector2f boundaries)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        location.x = snakeplusplus::GenerateRandomNumber(boundaries.x);
 | 
					        location.x = GenerateRandomNumber(boundaries.x);
 | 
				
			||||||
        location.y = snakeplusplus::GenerateRandomNumber(boundaries.y);
 | 
					        location.y = GenerateRandomNumber(boundaries.y);
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user