From 1a90050fc3559afe92d5f8beb311080dc4508ea4 Mon Sep 17 00:00:00 2001 From: Diego Moreira Date: Sun, 24 Oct 2021 18:30:39 -0300 Subject: [PATCH] Adding some comments --- Core/GameManager.cpp | 3 +++ Core/GameManager.h | 3 +++ Core/Grid.cpp | 9 ++++++++- Core/Grid.h | 2 +- Core/PositionState.h | 3 ++- Core/Random.h | 1 + Main.cpp | 4 +++- 7 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Core/GameManager.cpp b/Core/GameManager.cpp index 2798594..77619ef 100644 --- a/Core/GameManager.cpp +++ b/Core/GameManager.cpp @@ -45,10 +45,13 @@ void GameManager::InitGame() ShowDescription(); + // Game default values int width{ 10 }; int height{ 10 }; int numberOfMines{ 10 }; + + // Game customization std::cout << "Want to customize the game? Press (y/n)\n"; char key = _getch(); diff --git a/Core/GameManager.h b/Core/GameManager.h index 5d38568..0cdef50 100644 --- a/Core/GameManager.h +++ b/Core/GameManager.h @@ -4,8 +4,11 @@ class GameManager { private: + // Variables that indicate the selected position int selectedX; int selectedY; + + // Keep the state if the game is over bool gameEnded; GameManager(); diff --git a/Core/Grid.cpp b/Core/Grid.cpp index 7e702a0..b025a14 100644 --- a/Core/Grid.cpp +++ b/Core/Grid.cpp @@ -10,6 +10,7 @@ Grid* Grid::GetSingleton() return grid; } +// Shows grid and selected position void Grid::Draw(const int selectedX, const int selectedY) const { for (int j = 0; j <= height; ++j) @@ -93,6 +94,7 @@ void Grid::Draw(const int selectedX, const int selectedY) const } } +// Initializes the grid with the given values void Grid::Initialize(const int& width, const int& height, const int& numberOfMines) { GetSingleton()->width = width; @@ -122,6 +124,7 @@ void Grid::Initialize(const int& width, const int& height, const int& numberOfMi } } +// Check if there is a mine in the position, returns true if position has no mine bool Grid::OpenPosition(int x, int y) const { Position* position = positions[CoordsToVectorPosition(x, y)]; @@ -182,6 +185,7 @@ bool Grid::OpenPosition(int x, int y) const return true; } +// Put/Remove flag void Grid::FlagPosition(int x, int y) const { Position* position = positions[CoordsToVectorPosition(x, y)]; @@ -198,6 +202,7 @@ void Grid::OpenAll() const } } +// Function that validates victory bool Grid::IsAllMinesRevealed() const { bool winner = (width * height) - numberOfPositionsOpened == numberOfMines; @@ -233,7 +238,9 @@ Grid::~Grid() std::cout << "Grid destroyed!\n"; } -int Grid::CoordsToVectorPosition(int x, int y) const +/* As the grid is not structured in a multidimensional array, + * this function calculates the index for the given coordinates */ +int Grid::CoordsToVectorPosition(const int& x, const int& y) const { return width * y + x; } diff --git a/Core/Grid.h b/Core/Grid.h index 037b260..e240a08 100644 --- a/Core/Grid.h +++ b/Core/Grid.h @@ -18,7 +18,7 @@ class Grid Grid(); ~Grid(); - int CoordsToVectorPosition(int x, int y) const; + int CoordsToVectorPosition(const int& x, const int& y) const; void OpenAll() const; public: diff --git a/Core/PositionState.h b/Core/PositionState.h index 3723ee6..10fd7f7 100644 --- a/Core/PositionState.h +++ b/Core/PositionState.h @@ -1,5 +1,6 @@ #pragma once +// Enum to indicate the status of each position enum class PositionState : char { Opened = 0x01, @@ -7,7 +8,7 @@ enum class PositionState : char Flagged = 0x04 }; -//Bitwise operator overloading for enum class PositionState +// Bitwise operator overloading for enum class PositionState inline PositionState operator~(const PositionState& flag) { diff --git a/Core/Random.h b/Core/Random.h index 8759161..c0daffa 100644 --- a/Core/Random.h +++ b/Core/Random.h @@ -2,6 +2,7 @@ #include +// Basic Utility Functions for Random Values namespace Random { static std::mt19937 mersenneTwisterRand(std::mt19937::default_seed); diff --git a/Main.cpp b/Main.cpp index 2e66b7f..8a479cd 100644 --- a/Main.cpp +++ b/Main.cpp @@ -5,7 +5,9 @@ int main() { - char key{}; + char key{}; // Key to check if you want to play again after you finish + + // Starting the game do { GameManager::GetSingleton()->InitGame();