refactor: remove all code from being in namespace
This commit is contained in:
parent
d5c797d460
commit
0ff3ef3f62
@ -7,16 +7,14 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
namespace snakeplusplus
|
PlayerDirection lastKnownDirection = kNone;
|
||||||
{
|
|
||||||
PlayerDirection lastKnownDirection = kNone;
|
|
||||||
|
|
||||||
AISnake::AISnake() {
|
AISnake::AISnake() {
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
||||||
{
|
{
|
||||||
sf::Vector2f directionDelta;
|
sf::Vector2f directionDelta;
|
||||||
if (*source == path.top()) { path.pop(); }
|
if (*source == path.top()) { path.pop(); }
|
||||||
if (path.empty()) { return kUp; } // Snake is trapped
|
if (path.empty()) { return kUp; } // Snake is trapped
|
||||||
@ -35,27 +33,27 @@ namespace snakeplusplus
|
|||||||
&& (lastKnownDirection != kLeft))
|
&& (lastKnownDirection != kLeft))
|
||||||
{ lastKnownDirection = kRight; }
|
{ 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) { probabilityBFS = 1.0; }
|
||||||
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
|
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
|
||||||
std::cout << "[Info - AISnake] New BFS probability: " << probabilityBFS << std::endl;
|
std::cout << "[Info - AISnake] New BFS probability: " << probabilityBFS << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
void AISnake::GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
||||||
{
|
{
|
||||||
// Search for food
|
// Search for food
|
||||||
/*
|
/*
|
||||||
BFS(gameBoard, source, boundaries);
|
BFS(gameBoard, source, boundaries);
|
||||||
@ -80,9 +78,9 @@ namespace snakeplusplus
|
|||||||
}
|
}
|
||||||
botPathUnsanitized.pop();
|
botPathUnsanitized.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
void AISnake::BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
||||||
std::queue<sf::Vector2f> search;
|
std::queue<sf::Vector2f> search;
|
||||||
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false));
|
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false));
|
||||||
bool foodFound = false;
|
bool foodFound = false;
|
||||||
@ -124,9 +122,9 @@ namespace snakeplusplus
|
|||||||
}
|
}
|
||||||
visited.at(currentLocation.y).at(currentLocation.x) = true;
|
visited.at(currentLocation.y).at(currentLocation.x) = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AISnake::DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
void AISnake::DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
||||||
std::stack<sf::Vector2f> search;
|
std::stack<sf::Vector2f> search;
|
||||||
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false));
|
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false));
|
||||||
bool foodFound = false;
|
bool foodFound = false;
|
||||||
@ -174,5 +172,4 @@ namespace snakeplusplus
|
|||||||
}
|
}
|
||||||
visited.at(currentLocation.y).at(currentLocation.x) = true;
|
visited.at(currentLocation.y).at(currentLocation.x) = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -6,22 +6,19 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
namespace snakeplusplus
|
class AISnake {
|
||||||
{
|
public:
|
||||||
class AISnake {
|
|
||||||
public:
|
|
||||||
std::stack<sf::Vector2f> path;
|
std::stack<sf::Vector2f> path;
|
||||||
AISnake();
|
AISnake();
|
||||||
void GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize);
|
void GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize);
|
||||||
PlayerDirection GetInput(const sf::Vector2f* source);
|
PlayerDirection GetInput(const sf::Vector2f* source);
|
||||||
void UpdateProbability(int snakeSize);
|
void UpdateProbability(int snakeSize);
|
||||||
void AdjustProbability(double amount);
|
void AdjustProbability(double amount);
|
||||||
private:
|
private:
|
||||||
double probabilityBFS = 0.500;
|
double probabilityBFS = 0.500;
|
||||||
std::stack<sf::Vector2f> botPathUnsanitized;
|
std::stack<sf::Vector2f> botPathUnsanitized;
|
||||||
void BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
void BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
||||||
void DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
void DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -2,20 +2,17 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
|
|
||||||
namespace snakeplusplus
|
std::default_random_engine generator;
|
||||||
|
void InitializeGenerator(void)
|
||||||
{
|
{
|
||||||
std::default_random_engine generator;
|
|
||||||
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(snakeplusplus::generator);
|
generatedNumber = distribution(generator);
|
||||||
return generatedNumber;
|
return generatedNumber;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,16 @@
|
|||||||
#ifndef COMMON_HPP
|
#ifndef COMMON_HPP
|
||||||
#define COMMON_HPP
|
#define COMMON_HPP
|
||||||
|
|
||||||
namespace snakeplusplus
|
void InitializeGenerator(void);
|
||||||
{
|
int GenerateRandomNumber(int generationLimit);
|
||||||
void InitializeGenerator(void);
|
|
||||||
int GenerateRandomNumber(int generationLimit);
|
|
||||||
|
|
||||||
enum PlayerDirection
|
enum PlayerDirection
|
||||||
{
|
{
|
||||||
kNone = 0,
|
kNone = 0,
|
||||||
kLeft = 1,
|
kLeft = 1,
|
||||||
kUp = 2,
|
kUp = 2,
|
||||||
kDown = 3,
|
kDown = 3,
|
||||||
kRight = 4
|
kRight = 4
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -7,24 +7,22 @@
|
|||||||
#include "playerinterface.hpp"
|
#include "playerinterface.hpp"
|
||||||
#include "gamestate.hpp"
|
#include "gamestate.hpp"
|
||||||
|
|
||||||
namespace snakeplusplus
|
GameEngine::GameEngine()
|
||||||
{
|
{
|
||||||
GameEngine::GameEngine()
|
|
||||||
{
|
|
||||||
InitializeGenerator();
|
InitializeGenerator();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::Start()
|
void GameEngine::Start()
|
||||||
{
|
{
|
||||||
PrepareGameBoard();
|
PrepareGameBoard();
|
||||||
graphics.StartGameWindow();
|
graphics.StartGameWindow();
|
||||||
Loop();
|
Loop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::Reset()
|
void GameEngine::Reset()
|
||||||
{
|
{
|
||||||
AddIteration();
|
AddIteration();
|
||||||
player.Reset();
|
player.Reset();
|
||||||
if (isBotControlled) { while (!bot.path.empty()) { bot.path.pop(); } }
|
if (isBotControlled) { while (!bot.path.empty()) { bot.path.pop(); } }
|
||||||
@ -32,10 +30,10 @@ namespace snakeplusplus
|
|||||||
isGameOver = false;
|
isGameOver = false;
|
||||||
graphics.SetShowGame((amountPlayed + 1) % 50 == 0);
|
graphics.SetShowGame((amountPlayed + 1) % 50 == 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::AddIteration(void)
|
void GameEngine::AddIteration(void)
|
||||||
{
|
{
|
||||||
graphics.CheckContinue(isBotControlled);
|
graphics.CheckContinue(isBotControlled);
|
||||||
if (player.body.size() > 40)
|
if (player.body.size() > 40)
|
||||||
{
|
{
|
||||||
@ -47,10 +45,10 @@ namespace snakeplusplus
|
|||||||
std::cout << "[Info - GameEngine] Current average: " << average << std::endl;
|
std::cout << "[Info - GameEngine] Current average: " << average << std::endl;
|
||||||
std::cout << "[Info - GameEngine] Previous iteration size: " << player.body.size() << std::endl;
|
std::cout << "[Info - GameEngine] Previous iteration size: " << player.body.size() << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::Loop(void)
|
void GameEngine::Loop(void)
|
||||||
{
|
{
|
||||||
int currentScore = 0;
|
int currentScore = 0;
|
||||||
while (graphics.IsOpen())
|
while (graphics.IsOpen())
|
||||||
{
|
{
|
||||||
@ -63,20 +61,20 @@ namespace snakeplusplus
|
|||||||
graphics.DisplayGameState(gameBoard, currentScore);
|
graphics.DisplayGameState(gameBoard, currentScore);
|
||||||
}
|
}
|
||||||
return;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
char* locationState = &gameBoard.at(location.y).at(location.x);
|
char* locationState = &gameBoard.at(location.y).at(location.x);
|
||||||
@ -92,12 +90,12 @@ namespace snakeplusplus
|
|||||||
isGameOver = true; // Snake ran into edge
|
isGameOver = true; // Snake ran into edge
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Generates new food until not colliding with player
|
// Generates new food until not colliding with player
|
||||||
void GameEngine::RegenerateFood()
|
void GameEngine::RegenerateFood()
|
||||||
{
|
{
|
||||||
// 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) == 'O') {
|
while (gameBoard.at(playerFood.location.y).at(playerFood.location.x) == 'O') {
|
||||||
playerFood.GenerateNewFood(GetGameBoundaries());
|
playerFood.GenerateNewFood(GetGameBoundaries());
|
||||||
@ -105,11 +103,11 @@ namespace snakeplusplus
|
|||||||
|
|
||||||
// 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) = 'X';
|
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GameEngine::PrepareGameBoard(void)
|
void GameEngine::PrepareGameBoard(void)
|
||||||
{
|
{
|
||||||
gameBoard.clear();
|
gameBoard.clear();
|
||||||
sf::Vector2f boardDimensions = GetGameBoundaries();
|
sf::Vector2f boardDimensions = GetGameBoundaries();
|
||||||
gameBoard.resize(boardDimensions.y, std::vector<char> (boardDimensions.x, ' '));
|
gameBoard.resize(boardDimensions.y, std::vector<char> (boardDimensions.x, ' '));
|
||||||
@ -125,10 +123,10 @@ namespace snakeplusplus
|
|||||||
playerFood.GenerateNewFood(boardDimensions);
|
playerFood.GenerateNewFood(boardDimensions);
|
||||||
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::UpdatePlayerSpeed(void)
|
void GameEngine::UpdatePlayerSpeed(void)
|
||||||
{
|
{
|
||||||
PlayerDirection controller;
|
PlayerDirection controller;
|
||||||
if (isBotControlled) {
|
if (isBotControlled) {
|
||||||
if (bot.path.empty()) {
|
if (bot.path.empty()) {
|
||||||
@ -162,10 +160,10 @@ namespace snakeplusplus
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void GameEngine::UpdateAverage() {
|
|
||||||
|
void GameEngine::UpdateAverage() {
|
||||||
totalLength += player.body.size();
|
totalLength += player.body.size();
|
||||||
amountPlayed += 1;
|
amountPlayed += 1;
|
||||||
average = (double)totalLength / amountPlayed;
|
average = (double)totalLength / amountPlayed;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,23 +3,22 @@
|
|||||||
#define GAMESTATE_HPP
|
#define GAMESTATE_HPP
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <memory>
|
||||||
#include "botinterface.hpp"
|
#include "botinterface.hpp"
|
||||||
#include "snake.hpp"
|
#include "snake.hpp"
|
||||||
#include "playerinterface.hpp"
|
#include "playerinterface.hpp"
|
||||||
|
|
||||||
namespace snakeplusplus
|
const int kUnitSpeed = 1;
|
||||||
{
|
|
||||||
const int kUnitSpeed = 1;
|
|
||||||
|
|
||||||
class GameEngine
|
class GameEngine
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GameEngine();
|
GameEngine();
|
||||||
void Start(void);
|
void Start(void);
|
||||||
void Reset(void);
|
void Reset(void);
|
||||||
void AddIteration(void);
|
void AddIteration(void);
|
||||||
sf::Vector2f GetGameBoundaries(void);
|
sf::Vector2f GetGameBoundaries(void);
|
||||||
private:
|
private:
|
||||||
std::vector< std::vector<char> > gameBoard;
|
std::vector< std::vector<char> > gameBoard;
|
||||||
PlayerOutput graphics;
|
PlayerOutput graphics;
|
||||||
Snake player;
|
Snake player;
|
||||||
@ -38,7 +37,6 @@ namespace snakeplusplus
|
|||||||
int totalLength = 0;
|
int totalLength = 0;
|
||||||
int amountPlayed = 0;
|
int amountPlayed = 0;
|
||||||
double average = 0;
|
double average = 0;
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
snakeplusplus::GameEngine game;
|
GameEngine game;
|
||||||
game.Start();
|
game.Start();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,8 @@
|
|||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <SFML/Window/Keyboard.hpp>
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
|
||||||
namespace snakeplusplus
|
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;
|
||||||
@ -19,15 +17,15 @@ namespace snakeplusplus
|
|||||||
|| 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 gameWindow.isOpen();
|
return gameWindow.isOpen();
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
@ -36,10 +34,10 @@ namespace snakeplusplus
|
|||||||
gameVideoSettings = sf::VideoMode(kWidth, kHeight);
|
gameVideoSettings = sf::VideoMode(kWidth, kHeight);
|
||||||
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
|
drawObject.setSize(sf::Vector2f(kGridSize, kGridSize));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::CheckContinue(bool isBotControlled)
|
void PlayerOutput::CheckContinue(bool isBotControlled)
|
||||||
{
|
{
|
||||||
if (isBotControlled) { return; }
|
if (isBotControlled) { return; }
|
||||||
DisplayEndScreen();
|
DisplayEndScreen();
|
||||||
while (true)
|
while (true)
|
||||||
@ -54,10 +52,10 @@ namespace snakeplusplus
|
|||||||
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;
|
||||||
@ -69,9 +67,9 @@ namespace snakeplusplus
|
|||||||
gameWindow.draw(gameOverText);
|
gameWindow.draw(gameOverText);
|
||||||
gameWindow.display();
|
gameWindow.display();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::DisplayScore(int score) {
|
void PlayerOutput::DisplayScore(int score) {
|
||||||
sf::Vector2f textPosition(gameBoundaries);
|
sf::Vector2f textPosition(gameBoundaries);
|
||||||
textPosition.x = textPosition.x / 2;
|
textPosition.x = textPosition.x / 2;
|
||||||
textPosition.y = textPosition.y / 2;
|
textPosition.y = textPosition.y / 2;
|
||||||
@ -82,10 +80,10 @@ namespace snakeplusplus
|
|||||||
ScoreText.setPosition(textPosition);
|
ScoreText.setPosition(textPosition);
|
||||||
gameWindow.draw(ScoreText);
|
gameWindow.draw(ScoreText);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score)
|
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score)
|
||||||
{
|
{
|
||||||
CheckWindowEvents();
|
CheckWindowEvents();
|
||||||
if (delay == sf::milliseconds(0)) { return; }
|
if (delay == sf::milliseconds(0)) { return; }
|
||||||
char* letterOnBoard;
|
char* letterOnBoard;
|
||||||
@ -112,23 +110,23 @@ namespace snakeplusplus
|
|||||||
gameWindow.display();
|
gameWindow.display();
|
||||||
sf::sleep(delay);
|
sf::sleep(delay);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::StartGameWindow(void)
|
void PlayerOutput::StartGameWindow(void)
|
||||||
{
|
{
|
||||||
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
||||||
isWindowAlive = true;
|
isWindowAlive = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::SetShowGame(bool isShowing) {
|
void PlayerOutput::SetShowGame(bool isShowing) {
|
||||||
if (isShowing) { delay = sf::milliseconds(2); }
|
if (isShowing) { delay = sf::milliseconds(2); }
|
||||||
else { delay = sf::milliseconds(0); }
|
else { delay = sf::milliseconds(0); }
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::CheckWindowEvents(void)
|
void PlayerOutput::CheckWindowEvents(void)
|
||||||
{
|
{
|
||||||
while (gameWindow.pollEvent(event))
|
while (gameWindow.pollEvent(event))
|
||||||
{
|
{
|
||||||
if ((event.type == sf::Event::Closed)
|
if ((event.type == sf::Event::Closed)
|
||||||
@ -143,32 +141,31 @@ namespace snakeplusplus
|
|||||||
delay -= sf::milliseconds(1);
|
delay -= sf::milliseconds(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
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;
|
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;
|
return;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -6,13 +6,11 @@
|
|||||||
|
|
||||||
const int kGridSize = 25;
|
const int kGridSize = 25;
|
||||||
|
|
||||||
namespace snakeplusplus
|
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);
|
||||||
@ -21,7 +19,7 @@ namespace snakeplusplus
|
|||||||
void DisplayScore(int score);
|
void DisplayScore(int score);
|
||||||
void StartGameWindow(void);
|
void StartGameWindow(void);
|
||||||
void SetShowGame(bool isShowing);
|
void SetShowGame(bool isShowing);
|
||||||
private:
|
private:
|
||||||
void CheckWindowEvents(void);
|
void CheckWindowEvents(void);
|
||||||
void DisplayEndScreen(void);
|
void DisplayEndScreen(void);
|
||||||
void DrawEmpty(sf::Vector2f location);
|
void DrawEmpty(sf::Vector2f location);
|
||||||
@ -33,7 +31,6 @@ namespace snakeplusplus
|
|||||||
sf::Event event;
|
sf::Event event;
|
||||||
bool isWindowAlive;
|
bool isWindowAlive;
|
||||||
sf::Time delay = sf::milliseconds(1);
|
sf::Time delay = sf::milliseconds(1);
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -4,28 +4,25 @@
|
|||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
#include "snake.hpp"
|
#include "snake.hpp"
|
||||||
|
|
||||||
namespace snakeplusplus
|
void Snake::Pop(void)
|
||||||
{
|
{
|
||||||
void Snake::Pop(void)
|
|
||||||
{
|
|
||||||
*(body.front()) = ' ';
|
*(body.front()) = ' ';
|
||||||
body.pop();
|
body.pop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Snake::Reset(void)
|
void Snake::Reset(void)
|
||||||
{
|
{
|
||||||
while (!body.empty()) Pop();
|
while (!body.empty()) 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;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,25 +5,22 @@
|
|||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
namespace snakeplusplus
|
struct Snake
|
||||||
{
|
{
|
||||||
struct Snake
|
public:
|
||||||
{
|
|
||||||
public:
|
|
||||||
sf::Vector2f headLocation;
|
sf::Vector2f headLocation;
|
||||||
sf::Vector2f speed;
|
sf::Vector2f speed;
|
||||||
std::queue<char*> body;
|
std::queue<char*> body;
|
||||||
void Pop(void);
|
void Pop(void);
|
||||||
void Reset(void);
|
void Reset(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Food
|
struct Food
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
sf::Vector2f location;
|
sf::Vector2f location;
|
||||||
char* food;
|
char* food;
|
||||||
void GenerateNewFood(sf::Vector2f boundaries);
|
void GenerateNewFood(sf::Vector2f boundaries);
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user