Skip to content

Commit

Permalink
Merge pull request #33 from Bollos00/32-saving-scores
Browse files Browse the repository at this point in the history
* Closes #32 ;
* New workflow to build libremines on archlinux with qt6;
* Solved issue of game being completed more than 1 time sometimes.
  • Loading branch information
Bollos00 authored Oct 19, 2021
2 parents 1d93124 + a70f39b commit e171426
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 78 deletions.
24 changes: 22 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ on:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build-linux:
build-linux-qt5:
# The type of runner that the job will run on
runs-on: ubuntu-20.04

Expand All @@ -30,6 +30,9 @@ jobs:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: "Apt update"
run: "sudo apt-get update"

- name: "Install dependencies"
run: "sudo apt-get install build-essential qt5-default cmake libqt5svg5-dev"

Expand All @@ -38,7 +41,24 @@ jobs:

- name: "Build"
run: "cd build && make"


build-linux-qt6:
runs-on: ubuntu-20.04
container:
image: archlinux:latest
options: --privileged
steps:
- uses: actions/checkout@v2

- name: "Install dependencies"
run: "pacman -Syu --noconfirm base-devel qt6-base qt6-svg cmake"

- name: "Create build directory and run CMake"
run: "mkdir build && cd build && cmake -DUSE_QT6='YES' .."

- name: "Build"
run: "cd build && make"

build-windows:
runs-on: windows-2019
steps:
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
message(STATUS "Using CMake version ${CMAKE_VERSION}")
cmake_minimum_required(VERSION 3.1.0)
project(libremines
VERSION "1.6.3"
VERSION "1.7.0"
DESCRIPTION "A Qt based Minesweeper game"
HOMEPAGE_URL "https://github.com/Bollos00/LibreMines"
LANGUAGES "CXX"
Expand Down
123 changes: 69 additions & 54 deletions src/libreminesgameengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,17 @@ void LibreMinesGameEngine::vResetPrincipalMatrix()
principalMatrix.clear();
}

