Compare commits
No commits in common. "ai" and "master" have entirely different histories.
@ -6,106 +6,93 @@
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <stdexcept>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
PlayerDirection lastKnownDirection = kNone;
|
||||
|
||||
AISnake::AISnake(void) {
|
||||
AISnake::AISnake() {
|
||||
;
|
||||
}
|
||||
|
||||
PlayerDirection AISnake::GetInput(void) {
|
||||
sf::Vector2f source(g_pEngine->GetHeadLocation());
|
||||
PlayerDirection AISnake::GetInput(const sf::Vector2f* source)
|
||||
{
|
||||
sf::Vector2f directionDelta;
|
||||
|
||||
while (!path.empty() && source == path.top())
|
||||
path.pop();
|
||||
if (path.empty()) {
|
||||
if (g_pEngine->state.m_bSmart) {
|
||||
return CurrentBestDecision();
|
||||
} else
|
||||
path.push(GetAnyOpenPath());
|
||||
}
|
||||
|
||||
try {
|
||||
sf::Vector2f next = path.top();
|
||||
if (g_pEngine->gameBoard.at(next.y).at(next.x).m_bSnake)
|
||||
next = GetAnyOpenPath();
|
||||
directionDelta = source - next;
|
||||
path.pop();
|
||||
} catch (const std::out_of_range& error) {
|
||||
directionDelta = source - GetAnyOpenPath(); // Out of bounds
|
||||
EmptyPath();
|
||||
}
|
||||
|
||||
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 (!source)
|
||||
return kUp;
|
||||
while (*source == path.top() && !path.empty()) { path.pop(); }
|
||||
if (path.empty()) { path.push(GetAnyOpenPath(*source)); }
|
||||
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;
|
||||
}
|
||||
|
||||
void AISnake::UpdateProbability(int snakeSize) {
|
||||
thresholdDFS = 1 - ((double) snakeSize) / 1000;
|
||||
void AISnake::UpdateProbability(int snakeSize)
|
||||
{
|
||||
probabilityBFS = 1 - ((double) snakeSize) / 1000;
|
||||
return;
|
||||
}
|
||||
|
||||
void AISnake::AdjustProbability(double amount) {
|
||||
thresholdDFS += amount;
|
||||
if (thresholdDFS > 1.0)
|
||||
thresholdDFS = 1.0;
|
||||
if (thresholdDFS < 0.0)
|
||||
thresholdDFS = 0.0;
|
||||
void AISnake::AdjustProbability(double amount)
|
||||
{
|
||||
probabilityBFS += amount;
|
||||
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(const sf::Vector2f& source)
|
||||
{
|
||||
// Search for food
|
||||
// Probability-based approach for fun
|
||||
double roll = ((double) GenerateRandomNumber(RAND_MAX)) / ((double) RAND_MAX);
|
||||
if (roll <= thresholdDFS)
|
||||
BFS();
|
||||
else
|
||||
DFS();
|
||||
|
||||
if (roll <= probabilityBFS) { BFS(source); }
|
||||
else { DFS(source); }
|
||||
UnvisitBoard();
|
||||
|
||||
if (pathFailed) {
|
||||
pathFailed = false;
|
||||
EmptyPath();
|
||||
path.push(GetAnyOpenPath(source));
|
||||
} else {
|
||||
TrimPath();
|
||||
if (path.empty())
|
||||
path.push(GetAnyOpenPath());
|
||||
path.push(GetAnyOpenPath(source));
|
||||
}
|
||||
}
|
||||
|
||||
void AISnake::BFS(void) {
|
||||
void AISnake::BFS(const sf::Vector2f& source) {
|
||||
std::queue<sf::Vector2f> search;
|
||||
search.push(g_pEngine->GetHeadLocation());
|
||||
search.push(source);
|
||||
while (!search.empty()) {
|
||||
sf::Vector2f currentLocation = search.front();
|
||||
search.pop();
|
||||
@ -125,6 +112,8 @@ void AISnake::BFS(void) {
|
||||
botPathUnsanitized.push(nearby);
|
||||
return;
|
||||
}
|
||||
if (nearby.x < 1 || nearby.y < 1)
|
||||
continue;
|
||||
if (space->m_bVisited)
|
||||
continue;
|
||||
if (space->m_bSnake)
|
||||
@ -139,9 +128,9 @@ void AISnake::BFS(void) {
|
||||
pathFailed = true;
|
||||
}
|
||||
|
||||
void AISnake::DFS(void) {
|
||||
void AISnake::DFS(const sf::Vector2f& source) {
|
||||
std::stack<sf::Vector2f> search;
|
||||
search.push(g_pEngine->GetHeadLocation());
|
||||
search.push(source);
|
||||
while (!search.empty()) {
|
||||
sf::Vector2f currentLocation = search.top();
|
||||
search.pop();
|
||||
@ -161,6 +150,8 @@ void AISnake::DFS(void) {
|
||||
botPathUnsanitized.push(nearby);
|
||||
return;
|
||||
}
|
||||
if (nearby.x < 1 || nearby.x > g_pEngine->GetGameBoundaries().x - 2)
|
||||
continue;
|
||||
if (space->m_bVisited)
|
||||
continue;
|
||||
if (space->m_bSnake)
|
||||
@ -175,10 +166,10 @@ void AISnake::DFS(void) {
|
||||
pathFailed = true;
|
||||
}
|
||||
|
||||
sf::Vector2f AISnake::GetAnyOpenPath(void) {
|
||||
sf::Vector2f AISnake::GetAnyOpenPath(const sf::Vector2f& source) {
|
||||
sf::Vector2f bail;
|
||||
std::array<sf::Vector2f, 4> paths;
|
||||
paths.fill(g_pEngine->GetHeadLocation());
|
||||
paths.fill(source);
|
||||
paths[0].x -= 1;
|
||||
paths[1].x += 1;
|
||||
paths[2].y -= 1;
|
||||
@ -186,8 +177,6 @@ sf::Vector2f AISnake::GetAnyOpenPath(void) {
|
||||
|
||||
for (auto path : paths) {
|
||||
try {
|
||||
if (path == -g_pEngine->GetCurrentDirectionRaw())
|
||||
continue; // Impossible action
|
||||
bail = path;
|
||||
if (g_pEngine->gameBoard.at(path.y).at(path.x).m_bSnake)
|
||||
continue;
|
||||
@ -233,119 +222,3 @@ void AISnake::EmptyPath(void) {
|
||||
while (!botPathUnsanitized.empty())
|
||||
botPathUnsanitized.pop();
|
||||
}
|
||||
|
||||
// Main method for using AI snake
|
||||
PlayerDirection AISnake::CurrentBestDecision(void) {
|
||||
// Reset probabilities
|
||||
probabilityUp = 0.25;
|
||||
probabilityDown = 0.25;
|
||||
probabilityLeft = 0.25;
|
||||
probabilityRight = 0.25;
|
||||
|
||||
// Calculate options
|
||||
CheckLocalFreedom();
|
||||
CheckFoodDirection();
|
||||
|
||||
// Deny impossible movement
|
||||
RemoveImpossibleChoice();
|
||||
|
||||
// Make decision
|
||||
if (probabilityUp > probabilityDown) {
|
||||
if (probabilityUp < probabilityLeft)
|
||||
return kLeft;
|
||||
if (probabilityUp < probabilityRight)
|
||||
return kRight;
|
||||
return kUp;
|
||||
} else {
|
||||
if (probabilityDown < probabilityLeft)
|
||||
return kLeft;
|
||||
if (probabilityDown < probabilityRight)
|
||||
return kRight;
|
||||
return kDown;
|
||||
}
|
||||
}
|
||||
|
||||
// Improves probability based on amount of local open spaces
|
||||
// TODO: Add weights
|
||||
void AISnake::CheckLocalFreedom(void) {
|
||||
std::array<sf::Vector2f, 4> choices;
|
||||
std::array<double, 4> chances;
|
||||
chances.fill(0);
|
||||
choices.fill(g_pEngine->GetHeadLocation());
|
||||
choices[0].y += 1;
|
||||
choices[1].x += 1;
|
||||
choices[2].y -= 1;
|
||||
choices[3].x -= 1;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
try {
|
||||
if (g_pEngine->gameBoard.at(choices[i].y).at(choices[i].x).m_bSnake) {
|
||||
chances[i] = 0;
|
||||
continue;
|
||||
}
|
||||
if (g_pEngine->gameBoard.at(choices[i].y).at(choices[i].x).m_bFood) {
|
||||
chances[0] = 0;
|
||||
chances[1] = 0;
|
||||
chances[2] = 0;
|
||||
chances[3] = 0;
|
||||
chances[i] = 1;
|
||||
break;
|
||||
}
|
||||
} catch (const std::out_of_range& error) {
|
||||
chances[i] = 0;
|
||||
continue;
|
||||
}
|
||||
double openSpaces = 0;
|
||||
for (int j = -1; j < 2; ++j) {
|
||||
for (int k = -1; k < 2; ++k) {
|
||||
try {
|
||||
if (!g_pEngine->gameBoard.at(choices[i].y + j).at(choices[i].x + k).m_bSnake)
|
||||
++openSpaces;
|
||||
} catch (const std::out_of_range& error) {
|
||||
continue; // Out of bounds
|
||||
}
|
||||
}
|
||||
}
|
||||
chances[i] = openSpaces * 0.11111111111;
|
||||
}
|
||||
probabilityDown *= chances[0] * 4;
|
||||
probabilityRight *= chances[1] * 4;
|
||||
probabilityUp *= chances[2] * 4;
|
||||
probabilityLeft *= chances[3] * 4;
|
||||
}
|
||||
|
||||
// Improves probability that direction of food is best option
|
||||
// TODO: Add weights
|
||||
void AISnake::CheckFoodDirection(void) {
|
||||
sf::Vector2f delta = g_pEngine->GetHeadLocation() - g_pEngine->GetFoodLocation();
|
||||
if (delta.x > 0)
|
||||
probabilityLeft *= 1.5;
|
||||
if (delta.x < 0)
|
||||
probabilityRight *= 1.5;
|
||||
if (delta.y > 0)
|
||||
probabilityUp *= 1.5;
|
||||
if (delta.y < 0)
|
||||
probabilityDown *= 1.5;
|
||||
}
|
||||
|
||||
void AISnake::RemoveImpossibleChoice(void) {
|
||||
if (g_pEngine->GetPlayerSize() == 1)
|
||||
return; // Player can go any direction
|
||||
PlayerDirection currentDirection = g_pEngine->GetCurrentDirection();
|
||||
switch (currentDirection) {
|
||||
case (kUp):
|
||||
probabilityDown = 0;
|
||||
break;
|
||||
case (kDown):
|
||||
probabilityUp = 0;
|
||||
break;
|
||||
case (kLeft):
|
||||
probabilityRight = 0;
|
||||
break;
|
||||
case (kRight):
|
||||
probabilityLeft = 0;
|
||||
break;
|
||||
default:
|
||||
std::cout << "[ERR - AI] Impossibility defaulted somehow??" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,9 @@
|
||||
class AISnake {
|
||||
public:
|
||||
std::stack<sf::Vector2f> path;
|
||||
AISnake(void);
|
||||
void GetNewPath(void);
|
||||
PlayerDirection GetInput(void);
|
||||
AISnake();
|
||||
void GetNewPath(const sf::Vector2f& source);
|
||||
PlayerDirection GetInput(const sf::Vector2f* source);
|
||||
void UpdateProbability(int snakeSize);
|
||||
void AdjustProbability(double amount);
|
||||
void AddIteration(const int size);
|
||||
@ -19,30 +19,16 @@ public:
|
||||
private:
|
||||
int totalLength = 0;
|
||||
double average = 0;
|
||||
double thresholdDFS = 0.900;
|
||||
double probabilityBFS = 0.800;
|
||||
bool pathFailed = false;
|
||||
|
||||
// Generic search algorithms
|
||||
std::stack<sf::Vector2f> botPathUnsanitized;
|
||||
void BFS(void);
|
||||
void DFS(void);
|
||||
sf::Vector2f GetAnyOpenPath(void);
|
||||
void BFS(const sf::Vector2f& source);
|
||||
void DFS(const sf::Vector2f& source);
|
||||
sf::Vector2f GetAnyOpenPath(const sf::Vector2f& source);
|
||||
void UnvisitBoard(void);
|
||||
void UpdateAverage(const int size);
|
||||
void TrimPath(void);
|
||||
void EmptyPath(void);
|
||||
|
||||
// Unsupervised learning
|
||||
// Make decisions about current state of board
|
||||
double probabilityUp = 0.25;
|
||||
double probabilityDown = 0.25;
|
||||
double probabilityLeft = 0.25;
|
||||
double probabilityRight = 0.25;
|
||||
PlayerDirection CurrentBestDecision(void);
|
||||
void CheckLocalFreedom(void);
|
||||
void CheckFoodDirection(void);
|
||||
void RemoveImpossibleChoice(void);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -3,13 +3,14 @@
|
||||
#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);
|
||||
|
@ -4,7 +4,8 @@
|
||||
void InitializeGenerator(void);
|
||||
int GenerateRandomNumber(int generationLimit);
|
||||
|
||||
enum PlayerDirection {
|
||||
enum PlayerDirection
|
||||
{
|
||||
kNone = 0,
|
||||
kLeft = 1,
|
||||
kUp = 2,
|
||||
@ -13,7 +14,7 @@ enum PlayerDirection {
|
||||
};
|
||||
|
||||
struct GameSpace {
|
||||
GameSpace(void);
|
||||
GameSpace();
|
||||
unsigned char m_bFood : 1 = 0;
|
||||
unsigned char m_bSnake : 1 = 0;
|
||||
unsigned char m_bVisited : 1 = 0; // Used for BFS/DFS
|
||||
|
@ -6,20 +6,23 @@
|
||||
#include "playerinterface.hpp"
|
||||
#include "gamestate.hpp"
|
||||
|
||||
GameEngine::GameEngine(void) {
|
||||
GameEngine::GameEngine()
|
||||
{
|
||||
InitializeGenerator();
|
||||
return;
|
||||
}
|
||||
|
||||
void GameEngine::Start(void) {
|
||||
void GameEngine::Start()
|
||||
{
|
||||
PrepareGameBoard();
|
||||
if (!state.m_bNoDisplay)
|
||||
graphics.StartGameWindow();
|
||||
if (state.m_bIsBotControlled)
|
||||
graphics.SetShowGame(true);
|
||||
Loop();
|
||||
return;
|
||||
}
|
||||
|
||||
void GameEngine::Reset(void) {
|
||||
void GameEngine::Reset()
|
||||
{
|
||||
if (!state.m_bIsBotControlled)
|
||||
graphics.CheckContinue();
|
||||
else
|
||||
@ -27,25 +30,21 @@ void GameEngine::Reset(void) {
|
||||
player.Reset();
|
||||
PrepareGameBoard();
|
||||
state.m_bIsGameOver = false;
|
||||
if (!state.m_bIsBotControlled)
|
||||
return;
|
||||
if (!state.m_bSmart) {
|
||||
if (state.m_bIsBotControlled) {
|
||||
while (!bot.path.empty())
|
||||
bot.path.pop();
|
||||
}
|
||||
if (state.m_bNoDisplay)
|
||||
graphics.SetShowGame(false);
|
||||
if (state.m_bSkipIterations)
|
||||
if (state.m_bNoDisplay)
|
||||
graphics.SetShowGame(false);
|
||||
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();
|
||||
@ -53,54 +52,27 @@ 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;
|
||||
}
|
||||
|
||||
PlayerDirection GameEngine::GetCurrentDirection(void) {
|
||||
if (player.speed.x) {
|
||||
if (player.speed.x > 0)
|
||||
return kRight;
|
||||
else
|
||||
return kLeft;
|
||||
} else {
|
||||
if (player.speed.y > 0)
|
||||
return kDown;
|
||||
else
|
||||
return kUp;
|
||||
}
|
||||
}
|
||||
|
||||
sf::Vector2f GameEngine::GetCurrentDirectionRaw(void) {
|
||||
return player.speed;
|
||||
}
|
||||
|
||||
int GameEngine::GetPlayerSize(void) {
|
||||
return player.body.size();
|
||||
}
|
||||
|
||||
sf::Vector2f GameEngine::GetHeadLocation(void) {
|
||||
return player.headLocation;
|
||||
}
|
||||
|
||||
sf::Vector2f GameEngine::GetFoodLocation(void) {
|
||||
return playerFood.location;
|
||||
}
|
||||
|
||||
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;
|
||||
@ -114,70 +86,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) {
|
||||
void GameEngine::RegenerateFood()
|
||||
{
|
||||
// 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) {
|
||||
// Create empty game board
|
||||
void GameEngine::PrepareGameBoard(void)
|
||||
{
|
||||
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())
|
||||
bot.GetNewPath();
|
||||
controller = bot.GetInput();
|
||||
} else
|
||||
controller = GetPlayerInput();
|
||||
|
||||
if (bot.path.empty()) {
|
||||
bot.GetNewPath(player.headLocation);
|
||||
}
|
||||
controller = bot.GetInput(&player.headLocation);
|
||||
}
|
||||
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;
|
||||
|
@ -5,15 +5,15 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <memory>
|
||||
#include "botinterface.hpp"
|
||||
#include "common.hpp"
|
||||
#include "snake.hpp"
|
||||
#include "playerinterface.hpp"
|
||||
|
||||
const int kUnitSpeed = 1;
|
||||
|
||||
class GameEngine {
|
||||
class GameEngine
|
||||
{
|
||||
public:
|
||||
GameEngine(void);
|
||||
GameEngine();
|
||||
void Start(void);
|
||||
void Reset(void);
|
||||
sf::Vector2f GetGameBoundaries(void);
|
||||
@ -21,18 +21,13 @@ public:
|
||||
unsigned char m_bIsGameOver : 1 = 0;
|
||||
unsigned char m_bIsBotControlled : 1 = 0;
|
||||
unsigned char m_bNoDisplay : 1 = 0;
|
||||
unsigned char m_bSmart : 1 = 0;
|
||||
unsigned char m_bSkipIterations : 1 = 0;
|
||||
unsigned char _3 : 1 = 0;
|
||||
unsigned char _4 : 1 = 0;
|
||||
unsigned char _5 : 1 = 0;
|
||||
unsigned char _6 : 1 = 0;
|
||||
unsigned char _7 : 1 = 0;
|
||||
} state;
|
||||
std::vector< std::vector<GameSpace> > gameBoard;
|
||||
PlayerDirection GetCurrentDirection(void);
|
||||
sf::Vector2f GetCurrentDirectionRaw(void);
|
||||
int GetPlayerSize(void);
|
||||
sf::Vector2f GetHeadLocation(void);
|
||||
sf::Vector2f GetFoodLocation(void);
|
||||
private:
|
||||
PlayerOutput graphics;
|
||||
Snake player;
|
||||
@ -43,7 +38,7 @@ private:
|
||||
void PlaceNewSnakePart(sf::Vector2f location);
|
||||
void RegenerateFood(void);
|
||||
void PrepareGameBoard(void);
|
||||
void UpdatePlayerSpeed(void);
|
||||
void UpdatePlayerSpeed();
|
||||
};
|
||||
|
||||
inline std::unique_ptr<GameEngine> g_pEngine;
|
||||
|
@ -28,12 +28,6 @@ int main(int argc, char* argv[]) {
|
||||
} else if (args[i].compare("--auto") == 0) {
|
||||
g_pEngine->state.m_bIsBotControlled = true;
|
||||
std::cout << "[LOG - Main] Bot control enabled" << std::endl;
|
||||
} else if (args[i].compare("--smart") == 0) {
|
||||
g_pEngine->state.m_bSmart = true;
|
||||
std::cout << "[LOG - Main] Using AI" << std::endl;
|
||||
} else if (args[i].compare("--skip") == 0) {
|
||||
g_pEngine->state.m_bSkipIterations = true;
|
||||
std::cout << "[LOG - Main] Only showing every 50 epochs" << std::endl;
|
||||
} else if (args[i].compare("-h") == 0 || args[i].compare("--help") == 0) {
|
||||
Help();
|
||||
return 0;
|
||||
|
@ -1,30 +1,32 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
// TODO: Add board size limits
|
||||
// Add gaps and a border that accommodate the limits
|
||||
PlayerOutput::PlayerOutput(void) {
|
||||
PlayerOutput::PlayerOutput(void)
|
||||
{
|
||||
float kWidth = 1025;
|
||||
float kHeight = 725;
|
||||
float kBoardWidth = kWidth / kGridSize;
|
||||
@ -35,23 +37,26 @@ PlayerOutput::PlayerOutput(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
void PlayerOutput::CheckContinue(void) {
|
||||
void PlayerOutput::CheckContinue()
|
||||
{
|
||||
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;
|
||||
@ -75,20 +80,24 @@ 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);
|
||||
@ -97,22 +106,25 @@ 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);
|
||||
if (isShowing) { delay = sf::milliseconds(5); }
|
||||
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))) {
|
||||
void PlayerOutput::CheckWindowEvents(void)
|
||||
{
|
||||
while (gameWindow.pollEvent(event))
|
||||
{
|
||||
if ((event.type == sf::Event::Closed)
|
||||
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))) {
|
||||
gameWindow.close();
|
||||
isWindowAlive = false;
|
||||
}
|
||||
@ -127,23 +139,29 @@ 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;
|
||||
}
|
||||
|
@ -8,12 +8,13 @@ const int kGridSize = 25;
|
||||
|
||||
PlayerDirection GetPlayerInput(void);
|
||||
|
||||
class PlayerOutput {
|
||||
class PlayerOutput
|
||||
{
|
||||
public:
|
||||
sf::Vector2f gameBoundaries;
|
||||
PlayerOutput(void);
|
||||
bool IsOpen(void);
|
||||
void CheckContinue(void);
|
||||
void CheckContinue();
|
||||
void DisplayGameState(std::vector< std::vector<GameSpace> >& gameBoard, int score);
|
||||
void DisplayScore(int score);
|
||||
void StartGameWindow(void);
|
||||
|
@ -3,23 +3,25 @@
|
||||
#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;
|
||||
|
@ -6,7 +6,8 @@
|
||||
#include <queue>
|
||||
#include "common.hpp"
|
||||
|
||||
struct Snake {
|
||||
struct Snake
|
||||
{
|
||||
public:
|
||||
sf::Vector2f headLocation;
|
||||
sf::Vector2f speed;
|
||||
@ -15,7 +16,8 @@ public:
|
||||
void Reset(void);
|
||||
};
|
||||
|
||||
struct Food {
|
||||
struct Food
|
||||
{
|
||||
public:
|
||||
sf::Vector2f location;
|
||||
void GenerateNewFood(sf::Vector2f boundaries);
|
||||
|
Loading…
Reference in New Issue
Block a user