|
@ -293,6 +293,31 @@ int field_check(char** minefield, char** mines, int height, int width, int row, |
|
|
return 1; |
|
|
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 minesweeper() { |
|
|
int height, width, row, column; |
|
|
int height, width, row, column; |
|
|
printf("Enter the height of the game field:\n"); |
|
|
printf("Enter the height of the game field:\n"); |
|
@ -310,6 +335,52 @@ int minesweeper() { |
|
|
char** minefield = create_minefield(height, width, unknown_character); |
|
|
char** minefield = create_minefield(height, width, unknown_character); |
|
|
char** mines = create_minefield(height, width, 0); |
|
|
char** mines = create_minefield(height, width, 0); |
|
|
distribute_mines(mines, height, width, num_mines); |
|
|
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; |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|