Fix Bug and add second counting variant

This commit is contained in:
Matti 2024-11-21 10:09:25 +01:00
parent e95f7f74d2
commit 775144421e

View File

@ -26,6 +26,7 @@ struct MineSweeperBoard {
} }
} }
} }
std::string countNeighbourBombs(int i, int j) { std::string countNeighbourBombs(int i, int j) {
i--; i--;
j--; j--;
@ -33,7 +34,8 @@ struct MineSweeperBoard {
// int counter; // Not setting this nerd to 0 can cause a bug // int counter; // Not setting this nerd to 0 can cause a bug
for (int k = 0; k < 3; k++) { for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) { for (int l = 0; l < 3; l++) {
if ((i+k < 16) && (j+l < 16) && (i-l >= 0) && (j-l >= 0)) { if ((i+k < 16) && (j+l < 16) && (i+l >= 0) && (j+l >= 0)) {
// if ((i+k < 16) && (j+l < 16) && (i-l >= 0) && (j-l >= 0)) {
if (field[i+k][j+l] == "x") { if (field[i+k][j+l] == "x") {
counter++; counter++;
} }
@ -42,6 +44,22 @@ struct MineSweeperBoard {
} }
return std::to_string(counter); return std::to_string(counter);
} }
std::string countNeighbourBombsBetter(int i, int j) {
int counter = 0;
// int counter; // Not setting this nerd to 0 can cause a bug
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if ((i+k < 16) && (j+l < 16) && (i+l >= 0) && (j+l >= 0)) {
if (field[i+k][j+l] == "x") {
counter++;
}
}
}
}
return std::to_string(counter);
}
void calculateExplosiveNeighbours() { void calculateExplosiveNeighbours() {
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) { for (int j = 0; j < 16; j++) {
@ -51,6 +69,7 @@ struct MineSweeperBoard {
} }
} }
} }
void printBoard() { void printBoard() {
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) { for (int j = 0; j < 16; j++) {