Skip to content

Commit

Permalink
Adding some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
diegyoho committed Oct 24, 2021
1 parent 3ceb0b2 commit 1a90050
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Core/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions Core/GameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion Core/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)];
Expand Down Expand Up @@ -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)];
Expand All @@ -198,6 +202,7 @@ void Grid::OpenAll() const
}
}

// Function that validates victory
bool Grid::IsAllMinesRevealed() const
{
bool winner = (width * height) - numberOfPositionsOpened == numberOfMines;
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion Core/Grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion Core/PositionState.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#pragma once

// Enum to indicate the status of each position
enum class PositionState : char
{
Opened = 0x01,
HiddenMine = 0x02,
Flagged = 0x04
};

//Bitwise operator overloading for enum class PositionState
// Bitwise operator overloading for enum class PositionState

inline PositionState operator~(const PositionState& flag)
{
Expand Down
1 change: 1 addition & 0 deletions Core/Random.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <random>

// Basic Utility Functions for Random Values
namespace Random
{
static std::mt19937 mersenneTwisterRand(std::mt19937::default_seed);
Expand Down
4 changes: 3 additions & 1 deletion Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1a90050

Please sign in to comment.