/
solver.c
166 lines (142 loc) · 4.25 KB
/
solver.c
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
#include "solver.h"
extern int groupListArrayNumber;
int solveMineSweeper(Board boardh, Board boardv) {
// Initialize groups
groupListArrayNumber = 0;
for (int x = 0; x < boardv.width; x++) {
for (int y = 0; y < boardv.height; y++) {
char cell = get(boardv, x,y);
if (cell == MINE || cell == UNKNOWN) { // Not a number!
continue;
}
Group newGroup = buildGroup(boardv, x, y);
if (newGroup.size != 0) {
addToList(newGroup);
}
}
}
while (1) {
MAYBE(printf("creation of groups complete\n"));
for (int i = 0; i < groupListArrayNumber; i++) {
for (int j = i + 1; j < groupListArrayNumber; j++) {
MAYBE(printf("G%d[%d/%d]; G%d[%d/%d]; GLAN: %d)\n", i, groupList[i].mines, groupList[i].size, j, groupList[j].mines, groupList[j].size, groupListArrayNumber));
if (groupList[i].size == groupList[j].size // Groups are
&& containedIn(groupList[i], groupList[j])) { // the same
removeFromList(j);
MAYBE(printf("remove\n"));
}
else if (containedIn(groupList[i], groupList[j])) {
//remove elements of i from j
MAYBE(printf("SUB: G%d - G%d\n", j, i));
MAYBE(printGroup(groupList[j]));
MAYBE(printGroup(groupList[i]));
groupList[j] = subtract(groupList[j],groupList[i]);
MAYBE(printGroup(groupList[j]));
}
else if (containedIn(groupList[j], groupList[i])) {
//remove elements of j from i
MAYBE(printf("SUB: G%d - G%d\n", i, j));
MAYBE(printGroup(groupList[i]));
MAYBE(printGroup(groupList[j]));
groupList[i] = subtract(groupList[i],groupList[j]);
MAYBE(printGroup(groupList[i]));
}
else if (intersectSolver(groupList[i], groupList[j])>=0) {
int x = intersectSolver(groupList[i], groupList[j]);
MAYBE(printf("FIRST: %d, SECOND: %d\n", groupList[i].mines - (groupList[i].size - x), groupList[j].mines - (groupList[j].size - x)));
MAYBE(printf("\nINTERCEPTION: mine:%d\n", x));
MAYBE(printGroup(groupList[i]));
MAYBE(printGroup(groupList[j]));
Group a, b;
a = subtract(groupList[i], groupList[j]); // LEFT
b = subtract(groupList[i], a); //isto vai dar merda!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
b.mines = x;
addToList(b);
groupList[i] = subtract(groupList[i], b);
groupList[j] = subtract(groupList[j], b);
}
else {
continue;
}
i = -1; // Reset outer for
break;
}
}
MAYBE(printf("cleaning of groups complete\n"));
bool stallAlert = 0;
int forMax = groupListArrayNumber;
for (int i = 0; i < forMax; i++){
MAYBE(printf("for i:%d forMax:%d\n",i,forMax));
int result = NEUTRAL;
if (groupList[i].mines == 0) {
for (int j =0; j < groupList[i].size; j++) {
makePlay(boardh, boardv, groupList[i].positions[j][0], groupList[i].positions[j][1], CLEAR);
MAYBE(printf("Marking Clear (%d,%d)\n", groupList[i].positions[j][0], groupList[i].positions[j][1]));
}
}
else if (groupList[i].mines == groupList[i].size) {
for (int j = 0; j < groupList[i].size; j++) {
makePlay(boardh, boardv, groupList[i].positions[j][0], groupList[i].positions[j][1], MINE);
MAYBE(printf("Marking Mine (%d,%d)\n", groupList[i].positions[j][0], groupList[i].positions[j][1]));
}
removeFromList(i);
i--;
forMax--;
}
else {
continue;
}
// Delete group
stallAlert = 1;
// maybe break;
}
for (int i = 0; i < forMax; i++){
int result = NEUTRAL;
if (groupList[i].mines == 0) {
for (int j =0; j < groupList[i].size; j++) {
Group newGroup = buildGroup(boardv, groupList[i].positions[j][0], groupList[i].positions[j][1]);
if (newGroup.size > 0) {
addToList(newGroup);
}
}
// Delete group
removeFromList(i);
i--;
forMax--;
}
}
printBoard(boardv);
if (stallAlert == 0) {
printBoard(boardv);
printBoard(boardh);
printf("Stalled!\n glan: %d \n", groupListArrayNumber);
for (int i = 0; i < groupListArrayNumber; i++) {
printGroup(groupList[i]);
}
return NEUTRAL;
}
}
}
int intersectSolver(Group a, Group b){
if (intersect(a, b)){
int i = intersect(a, b);
int minmine = MAX(
a.mines - (a.size - i),
(MAX(
b.mines - (b.size - i),
0
))
);
int maxmine = MIN(
a.mines,
(MIN(
b.mines,
i
))
);
if (minmine==maxmine){
return minmine;
}
}
return -1;
}