void LibreMinesGameEngine::vCleanCell(const uchar _X, const uchar _Y)
bool LibreMinesGameEngine::bCleanCell(const uchar _X, const uchar _Y)
{
if(!principalMatrix[_X][_Y].hasFlag &&
principalMatrix[_X][_Y].state == MINE)
{
// If the user tried to unlock an unflaged mined cell, (s)he lose
vGameLost(_X, _Y);
return false;
}
else if(principalMatrix[_X][_Y].isHidden &&

if(principalMatrix[_X][_Y].isHidden &&
!principalMatrix[_X][_Y].hasFlag)
{
// Unlock the cell
Expand All @@ -309,140 +311,140 @@ void LibreMinesGameEngine::vCleanCell(const uchar _X, const uchar _Y)
_Y == 0)
{
if(principalMatrix[_X+1][_Y].isHidden)
vCleanCell(_X+1, _Y);
bCleanCell(_X+1, _Y);
if(principalMatrix[_X][_Y+1].isHidden)
vCleanCell(_X, _Y+1);
bCleanCell(_X, _Y+1);
if(principalMatrix[_X+1][_Y+1].isHidden)
vCleanCell(_X+1, _Y+1);
bCleanCell(_X+1, _Y+1);
}
else if(_X == 0 &&
_Y == iY-1)
{
if(principalMatrix[_X+1][_Y].isHidden)
vCleanCell(_X+1, _Y);
bCleanCell(_X+1, _Y);
if(principalMatrix[_X][_Y-1].isHidden)
vCleanCell(_X, _Y-1);
bCleanCell(_X, _Y-1);
if(principalMatrix[_X+1][_Y-1].isHidden)
vCleanCell(_X+1, _Y-1);
bCleanCell(_X+1, _Y-1);
}
else if(_X == iX-1 &&
_Y == 0)
{
if(principalMatrix[_X-1][_Y].isHidden)
vCleanCell(_X-1, _Y);
bCleanCell(_X-1, _Y);
if(principalMatrix[_X][_Y+1].isHidden)
vCleanCell(_X, _Y+1);
bCleanCell(_X, _Y+1);
if(principalMatrix[_X-1][_Y+1].isHidden)
vCleanCell(_X-1, _Y+1);
bCleanCell(_X-1, _Y+1);
}
else if(_X == iX-1 &&
_Y == iY-1)
{
if(principalMatrix[_X-1][_Y].isHidden)
vCleanCell(_X-1, _Y);
bCleanCell(_X-1, _Y);
if(principalMatrix[_X][_Y-1].isHidden)
vCleanCell(_X, _Y-1);
bCleanCell(_X, _Y-1);
if(principalMatrix[_X-1][_Y-1].isHidden)
vCleanCell(_X-1, _Y-1);
bCleanCell(_X-1, _Y-1);
}
else if(_X == iX-1 &&
_Y == 0)
{
if(principalMatrix[_X-1][_Y].isHidden)
vCleanCell(_X-1, _Y);
bCleanCell(_X-1, _Y);
if(principalMatrix[_X][_Y+1].isHidden)
vCleanCell(_X, _Y+1);
bCleanCell(_X, _Y+1);
if(principalMatrix[_X-1][_Y+1].isHidden)
vCleanCell(_X-1, _Y+1);
bCleanCell(_X-1, _Y+1);
}
else if(_X == iX-1 &&
_Y == iY-1)
{
if(principalMatrix[_X-1][_Y].isHidden)
vCleanCell(_X-1, _Y);
bCleanCell(_X-1, _Y);
if(principalMatrix[_X][_Y-1].isHidden)
vCleanCell(_X, _Y-1);
bCleanCell(_X, _Y-1);
if(principalMatrix[_X-1][_Y-1].isHidden)
vCleanCell(_X-1, _Y-1);
bCleanCell(_X-1, _Y-1);
}
else if(_X == 0 &&
_Y > 0 &&
_Y < iY-1)
{
if(principalMatrix[_X+1][_Y].isHidden)
vCleanCell(_X+1, _Y);
bCleanCell(_X+1, _Y);
if(principalMatrix[_X][_Y+1].isHidden)
vCleanCell(_X, _Y+1);
bCleanCell(_X, _Y+1);
if(principalMatrix[_X+1][_Y+1].isHidden)
vCleanCell(_X+1, _Y+1);
bCleanCell(_X+1, _Y+1);
if(principalMatrix[_X][_Y-1].isHidden)
vCleanCell(_X, _Y-1);
bCleanCell(_X, _Y-1);
if(principalMatrix[_X+1][_Y-1].isHidden)
vCleanCell(_X+1, _Y-1);
bCleanCell(_X+1, _Y-1);
}
else if(_X == iX-1 &&
_Y > 0 &&
_Y < iY-1)
{
if(principalMatrix[_X-1][_Y].isHidden)
vCleanCell(_X-1, _Y);
bCleanCell(_X-1, _Y);
if(principalMatrix[_X][_Y+1].isHidden)
vCleanCell(_X, _Y+1);
bCleanCell(_X, _Y+1);
if(principalMatrix[_X-1][_Y+1].isHidden)
vCleanCell(_X-1, _Y+1);
bCleanCell(_X-1, _Y+1);
if(principalMatrix[_X][_Y-1].isHidden)
vCleanCell(_X, _Y-1);
bCleanCell(_X, _Y-1);
if(principalMatrix[_X-1][_Y-1].isHidden)
vCleanCell(_X-1, _Y-1);
bCleanCell(_X-1, _Y-1);
}
else if(_X > 0 &&
_X < iX-1 &&
_Y == 0)
{
if(principalMatrix[_X-1][_Y].isHidden)
vCleanCell(_X-1, _Y);
bCleanCell(_X-1, _Y);
if(principalMatrix[_X+1][_Y].isHidden)
vCleanCell(_X+1, _Y);
bCleanCell(_X+1, _Y);
if(principalMatrix[_X-1][_Y+1].isHidden)
vCleanCell(_X-1, _Y+1);
bCleanCell(_X-1, _Y+1);
if(principalMatrix[_X][_Y+1].isHidden)
vCleanCell(_X, _Y+1);
bCleanCell(_X, _Y+1);
if(principalMatrix[_X+1][_Y+1].isHidden)
vCleanCell(_X+1, _Y+1);
bCleanCell(_X+1, _Y+1);
}
else if(_X > 0 &&
_X < iX-1 &&
_Y == iY-1)
{
if(principalMatrix[_X+1][_Y].isHidden)
vCleanCell(_X+1, _Y);
bCleanCell(_X+1, _Y);
if(principalMatrix[_X-1][_Y].isHidden)
vCleanCell(_X-1, _Y);
bCleanCell(_X-1, _Y);
if(principalMatrix[_X-1][_Y-1].isHidden)
vCleanCell(_X-1, _Y-1);
bCleanCell(_X-1, _Y-1);
if(principalMatrix[_X][_Y-1].isHidden)
vCleanCell(_X, _Y-1);
bCleanCell(_X, _Y-1);
if(principalMatrix[_X+1][_Y-1].isHidden)
vCleanCell(_X+1, _Y-1);
bCleanCell(_X+1, _Y-1);
}
else
{
if(principalMatrix[_X-1][_Y-1].isHidden)
vCleanCell(_X-1, _Y-1);
bCleanCell(_X-1, _Y-1);
if(principalMatrix[_X-1][_Y].isHidden)
vCleanCell(_X-1, _Y);
bCleanCell(_X-1, _Y);
if(principalMatrix[_X-1][_Y+1].isHidden)
vCleanCell(_X-1, _Y+1);
bCleanCell(_X-1, _Y+1);
if(principalMatrix[_X][_Y-1].isHidden)
vCleanCell(_X, _Y-1);
bCleanCell(_X, _Y-1);
if(principalMatrix[_X][_Y+1].isHidden)
vCleanCell(_X, _Y+1);
bCleanCell(_X, _Y+1);
if(principalMatrix[_X+1][_Y-1].isHidden)
vCleanCell(_X+1, _Y-1);
bCleanCell(_X+1, _Y-1);
if(principalMatrix[_X+1][_Y].isHidden)
vCleanCell(_X+1, _Y);
bCleanCell(_X+1, _Y);
if(principalMatrix[_X+1][_Y+1].isHidden)
vCleanCell(_X+1, _Y+1);
bCleanCell(_X+1, _Y+1);
}
}

Expand All @@ -453,6 +455,8 @@ void LibreMinesGameEngine::vCleanCell(const uchar _X, const uchar _Y)
vGameWon();
}
}

