core: formatting cleanup
This commit is contained in:
parent
01f24e4ebb
commit
677466d99e
@ -13,72 +13,76 @@ AISnake::AISnake(void) {
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerDirection AISnake::GetInput(void)
|
PlayerDirection AISnake::GetInput(void) {
|
||||||
{
|
|
||||||
sf::Vector2f source(g_pEngine->GetHeadLocation());
|
sf::Vector2f source(g_pEngine->GetHeadLocation());
|
||||||
//if (g_pEngine->state.m_bSmart && path.empty())
|
//if (g_pEngine->state.m_bSmart && path.empty())
|
||||||
// return CurrentBestDecision();
|
// return CurrentBestDecision();
|
||||||
sf::Vector2f directionDelta;
|
sf::Vector2f directionDelta;
|
||||||
while (source == path.top() && !path.empty()) { path.pop(); }
|
|
||||||
if (path.empty()) { path.push(GetAnyOpenPath()); }
|
while (source == path.top() && !path.empty())
|
||||||
|
path.pop();
|
||||||
|
if (path.empty())
|
||||||
|
path.push(GetAnyOpenPath());
|
||||||
|
|
||||||
directionDelta = source - path.top();
|
directionDelta = source - path.top();
|
||||||
path.pop();
|
path.pop();
|
||||||
if ((directionDelta.y == 1)
|
|
||||||
&& (lastKnownDirection != kDown))
|
if ((directionDelta.y == 1) && (lastKnownDirection != kDown))
|
||||||
{ lastKnownDirection = kUp; }
|
lastKnownDirection = kUp;
|
||||||
else if ((directionDelta.y == -1)
|
else if ((directionDelta.y == -1) && (lastKnownDirection != kUp))
|
||||||
&& (lastKnownDirection != kUp))
|
lastKnownDirection = kDown;
|
||||||
{ lastKnownDirection = kDown; }
|
else if ((directionDelta.x == 1) && (lastKnownDirection != kRight))
|
||||||
else if ((directionDelta.x == 1)
|
lastKnownDirection = kLeft;
|
||||||
&& (lastKnownDirection != kRight))
|
else if ((directionDelta.x == -1) && (lastKnownDirection != kLeft))
|
||||||
{ lastKnownDirection = kLeft; }
|
lastKnownDirection = kRight;
|
||||||
else if ((directionDelta.x == -1)
|
|
||||||
&& (lastKnownDirection != kLeft))
|
|
||||||
{ lastKnownDirection = kRight; }
|
|
||||||
return lastKnownDirection;
|
return lastKnownDirection;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::UpdateProbability(int snakeSize)
|
void AISnake::UpdateProbability(int snakeSize) {
|
||||||
{
|
|
||||||
probabilityBFS = 1 - ((double) snakeSize) / 1000;
|
probabilityBFS = 1 - ((double) snakeSize) / 1000;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::AdjustProbability(double amount)
|
void AISnake::AdjustProbability(double amount) {
|
||||||
{
|
|
||||||
probabilityBFS += amount;
|
probabilityBFS += amount;
|
||||||
if (probabilityBFS > 1.0) { probabilityBFS = 1.0; }
|
if (probabilityBFS > 1.0)
|
||||||
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
|
probabilityBFS = 1.0;
|
||||||
|
if (probabilityBFS < 0.0)
|
||||||
|
probabilityBFS = 0.0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::AddIteration(const int size)
|
void AISnake::AddIteration(const int size) {
|
||||||
{
|
if (size > 40) {
|
||||||
if (size > 40)
|
|
||||||
{
|
|
||||||
UpdateAverage(size);
|
UpdateAverage(size);
|
||||||
double adjustmentAmount = 0.002;
|
double adjustmentAmount = 0.002;
|
||||||
if (average > size) { AdjustProbability(adjustmentAmount); }
|
if (average > size)
|
||||||
else { AdjustProbability(-adjustmentAmount); }
|
AdjustProbability(adjustmentAmount);
|
||||||
|
else
|
||||||
|
AdjustProbability(-adjustmentAmount);
|
||||||
}
|
}
|
||||||
std::cout << "[LOG - AI] Current average: " << average << std::endl;
|
std::cout << "[LOG - AI] Current average: " << average << std::endl;
|
||||||
std::cout << "[LOG - AI] Previous iteration size: " << size << std::endl;
|
std::cout << "[LOG - AI] Previous iteration size: " << size << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::ResetPath(void) {
|
void AISnake::ResetPath(void) {
|
||||||
while (!path.empty()) { path.pop(); }
|
while (!path.empty())
|
||||||
|
path.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(void)
|
void AISnake::GetNewPath(void) {
|
||||||
{
|
|
||||||
// Search for food
|
// Search for food
|
||||||
// Probability-based approach for fun
|
// Probability-based approach for fun
|
||||||
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
|
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
|
||||||
if (roll <= probabilityBFS) { BFS(); }
|
if (roll <= probabilityBFS)
|
||||||
else { DFS(); }
|
BFS();
|
||||||
|
else
|
||||||
|
DFS();
|
||||||
|
|
||||||
UnvisitBoard();
|
UnvisitBoard();
|
||||||
|
|
||||||
if (pathFailed) {
|
if (pathFailed) {
|
||||||
pathFailed = false;
|
pathFailed = false;
|
||||||
EmptyPath();
|
EmptyPath();
|
||||||
|
@ -3,14 +3,13 @@
|
|||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
|
|
||||||
std::default_random_engine generator;
|
std::default_random_engine generator;
|
||||||
void InitializeGenerator(void)
|
|
||||||
{
|
void InitializeGenerator(void) {
|
||||||
generator.seed(std::random_device{}());
|
generator.seed(std::random_device{}());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a newly generated number
|
// Returns a newly generated number
|
||||||
int GenerateRandomNumber(int generationLimit)
|
int GenerateRandomNumber(int generationLimit) {
|
||||||
{
|
|
||||||
int generatedNumber;
|
int generatedNumber;
|
||||||
std::uniform_int_distribution<> distribution(0, generationLimit - 1);
|
std::uniform_int_distribution<> distribution(0, generationLimit - 1);
|
||||||
generatedNumber = distribution(generator);
|
generatedNumber = distribution(generator);
|
||||||
|
@ -4,8 +4,7 @@
|
|||||||
void InitializeGenerator(void);
|
void InitializeGenerator(void);
|
||||||
int GenerateRandomNumber(int generationLimit);
|
int GenerateRandomNumber(int generationLimit);
|
||||||
|
|
||||||
enum PlayerDirection
|
enum PlayerDirection {
|
||||||
{
|
|
||||||
kNone = 0,
|
kNone = 0,
|
||||||
kLeft = 1,
|
kLeft = 1,
|
||||||
kUp = 2,
|
kUp = 2,
|
||||||
@ -14,7 +13,7 @@ enum PlayerDirection
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct GameSpace {
|
struct GameSpace {
|
||||||
GameSpace();
|
GameSpace(void);
|
||||||
unsigned char m_bFood : 1 = 0;
|
unsigned char m_bFood : 1 = 0;
|
||||||
unsigned char m_bSnake : 1 = 0;
|
unsigned char m_bSnake : 1 = 0;
|
||||||
unsigned char m_bVisited : 1 = 0; // Used for BFS/DFS
|
unsigned char m_bVisited : 1 = 0; // Used for BFS/DFS
|
||||||
|
@ -6,23 +6,18 @@
|
|||||||
#include "playerinterface.hpp"
|
#include "playerinterface.hpp"
|
||||||
#include "gamestate.hpp"
|
#include "gamestate.hpp"
|
||||||
|
|
||||||
GameEngine::GameEngine()
|
GameEngine::GameEngine(void) {
|
||||||
{
|
|
||||||
InitializeGenerator();
|
InitializeGenerator();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::Start()
|
void GameEngine::Start(void) {
|
||||||
{
|
|
||||||
PrepareGameBoard();
|
PrepareGameBoard();
|
||||||
if (!state.m_bNoDisplay)
|
if (!state.m_bNoDisplay)
|
||||||
graphics.StartGameWindow();
|
graphics.StartGameWindow();
|
||||||
Loop();
|
Loop();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::Reset()
|
void GameEngine::Reset(void) {
|
||||||
{
|
|
||||||
if (!state.m_bIsBotControlled)
|
if (!state.m_bIsBotControlled)
|
||||||
graphics.CheckContinue();
|
graphics.CheckContinue();
|
||||||
else
|
else
|
||||||
@ -30,7 +25,8 @@ void GameEngine::Reset()
|
|||||||
player.Reset();
|
player.Reset();
|
||||||
PrepareGameBoard();
|
PrepareGameBoard();
|
||||||
state.m_bIsGameOver = false;
|
state.m_bIsGameOver = false;
|
||||||
if (state.m_bIsBotControlled) {
|
if (!state.m_bIsBotControlled)
|
||||||
|
return;
|
||||||
if (!state.m_bSmart) {
|
if (!state.m_bSmart) {
|
||||||
while (!bot.path.empty())
|
while (!bot.path.empty())
|
||||||
bot.path.pop();
|
bot.path.pop();
|
||||||
@ -39,16 +35,15 @@ void GameEngine::Reset()
|
|||||||
graphics.SetShowGame(false);
|
graphics.SetShowGame(false);
|
||||||
if (state.m_bSkipIterations)
|
if (state.m_bSkipIterations)
|
||||||
graphics.SetShowGame((bot.amountPlayed + 1) % 50 == 0);
|
graphics.SetShowGame((bot.amountPlayed + 1) % 50 == 0);
|
||||||
|
// TODO: Replace with value to force this effect
|
||||||
graphics.SetShowGame(true);
|
graphics.SetShowGame(true);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::Loop(void)
|
void GameEngine::Loop(void) {
|
||||||
{
|
|
||||||
int currentScore = 0;
|
int currentScore = 0;
|
||||||
while (graphics.IsOpen() || state.m_bNoDisplay)
|
while (graphics.IsOpen() || state.m_bNoDisplay) {
|
||||||
{
|
if (state.m_bIsGameOver)
|
||||||
if (state.m_bIsGameOver) { Reset(); }
|
Reset();
|
||||||
UpdatePlayerSpeed();
|
UpdatePlayerSpeed();
|
||||||
PlaceNewSnakePart(MovePlayer());
|
PlaceNewSnakePart(MovePlayer());
|
||||||
RegenerateFood();
|
RegenerateFood();
|
||||||
@ -56,17 +51,14 @@ void GameEngine::Loop(void)
|
|||||||
if (!state.m_bNoDisplay)
|
if (!state.m_bNoDisplay)
|
||||||
graphics.DisplayGameState(gameBoard, currentScore);
|
graphics.DisplayGameState(gameBoard, currentScore);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Vector2f GameEngine::MovePlayer(void)
|
sf::Vector2f GameEngine::MovePlayer(void) {
|
||||||
{
|
|
||||||
return sf::Vector2f(player.headLocation.x + player.speed.x, player.headLocation.y + player.speed.y);
|
return sf::Vector2f(player.headLocation.x + player.speed.x, player.headLocation.y + player.speed.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sf::Vector2f GameEngine::GetGameBoundaries(void)
|
sf::Vector2f GameEngine::GetGameBoundaries(void) {
|
||||||
{
|
|
||||||
return graphics.gameBoundaries;
|
return graphics.gameBoundaries;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,12 +89,12 @@ sf::Vector2f GameEngine::GetFoodLocation(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
|
void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
|
||||||
if (!player.speed.x && !player.speed.y) { return; }
|
if (!player.speed.x && !player.speed.y)
|
||||||
|
return;
|
||||||
try {
|
try {
|
||||||
GameSpace* locationState = &gameBoard.at(location.y).at(location.x);
|
GameSpace* locationState = &gameBoard.at(location.y).at(location.x);
|
||||||
if (locationState->m_bSnake && (player.body.size() > 1)) {
|
if (locationState->m_bSnake && (player.body.size() > 1))
|
||||||
state.m_bIsGameOver = true; // Game should end (Snake touching snake)
|
state.m_bIsGameOver = true; // Game should end (Snake touching snake)
|
||||||
}
|
|
||||||
locationState->m_bSnake = true;
|
locationState->m_bSnake = true;
|
||||||
player.body.push(locationState);
|
player.body.push(locationState);
|
||||||
player.headLocation = location;
|
player.headLocation = location;
|
||||||
@ -116,70 +108,70 @@ void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
|
|||||||
} catch (const std::out_of_range& error) {
|
} catch (const std::out_of_range& error) {
|
||||||
state.m_bIsGameOver = true; // Snake ran into edge
|
state.m_bIsGameOver = true; // Snake ran into edge
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Generates new food until not colliding with player
|
// Generates new food until not colliding with player
|
||||||
void GameEngine::RegenerateFood()
|
void GameEngine::RegenerateFood(void) {
|
||||||
{
|
|
||||||
// Generate a new food location if the current one is occupied
|
// Generate a new food location if the current one is occupied
|
||||||
while (gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bSnake) {
|
while (gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bSnake)
|
||||||
playerFood.GenerateNewFood(GetGameBoundaries());
|
playerFood.GenerateNewFood(GetGameBoundaries());
|
||||||
}
|
|
||||||
|
|
||||||
// Update the game board with the new food location
|
// Update the game board with the new food location
|
||||||
gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bFood = 1;
|
gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bFood = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GameEngine::PrepareGameBoard(void)
|
void GameEngine::PrepareGameBoard(void) {
|
||||||
{
|
// Create empty game board
|
||||||
gameBoard.clear();
|
gameBoard.clear();
|
||||||
sf::Vector2f boardDimensions = GetGameBoundaries();
|
sf::Vector2f boardDimensions = GetGameBoundaries();
|
||||||
gameBoard.resize(boardDimensions.y, std::vector<GameSpace>(boardDimensions.x));
|
gameBoard.resize(boardDimensions.y, std::vector<GameSpace>(boardDimensions.x));
|
||||||
|
|
||||||
// Snake setup
|
// Snake setup
|
||||||
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
|
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
|
||||||
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
|
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
|
||||||
{
|
|
||||||
GameSpace* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
|
GameSpace* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
|
||||||
player.body.push(locationState);
|
player.body.push(locationState);
|
||||||
locationState->m_bSnake = true;
|
locationState->m_bSnake = true;
|
||||||
}
|
|
||||||
// Food setup
|
// Food setup
|
||||||
playerFood.GenerateNewFood(boardDimensions);
|
playerFood.GenerateNewFood(boardDimensions);
|
||||||
gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bFood = true;
|
gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bFood = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::UpdatePlayerSpeed(void)
|
void GameEngine::UpdatePlayerSpeed(void) {
|
||||||
{
|
|
||||||
PlayerDirection controller;
|
PlayerDirection controller;
|
||||||
if (state.m_bIsBotControlled) {
|
if (state.m_bIsBotControlled) {
|
||||||
if (bot.path.empty()) {
|
if (bot.path.empty())
|
||||||
bot.GetNewPath();
|
bot.GetNewPath();
|
||||||
}
|
|
||||||
controller = bot.GetInput();
|
controller = bot.GetInput();
|
||||||
}
|
} else
|
||||||
else { controller = GetPlayerInput(); }
|
controller = GetPlayerInput();
|
||||||
|
|
||||||
switch (controller) {
|
switch (controller) {
|
||||||
case kUp:
|
case kUp:
|
||||||
if (player.speed.y == kUnitSpeed) { break; }
|
if (player.speed.y == kUnitSpeed)
|
||||||
|
break;
|
||||||
player.speed.x = 0;
|
player.speed.x = 0;
|
||||||
player.speed.y = -kUnitSpeed;
|
player.speed.y = -kUnitSpeed;
|
||||||
break;
|
break;
|
||||||
case kLeft:
|
case kLeft:
|
||||||
if (player.speed.x == kUnitSpeed) { break; }
|
if (player.speed.x == kUnitSpeed)
|
||||||
|
break;
|
||||||
player.speed.x = -kUnitSpeed;
|
player.speed.x = -kUnitSpeed;
|
||||||
player.speed.y = 0;
|
player.speed.y = 0;
|
||||||
break;
|
break;
|
||||||
case kRight:
|
case kRight:
|
||||||
if (player.speed.x == -kUnitSpeed) { break; }
|
if (player.speed.x == -kUnitSpeed)
|
||||||
|
break;
|
||||||
player.speed.x = kUnitSpeed;
|
player.speed.x = kUnitSpeed;
|
||||||
player.speed.y = 0;
|
player.speed.y = 0;
|
||||||
break;
|
break;
|
||||||
case kDown:
|
case kDown:
|
||||||
if (player.speed.y == -kUnitSpeed) { break; }
|
if (player.speed.y == -kUnitSpeed)
|
||||||
|
break;
|
||||||
player.speed.x = 0;
|
player.speed.x = 0;
|
||||||
player.speed.y = kUnitSpeed;
|
player.speed.y = kUnitSpeed;
|
||||||
break;
|
break;
|
||||||
|
@ -11,10 +11,9 @@
|
|||||||
|
|
||||||
const int kUnitSpeed = 1;
|
const int kUnitSpeed = 1;
|
||||||
|
|
||||||
class GameEngine
|
class GameEngine {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
GameEngine();
|
GameEngine(void);
|
||||||
void Start(void);
|
void Start(void);
|
||||||
void Reset(void);
|
void Reset(void);
|
||||||
sf::Vector2f GetGameBoundaries(void);
|
sf::Vector2f GetGameBoundaries(void);
|
||||||
@ -43,7 +42,7 @@ private:
|
|||||||
void PlaceNewSnakePart(sf::Vector2f location);
|
void PlaceNewSnakePart(sf::Vector2f location);
|
||||||
void RegenerateFood(void);
|
void RegenerateFood(void);
|
||||||
void PrepareGameBoard(void);
|
void PrepareGameBoard(void);
|
||||||
void UpdatePlayerSpeed();
|
void UpdatePlayerSpeed(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
inline std::unique_ptr<GameEngine> g_pEngine;
|
inline std::unique_ptr<GameEngine> g_pEngine;
|
||||||
|
@ -1,32 +1,28 @@
|
|||||||
#include "playerinterface.hpp"
|
#include "playerinterface.hpp"
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <SFML/Window/Keyboard.hpp>
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
PlayerDirection GetPlayerInput(void)
|
PlayerDirection GetPlayerInput(void) {
|
||||||
{
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ||
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)
|
(sf::Keyboard::isKeyPressed(sf::Keyboard::A)))
|
||||||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::A))
|
|
||||||
return kLeft;
|
return kLeft;
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) ||
|
||||||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::W))
|
(sf::Keyboard::isKeyPressed(sf::Keyboard::W)))
|
||||||
return kUp;
|
return kUp;
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) ||
|
||||||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::S))
|
(sf::Keyboard::isKeyPressed(sf::Keyboard::S)))
|
||||||
return kDown;
|
return kDown;
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) ||
|
||||||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::D))
|
(sf::Keyboard::isKeyPressed(sf::Keyboard::D)))
|
||||||
return kRight;
|
return kRight;
|
||||||
return kNone;
|
return kNone;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PlayerOutput::IsOpen(void)
|
bool PlayerOutput::IsOpen(void) {
|
||||||
{
|
|
||||||
return isWindowAlive;
|
return isWindowAlive;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerOutput::PlayerOutput(void)
|
PlayerOutput::PlayerOutput(void) {
|
||||||
{
|
|
||||||
float kWidth = 1025;
|
float kWidth = 1025;
|
||||||
float kHeight = 725;
|
float kHeight = 725;
|
||||||
float kBoardWidth = kWidth / kGridSize;
|
float kBoardWidth = kWidth / kGridSize;
|
||||||
@ -37,26 +33,23 @@ PlayerOutput::PlayerOutput(void)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::CheckContinue()
|
void PlayerOutput::CheckContinue(void) {
|
||||||
{
|
|
||||||
DisplayEndScreen();
|
DisplayEndScreen();
|
||||||
while (true)
|
while (true) {
|
||||||
{
|
|
||||||
gameWindow.pollEvent(event);
|
gameWindow.pollEvent(event);
|
||||||
if ((event.type == sf::Event::Closed)
|
if ((event.type == sf::Event::Closed) ||
|
||||||
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) {
|
||||||
{
|
|
||||||
gameWindow.close();
|
gameWindow.close();
|
||||||
isWindowAlive = false;
|
isWindowAlive = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) { return; }
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
|
||||||
|
return;
|
||||||
sf::sleep(delay);
|
sf::sleep(delay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::DisplayEndScreen(void)
|
void PlayerOutput::DisplayEndScreen(void) {
|
||||||
{
|
|
||||||
gameWindow.clear();
|
gameWindow.clear();
|
||||||
sf::Vector2f textPosition(gameBoundaries);
|
sf::Vector2f textPosition(gameBoundaries);
|
||||||
textPosition.x = textPosition.x / 2;
|
textPosition.x = textPosition.x / 2;
|
||||||
@ -80,18 +73,14 @@ void PlayerOutput::DisplayScore(int score) {
|
|||||||
sf::Text ScoreText(text, font);
|
sf::Text ScoreText(text, font);
|
||||||
ScoreText.setPosition(textPosition);
|
ScoreText.setPosition(textPosition);
|
||||||
gameWindow.draw(ScoreText);
|
gameWindow.draw(ScoreText);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::DisplayGameState(std::vector< std::vector<GameSpace> >& gameBoard, int score)
|
void PlayerOutput::DisplayGameState(std::vector< std::vector<GameSpace> >& gameBoard, int score) {
|
||||||
{
|
|
||||||
CheckWindowEvents();
|
CheckWindowEvents();
|
||||||
if (delay == sf::milliseconds(0)) { return; }
|
if (delay == sf::milliseconds(0)) { return; }
|
||||||
char* letterOnBoard;
|
char* letterOnBoard;
|
||||||
for (float y = 0; y < gameBoundaries.y; y++)
|
for (float y = 0; y < gameBoundaries.y; y++) {
|
||||||
{
|
for (float x = 0; x < gameBoundaries.x; x++) {
|
||||||
for (float x = 0; x < gameBoundaries.x; x++)
|
|
||||||
{
|
|
||||||
if (gameBoard.at(y).at(x).m_bSnake)
|
if (gameBoard.at(y).at(x).m_bSnake)
|
||||||
DrawSnake(sf::Vector2f(x, y));
|
DrawSnake(sf::Vector2f(x, y));
|
||||||
else if (gameBoard.at(y).at(x).m_bFood)
|
else if (gameBoard.at(y).at(x).m_bFood)
|
||||||
@ -106,25 +95,22 @@ void PlayerOutput::DisplayGameState(std::vector< std::vector<GameSpace> >& gameB
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::StartGameWindow(void)
|
void PlayerOutput::StartGameWindow(void) {
|
||||||
{
|
|
||||||
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
||||||
isWindowAlive = true;
|
isWindowAlive = true;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::SetShowGame(bool isShowing) {
|
void PlayerOutput::SetShowGame(bool isShowing) {
|
||||||
if (isShowing) { delay = sf::milliseconds(5); }
|
if (isShowing)
|
||||||
else { delay = sf::milliseconds(0); }
|
delay = sf::milliseconds(5);
|
||||||
return;
|
else
|
||||||
|
delay = sf::milliseconds(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::CheckWindowEvents(void)
|
void PlayerOutput::CheckWindowEvents(void) {
|
||||||
{
|
while (gameWindow.pollEvent(event)) {
|
||||||
while (gameWindow.pollEvent(event))
|
if ((event.type == sf::Event::Closed) ||
|
||||||
{
|
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) {
|
||||||
if ((event.type == sf::Event::Closed)
|
|
||||||
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) {
|
|
||||||
gameWindow.close();
|
gameWindow.close();
|
||||||
isWindowAlive = false;
|
isWindowAlive = false;
|
||||||
}
|
}
|
||||||
@ -139,29 +125,23 @@ void PlayerOutput::CheckWindowEvents(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::DrawEmpty(sf::Vector2f location)
|
void PlayerOutput::DrawEmpty(sf::Vector2f location) {
|
||||||
{
|
|
||||||
location *= static_cast<float>(kGridSize);
|
location *= static_cast<float>(kGridSize);
|
||||||
drawObject.setPosition(location);
|
drawObject.setPosition(location);
|
||||||
drawObject.setFillColor(sf::Color::Black);
|
drawObject.setFillColor(sf::Color::Black);
|
||||||
gameWindow.draw(drawObject);
|
gameWindow.draw(drawObject);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::DrawFood(sf::Vector2f location)
|
void PlayerOutput::DrawFood(sf::Vector2f location) {
|
||||||
{
|
|
||||||
location *= static_cast<float>(kGridSize);
|
location *= static_cast<float>(kGridSize);
|
||||||
drawObject.setPosition(location);
|
drawObject.setPosition(location);
|
||||||
drawObject.setFillColor(sf::Color::Red);
|
drawObject.setFillColor(sf::Color::Red);
|
||||||
gameWindow.draw(drawObject);
|
gameWindow.draw(drawObject);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::DrawSnake(sf::Vector2f location)
|
void PlayerOutput::DrawSnake(sf::Vector2f location) {
|
||||||
{
|
|
||||||
location *= static_cast<float>(kGridSize);
|
location *= static_cast<float>(kGridSize);
|
||||||
drawObject.setPosition(location);
|
drawObject.setPosition(location);
|
||||||
drawObject.setFillColor(sf::Color::Green);
|
drawObject.setFillColor(sf::Color::Green);
|
||||||
gameWindow.draw(drawObject);
|
gameWindow.draw(drawObject);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,12 @@ const int kGridSize = 25;
|
|||||||
|
|
||||||
PlayerDirection GetPlayerInput(void);
|
PlayerDirection GetPlayerInput(void);
|
||||||
|
|
||||||
class PlayerOutput
|
class PlayerOutput {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
sf::Vector2f gameBoundaries;
|
sf::Vector2f gameBoundaries;
|
||||||
PlayerOutput(void);
|
PlayerOutput(void);
|
||||||
bool IsOpen(void);
|
bool IsOpen(void);
|
||||||
void CheckContinue();
|
void CheckContinue(void);
|
||||||
void DisplayGameState(std::vector< std::vector<GameSpace> >& gameBoard, int score);
|
void DisplayGameState(std::vector< std::vector<GameSpace> >& gameBoard, int score);
|
||||||
void DisplayScore(int score);
|
void DisplayScore(int score);
|
||||||
void StartGameWindow(void);
|
void StartGameWindow(void);
|
||||||
|
@ -4,24 +4,22 @@
|
|||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
#include "snake.hpp"
|
#include "snake.hpp"
|
||||||
|
|
||||||
void Snake::Pop(void)
|
void Snake::Pop(void) {
|
||||||
{
|
|
||||||
body.front()->m_bSnake = false;
|
body.front()->m_bSnake = false;
|
||||||
body.pop();
|
body.pop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Snake::Reset(void)
|
void Snake::Reset(void) {
|
||||||
{
|
while (!body.empty())
|
||||||
while (!body.empty()) Pop();
|
Pop();
|
||||||
speed.x = 0;
|
speed.x = 0;
|
||||||
speed.y = 0;
|
speed.y = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 = GenerateRandomNumber(boundaries.x);
|
location.x = GenerateRandomNumber(boundaries.x);
|
||||||
location.y = GenerateRandomNumber(boundaries.y);
|
location.y = GenerateRandomNumber(boundaries.y);
|
||||||
return;
|
return;
|
||||||
|
@ -6,8 +6,7 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
|
|
||||||
struct Snake
|
struct Snake {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
sf::Vector2f headLocation;
|
sf::Vector2f headLocation;
|
||||||
sf::Vector2f speed;
|
sf::Vector2f speed;
|
||||||
@ -16,8 +15,7 @@ public:
|
|||||||
void Reset(void);
|
void Reset(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Food
|
struct Food {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
sf::Vector2f location;
|
sf::Vector2f location;
|
||||||
void GenerateNewFood(sf::Vector2f boundaries);
|
void GenerateNewFood(sf::Vector2f boundaries);
|
||||||
|
Loading…
Reference in New Issue
Block a user