-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameSolver.cpp
More file actions
257 lines (232 loc) · 7.73 KB
/
gameSolver.cpp
File metadata and controls
257 lines (232 loc) · 7.73 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <iostream>
#include <fstream>
#include <vector>
#include <chrono>
#include <cmath>
#include <filesystem>
using namespace std;
typedef struct _solution
{
vector<vector<int>> board;
vector<pair<int, int>> clicks;
} SOLUTION;
/* helper function to display and store solutions */
string createFilename(int rows, int columns, int colorAmount)
{
string filename = to_string(rows);
filename += "x";
filename += to_string(columns);
filename += "-";
filename += to_string(colorAmount);
filename += ".txt";
return filename;
}
void printBoard(vector<vector<int>> board)
{
for (vector<int> row : board)
{
for (int item : row)
{
cout << item << " ";
}
cout << endl;
}
cout << endl;
}
void printSolution(SOLUTION solution)
{
printBoard(solution.board);
for (int i = 1; i < solution.clicks.size(); i++)
{
pair<int, int> click = solution.clicks[i];
const int row = click.first;
const int column = click.second;
cout << "(" << row << "," << column << "), ";
}
cout << endl;
}
void storeSolutionsNew(vector<int> solutions, string filename)
{
ofstream file;
file.open(filename, std::ofstream::app);
file << "New solutions per step: ";
for (int solution : solutions)
{
file << solution << ",";
}
file << endl;
file.close();
}
void storeSolution(SOLUTION solution, string filename)
{
ofstream file;
file.open(filename, std::ofstream::app);
for (int row = 0; row < solution.board.size(); row++)
{
for (int column = 0; column < solution.board[0].size(); column++)
{
file << solution.board[row][column] << " ";
}
file << "| ";
}
file << " clicks: ";
for (int i = 1; i < solution.clicks.size(); i++)
{
pair<int, int> click = solution.clicks[i];
const int row = click.first;
const int column = click.second;
file << "(" << row << "," << column << "), ";
}
file << endl;
file.close();
}
void storeSolutions(vector<SOLUTION> solutions, int step, string filename)
{
ofstream file;
file.open(filename, std::ofstream::app);
file << "===== step: " << step << "=====" << endl;
file.close();
for (SOLUTION solution : solutions)
{
storeSolution(solution, filename);
}
}
/*game logic*/
int manageColor(int color, int colorAmount)
{
return (color + 1) % colorAmount;
}
vector<std::vector<int>> clickBoard(int row, int column, vector<vector<int>> board, int colorAmount)
{
vector<vector<int>> tempBoard(board);
const int rows = tempBoard.size();
const int columns = tempBoard[0].size();
tempBoard[row][column] = manageColor(tempBoard[row][column], colorAmount);
for (int offset = -1; offset < 2; offset += 2)
{
if (row + offset >= 0 && row + offset < columns)
tempBoard[row + offset][column] = manageColor(tempBoard[row + offset][column], colorAmount);
if (column + offset >= 0 && column + offset < rows)
tempBoard[row][column + offset] = manageColor(tempBoard[row][column + offset], colorAmount);
}
return tempBoard;
}
bool checkWin(vector<vector<int>> board)
{
const int check = board[0][0];
for (vector<int> row : board)
{
for (int item : row)
{
if (check != item)
return false;
}
}
return true;
}
bool containsBoard(vector<pair<vector<vector<int>>, vector<pair<int, int>>>> solutions, vector<vector<int>> board)
{
for (pair<vector<vector<int>>, vector<pair<int, int>>> solution : solutions)
{
if (solution.first == board)
{
return true;
}
}
return false;
}
/* Solver*/
int calcIndex(vector<vector<int>> board, int colorAmount)
{
//basically x-ary coversion to decimal
const int rows = board.size();
const int columns = board[0].size();
int sum = 0;
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
sum += board[row][column] * pow(colorAmount, row * columns + column);
}
}
return sum;
}
vector<vector<int>> initBoard(int rows, int columns, int color)
{
vector<int> tempRow(columns, color);
vector<vector<int>> tempBoard(rows, tempRow);
return tempBoard;
}
void findSolutions(int rows, int columns, int colorAmount, int storeMode)
{
string filename = createFilename(rows, columns, colorAmount);
cout << "Computing all possible solutions for a " << rows << " * " << columns << " board with " << colorAmount << " colors." << endl;
//vector<SOLUTION> solutionsAll(pow(colorAmount, rows * columns), {initBoard(rows, columns, -1), vector<pair<int, int>>{make_pair(-1, -1)}});
vector<bool> solutionsAll(pow(colorAmount, rows * columns), false);
vector<SOLUTION> solutionsLast;
vector<SOLUTION> solutionsCurrent;
vector<int> solutionsNew;
bool finished = false;
for (int color = 0; color < colorAmount; color++)
{
vector<vector<int>> tempBoard(initBoard(rows, columns, color));
//solutionsAll[calcIndex(tempBoard, colorAmount)] = {tempBoard, vector<pair<int, int>>{make_pair(-1, -1)}};
solutionsLast.push_back({tempBoard, vector<pair<int, int>>{make_pair(-1, -1)}});
}
for (int step = 1; !finished; step++)
{
for (int click = 0; click < rows * columns; click++)
{
const int row = click / columns;
const int column = click % columns;
for (SOLUTION solution : solutionsLast)
{
vector<vector<int>> tempBoard = clickBoard(row, column, solution.board, colorAmount);
const int index = calcIndex(tempBoard, colorAmount);
if (!solutionsAll[index])
{
vector<pair<int, int>> tempClicks(solution.clicks);
tempClicks.push_back(make_pair(row, column));
solutionsAll[index] = true;
solutionsCurrent.push_back({tempBoard, tempClicks});
}
}
}
solutionsLast = solutionsCurrent;
solutionsNew.push_back(solutionsLast.size());
solutionsCurrent.clear();
cout << solutionsLast.size() << " new solutions found with " << step << " steps." << endl;
if (!solutionsLast.size())
{
finished = true;
cout << "Number of unique boards: " << solutionsAll.size() << "amount basically hardcoded due to vector allocation" << endl;
if (storeMode > 0)
{
storeSolutionsNew(solutionsNew, filename);
}
}
cout << storeMode << endl;
if (storeMode > 1)
{
storeSolutions(solutionsLast, step, filename);
}
}
}
int main(int argc, char *argv[])
{
if (argc < 5)
{
cout << "Too less arguments \n Usage: ./program rows[int] columns[int] colorAmount[int] storeMode[0/1/2]" << endl;
cout << "store modes: \n0: nothing\n1: just the amount of new solutions\n2: all solutions with clicks " << endl;
return 0;
}
int rows = atoi(argv[1]);
int columns = atoi(argv[2]);
int colorAmount = atoi(argv[3]);
int storeMode = atoi(argv[4]);
const auto start = chrono::steady_clock::now();
findSolutions(rows, columns, colorAmount, storeMode);
const auto end = chrono::steady_clock::now();
cout << "Done in " << chrono::duration_cast<chrono::seconds>(end - start).count() << " s!" << endl;
return 0;
}