return true;
}

void LibreMinesGameEngine::vGameLost(const uchar _X, const uchar _Y)
Expand All @@ -476,8 +480,12 @@ void LibreMinesGameEngine::vGameWon()
}


void LibreMinesGameEngine::vGenerateEndGameScore(qint64 iTimeInNs)
void LibreMinesGameEngine::vGenerateEndGameScore(qint64 iTimeInNs, bool ignorePreferences)
{
static qint64 timeLastGame = -1;

if(iTimeInNs != -1)
timeLastGame = iTimeInNs;

int iCorrectFlags = 0,
iWrongFlags = 0,
Expand All @@ -497,22 +505,23 @@ void LibreMinesGameEngine::vGenerateEndGameScore(qint64 iTimeInNs)
}
}

double timeInSecs = (double)iTimeInNs*1e-9;
double timeInSecs = (double)timeLastGame*1e-9;

double dFlagsPerSecond = (double)iCorrectFlags/timeInSecs,
dCellsPerSecond = (double)iUnlockedCells/timeInSecs,
dPercentageGameComplete = (double)100*iUnlockedCells/iCellsToUnlock;

LibreMinesScore score;
score.iTimeInNs = iTimeInNs;
score.iTimeInNs = timeLastGame;
score.gameDifficulty = NONE;
score.width = iX;
score.heigth = iY;
score.mines = nMines;
score.bGameCompleted = iUnlockedCells == iCellsToUnlock;
score.dPercentageGameCompleted = dPercentageGameComplete;

Q_EMIT SIGNAL_endGameScore(score, iCorrectFlags, iWrongFlags, iUnlockedCells, dFlagsPerSecond, dCellsPerSecond);
Q_EMIT SIGNAL_endGameScore(score, iCorrectFlags, iWrongFlags, iUnlockedCells,
dFlagsPerSecond, dCellsPerSecond, ignorePreferences);
}

const std::vector<std::vector<LibreMinesGameEngine::CellGameEngine> >& LibreMinesGameEngine::getPrincipalMatrix() const
Expand Down Expand Up @@ -569,7 +578,7 @@ void LibreMinesGameEngine::SLOT_cleanCell(const uchar _X, const uchar _Y)
SLOT_startTimer();
bFirst = false;
}
vCleanCell(_X, _Y);
bCleanCell(_X, _Y);
}

void LibreMinesGameEngine::SLOT_addOrRemoveFlag(const uchar _X, const uchar _Y)
Expand Down Expand Up @@ -628,12 +637,18 @@ void LibreMinesGameEngine::SLOT_cleanNeighborCells(const uchar _X, const uchar _

if(cell.isHidden && !cell.hasFlag)
{
vCleanCell(i, j);
if(!bCleanCell(i, j))
return;
}
}
}
}

void LibreMinesGameEngine::SLOT_generateEndGameScoreAgain()
{
vGenerateEndGameScore(-1, true);
}

void LibreMinesGameEngine::SLOT_UpdateTime()
{
iTimeInSeconds++;
Expand Down
8 changes: 5 additions & 3 deletions src/libreminesgameengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class LibreMinesGameEngine: public QObject
void setFirstCellClean(const bool x);
private:
void vResetPrincipalMatrix();
void vCleanCell(const uchar _X, const uchar _Y);
bool bCleanCell(const uchar _X, const uchar _Y);
void vGameLost(const uchar _X, const uchar _Y);
void vGameWon();

void vGenerateEndGameScore(qint64 iTimeInNs);
void vGenerateEndGameScore(qint64 iTimeInNs = -1, bool ignorePreferences=false);

std::vector< std::vector<CellGameEngine> > principalMatrix; /**< TODO: describe */

Expand Down Expand Up @@ -93,7 +93,8 @@ class LibreMinesGameEngine: public QObject
int iWrongFlags,
int iUnlockedCells,
double dFlagsPerSecond,
double dCellsPerSecond);
double dCellsPerSecond,
bool ignorePreferences=false);
void SIGNAL_currentTime(const ushort);
void SIGNAL_minesLeft(const ushort);
void SIGNAL_flagCell(const uchar _X, const uchar _Y);
Expand All @@ -108,6 +109,7 @@ public Q_SLOTS:
void SLOT_stop();
void SLOT_startTimer();
void SLOT_cleanNeighborCells(const uchar _X, const uchar _Y);
void SLOT_generateEndGameScoreAgain();

private Q_SLOTS:
void SLOT_UpdateTime();
Expand Down
Loading

0 comments on commit e171426

Please sign in to comment.