|
@ -8,6 +8,8 @@ |
|
|
#define unknown_character 'x' |
|
|
#define unknown_character 'x' |
|
|
#define mine_character '@' |
|
|
#define mine_character '@' |
|
|
|
|
|
|
|
|
|
|
|
/*--------------------TicTacTo--------------------*/ |
|
|
|
|
|
|
|
|
int checkAge(int age) { |
|
|
int checkAge(int age) { |
|
|
return age >= MIN_AGE ? 1 : 0; |
|
|
return age >= MIN_AGE ? 1 : 0; |
|
|
} |
|
|
} |
|
@ -138,17 +140,21 @@ GameResult checkGameResult(const TicTacToeGame* game) { |
|
|
return draw ? GAME_DRAW : SUCCESS; |
|
|
return draw ? GAME_DRAW : SUCCESS; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*--------------------Minsweeper--------------------*/ |
|
|
|
|
|
|
|
|
|
|
|
//Nach eingabe zurberechnung der Minen anzahl |
|
|
int calculate_mines(int height, int width) { |
|
|
int calculate_mines(int height, int width) { |
|
|
int num_mines; |
|
|
|
|
|
|
|
|
int number_mines; |
|
|
if (height <= 1 || width <= 1) { |
|
|
if (height <= 1 || width <= 1) { |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
else { |
|
|
else { |
|
|
num_mines = ((height * width) / 4); |
|
|
|
|
|
|
|
|
number_mines = ((height * width) / 4); |
|
|
} |
|
|
} |
|
|
return num_mines; |
|
|
|
|
|
|
|
|
return number_mines; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Zum erstellen des Spielfeldes |
|
|
char** create_minefield(int height, int width, char within) { |
|
|
char** create_minefield(int height, int width, char within) { |
|
|
char** minefield = (char**)calloc(height, sizeof(char*)); |
|
|
char** minefield = (char**)calloc(height, sizeof(char*)); |
|
|
for (int i = 0; i < height; i++) { |
|
|
for (int i = 0; i < height; i++) { |
|
@ -162,6 +168,7 @@ char** create_minefield(int height, int width, char within) { |
|
|
return minefield; |
|
|
return minefield; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Zum Verteilen der Minen |
|
|
void distribute_mines(char** mines, int height, int width, int num_mines) { |
|
|
void distribute_mines(char** mines, int height, int width, int num_mines) { |
|
|
int i, rand_height, rand_width; |
|
|
int i, rand_height, rand_width; |
|
|
|
|
|
|
|
@ -179,6 +186,7 @@ void distribute_mines(char** mines, int height, int width, int num_mines) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//User das Minenfeld zu zeigen |
|
|
void show_minefield(char** minefield, int height, int width) { |
|
|
void show_minefield(char** minefield, int height, int width) { |
|
|
int i, j; |
|
|
int i, j; |
|
|
|
|
|
|
|
@ -214,19 +222,17 @@ void show_minefield(char** minefield, int height, int width) { |
|
|
printf("\n\n"); |
|
|
printf("\n\n"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Ist keine Mine in der nähe wird die freie Fläche erweitert |
|
|
int field_check(char** minefield, char** mines, int height, int width, int row, int column) { |
|
|
int field_check(char** minefield, char** mines, int height, int width, int row, int column) { |
|
|
char counter = 0; |
|
|
char counter = 0; |
|
|
|
|
|
|
|
|
if ((row < 0) || (row >= height) || (column < 0) || (column >= width)) { |
|
|
if ((row < 0) || (row >= height) || (column < 0) || (column >= width)) { |
|
|
printf("Exception: Outside the minefield!\n"); |
|
|
|
|
|
|
|
|
printf("Auserhalb des Minenfeldes!\n"); |
|
|
return -1; |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
// If there is a mine at this position |
|
|
|
|
|
|
|
|
|
|
|
if (mines[row][column] == 1) { |
|
|
if (mines[row][column] == 1) { |
|
|
return 0; |
|
|
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)) { |
|
|
if ((row + 1 < height) && (mines[row + 1][column] == 1)) { |
|
|
counter++; |
|
|
counter++; |
|
|
} |
|
|
} |
|
@ -289,28 +295,25 @@ int field_check(char** minefield, char** mines, int height, int width, int row, |
|
|
if ((row - 1 >= 0) && (column - 1 >= 0) && (minefield[row - 1][column - 1] == unknown_character) && (mines[row - 1][column - 1] == 0)) { |
|
|
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); |
|
|
field_check(minefield, mines, height, width, row - 1, column - 1); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return 1; |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Überprüfe ob eine Mine getroffen wurde |
|
|
int end_check(char** minefield, char** mines, int height, int width) { |
|
|
int end_check(char** minefield, char** mines, int height, int width) { |
|
|
int i, j; |
|
|
int i, j; |
|
|
|
|
|
|
|
|
for (i = 0; i < height; i++) { |
|
|
for (i = 0; i < height; i++) { |
|
|
for (j = 0; j < width; j++) { |
|
|
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)) { |
|
|
if ((mines[i][j] == 0) && (minefield[i][j] == unknown_character)) { |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return 1; |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//befreie anliegendes Feld |
|
|
void free_minefield(char** minefield, int height) { |
|
|
void free_minefield(char** minefield, int height) { |
|
|
int i; |
|
|
int i; |
|
|
// Free each vector |
|
|
|
|
|
for (i = 0; i < height; i++) { |
|
|
for (i = 0; i < height; i++) { |
|
|
free(minefield[i]); |
|
|
free(minefield[i]); |
|
|
} |
|
|
} |
|
@ -318,63 +321,60 @@ void free_minefield(char** minefield, int height) { |
|
|
minefield = NULL; |
|
|
minefield = NULL; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Start des Spiels |
|
|
int minesweeper() { |
|
|
int minesweeper() { |
|
|
int height, width, row, column; |
|
|
int height, width, row, column; |
|
|
printf("Enter the height of the game field:\n"); |
|
|
|
|
|
|
|
|
printf("Geben Sie die Grösse des spielfeldes ein:\n"); |
|
|
scanf("%d", &height); |
|
|
scanf("%d", &height); |
|
|
printf("Enter the width of the game field:\n"); |
|
|
|
|
|
|
|
|
printf("Geben Sie breite des Spielfeldes ein:\n"); |
|
|
scanf("%d", &width); |
|
|
scanf("%d", &width); |
|
|
|
|
|
|
|
|
if (height <= 0 || width <= 0 || height > max_height || width > max_width) { |
|
|
if (height <= 0 || width <= 0 || height > max_height || width > max_width) { |
|
|
printf("Invalid field dimensions.\n"); |
|
|
|
|
|
|
|
|
printf("unpassen form gewählt.\n"); |
|
|
return 1; |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
int num_mines = calculate_mines(height, width); |
|
|
int num_mines = calculate_mines(height, width); |
|
|
printf("The game field is >%d< in height and >%d< in width.\n", height, width); |
|
|
|
|
|
printf("There are %d mines on the game field.\n", num_mines); |
|
|
|
|
|
// Matrix for the game field |
|
|
|
|
|
|
|
|
printf("Das spielfeld hat eine groesse von >%d< und eine breite von: >%d<.\n", height, width); |
|
|
|
|
|
printf("Es gibt %d Minen auf dem Spielfeld.\n", num_mines); |
|
|
|
|
|
|
|
|
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"); |
|
|
|
|
|
|
|
|
while (1) { |
|
|
|
|
|
printf("Geben 'vertikale horizonatle' ein um zu spielen Bsp vertikal 0 und horizontal: 0\n"); |
|
|
if (scanf("%d %d", &row, &column) != 2) { |
|
|
if (scanf("%d %d", &row, &column) != 2) { |
|
|
printf("Incorrect input!\n"); |
|
|
|
|
|
|
|
|
printf("Falsche eingabe!\n"); |
|
|
return 1; |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
row--; |
|
|
row--; |
|
|
column--; |
|
|
column--; |
|
|
|
|
|
|
|
|
// Quit |
|
|
|
|
|
if ((row == -1) && (column == -1)) { |
|
|
if ((row == -1) && (column == -1)) { |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
// Check field boundaries |
|
|
|
|
|
if (row >= height || row < 0) { |
|
|
if (row >= height || row < 0) { |
|
|
printf("Row must be between 1 and %d!\n", height); |
|
|
|
|
|
|
|
|
printf("hoehe zwischen 1 und %d!\n", height); |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
if (column >= width || column < 0) { |
|
|
if (column >= width || column < 0) { |
|
|
printf("Column must be between 1 and %d!\n", width); |
|
|
|
|
|
|
|
|
printf("breite zwischen 1 und %d!\n", width); |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Did you land on a mine? |
|
|
|
|
|
if (field_check(minefield, mines, height, width, row, column) == 0) { |
|
|
if (field_check(minefield, mines, height, width, row, column) == 0) { |
|
|
// Show the mines |
|
|
|
|
|
show_minefield(mines, height, width); |
|
|
show_minefield(mines, height, width); |
|
|
printf("Row: %d Column: %d was unfortunately a mine!\n", row + 1, column + 1); |
|
|
|
|
|
|
|
|
printf("höhe: %d breite: %d war leider eine mine!\n", row + 1, column + 1); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
// Are you done? |
|
|
|
|
|
|
|
|
|
|
|
else if (end_check(minefield, mines, height, width)) { |
|
|
else if (end_check(minefield, mines, height, width)) { |
|
|
// Show the mines |
|
|
|
|
|
show_minefield(mines, height, width); |
|
|
show_minefield(mines, height, width); |
|
|
printf("Great! Solved correctly!\n"); |
|
|
|
|
|
|
|
|
printf("Sie haben es erfolgreich geloesst!\n"); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
// Show current field to continue playing |
|
|
|
|
|
|
|
|
|
|
|
else { |
|
|
else { |
|
|
show_minefield(mines, height, width); |
|
|
show_minefield(mines, height, width); |
|
|
} |
|
|
} |
|
|