Skip to content

Commit bc473f8

Browse files
committed
Difficulty can now be selected through the GUI and it will only Generate levels of the chosen difficulty
1 parent 7672a38 commit bc473f8

5 files changed

+53
-16
lines changed

difficultyanalyser.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,36 @@ DifficultyAnalyser::DifficultyAnalyser(QObject *parent) : QObject(parent)
55

66
}
77

8-
int DifficultyAnalyser::calculateDifficulty(SokoGenerator::Level level){
8+
QString DifficultyAnalyser::calculateDifficulty(SokoGenerator::Level level){
99
int pushes = calculatePushes(level.solution);
1010
int lines = calculateLines(level.solution);
1111
int boxes = calculateBoxes(level);
1212
int score = 100 * (pushes + (4*lines) - (12*boxes));
13+
int roomSize = (level.grid.size() - 2) * (level.grid[0].size() - 2);
1314

14-
int fC = furtherCalculations(level);
1515

16-
return score += fC;
16+
if(score/roomSize <= 10){
17+
return "Very Easy";
18+
}
19+
else if(score/roomSize >= 11 && score/roomSize <= 20){
20+
return "Easy";
21+
}
22+
else if(score/roomSize >= 21 && score/roomSize <= 34){
23+
return "Medium";
24+
}
25+
else if(score/roomSize >= 35 && score/roomSize <= 49){
26+
return "Hard";
27+
}
28+
else if(score/roomSize >= 50){
29+
return "Very Hard";
30+
}
31+
else{
32+
return "No Value";
33+
}
34+
35+
//int fC = furtherCalculations(level);
36+
37+
//return score/roomSize; //+= fC;
1738
}
1839

1940
int DifficultyAnalyser::calculatePushes(string solution){

difficultyanalyser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class DifficultyAnalyser : public QObject
1212
Q_OBJECT
1313
public:
1414
explicit DifficultyAnalyser(QObject *parent = 0);
15-
int calculateDifficulty(SokoGenerator::Level level);
15+
QString calculateDifficulty(SokoGenerator::Level level);
1616

1717
private:
1818
int calculatePushes(string solution);

mainwindow.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#include "mainwindow.h"
22
#include "ui_mainwindow.h"
33
#include "sokogenerator.h"
4-
#include "difficultyanalyser.h"
54
#include <iostream>
65

76
SokoGenerator Generator;
8-
DifficultyAnalyser diffAnanlyser;
7+
//DifficultyAnalyser diffAnanlyser;
98

109
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),ui(new Ui::MainWindow)
1110
{
@@ -88,8 +87,8 @@ void MainWindow::addToList(int value){
8887
QString padMillis = QString("%1").arg(millis, 3, 10, QChar('0'));
8988
QString padSeconds = QString("%1").arg(seconds, 2, 10, QChar('0'));
9089
QString padMinutes = QString("%1").arg(minutes, 2, 10, QChar('0'));
91-
int difficulty = diffAnanlyser.calculateDifficulty(levels[value-1]);
92-
ui->list_LevelSet->addItem("Level " + QString::number(value) + " - " + padMinutes + ":" + padSeconds + ":" + padMillis + " - " + QString::number(difficulty));
90+
QString diff = levels[value-1].difficulty;
91+
ui->list_LevelSet->addItem("Level " + QString::number(value) + " - " + padMinutes + ":" + padSeconds + ":" + padMillis + " - " + diff);
9392
QVariant dataValue(value-1);
9493
ui->list_LevelSet->item(value-1)->setData(Qt::UserRole, dataValue);
9594
}

sokogenerator.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "mainwindow.h"
33
#include <QMessageBox>
44
#include <QDebug>
5+
#include "difficultyanalyser.h"
56

67

78
SokoGenerator::SokoGenerator(QObject *parent):QObject(parent){
@@ -105,14 +106,16 @@ void SokoGenerator::generateLevel(int roomWidth, int roomHeight, int noOfBoxes,
105106
clock_t start = clock();
106107

107108
while(!generationSuccessful && !threadStop){
108-
if(isTimeout(start, timeout)){
109+
if(isTimeout(start, timeout * 2)){
109110
generationSuccessful = false;
110111
}
111112

112113
newLevel.grid.clear();
113-
int _roomW, _roomH, _Boxes, _difficulty;
114+
int _roomW, _roomH, _Boxes;
115+
QString _difficulty;
114116
if(noOfBoxes == 0){ _Boxes = randomNumber(3, 6); } else { _Boxes = noOfBoxes; }
115-
if(difficulty == 0){ _difficulty = randomNumber(1, 5); } else { _difficulty = difficulty; }
117+
if(difficulty == 0){ difficulty = randomNumber(1, 5); } else { difficulty = difficulty; }
118+
_difficulty = difficulties[difficulty-1];
116119
if(roomWidth == 0){ _roomW = randomNumber(3, 15, 3); } else { _roomW = roomWidth; }
117120
if(roomHeight == 0){
118121
if(_roomW == 3){ _roomH = randomNumber(6, 15, 3); }
@@ -132,7 +135,13 @@ void SokoGenerator::generateLevel(int roomWidth, int roomHeight, int noOfBoxes,
132135
struct solution sol;
133136
qDebug() << "Generation Started: ";
134137
generationSuccessful = solver.solve(lvl, timeout, sol);
135-
if(generationSuccessful) newLevel.solution = cSolToString(sol);
138+
if(generationSuccessful){
139+
newLevel.solution = cSolToString(sol);
140+
diffAnalyser = new DifficultyAnalyser;
141+
newLevel.difficulty = diffAnalyser->calculateDifficulty(newLevel);
142+
if(newLevel.difficulty != _difficulty){ generationSuccessful = false; }
143+
delete diffAnalyser;
144+
}
136145
qDebug() << "Generation Successful: " << generationSuccessful;
137146
}
138147
}
@@ -325,7 +334,7 @@ bool SokoGenerator::placeGoalsAndBoxes(SokoGenerator::Level &level, int roomWidt
325334
Level deadFields;
326335

327336
while(!goalsPlaced && !threadStop){
328-
if(isTimeout(start, timeout)){
337+
if(isTimeout(start, timeout * 2)){
329338
break;
330339
}
331340

@@ -343,7 +352,7 @@ bool SokoGenerator::placeGoalsAndBoxes(SokoGenerator::Level &level, int roomWidt
343352
deadFields = calcDeadFields(level);
344353

345354
while(!boxesPlaced && !threadStop){
346-
if(isTimeout(start, timeout)){
355+
if(isTimeout(start, timeout * 2)){
347356
break;
348357
}
349358
xCoord = randomNumber(1, roomWidth);
@@ -377,7 +386,7 @@ bool SokoGenerator::placePlayer(SokoGenerator::Level &level, int roomWidth, int
377386
clock_t start = clock();
378387

379388
while(!playerPlaced && !threadStop){
380-
if(isTimeout(start, timeout)){
389+
if(isTimeout(start, timeout * 2)){
381390
break;
382391
}
383392

@@ -416,7 +425,9 @@ int SokoGenerator::neighbourCheck(SokoGenerator::Level &level, int yCoord, int x
416425
}
417426

418427
void SokoGenerator::regenerateLevel(int lvlNum){
419-
//srand(std::time(NULL));
428+
generator.seed(chrono::steady_clock::now().time_since_epoch().count());
429+
if(genSeed == 0){ genSeed = distribution(generator); }
430+
srand(genSeed);
420431
bool generationSuccessful = false;
421432
Level newLevel;
422433
QTime timer;

sokogenerator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <random>
1616
#include "solvercpp/solver.h"
1717

18+
class DifficultyAnalyser;
19+
1820
using namespace std;
1921

2022
#define WALL '#'
@@ -41,12 +43,14 @@ class SokoGenerator : public QObject{
4143
TwoDVector_char grid;
4244
string solution;
4345
int generationTime;
46+
QString difficulty;
4447
};
4548

4649
explicit SokoGenerator(QObject *parent = 0);
4750
~SokoGenerator();
4851

4952
Solver solver;
53+
DifficultyAnalyser* diffAnalyser;
5054
std::default_random_engine generator;
5155
std::uniform_int_distribution<int> distribution;
5256

@@ -104,6 +108,8 @@ class SokoGenerator : public QObject{
104108
bool regenLevel = false;
105109
int regenLvlNum = 0;
106110

111+
QString difficulties[5] = { "Very Easy", "Easy", "Medium", "Hard", "Very Hard" };
112+
107113
std::vector<Level> levels;
108114
std::vector<Level> patterns;
109115

0 commit comments

Comments
 (0)