core: formatting cleanup

This commit is contained in:
Trianta 2024-08-21 02:59:30 -05:00
parent 01f24e4ebb
commit 677466d99e
9 changed files with 137 additions and 169 deletions

View File

@ -13,72 +13,76 @@ AISnake::AISnake(void) {
;
}
PlayerDirection AISnake::GetInput(void)
{
PlayerDirection AISnake::GetInput(void) {
sf::Vector2f source(g_pEngine->GetHeadLocation());
//if (g_pEngine->state.m_bSmart && path.empty())
// return CurrentBestDecision();
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();
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; }
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;
}
void AISnake::UpdateProbability(int snakeSize)
{
void AISnake::UpdateProbability(int snakeSize) {
probabilityBFS = 1 - ((double) snakeSize) / 1000;
return;
}
void AISnake::AdjustProbability(double amount)
{
void AISnake::AdjustProbability(double amount) {
probabilityBFS += amount;
if (probabilityBFS > 1.0) { probabilityBFS = 1.0; }
if (probabilityBFS < 0.0) { probabilityBFS = 0.0; }
if (probabilityBFS > 1.0)
probabilityBFS = 1.0;
if (probabilityBFS < 0.0)
probabilityBFS = 0.0;
return;
}
void AISnake::AddIteration(const int size)
{
if (size > 40)
{
void AISnake::AddIteration(const int size) {
if (size > 40) {
UpdateAverage(size);
double adjustmentAmount = 0.002;
if (average > size) { AdjustProbability(adjustmentAmount); }
else { AdjustProbability(-adjustmentAmount); }
if (average > size)
AdjustProbability(adjustmentAmount);
else
AdjustProbability(-adjustmentAmount);
}
std::cout << "[LOG - AI] Current average: " << average << std::endl;
std::cout << "[LOG - AI] Previous iteration size: " << size << std::endl;
}
void AISnake::ResetPath(void) {
while (!path.empty()) { path.pop(); }
while (!path.empty())
path.pop();
}
// Gets a new path for the bot to follow
// Uses DFS algorithm
void AISnake::GetNewPath(void)
{
void AISnake::GetNewPath(void) {
// Search for food
// Probability-based approach for fun
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
if (roll <= probabilityBFS) { BFS(); }
else { DFS(); }
if (roll <= probabilityBFS)
BFS();
else
DFS();
UnvisitBoard();
if (pathFailed) {
pathFailed = false;
EmptyPath();

View File

@ -3,14 +3,13 @@
#include "common.hpp"
std::default_random_engine generator;
void InitializeGenerator(void)
{
void InitializeGenerator(void) {
generator.seed(std::random_device{}());
}
// Returns a newly generated number
int GenerateRandomNumber(int generationLimit)
{
int GenerateRandomNumber(int generationLimit) {
int generatedNumber;
std::uniform_int_distribution<> distribution(0, generationLimit - 1);
generatedNumber = distribution(generator);

View File

@ -4,8 +4,7 @@
void InitializeGenerator(void);
int GenerateRandomNumber(int generationLimit);
enum PlayerDirection
{
enum PlayerDirection {
kNone = 0,
kLeft = 1,
kUp = 2,
@ -14,7 +13,7 @@ enum PlayerDirection
};
struct GameSpace {
GameSpace();
GameSpace(void);
unsigned char m_bFood : 1 = 0;
unsigned char m_bSnake : 1 = 0;
unsigned char m_bVisited : 1 = 0; // Used for BFS/DFS

View File

@ -6,23 +6,18 @@
#include "playerinterface.hpp"
#include "gamestate.hpp"
GameEngine::GameEngine()
{
GameEngine::GameEngine(void) {
InitializeGenerator();
return;
}
void GameEngine::Start()
{
void GameEngine::Start(void) {
PrepareGameBoard();
if (!state.m_bNoDisplay)
graphics.StartGameWindow();
Loop();
return;
}
void GameEngine::Reset()
{
void GameEngine::Reset(void) {
if (!state.m_bIsBotControlled)
graphics.CheckContinue();
else
@ -30,25 +25,25 @@ void GameEngine::Reset()
player.Reset();
PrepareGameBoard();
state.m_bIsGameOver = false;
if (state.m_bIsBotControlled) {
if (!state.m_bSmart) {
while (!bot.path.empty())
bot.path.pop();
}
if (state.m_bNoDisplay)
graphics.SetShowGame(false);
if (state.m_bSkipIterations)
graphics.SetShowGame((bot.amountPlayed + 1) % 50 == 0);
graphics.SetShowGame(true);
if (!state.m_bIsBotControlled)
return;
if (!state.m_bSmart) {
while (!bot.path.empty())
bot.path.pop();
}
if (state.m_bNoDisplay)
graphics.SetShowGame(false);
if (state.m_bSkipIterations)
graphics.SetShowGame((bot.amountPlayed + 1) % 50 == 0);
// TODO: Replace with value to force this effect
graphics.SetShowGame(true);
}
void GameEngine::Loop(void)
{
void GameEngine::Loop(void) {
int currentScore = 0;
while (graphics.IsOpen() || state.m_bNoDisplay)
{
if (state.m_bIsGameOver) { Reset(); }
while (graphics.IsOpen() || state.m_bNoDisplay) {
if (state.m_bIsGameOver)
Reset();
UpdatePlayerSpeed();
PlaceNewSnakePart(MovePlayer());
RegenerateFood();
@ -56,17 +51,14 @@ void GameEngine::Loop(void)
if (!state.m_bNoDisplay)
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);
}
sf::Vector2f GameEngine::GetGameBoundaries(void)
{
sf::Vector2f GameEngine::GetGameBoundaries(void) {
return graphics.gameBoundaries;
}
@ -97,12 +89,12 @@ sf::Vector2f GameEngine::GetFoodLocation(void) {
}
void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
if (!player.speed.x && !player.speed.y) { return; }
if (!player.speed.x && !player.speed.y)
return;
try {
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)
}
locationState->m_bSnake = true;
player.body.push(locationState);
player.headLocation = location;
@ -116,70 +108,70 @@ void GameEngine::PlaceNewSnakePart(sf::Vector2f location) {
} catch (const std::out_of_range& error) {
state.m_bIsGameOver = true; // Snake ran into edge
}
return;
}
// 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
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());
}
// Update the game board with the new food location
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();
sf::Vector2f boardDimensions = GetGameBoundaries();
gameBoard.resize(boardDimensions.y, std::vector<GameSpace>(boardDimensions.x));
// Snake setup
player.headLocation.x = GenerateRandomNumber(boardDimensions.x);
player.headLocation.y = GenerateRandomNumber(boardDimensions.y);
{
GameSpace* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
player.body.push(locationState);
locationState->m_bSnake = true;
}
GameSpace* locationState = &gameBoard.at(player.headLocation.y).at(player.headLocation.x);
player.body.push(locationState);
locationState->m_bSnake = true;
// Food setup
playerFood.GenerateNewFood(boardDimensions);
gameBoard.at(playerFood.location.y).at(playerFood.location.x).m_bFood = true;
return;
}
void GameEngine::UpdatePlayerSpeed(void)
{
void GameEngine::UpdatePlayerSpeed(void) {
PlayerDirection controller;
if (state.m_bIsBotControlled) {
if (bot.path.empty()) {
if (bot.path.empty())
bot.GetNewPath();
}
controller = bot.GetInput();
}
else { controller = GetPlayerInput(); }
controller = bot.GetInput();
} else
controller = GetPlayerInput();
switch (controller) {
case kUp:
if (player.speed.y == kUnitSpeed) { break; }
if (player.speed.y == kUnitSpeed)
break;
player.speed.x = 0;
player.speed.y = -kUnitSpeed;
break;
case kLeft:
if (player.speed.x == kUnitSpeed) { break; }
if (player.speed.x == kUnitSpeed)
break;
player.speed.x = -kUnitSpeed;
player.speed.y = 0;
break;
case kRight:
if (player.speed.x == -kUnitSpeed) { break; }
if (player.speed.x == -kUnitSpeed)
break;
player.speed.x = kUnitSpeed;
player.speed.y = 0;
break;
case kDown:
if (player.speed.y == -kUnitSpeed) { break; }
if (player.speed.y == -kUnitSpeed)
break;
player.speed.x = 0;
player.speed.y = kUnitSpeed;
break;

View File

@ -11,10 +11,9 @@
const int kUnitSpeed = 1;
class GameEngine
{
class GameEngine {
public:
GameEngine();
GameEngine(void);
void Start(void);
void Reset(void);
sf::Vector2f GetGameBoundaries(void);
@ -43,7 +42,7 @@ private:
void PlaceNewSnakePart(sf::Vector2f location);
void RegenerateFood(void);
void PrepareGameBoard(void);
void UpdatePlayerSpeed();
void UpdatePlayerSpeed(void);
};
inline std::unique_ptr<GameEngine> g_pEngine;

View File

@ -1,32 +1,28 @@
#include "playerinterface.hpp"
#include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <iostream>
PlayerDirection GetPlayerInput(void)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::A))
PlayerDirection GetPlayerInput(void) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::A)))
return kLeft;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::W))
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::W)))
return kUp;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::S))
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::S)))
return kDown;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::D))
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::D)))
return kRight;
return kNone;
}
bool PlayerOutput::IsOpen(void)
{
bool PlayerOutput::IsOpen(void) {
return isWindowAlive;
}
PlayerOutput::PlayerOutput(void)
{
PlayerOutput::PlayerOutput(void) {
float kWidth = 1025;
float kHeight = 725;
float kBoardWidth = kWidth / kGridSize;
@ -37,26 +33,23 @@ PlayerOutput::PlayerOutput(void)
return;
}
void PlayerOutput::CheckContinue()
{
void PlayerOutput::CheckContinue(void) {
DisplayEndScreen();
while (true)
{
while (true) {
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();
isWindowAlive = false;
return;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) { return; }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
return;
sf::sleep(delay);
}
}
void PlayerOutput::DisplayEndScreen(void)
{
void PlayerOutput::DisplayEndScreen(void) {
gameWindow.clear();
sf::Vector2f textPosition(gameBoundaries);
textPosition.x = textPosition.x / 2;
@ -80,24 +73,20 @@ void PlayerOutput::DisplayScore(int score) {
sf::Text ScoreText(text, font);
ScoreText.setPosition(textPosition);
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();
if (delay == sf::milliseconds(0)) { return; }
char* letterOnBoard;
for (float y = 0; y < gameBoundaries.y; y++)
{
for (float x = 0; x < gameBoundaries.x; x++)
{
for (float y = 0; y < gameBoundaries.y; y++) {
for (float x = 0; x < gameBoundaries.x; x++) {
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)
DrawFood(sf::Vector2f(x,y));
DrawFood(sf::Vector2f(x,y));
else
DrawEmpty(sf::Vector2f(x,y));
DrawEmpty(sf::Vector2f(x,y));
}
}
DisplayScore(score);
@ -106,25 +95,22 @@ void PlayerOutput::DisplayGameState(std::vector< std::vector<GameSpace> >& gameB
return;
}
void PlayerOutput::StartGameWindow(void)
{
void PlayerOutput::StartGameWindow(void) {
gameWindow.create(gameVideoSettings, "SnakePlusPlus");
isWindowAlive = true;
return;
}
void PlayerOutput::SetShowGame(bool isShowing) {
if (isShowing) { delay = sf::milliseconds(5); }
else { delay = sf::milliseconds(0); }
return;
if (isShowing)
delay = sf::milliseconds(5);
else
delay = sf::milliseconds(0);
}
void PlayerOutput::CheckWindowEvents(void)
{
while (gameWindow.pollEvent(event))
{
if ((event.type == sf::Event::Closed)
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) {
void PlayerOutput::CheckWindowEvents(void) {
while (gameWindow.pollEvent(event)) {
if ((event.type == sf::Event::Closed) ||
(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) {
gameWindow.close();
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);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Black);
gameWindow.draw(drawObject);
return;
}
void PlayerOutput::DrawFood(sf::Vector2f location)
{
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)
{
void PlayerOutput::DrawSnake(sf::Vector2f location) {
location *= static_cast<float>(kGridSize);
drawObject.setPosition(location);
drawObject.setFillColor(sf::Color::Green);
gameWindow.draw(drawObject);
return;
}

View File

@ -8,13 +8,12 @@ const int kGridSize = 25;
PlayerDirection GetPlayerInput(void);
class PlayerOutput
{
class PlayerOutput {
public:
sf::Vector2f gameBoundaries;
PlayerOutput(void);
bool IsOpen(void);
void CheckContinue();
void CheckContinue(void);
void DisplayGameState(std::vector< std::vector<GameSpace> >& gameBoard, int score);
void DisplayScore(int score);
void StartGameWindow(void);

View File

@ -3,25 +3,23 @@
#include <SFML/Graphics.hpp>
#include "common.hpp"
#include "snake.hpp"
void Snake::Pop(void)
{
void Snake::Pop(void) {
body.front()->m_bSnake = false;
body.pop();
return;
}
void Snake::Reset(void)
{
while (!body.empty()) Pop();
void Snake::Reset(void) {
while (!body.empty())
Pop();
speed.x = 0;
speed.y = 0;
return;
}
// 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.y = GenerateRandomNumber(boundaries.y);
return;

View File

@ -6,8 +6,7 @@
#include <queue>
#include "common.hpp"
struct Snake
{
struct Snake {
public:
sf::Vector2f headLocation;
sf::Vector2f speed;
@ -16,8 +15,7 @@ public:
void Reset(void);
};
struct Food
{
struct Food {
public:
sf::Vector2f location;
void GenerateNewFood(sf::Vector2f boundaries);