diff --git a/src/main/duellist-spielesammlung-projekt.c b/src/main/duellist-spielesammlung-projekt.c index 9ff0743..9aa1d88 100644 --- a/src/main/duellist-spielesammlung-projekt.c +++ b/src/main/duellist-spielesammlung-projekt.c @@ -214,6 +214,85 @@ void show_minefield(char** minefield, int height, int width) { printf("\n\n"); } +int field_check(char** minefield, char** mines, int height, int width, int row, int column) { + char counter = 0; + + if ((row < 0) || (row >= height) || (column < 0) || (column >= width)) { + printf("Exception: Outside the minefield!\n"); + return -1; + } + // If there is a mine at this position + if (mines[row][column] == 1) { + return 0; + } + + // If there is no mine at the position, count how many mines are around it + if ((row + 1 < height) && (mines[row + 1][column] == 1)) { + counter++; + } + if ((row - 1 >= 0) && (mines[row - 1][column] == 1)) { + counter++; + } + if ((column + 1 < width) && (mines[row][column + 1] == 1)) { + counter++; + } + if ((column - 1 >= 0) && (mines[row][column - 1] == 1)) { + counter++; + } + if ((row + 1 < height) && (column - 1 >= 0) && (mines[row + 1][column - 1] == 1)) { + counter++; + } + if ((row - 1 >= 0) && (column + 1 < width) && (mines[row - 1][column + 1] == 1)) { + counter++; + } + if ((row + 1 < height) && (column + 1 < width) && (mines[row + 1][column + 1] == 1)) { + counter++; + } + if ((row - 1 >= 0) && (column - 1 >= 0) && (mines[row - 1][column - 1] == 1)) { + counter++; + } + + minefield[row][column] = 48 + counter; + + if (counter > 0) { + return 1; + } + + if ((row + 1 < height) && (minefield[row + 1][column] == unknown_character) && (mines[row + 1][column] == 0)) { + field_check(minefield, mines, height, width, row + 1, column); + } + + if ((row - 1 >= 0) && (minefield[row - 1][column] == unknown_character) && (mines[row - 1][column] == 0)) { + field_check(minefield, mines, height, width, row - 1, column); + } + + if ((column + 1 < width) && (minefield[row][column + 1] == unknown_character) && (mines[row][column + 1] == 0)) { + field_check(minefield, mines, height, width, row, column + 1); + } + + if ((column - 1 >= 0) && (minefield[row][column - 1] == unknown_character) && (mines[row][column - 1] == 0)) { + field_check(minefield, mines, height, width, row, column - 1); + } + + if ((row + 1 < height) && (column - 1 >= 0) && (minefield[row + 1][column - 1] == unknown_character) && (mines[row + 1][column - 1] == 0)) { + field_check(minefield, mines, height, width, row + 1, column - 1); + } + + if ((row - 1 >= 0) && (column + 1 < width) && (minefield[row - 1][column + 1] == unknown_character) && (mines[row - 1][column + 1] == 0)) { + field_check(minefield, mines, height, width, row - 1, column + 1); + } + + if ((row + 1 < height) && (column + 1 < width) && (minefield[row + 1][column + 1] == unknown_character) && (mines[row + 1][column + 1] == 0)) { + field_check(minefield, mines, height, width, row + 1, column + 1); + } + + if ((row - 1 >= 0) && (column - 1 >= 0) && (minefield[row - 1][column - 1] == unknown_character) && (mines[row - 1][column - 1] == 0)) { + field_check(minefield, mines, height, width, row - 1, column - 1); + } + + return 1; +} + int minesweeper() { int height, width, row, column; printf("Enter the height of the game field:\n");