Browse Source

Merge branch 'Daniel' into 'main'

refactoring : Entfern und hinzufügen von weiteren kommetaren um besser die...

See merge request fdai7745/duellist-spielesammlung-projekt!16
remotes/origin/Areeb
fdai7745 11 months ago
parent
commit
1e51f9a0c5
  1. BIN
      build/test/out/c/duellist-spielesammlung-projekt.o
  2. BIN
      build/test/out/test_duellist_spielesammlung_projekt.out
  3. 2
      build/test/results/test_duellist_spielesammlung_projekt.pass
  4. 68
      src/main/duellist-spielesammlung-projekt.c

BIN
build/test/out/c/duellist-spielesammlung-projekt.o

BIN
build/test/out/test_duellist_spielesammlung_projekt.out

2
build/test/results/test_duellist_spielesammlung_projekt.pass

@ -115,4 +115,4 @@
:failed: 0
:ignored: 0
:stdout: []
:time: 0.0499390999998468
:time: 0.05030940000051487

68
src/main/duellist-spielesammlung-projekt.c

@ -8,6 +8,8 @@
#define unknown_character 'x'
#define mine_character '@'
/*--------------------TicTacTo--------------------*/
int checkAge(int age) {
return age >= MIN_AGE ? 1 : 0;
}
@ -138,17 +140,21 @@ GameResult checkGameResult(const TicTacToeGame* game) {
return draw ? GAME_DRAW : SUCCESS;
}
/*--------------------Minsweeper--------------------*/
//Nach eingabe zurberechnung der Minen anzahl
int calculate_mines(int height, int width) {
int num_mines;
int number_mines;
if (height <= 1 || width <= 1) {
return 0;
}
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** minefield = (char**)calloc(height, sizeof(char*));
for (int i = 0; i < height; i++) {
@ -162,6 +168,7 @@ char** create_minefield(int height, int width, char within) {
return minefield;
}
//Zum Verteilen der Minen
void distribute_mines(char** mines, int height, int width, int num_mines) {
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) {
int i, j;
@ -214,19 +222,17 @@ void show_minefield(char** minefield, int height, int width) {
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) {
char counter = 0;
if ((row < 0) || (row >= height) || (column < 0) || (column >= width)) {
printf("Exception: Outside the minefield!\n");
printf("Auserhalb des Minenfeldes!\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++;
}
@ -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)) {
field_check(minefield, mines, height, width, row - 1, column - 1);
}
return 1;
}
//Überprüfe ob eine Mine getroffen wurde
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;
}
//befreie anliegendes Feld
void free_minefield(char** minefield, int height) {
int i;
// Free each vector
for (i = 0; i < height; i++) {
free(minefield[i]);
}
@ -318,63 +321,60 @@ void free_minefield(char** minefield, int height) {
minefield = NULL;
}
//Start des Spiels
int minesweeper() {
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);
printf("Enter the width of the game field:\n");
printf("Geben Sie breite des Spielfeldes ein:\n");
scanf("%d", &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;
}
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** 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");
while (1) {
printf("Geben 'vertikale horizonatle' ein um zu spielen Bsp vertikal 0 und horizontal: 0\n");
if (scanf("%d %d", &row, &column) != 2) {
printf("Incorrect input!\n");
printf("Falsche eingabe!\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);
printf("hoehe zwischen 1 und %d!\n", height);
continue;
}
if (column >= width || column < 0) {
printf("Column must be between 1 and %d!\n", width);
printf("breite zwischen 1 und %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);
printf("höhe: %d breite: %d war leider eine 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");
printf("Sie haben es erfolgreich geloesst!\n");
break;
}
// Show current field to continue playing
else {
show_minefield(mines, height, width);
}

Loading…
Cancel
Save