diff --git a/build/test/out/c/duellist-spielesammlung-projekt.o b/build/test/out/c/duellist-spielesammlung-projekt.o index ca060d1..c4f8ca1 100644 Binary files a/build/test/out/c/duellist-spielesammlung-projekt.o and b/build/test/out/c/duellist-spielesammlung-projekt.o differ diff --git a/build/test/out/test_duellist_spielesammlung_projekt.out b/build/test/out/test_duellist_spielesammlung_projekt.out index 003d98b..202fa72 100644 Binary files a/build/test/out/test_duellist_spielesammlung_projekt.out and b/build/test/out/test_duellist_spielesammlung_projekt.out differ diff --git a/build/test/results/test_duellist_spielesammlung_projekt.pass b/build/test/results/test_duellist_spielesammlung_projekt.pass index 1ad831c..2f9ec3f 100644 --- a/build/test/results/test_duellist_spielesammlung_projekt.pass +++ b/build/test/results/test_duellist_spielesammlung_projekt.pass @@ -75,4 +75,4 @@ :failed: 0 :ignored: 0 :stdout: [] -:time: 0.04396939999423921 +:time: 0.04444100000000617 diff --git a/src/main/duellist-spielesammlung-projekt.c b/src/main/duellist-spielesammlung-projekt.c index 9aa1d88..d0a7c8b 100644 --- a/src/main/duellist-spielesammlung-projekt.c +++ b/src/main/duellist-spielesammlung-projekt.c @@ -293,6 +293,31 @@ int field_check(char** minefield, char** mines, int height, int width, int row, return 1; } +int end_check(char** minefield, char** mines, int height, int width) { + int i, j; + + for (i = 0; i < height; i++) { + for (j = 0; j < width; j++) { + // If a field is not uncovered and does not contain a mine, the game is not over + if ((mines[i][j] == 0) && (minefield[i][j] == unknown_character)) { + return 0; + } + } + } + + return 1; +} + +void free_minefield(char** minefield, int height) { + int i; + // Free each vector + for (i = 0; i < height; i++) { + free(minefield[i]); + } + free(minefield); + minefield = NULL; +} + int minesweeper() { int height, width, row, column; printf("Enter the height of the game field:\n"); @@ -310,6 +335,52 @@ int minesweeper() { char** minefield = create_minefield(height, width, unknown_character); char** mines = create_minefield(height, width, 0); distribute_mines(mines, height, width, num_mines); + while (1) { + + printf("Enter 'row column' to uncover (to quit enter '0 0'):\n"); + if (scanf("%d %d", &row, &column) != 2) { + printf("Incorrect input!\n"); + return 1; + } + + row--; + column--; + + // Quit + if ((row == -1) && (column == -1)) { + break; + } + // Check field boundaries + if (row >= height || row < 0) { + printf("Row must be between 1 and %d!\n", height); + continue; + } + if (column >= width || column < 0) { + printf("Column must be between 1 and %d!\n", width); + continue; + } + + // Did you land on a mine? + if (field_check(minefield, mines, height, width, row, column) == 0) { + // Show the mines + show_minefield(mines, height, width); + printf("Row: %d Column: %d was unfortunately a mine!\n", row + 1, column + 1); + break; + } + // Are you done? + else if (end_check(minefield, mines, height, width)) { + // Show the mines + show_minefield(mines, height, width); + printf("Great! Solved correctly!\n"); + break; + } + // Show current field to continue playing + else { + show_minefield(mines, height, width); + } + } + free_minefield(minefield, height); + free_minefield(mines, height); return 1; }