refactor: remove all code from being in namespace
This commit is contained in:
parent
d5c797d460
commit
0ff3ef3f62
@ -7,172 +7,169 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
namespace snakeplusplus
|
PlayerDirection lastKnownDirection = kNone;
|
||||||
|
|
||||||
|
AISnake::AISnake() {
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
||||||
{
|
{
|
||||||
PlayerDirection lastKnownDirection = kNone;
|
sf::Vector2f directionDelta;
|
||||||
|
if (*source == path.top()) { path.pop(); }
|
||||||
|
if (path.empty()) { return kUp; } // Snake is trapped
|
||||||
|
directionDelta = *source - path.top();
|
||||||
|
path.pop();
|
||||||
|
if ((directionDelta.y == 1)
|
||||||
|
&& (lastKnownDirection != kDown))
|
||||||
|
{ lastKnownDirection = kUp; }
|
||||||
|
else if ((directionDelta.y == -1)
|
||||||
|
&& (lastKnownDirection != kUp))
|
||||||
|
{ lastKnownDirection = kDown; }
|
||||||
|
else if ((directionDelta.x == 1)
|
||||||
|
&& (lastKnownDirection != kRight))
|
||||||
|
{ lastKnownDirection = kLeft; }
|
||||||
|
else if ((directionDelta.x == -1)
|
||||||
|
&& (lastKnownDirection != kLeft))
|
||||||
|
{ lastKnownDirection = kRight; }
|
||||||
|
return lastKnownDirection;
|
||||||
|
}
|
||||||
|
|
||||||
AISnake::AISnake() {
|
void AISnake::UpdateProbability(int snakeSize)
|
||||||
;
|
{
|
||||||
|
probabilityBFS = 1 - ((double) snakeSize) / 1000;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AISnake::AdjustProbability(double amount)
|
||||||
|
{
|
||||||
|
probabilityBFS += amount;
|
||||||
|
if (probabilityBFS > 1.0) { probabilityBFS = 1.0; }
|
||||||
|
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
|
||||||
|
std::cout << "[Info - AISnake] New BFS probability: " << probabilityBFS << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gets a new path for the bot to follow
|
||||||
|
// Uses DFS algorithm
|
||||||
|
void AISnake::GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
||||||
|
{
|
||||||
|
// Search for food
|
||||||
|
/*
|
||||||
|
BFS(gameBoard, source, boundaries);
|
||||||
|
if (gameBoard[botPathUnsanitized.top().y][botPathUnsanitized.top().x] != 'X') {
|
||||||
|
while (!botPathUnsanitized.empty()) { botPathUnsanitized.pop(); }
|
||||||
|
DFS(gameBoard, source, boundaries);
|
||||||
|
while (botPathUnsanitized.size() > 15) { botPathUnsanitized.pop(); }
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
// Probability-based approach for fun
|
||||||
{
|
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
|
||||||
sf::Vector2f directionDelta;
|
if (roll <= probabilityBFS) { BFS(gameBoard, source, boundaries); }
|
||||||
if (*source == path.top()) { path.pop(); }
|
else { DFS(gameBoard, source, boundaries); }
|
||||||
if (path.empty()) { return kUp; } // Snake is trapped
|
// Create path for food
|
||||||
directionDelta = *source - path.top();
|
path.push(botPathUnsanitized.top());
|
||||||
path.pop();
|
botPathUnsanitized.pop();
|
||||||
if ((directionDelta.y == 1)
|
while (!botPathUnsanitized.empty()) {
|
||||||
&& (lastKnownDirection != kDown))
|
sf::Vector2f deltaVector = botPathUnsanitized.top() - path.top();
|
||||||
{ lastKnownDirection = kUp; }
|
int delta = abs(deltaVector.x) + abs(deltaVector.y);
|
||||||
else if ((directionDelta.y == -1)
|
if (delta == 1) {
|
||||||
&& (lastKnownDirection != kUp))
|
path.push(botPathUnsanitized.top());
|
||||||
{ lastKnownDirection = kDown; }
|
|
||||||
else if ((directionDelta.x == 1)
|
|
||||||
&& (lastKnownDirection != kRight))
|
|
||||||
{ lastKnownDirection = kLeft; }
|
|
||||||
else if ((directionDelta.x == -1)
|
|
||||||
&& (lastKnownDirection != kLeft))
|
|
||||||
{ lastKnownDirection = kRight; }
|
|
||||||
return lastKnownDirection;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AISnake::UpdateProbability(int snakeSize)
|
|
||||||
{
|
|
||||||
probabilityBFS = 1 - ((double) snakeSize) / 1000;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AISnake::AdjustProbability(double amount)
|
|
||||||
{
|
|
||||||
probabilityBFS += amount;
|
|
||||||
if (probabilityBFS > 1.0) { probabilityBFS = 1.0; }
|
|
||||||
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
|
|
||||||
std::cout << "[Info - AISnake] New BFS probability: " << probabilityBFS << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets a new path for the bot to follow
|
|
||||||
// Uses DFS algorithm
|
|
||||||
void AISnake::GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize)
|
|
||||||
{
|
|
||||||
// Search for food
|
|
||||||
/*
|
|
||||||
BFS(gameBoard, source, boundaries);
|
|
||||||
if (gameBoard[botPathUnsanitized.top().y][botPathUnsanitized.top().x] != 'X') {
|
|
||||||
while (!botPathUnsanitized.empty()) { botPathUnsanitized.pop(); }
|
|
||||||
DFS(gameBoard, source, boundaries);
|
|
||||||
while (botPathUnsanitized.size() > 15) { botPathUnsanitized.pop(); }
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
// Probability-based approach for fun
|
|
||||||
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
|
|
||||||
if (roll <= probabilityBFS) { BFS(gameBoard, source, boundaries); }
|
|
||||||
else { DFS(gameBoard, source, boundaries); }
|
|
||||||
// Create path for food
|
|
||||||
path.push(botPathUnsanitized.top());
|
|
||||||
botPathUnsanitized.pop();
|
botPathUnsanitized.pop();
|
||||||
while (!botPathUnsanitized.empty()) {
|
}
|
||||||
sf::Vector2f deltaVector = botPathUnsanitized.top() - path.top();
|
}
|
||||||
int delta = abs(deltaVector.x) + abs(deltaVector.y);
|
|
||||||
if (delta == 1) {
|
void AISnake::BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
||||||
path.push(botPathUnsanitized.top());
|
std::queue<sf::Vector2f> search;
|
||||||
}
|
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false));
|
||||||
botPathUnsanitized.pop();
|
bool foodFound = false;
|
||||||
}
|
search.push(source);
|
||||||
}
|
while (!search.empty()) {
|
||||||
|
sf::Vector2f currentLocation = search.front();
|
||||||
void AISnake::BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
search.pop();
|
||||||
std::queue<sf::Vector2f> search;
|
if (foodFound) { break; }
|
||||||
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false));
|
if (visited.at(currentLocation.y).at(currentLocation.x)) { continue; }
|
||||||
bool foodFound = false;
|
if (gameBoard.at(currentLocation.y).at(currentLocation.x) == 'X') {
|
||||||
search.push(source);
|
foodFound = true;
|
||||||
while (!search.empty()) {
|
}
|
||||||
sf::Vector2f currentLocation = search.front();
|
botPathUnsanitized.push(currentLocation);
|
||||||
search.pop();
|
std::array<sf::Vector2f, 4> localLocations;
|
||||||
if (foodFound) { break; }
|
localLocations.fill(currentLocation);
|
||||||
if (visited.at(currentLocation.y).at(currentLocation.x)) { continue; }
|
localLocations[0].y += 1;
|
||||||
if (gameBoard.at(currentLocation.y).at(currentLocation.x) == 'X') {
|
localLocations[1].x += 1;
|
||||||
foodFound = true;
|
localLocations[2].y -= 1;
|
||||||
}
|
localLocations[3].x -= 1;
|
||||||
botPathUnsanitized.push(currentLocation);
|
for (auto i : localLocations) {
|
||||||
std::array<sf::Vector2f, 4> localLocations;
|
try {
|
||||||
localLocations.fill(currentLocation);
|
if (gameBoard.at(i.y).at(i.x) == 'X') {
|
||||||
localLocations[0].y += 1;
|
botPathUnsanitized.push(i);
|
||||||
localLocations[1].x += 1;
|
foodFound = true;
|
||||||
localLocations[2].y -= 1;
|
}
|
||||||
localLocations[3].x -= 1;
|
} catch (const std::out_of_range& error) {
|
||||||
for (auto i : localLocations) {
|
continue; // Out of bounds
|
||||||
try {
|
}
|
||||||
if (gameBoard.at(i.y).at(i.x) == 'X') {
|
}
|
||||||
botPathUnsanitized.push(i);
|
for (sf::Vector2f newLocation : localLocations) {
|
||||||
foodFound = true;
|
try {
|
||||||
}
|
if ((!visited.at(newLocation.y).at(newLocation.x))
|
||||||
} catch (const std::out_of_range& error) {
|
&& (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) {
|
||||||
continue; // Out of bounds
|
search.push(newLocation);
|
||||||
}
|
}
|
||||||
}
|
} catch (const std::out_of_range& error) {
|
||||||
for (sf::Vector2f newLocation : localLocations) {
|
continue; // Out of bounds
|
||||||
try {
|
}
|
||||||
if ((!visited.at(newLocation.y).at(newLocation.x))
|
}
|
||||||
&& (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) {
|
visited.at(currentLocation.y).at(currentLocation.x) = true;
|
||||||
search.push(newLocation);
|
}
|
||||||
}
|
}
|
||||||
} catch (const std::out_of_range& error) {
|
|
||||||
continue; // Out of bounds
|
void AISnake::DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
||||||
}
|
std::stack<sf::Vector2f> search;
|
||||||
}
|
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false));
|
||||||
visited.at(currentLocation.y).at(currentLocation.x) = true;
|
bool foodFound = false;
|
||||||
}
|
search.push(source);
|
||||||
}
|
while (!search.empty()) {
|
||||||
|
sf::Vector2f currentLocation = search.top();
|
||||||
void AISnake::DFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries) {
|
search.pop();
|
||||||
std::stack<sf::Vector2f> search;
|
if (foodFound) { break; }
|
||||||
std::vector<std::vector<bool>> visited(boundaries.y, std::vector<bool> (boundaries.x, false));
|
if (visited.at(currentLocation.y).at(currentLocation.x)) { continue; }
|
||||||
bool foodFound = false;
|
if (gameBoard.at(currentLocation.y).at(currentLocation.x) == 'X') {
|
||||||
search.push(source);
|
foodFound = true;
|
||||||
while (!search.empty()) {
|
}
|
||||||
sf::Vector2f currentLocation = search.top();
|
botPathUnsanitized.push(currentLocation);
|
||||||
search.pop();
|
std::array<sf::Vector2f, 4> localLocations;
|
||||||
if (foodFound) { break; }
|
localLocations.fill(currentLocation);
|
||||||
if (visited.at(currentLocation.y).at(currentLocation.x)) { continue; }
|
localLocations[0].y += 1;
|
||||||
if (gameBoard.at(currentLocation.y).at(currentLocation.x) == 'X') {
|
localLocations[1].x += 1;
|
||||||
foodFound = true;
|
localLocations[2].y -= 1;
|
||||||
}
|
localLocations[3].x -= 1;
|
||||||
botPathUnsanitized.push(currentLocation);
|
for (auto i : localLocations) {
|
||||||
std::array<sf::Vector2f, 4> localLocations;
|
try {
|
||||||
localLocations.fill(currentLocation);
|
if (gameBoard.at(i.y).at(i.x) == 'X') {
|
||||||
localLocations[0].y += 1;
|
botPathUnsanitized.push(i);
|
||||||
localLocations[1].x += 1;
|
foodFound = true;
|
||||||
localLocations[2].y -= 1;
|
}
|
||||||
localLocations[3].x -= 1;
|
} catch (const std::out_of_range& error) {
|
||||||
for (auto i : localLocations) {
|
continue; // Out of bounds
|
||||||
try {
|
}
|
||||||
if (gameBoard.at(i.y).at(i.x) == 'X') {
|
}
|
||||||
botPathUnsanitized.push(i);
|
for (sf::Vector2f newLocation : localLocations) {
|
||||||
foodFound = true;
|
try {
|
||||||
}
|
if (newLocation.x < 1 || newLocation.y < 1
|
||||||
} catch (const std::out_of_range& error) {
|
|| newLocation.x > boundaries.x - 2
|
||||||
continue; // Out of bounds
|
|| newLocation.y > boundaries.y - 2) {
|
||||||
}
|
continue;
|
||||||
}
|
|
||||||
for (sf::Vector2f newLocation : localLocations) {
|
}
|
||||||
try {
|
if ((!visited.at(newLocation.y).at(newLocation.x))
|
||||||
if (newLocation.x < 1 || newLocation.y < 1
|
&& (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) {
|
||||||
|| newLocation.x > boundaries.x - 2
|
search.push(newLocation);
|
||||||
|| newLocation.y > boundaries.y - 2) {
|
}
|
||||||
continue;
|
} catch (const std::out_of_range& error) {
|
||||||
|
continue; // Out of bounds
|
||||||
}
|
}
|
||||||
if ((!visited.at(newLocation.y).at(newLocation.x))
|
}
|
||||||
&& (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) {
|
visited.at(currentLocation.y).at(currentLocation.x) = true;
|
||||||
search.push(newLocation);
|
|
||||||
}
|
|
||||||
} catch (const std::out_of_range& error) {
|
|
||||||
continue; // Out of bounds
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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 {
|
std::stack<sf::Vector2f> path;
|
||||||
public:
|
AISnake();
|
||||||
std::stack<sf::Vector2f> path;
|
void GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize);
|
||||||
AISnake();
|
PlayerDirection GetInput(const sf::Vector2f* source);
|
||||||
void GetNewPath(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries, const int snakeSize);
|
void UpdateProbability(int snakeSize);
|
||||||
PlayerDirection GetInput(const sf::Vector2f* source);
|
void AdjustProbability(double amount);
|
||||||
void UpdateProbability(int snakeSize);
|
private:
|
||||||
void AdjustProbability(double amount);
|
double probabilityBFS = 0.500;
|
||||||
private:
|
std::stack<sf::Vector2f> botPathUnsanitized;
|
||||||
double probabilityBFS = 0.500;
|
void BFS(const std::vector< std::vector<char> >& gameBoard, const sf::Vector2f& source, const sf::Vector2f& boundaries);
|
||||||
std::stack<sf::Vector2f> botPathUnsanitized;
|
void DFS(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);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#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;
|
generator.seed(std::random_device{}());
|
||||||
void InitializeGenerator(void)
|
}
|
||||||
{
|
|
||||||
generator.seed(std::random_device{}());
|
// Returns a newly generated number
|
||||||
}
|
int GenerateRandomNumber(int generationLimit)
|
||||||
|
{
|
||||||
// Returns a newly generated number
|
int generatedNumber;
|
||||||
int GenerateRandomNumber(int generationLimit)
|
std::uniform_int_distribution<> distribution(0, generationLimit - 1);
|
||||||
{
|
generatedNumber = distribution(generator);
|
||||||
int generatedNumber;
|
return generatedNumber;
|
||||||
std::uniform_int_distribution<> distribution(0, generationLimit - 1);
|
|
||||||
generatedNumber = distribution(snakeplusplus::generator);
|
|
||||||
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);
|
||||||
|
|
||||||
|
enum PlayerDirection
|
||||||
{
|
{
|
||||||
void InitializeGenerator(void);
|
kNone = 0,
|
||||||
int GenerateRandomNumber(int generationLimit);
|
kLeft = 1,
|
||||||
|
kUp = 2,
|
||||||
enum PlayerDirection
|
kDown = 3,
|
||||||
{
|
kRight = 4
|
||||||
kNone = 0,
|
};
|
||||||
kLeft = 1,
|
|
||||||
kUp = 2,
|
|
||||||
kDown = 3,
|
|
||||||
kRight = 4
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -7,165 +7,163 @@
|
|||||||
#include "playerinterface.hpp"
|
#include "playerinterface.hpp"
|
||||||
#include "gamestate.hpp"
|
#include "gamestate.hpp"
|
||||||
|
|
||||||
namespace snakeplusplus
|
GameEngine::GameEngine()
|
||||||
{
|
{
|
||||||
GameEngine::GameEngine()
|
InitializeGenerator();
|
||||||
{
|
return;
|
||||||
InitializeGenerator();
|
}
|
||||||
return;
|
|
||||||
}
|
void GameEngine::Start()
|
||||||
|
{
|
||||||
void GameEngine::Start()
|
PrepareGameBoard();
|
||||||
{
|
graphics.StartGameWindow();
|
||||||
PrepareGameBoard();
|
Loop();
|
||||||
graphics.StartGameWindow();
|
return;
|
||||||
Loop();
|
}
|
||||||
return;
|
|
||||||
}
|
void GameEngine::Reset()
|
||||||
|
{
|
||||||
void GameEngine::Reset()
|
AddIteration();
|
||||||
{
|
player.Reset();
|
||||||
AddIteration();
|
if (isBotControlled) { while (!bot.path.empty()) { bot.path.pop(); } }
|
||||||
player.Reset();
|
PrepareGameBoard();
|
||||||
if (isBotControlled) { while (!bot.path.empty()) { bot.path.pop(); } }
|
isGameOver = false;
|
||||||
PrepareGameBoard();
|
graphics.SetShowGame((amountPlayed + 1) % 50 == 0);
|
||||||
isGameOver = false;
|
return;
|
||||||
graphics.SetShowGame((amountPlayed + 1) % 50 == 0);
|
}
|
||||||
return;
|
|
||||||
}
|
void GameEngine::AddIteration(void)
|
||||||
|
{
|
||||||
void GameEngine::AddIteration(void)
|
graphics.CheckContinue(isBotControlled);
|
||||||
{
|
if (player.body.size() > 40)
|
||||||
graphics.CheckContinue(isBotControlled);
|
{
|
||||||
if (player.body.size() > 40)
|
UpdateAverage();
|
||||||
{
|
double adjustmentAmount = 0.002;
|
||||||
UpdateAverage();
|
if (average > player.body.size()) { bot.AdjustProbability(adjustmentAmount); }
|
||||||
double adjustmentAmount = 0.002;
|
else { bot.AdjustProbability(-adjustmentAmount); }
|
||||||
if (average > player.body.size()) { bot.AdjustProbability(adjustmentAmount); }
|
}
|
||||||
else { bot.AdjustProbability(-adjustmentAmount); }
|
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] Current average: " << average << 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;
|
||||||
{
|
while (graphics.IsOpen())
|
||||||
int currentScore = 0;
|
{
|
||||||
while (graphics.IsOpen())
|
if (isGameOver) { Reset(); }
|
||||||
{
|
UpdatePlayerSpeed();
|
||||||
if (isGameOver) { Reset(); }
|
PlaceNewSnakePart(MovePlayer());
|
||||||
UpdatePlayerSpeed();
|
RegenerateFood();
|
||||||
PlaceNewSnakePart(MovePlayer());
|
currentScore = player.body.size() * 100;
|
||||||
RegenerateFood();
|
//bot.UpdateProbability(player.body.size());
|
||||||
currentScore = player.body.size() * 100;
|
graphics.DisplayGameState(gameBoard, currentScore);
|
||||||
//bot.UpdateProbability(player.body.size());
|
}
|
||||||
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) {
|
||||||
|
if (!player.speed.x && !player.speed.y) { return; }
|
||||||
void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
|
try {
|
||||||
if (!player.speed.x && !player.speed.y) { return; }
|
char* locationState = &gameBoard.at(location.y).at(location.x);
|
||||||
try {
|
if (*locationState == 'O' && (player.body.size() > 1)) {
|
||||||
char* locationState = &gameBoard.at(location.y).at(location.x);
|
isGameOver = true; // Game should end (Snake touching snake)
|
||||||
if (*locationState == 'O' && (player.body.size() > 1)) {
|
}
|
||||||
isGameOver = true; // Game should end (Snake touching snake)
|
*locationState = 'O';
|
||||||
}
|
player.body.push(locationState);
|
||||||
*locationState = 'O';
|
player.headLocation = location;
|
||||||
player.body.push(locationState);
|
if (playerFood.location != location)
|
||||||
player.headLocation = location;
|
player.Pop();
|
||||||
if (playerFood.location != location)
|
} catch (const std::out_of_range& error) {
|
||||||
player.Pop();
|
isGameOver = true; // Snake ran into edge
|
||||||
} catch (const std::out_of_range& error) {
|
}
|
||||||
isGameOver = true; // Snake ran into edge
|
return;
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
// Generates new food until not colliding with player
|
||||||
|
void GameEngine::RegenerateFood()
|
||||||
// Generates new food until not colliding with player
|
{
|
||||||
void GameEngine::RegenerateFood()
|
// Generate a new food location if the current one is occupied
|
||||||
{
|
while (gameBoard.at(playerFood.location.y).at(playerFood.location.x) == 'O') {
|
||||||
// Generate a new food location if the current one is occupied
|
playerFood.GenerateNewFood(GetGameBoundaries());
|
||||||
while (gameBoard.at(playerFood.location.y).at(playerFood.location.x) == 'O') {
|
}
|
||||||
playerFood.GenerateNewFood(GetGameBoundaries());
|
|
||||||
}
|
// Update the game board with the new food location
|
||||||
|
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
||||||
// Update the game board with the new food location
|
}
|
||||||
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
|
||||||
}
|
|
||||||
|
void GameEngine::PrepareGameBoard(void)
|
||||||
|
{
|
||||||
void GameEngine::PrepareGameBoard(void)
|
gameBoard.clear();
|
||||||
{
|
sf::Vector2f boardDimensions = GetGameBoundaries();
|
||||||
gameBoard.clear();
|
gameBoard.resize(boardDimensions.y, std::vector<char> (boardDimensions.x, ' '));
|
||||||
sf::Vector2f boardDimensions = GetGameBoundaries();
|
// Snake setup
|
||||||
gameBoard.resize(boardDimensions.y, std::vector<char> (boardDimensions.x, ' '));
|
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
|
||||||
// Snake setup
|
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
|
||||||
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
|
{
|
||||||
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
|
char* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
|
||||||
{
|
player.body.push(locationState);
|
||||||
char* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
|
*locationState = 'O';
|
||||||
player.body.push(locationState);
|
}
|
||||||
*locationState = 'O';
|
// Food setup
|
||||||
}
|
playerFood.GenerateNewFood(boardDimensions);
|
||||||
// Food setup
|
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
||||||
playerFood.GenerateNewFood(boardDimensions);
|
return;
|
||||||
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
}
|
||||||
return;
|
|
||||||
}
|
void GameEngine::UpdatePlayerSpeed(void)
|
||||||
|
{
|
||||||
void GameEngine::UpdatePlayerSpeed(void)
|
PlayerDirection controller;
|
||||||
{
|
if (isBotControlled) {
|
||||||
PlayerDirection controller;
|
if (bot.path.empty()) {
|
||||||
if (isBotControlled) {
|
bot.GetNewPath(gameBoard, player.headLocation, GetGameBoundaries(), player.body.size());
|
||||||
if (bot.path.empty()) {
|
}
|
||||||
bot.GetNewPath(gameBoard, player.headLocation, GetGameBoundaries(), player.body.size());
|
controller = bot.GetInput(&player.headLocation);
|
||||||
}
|
}
|
||||||
controller = bot.GetInput(&player.headLocation);
|
else { controller = GetPlayerInput(); }
|
||||||
}
|
switch (controller) {
|
||||||
else { controller = GetPlayerInput(); }
|
case kUp:
|
||||||
switch (controller) {
|
if (player.speed.y == kUnitSpeed) { break; }
|
||||||
case kUp:
|
player.speed.x = 0;
|
||||||
if (player.speed.y == kUnitSpeed) { break; }
|
player.speed.y = -kUnitSpeed;
|
||||||
player.speed.x = 0;
|
break;
|
||||||
player.speed.y = -kUnitSpeed;
|
case kLeft:
|
||||||
break;
|
if (player.speed.x == kUnitSpeed) { break; }
|
||||||
case kLeft:
|
player.speed.x = -kUnitSpeed;
|
||||||
if (player.speed.x == kUnitSpeed) { break; }
|
player.speed.y = 0;
|
||||||
player.speed.x = -kUnitSpeed;
|
break;
|
||||||
player.speed.y = 0;
|
case kRight:
|
||||||
break;
|
if (player.speed.x == -kUnitSpeed) { break; }
|
||||||
case kRight:
|
player.speed.x = kUnitSpeed;
|
||||||
if (player.speed.x == -kUnitSpeed) { break; }
|
player.speed.y = 0;
|
||||||
player.speed.x = kUnitSpeed;
|
break;
|
||||||
player.speed.y = 0;
|
case kDown:
|
||||||
break;
|
if (player.speed.y == -kUnitSpeed) { break; }
|
||||||
case kDown:
|
player.speed.x = 0;
|
||||||
if (player.speed.y == -kUnitSpeed) { break; }
|
player.speed.y = kUnitSpeed;
|
||||||
player.speed.x = 0;
|
break;
|
||||||
player.speed.y = kUnitSpeed;
|
default:
|
||||||
break;
|
break;
|
||||||
default:
|
}
|
||||||
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,42 +3,40 @@
|
|||||||
#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;
|
||||||
Food playerFood;
|
Food playerFood;
|
||||||
AISnake bot;
|
AISnake bot;
|
||||||
bool isGameOver = 0;
|
bool isGameOver = 0;
|
||||||
bool isBotControlled = 1;
|
bool isBotControlled = 1;
|
||||||
void DisplayEndScreen(void);
|
void DisplayEndScreen(void);
|
||||||
void Loop(void);
|
void Loop(void);
|
||||||
sf::Vector2f MovePlayer(void);
|
sf::Vector2f MovePlayer(void);
|
||||||
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 UpdateAverage();
|
void UpdateAverage();
|
||||||
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,173 +2,170 @@
|
|||||||
#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)
|
||||||
{
|
|| sf::Keyboard::isKeyPressed(sf::Keyboard::A))
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)
|
return kLeft;
|
||||||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::A))
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
|
||||||
return kLeft;
|
|| sf::Keyboard::isKeyPressed(sf::Keyboard::W))
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
|
return kUp;
|
||||||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::W))
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)
|
||||||
return kUp;
|
|| sf::Keyboard::isKeyPressed(sf::Keyboard::S))
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)
|
return kDown;
|
||||||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::S))
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)
|
||||||
return kDown;
|
|| sf::Keyboard::isKeyPressed(sf::Keyboard::D))
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)
|
return kRight;
|
||||||
|| sf::Keyboard::isKeyPressed(sf::Keyboard::D))
|
return kNone;
|
||||||
return kRight;
|
}
|
||||||
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;
|
||||||
float kBoardHeight = kHeight / kGridSize;
|
float kBoardHeight = kHeight / kGridSize;
|
||||||
gameBoundaries = sf::Vector2f(kBoardWidth, kBoardHeight);
|
gameBoundaries = sf::Vector2f(kBoardWidth, kBoardHeight);
|
||||||
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; }
|
||||||
|
DisplayEndScreen();
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
if (isBotControlled) { return; }
|
gameWindow.pollEvent(event);
|
||||||
DisplayEndScreen();
|
if ((event.type == sf::Event::Closed)
|
||||||
while (true)
|
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
||||||
{
|
{
|
||||||
gameWindow.pollEvent(event);
|
gameWindow.close();
|
||||||
if ((event.type == sf::Event::Closed)
|
return;
|
||||||
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
|
||||||
{
|
|
||||||
gameWindow.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) { return; }
|
|
||||||
sf::sleep(delay);
|
|
||||||
}
|
}
|
||||||
}
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) { return; }
|
||||||
|
|
||||||
void PlayerOutput::DisplayEndScreen(void)
|
|
||||||
{
|
|
||||||
gameWindow.clear();
|
|
||||||
sf::Vector2f textPosition(gameBoundaries);
|
|
||||||
textPosition.x = textPosition.x / 2;
|
|
||||||
textPosition.y = textPosition.y / 2;
|
|
||||||
sf::Font font;
|
|
||||||
font.loadFromFile("Arial.ttf");
|
|
||||||
sf::Text gameOverText("Game Over\nPress 'Enter' to play again", font);
|
|
||||||
gameOverText.setPosition(textPosition);
|
|
||||||
gameWindow.draw(gameOverText);
|
|
||||||
gameWindow.display();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PlayerOutput::DisplayScore(int score) {
|
|
||||||
sf::Vector2f textPosition(gameBoundaries);
|
|
||||||
textPosition.x = textPosition.x / 2;
|
|
||||||
textPosition.y = textPosition.y / 2;
|
|
||||||
sf::Font font;
|
|
||||||
font.loadFromFile("Arial.ttf");
|
|
||||||
std::string text = "Score: " + std::to_string(score);
|
|
||||||
sf::Text ScoreText(text, font);
|
|
||||||
ScoreText.setPosition(textPosition);
|
|
||||||
gameWindow.draw(ScoreText);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score)
|
|
||||||
{
|
|
||||||
CheckWindowEvents();
|
|
||||||
if (delay == sf::milliseconds(0)) { return; }
|
|
||||||
char* letterOnBoard;
|
|
||||||
for (float y = 0; y < gameBoundaries.y; y++)
|
|
||||||
{
|
|
||||||
for (float x = 0; x < gameBoundaries.x; x++)
|
|
||||||
{
|
|
||||||
letterOnBoard = &gameBoard.at(y).at(x);
|
|
||||||
switch (*letterOnBoard)
|
|
||||||
{
|
|
||||||
case 'O':
|
|
||||||
DrawSnake(sf::Vector2f(x, y));
|
|
||||||
break;
|
|
||||||
case 'X':
|
|
||||||
DrawFood(sf::Vector2f(x,y));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
DrawEmpty(sf::Vector2f(x,y));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DisplayScore(score);
|
|
||||||
gameWindow.display();
|
|
||||||
sf::sleep(delay);
|
sf::sleep(delay);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PlayerOutput::StartGameWindow(void)
|
|
||||||
{
|
|
||||||
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
|
||||||
isWindowAlive = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PlayerOutput::SetShowGame(bool isShowing) {
|
|
||||||
if (isShowing) { delay = sf::milliseconds(2); }
|
|
||||||
else { delay = sf::milliseconds(0); }
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PlayerOutput::CheckWindowEvents(void)
|
|
||||||
{
|
|
||||||
while (gameWindow.pollEvent(event))
|
|
||||||
{
|
|
||||||
if ((event.type == sf::Event::Closed)
|
|
||||||
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
|
||||||
gameWindow.close();
|
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Equal)) {
|
|
||||||
if (delay > sf::milliseconds(16)) { continue; }
|
|
||||||
delay += sf::milliseconds(1);
|
|
||||||
}
|
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Hyphen)) {
|
|
||||||
if (delay == sf::milliseconds(0)) { continue; }
|
|
||||||
delay -= sf::milliseconds(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void PlayerOutput::DrawEmpty(sf::Vector2f location)
|
|
||||||
{
|
|
||||||
location *= static_cast<float>(kGridSize);
|
|
||||||
drawObject.setPosition(location);
|
|
||||||
drawObject.setFillColor(sf::Color::Black);
|
|
||||||
gameWindow.draw(drawObject);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PlayerOutput::DrawFood(sf::Vector2f location)
|
|
||||||
{
|
|
||||||
location *= static_cast<float>(kGridSize);
|
|
||||||
drawObject.setPosition(location);
|
|
||||||
drawObject.setFillColor(sf::Color::Red);
|
|
||||||
gameWindow.draw(drawObject);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PlayerOutput::DrawSnake(sf::Vector2f location)
|
|
||||||
{
|
|
||||||
location *= static_cast<float>(kGridSize);
|
|
||||||
drawObject.setPosition(location);
|
|
||||||
drawObject.setFillColor(sf::Color::Green);
|
|
||||||
gameWindow.draw(drawObject);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PlayerOutput::DisplayEndScreen(void)
|
||||||
|
{
|
||||||
|
gameWindow.clear();
|
||||||
|
sf::Vector2f textPosition(gameBoundaries);
|
||||||
|
textPosition.x = textPosition.x / 2;
|
||||||
|
textPosition.y = textPosition.y / 2;
|
||||||
|
sf::Font font;
|
||||||
|
font.loadFromFile("Arial.ttf");
|
||||||
|
sf::Text gameOverText("Game Over\nPress 'Enter' to play again", font);
|
||||||
|
gameOverText.setPosition(textPosition);
|
||||||
|
gameWindow.draw(gameOverText);
|
||||||
|
gameWindow.display();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerOutput::DisplayScore(int score) {
|
||||||
|
sf::Vector2f textPosition(gameBoundaries);
|
||||||
|
textPosition.x = textPosition.x / 2;
|
||||||
|
textPosition.y = textPosition.y / 2;
|
||||||
|
sf::Font font;
|
||||||
|
font.loadFromFile("Arial.ttf");
|
||||||
|
std::string text = "Score: " + std::to_string(score);
|
||||||
|
sf::Text ScoreText(text, font);
|
||||||
|
ScoreText.setPosition(textPosition);
|
||||||
|
gameWindow.draw(ScoreText);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score)
|
||||||
|
{
|
||||||
|
CheckWindowEvents();
|
||||||
|
if (delay == sf::milliseconds(0)) { return; }
|
||||||
|
char* letterOnBoard;
|
||||||
|
for (float y = 0; y < gameBoundaries.y; y++)
|
||||||
|
{
|
||||||
|
for (float x = 0; x < gameBoundaries.x; x++)
|
||||||
|
{
|
||||||
|
letterOnBoard = &gameBoard.at(y).at(x);
|
||||||
|
switch (*letterOnBoard)
|
||||||
|
{
|
||||||
|
case 'O':
|
||||||
|
DrawSnake(sf::Vector2f(x, y));
|
||||||
|
break;
|
||||||
|
case 'X':
|
||||||
|
DrawFood(sf::Vector2f(x,y));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
DrawEmpty(sf::Vector2f(x,y));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DisplayScore(score);
|
||||||
|
gameWindow.display();
|
||||||
|
sf::sleep(delay);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerOutput::StartGameWindow(void)
|
||||||
|
{
|
||||||
|
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
|
||||||
|
isWindowAlive = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerOutput::SetShowGame(bool isShowing) {
|
||||||
|
if (isShowing) { delay = sf::milliseconds(2); }
|
||||||
|
else { delay = sf::milliseconds(0); }
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerOutput::CheckWindowEvents(void)
|
||||||
|
{
|
||||||
|
while (gameWindow.pollEvent(event))
|
||||||
|
{
|
||||||
|
if ((event.type == sf::Event::Closed)
|
||||||
|
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
||||||
|
gameWindow.close();
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Equal)) {
|
||||||
|
if (delay > sf::milliseconds(16)) { continue; }
|
||||||
|
delay += sf::milliseconds(1);
|
||||||
|
}
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Hyphen)) {
|
||||||
|
if (delay == sf::milliseconds(0)) { continue; }
|
||||||
|
delay -= sf::milliseconds(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerOutput::DrawEmpty(sf::Vector2f location)
|
||||||
|
{
|
||||||
|
location *= static_cast<float>(kGridSize);
|
||||||
|
drawObject.setPosition(location);
|
||||||
|
drawObject.setFillColor(sf::Color::Black);
|
||||||
|
gameWindow.draw(drawObject);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerOutput::DrawFood(sf::Vector2f location)
|
||||||
|
{
|
||||||
|
location *= static_cast<float>(kGridSize);
|
||||||
|
drawObject.setPosition(location);
|
||||||
|
drawObject.setFillColor(sf::Color::Red);
|
||||||
|
gameWindow.draw(drawObject);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerOutput::DrawSnake(sf::Vector2f location)
|
||||||
|
{
|
||||||
|
location *= static_cast<float>(kGridSize);
|
||||||
|
drawObject.setPosition(location);
|
||||||
|
drawObject.setFillColor(sf::Color::Green);
|
||||||
|
gameWindow.draw(drawObject);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
@ -6,34 +6,31 @@
|
|||||||
|
|
||||||
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);
|
||||||
void CheckContinue(bool isBotControlled);
|
void CheckContinue(bool isBotControlled);
|
||||||
void DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score);
|
void DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score);
|
||||||
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);
|
||||||
void DrawFood(sf::Vector2f location);
|
void DrawFood(sf::Vector2f location);
|
||||||
void DrawSnake(sf::Vector2f location);
|
void DrawSnake(sf::Vector2f location);
|
||||||
sf::RenderWindow gameWindow;
|
sf::RenderWindow gameWindow;
|
||||||
sf::VideoMode gameVideoSettings;
|
sf::VideoMode gameVideoSettings;
|
||||||
sf::RectangleShape drawObject;
|
sf::RectangleShape drawObject;
|
||||||
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.pop();
|
||||||
*(body.front()) = ' ';
|
return;
|
||||||
body.pop();
|
}
|
||||||
return;
|
|
||||||
}
|
void Snake::Reset(void)
|
||||||
|
{
|
||||||
void Snake::Reset(void)
|
while (!body.empty()) Pop();
|
||||||
{
|
speed.x = 0;
|
||||||
while (!body.empty()) Pop();
|
speed.y = 0;
|
||||||
speed.x = 0;
|
return;
|
||||||
speed.y = 0;
|
}
|
||||||
return;
|
|
||||||
}
|
// Returns a new food object for the snakeFood
|
||||||
|
void Food::GenerateNewFood(sf::Vector2f boundaries)
|
||||||
// Returns a new food object for the snakeFood
|
{
|
||||||
void Food::GenerateNewFood(sf::Vector2f boundaries)
|
location.x = GenerateRandomNumber(boundaries.x);
|
||||||
{
|
location.y = GenerateRandomNumber(boundaries.y);
|
||||||
location.x = GenerateRandomNumber(boundaries.x);
|
return;
|
||||||
location.y = GenerateRandomNumber(boundaries.y);
|
|
||||||
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:
|
||||||
{
|
sf::Vector2f headLocation;
|
||||||
public:
|
sf::Vector2f speed;
|
||||||
sf::Vector2f headLocation;
|
std::queue<char*> body;
|
||||||
sf::Vector2f speed;
|
void Pop(void);
|
||||||
std::queue<char*> body;
|
void Reset(void);
|
||||||
void Pop(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