-
Notifications
You must be signed in to change notification settings - Fork 2
/
difficultyanalyser.cpp
133 lines (110 loc) · 3.57 KB
/
difficultyanalyser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "difficultyanalyser.h"
DifficultyAnalyser::DifficultyAnalyser(QObject *parent) : QObject(parent)
{
}
QString DifficultyAnalyser::calculateDifficulty(SokoGenerator::Level level){
int pushes = calculatePushes(level.solution);
int lines = calculateLines(level.solution);
int boxes = calculateBoxes(level);
int score = 100 * (pushes + (4*lines) - (12*boxes));
int roomSize = (level.grid.size() - 2) * (level.grid[0].size() - 2);
if(score/roomSize <= 10){
return "Very Easy";
}
else if(score/roomSize >= 11 && score/roomSize <= 20){
return "Easy";
}
else if(score/roomSize >= 21 && score/roomSize <= 34){
return "Medium";
}
else if(score/roomSize >= 35 && score/roomSize <= 49){
return "Hard";
}
else if(score/roomSize >= 50){
return "Very Hard";
}
else{
return "No Value";
}
//int fC = furtherCalculations(level);
//return score/roomSize; //+= fC;
}
int DifficultyAnalyser::calculatePushes(string solution){
string chars = "lurdLURD";
int pushCount = 0;
for(int i = 0; i < solution.length(); i++){
if(chars.find(solution[i]) != string::npos && isupper(solution[i])){
pushCount++;
}
}
return pushCount;
}
int DifficultyAnalyser::calculateLines(string solution){
string chars = "lurdLURD";
int pushCount = 0;
char previousChar = ' ';
for(int i = 0; i < solution.length(); i++){
if(chars.find(solution[i]) != string::npos && isupper(solution[i])){
pushCount++;
previousChar = solution[i];
while(solution[i] == previousChar){
solution.erase(i, 1);
}
}
}
return pushCount;
}
int DifficultyAnalyser::calculateBoxes(SokoGenerator::Level level){
int boxCount = 0;
for(int y = 0; y < level.grid.size(); y++){
for(int x = 0; x < level.grid[0].size(); x++){
if(level.grid[y][x] == BOX || level.grid[y][x] == BOXONGOAL){
boxCount++;
}
}
}
return boxCount;
}
int DifficultyAnalyser::neighbourCheck(SokoGenerator::Level Level, int x, int y, char first, char second){
int count = 0;
if(Level.grid[y-1][x] == first || Level.grid[y-1][x] == second){
count++;
}
if(Level.grid[y+1][x] == first || Level.grid[y+1][x] == second){
count++;
}
if(Level.grid[y][x-1] == first || Level.grid[y][x-1] == second){
count++;
}
if(Level.grid[y][x+1] == first || Level.grid[y][x+1] == second){
count++;
}
return count;
}
int DifficultyAnalyser::furtherCalculations(SokoGenerator::Level level){
int neighbourBoxes = 0;
int neighbourWalls = 0;
int neighbourPlayer = 0;
int neighbourGoals = 0;
for(int y = 0; y < level.grid.size(); y++){
for(int x = 0; x < level.grid[0].size(); x++){
if(level.grid[y][x] == BOX){
if(neighbourCheck(level, x, y, BOX, BOXONGOAL) > 0){
neighbourBoxes++;
}
if(neighbourCheck(level, x, y, WALL) > 0){
neighbourWalls++;
}
if(neighbourCheck(level, x, y, PLAYER, PONGOAL) > 0){
neighbourPlayer++;
}
}
if(level.grid[y][x] == GOAL){
if(neighbourCheck(level, x, y, GOAL, PONGOAL) > 0){
neighbourGoals++;
}
}
}
}
return (neighbourBoxes * 30) + (neighbourWalls * -150) + (neighbourPlayer * 50) + (neighbourGoals * 30);
}