Added scoring and chance-based algorithm picking #3

Merged
Trianta merged 3 commits from dev into master 2023-10-24 00:55:31 -05:00
4 changed files with 26 additions and 3 deletions
Showing only changes of commit da6ceebb55 - Show all commits

View File

@ -136,6 +136,12 @@ namespace snakeplusplus
} }
for (sf::Vector2f newLocation : localLocations) { for (sf::Vector2f newLocation : localLocations) {
try { try {
if (newLocation.x < 2 || newLocation.y < 2
|| newLocation.x > boundaries.x - 2
|| newLocation.y > boundaries.y - 2) {
continue;
}
if ((!visited.at(newLocation.y).at(newLocation.x)) if ((!visited.at(newLocation.y).at(newLocation.x))
&& (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) { && (gameBoard.at(newLocation.y).at(newLocation.x) == ' ')) {
search.push(newLocation); search.push(newLocation);

View File

@ -36,13 +36,15 @@ namespace snakeplusplus
void GameEngine::Loop(void) void GameEngine::Loop(void)
{ {
int currentScore = 0;
while (graphics.IsOpen()) while (graphics.IsOpen())
{ {
if (isGameOver) { Reset(); } if (isGameOver) { Reset(); }
UpdatePlayerSpeed(); UpdatePlayerSpeed();
PlaceNewSnakePart(MovePlayer()); PlaceNewSnakePart(MovePlayer());
RegenerateFood(); RegenerateFood();
graphics.DisplayGameState(gameBoard); currentScore = player.body.size() * 100;
graphics.DisplayGameState(gameBoard, currentScore);
} }
return; return;
} }

View File

@ -71,7 +71,20 @@ namespace snakeplusplus
return; return;
} }
void PlayerOutput::DisplayGameState(std::vector< std::vector<char> >& gameBoard) 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(); CheckWindowEvents();
char* letterOnBoard; char* letterOnBoard;
@ -94,6 +107,7 @@ namespace snakeplusplus
} }
} }
} }
DisplayScore(score);
gameWindow.display(); gameWindow.display();
sf::sleep(delay); sf::sleep(delay);
return; return;

View File

@ -17,7 +17,8 @@ namespace snakeplusplus
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); void DisplayGameState(std::vector< std::vector<char> >& gameBoard, int score);
void DisplayScore(int score);
void StartGameWindow(void); void StartGameWindow(void);
private: private:
void CheckWindowEvents(void); void CheckWindowEvents(void);