Compare commits
No commits in common. "eefc2bf60b872121a223e5125578bcef89646aea" and "30ffcc4bb31b5c38eaf0333f49a2856e9186ea8c" have entirely different histories.
eefc2bf60b
...
30ffcc4bb3
16
.gitignore
vendored
16
.gitignore
vendored
@ -33,19 +33,7 @@
|
|||||||
*.json
|
*.json
|
||||||
*.ps1
|
*.ps1
|
||||||
|
|
||||||
# ---> CMake
|
|
||||||
CMakeLists.txt.user
|
|
||||||
CMakeCache.txt
|
|
||||||
CMakeFiles
|
|
||||||
CMakeScripts
|
|
||||||
Testing
|
|
||||||
Makefile
|
|
||||||
cmake_install.cmake
|
|
||||||
install_manifest.txt
|
|
||||||
compile_commands.json
|
|
||||||
CTestTestfile.cmake
|
|
||||||
_deps
|
|
||||||
build
|
|
||||||
|
|
||||||
# Extras
|
# Extras
|
||||||
.vs*
|
.vs*
|
||||||
|
build
|
||||||
|
bin
|
@ -2,7 +2,6 @@
|
|||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
@ -43,15 +42,6 @@ namespace snakeplusplus
|
|||||||
return;
|
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
|
// 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)
|
||||||
|
@ -15,9 +15,8 @@ namespace snakeplusplus
|
|||||||
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);
|
|
||||||
private:
|
private:
|
||||||
double probabilityBFS = 0.500;
|
double probabilityBFS = 1.000;
|
||||||
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);
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
// GameState.cpp
|
// GameState.cpp
|
||||||
#include <iostream>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include "botinterface.hpp"
|
#include "botinterface.hpp"
|
||||||
@ -25,30 +24,16 @@ namespace snakeplusplus
|
|||||||
|
|
||||||
void GameEngine::Reset()
|
void GameEngine::Reset()
|
||||||
{
|
{
|
||||||
AddIteration();
|
graphics.CheckContinue(isBotControlled);
|
||||||
player.Reset();
|
player.Reset();
|
||||||
if (isBotControlled) { while (!bot.path.empty()) { bot.path.pop(); } }
|
if (isBotControlled) {
|
||||||
|
while (!bot.path.empty()) { bot.path.pop(); }
|
||||||
|
}
|
||||||
PrepareGameBoard();
|
PrepareGameBoard();
|
||||||
isGameOver = false;
|
isGameOver = false;
|
||||||
graphics.SetShowGame((amountPlayed + 1) % 50 == 0);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameEngine::AddIteration(void)
|
|
||||||
{
|
|
||||||
graphics.CheckContinue(isBotControlled);
|
|
||||||
if (player.body.size() > 40)
|
|
||||||
{
|
|
||||||
UpdateAverage();
|
|
||||||
double adjustmentAmount = 0.002;
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void GameEngine::Loop(void)
|
void GameEngine::Loop(void)
|
||||||
{
|
{
|
||||||
int currentScore = 0;
|
int currentScore = 0;
|
||||||
@ -59,7 +44,7 @@ namespace snakeplusplus
|
|||||||
PlaceNewSnakePart(MovePlayer());
|
PlaceNewSnakePart(MovePlayer());
|
||||||
RegenerateFood();
|
RegenerateFood();
|
||||||
currentScore = player.body.size() * 100;
|
currentScore = player.body.size() * 100;
|
||||||
//bot.UpdateProbability(player.body.size());
|
bot.UpdateProbability(player.body.size());
|
||||||
graphics.DisplayGameState(gameBoard, currentScore);
|
graphics.DisplayGameState(gameBoard, currentScore);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -67,22 +52,26 @@ namespace snakeplusplus
|
|||||||
|
|
||||||
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 newHeadPosition;
|
||||||
|
newHeadPosition.x = player.headLocation.x + player.speed.x;
|
||||||
|
newHeadPosition.y = player.headLocation.y + player.speed.y;
|
||||||
|
return newHeadPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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);
|
{
|
||||||
if (*locationState == 'O' && (player.body.size() > 1)) {
|
char* locationState;
|
||||||
|
locationState = &gameBoard.at(location.y).at(location.x);
|
||||||
|
if (*locationState == 'O' && (player.body.size() > 1))
|
||||||
isGameOver = true; // Game should end (Snake touching snake)
|
isGameOver = true; // Game should end (Snake touching snake)
|
||||||
}
|
|
||||||
*locationState = 'O';
|
*locationState = 'O';
|
||||||
player.body.push(locationState);
|
player.body.push(locationState);
|
||||||
player.headLocation = location;
|
player.headLocation = location;
|
||||||
@ -94,19 +83,22 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Generates new food until not colliding with player
|
// Generates new food until not colliding with player
|
||||||
void GameEngine::RegenerateFood()
|
void GameEngine::RegenerateFood(void)
|
||||||
{
|
{
|
||||||
// Generate a new food location if the current one is occupied
|
sf::Vector2f newLocation = playerFood.location;
|
||||||
while (gameBoard.at(playerFood.location.y).at(playerFood.location.x) == 'O') {
|
bool isUpdated = false;
|
||||||
|
while (gameBoard.at(newLocation.y).at(newLocation.x) == 'O')
|
||||||
|
{
|
||||||
|
isUpdated = true;
|
||||||
playerFood.GenerateNewFood(GetGameBoundaries());
|
playerFood.GenerateNewFood(GetGameBoundaries());
|
||||||
|
newLocation = playerFood.location;
|
||||||
}
|
}
|
||||||
|
if (isUpdated) {
|
||||||
// Update the game board with the new food location
|
gameBoard.at(newLocation.y).at(newLocation.x) = 'X';
|
||||||
gameBoard.at(playerFood.location.y).at(playerFood.location.x) = 'X';
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GameEngine::PrepareGameBoard(void)
|
void GameEngine::PrepareGameBoard(void)
|
||||||
{
|
{
|
||||||
@ -163,9 +155,4 @@ namespace snakeplusplus
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void GameEngine::UpdateAverage() {
|
|
||||||
totalLength += player.body.size();
|
|
||||||
amountPlayed += 1;
|
|
||||||
average = (double)totalLength / amountPlayed;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ namespace snakeplusplus
|
|||||||
GameEngine();
|
GameEngine();
|
||||||
void Start(void);
|
void Start(void);
|
||||||
void Reset(void);
|
void Reset(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;
|
||||||
@ -34,10 +33,6 @@ namespace snakeplusplus
|
|||||||
void RegenerateFood(void);
|
void RegenerateFood(void);
|
||||||
void PrepareGameBoard(void);
|
void PrepareGameBoard(void);
|
||||||
void UpdatePlayerSpeed();
|
void UpdatePlayerSpeed();
|
||||||
void UpdateAverage();
|
|
||||||
int totalLength = 0;
|
|
||||||
int amountPlayed = 0;
|
|
||||||
double average = 0;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +87,6 @@ namespace snakeplusplus
|
|||||||
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; }
|
|
||||||
char* letterOnBoard;
|
char* letterOnBoard;
|
||||||
for (float y = 0; y < gameBoundaries.y; y++)
|
for (float y = 0; y < gameBoundaries.y; y++)
|
||||||
{
|
{
|
||||||
@ -121,12 +120,6 @@ namespace snakeplusplus
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerOutput::SetShowGame(bool isShowing) {
|
|
||||||
if (isShowing) { delay = sf::milliseconds(2); }
|
|
||||||
else { delay = sf::milliseconds(0); }
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PlayerOutput::CheckWindowEvents(void)
|
void PlayerOutput::CheckWindowEvents(void)
|
||||||
{
|
{
|
||||||
while (gameWindow.pollEvent(event))
|
while (gameWindow.pollEvent(event))
|
||||||
@ -134,14 +127,6 @@ namespace snakeplusplus
|
|||||||
if ((event.type == sf::Event::Closed)
|
if ((event.type == sf::Event::Closed)
|
||||||
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
|| (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)))
|
||||||
gameWindow.close();
|
gameWindow.close();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,6 @@ namespace snakeplusplus
|
|||||||
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);
|
|
||||||
private:
|
private:
|
||||||
void CheckWindowEvents(void);
|
void CheckWindowEvents(void);
|
||||||
void DisplayEndScreen(void);
|
void DisplayEndScreen(void);
|
||||||
@ -32,7 +31,7 @@ namespace snakeplusplus
|
|||||||
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(10);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user