From 7450135530b2079a2910e6b7a6aeba980f0fab11 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 11:42:09 +0100 Subject: [PATCH 01/18] minesweeper rough structure --- src/main/c/Minesweeper/minesweeper_start.c | 62 ++++++++++++++++++++++ src/main/c/Minesweeper/minesweeper_start.h | 16 ++++++ src/main/c/Snake/snake_start.c | 22 ++++---- src/main/c/Snake/snake_start.h | 8 +-- src/main/c/main.c | 12 ++--- 5 files changed, 98 insertions(+), 22 deletions(-) create mode 100644 src/main/c/Minesweeper/minesweeper_start.c create mode 100644 src/main/c/Minesweeper/minesweeper_start.h diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c new file mode 100644 index 0000000..7b28bd1 --- /dev/null +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -0,0 +1,62 @@ +#include +#include +#define BLOCK 219 +#define FLAG '?' +#define EMPTY ' ' + + +#pragma region Funktion_heads +void main_menu_minesweeper(); +void game_minesweeper(); +void options_minesweeper(); +#pragma endregion + +#pragma region Global +#pragma endregion //Global + + +int minesweeper_start(){ + system("clear"); + main_menu_minesweeper(); +} + +void main_menu_minesweeper(){ + bool running = true; + while (running){ + int option = 0; + + system("clear"); + printf("Waehlen Sie eine Option:\n"); + printf("\t1.Start\n"); + printf("\t2.Options\n"); + printf("\t3.Exit\n"); + + scanf("%d", &option); + getchar(); + + system("clear"); + + switch (option){ + case 1: + game_minesweeper(); + break; + case 2: + options_minesweeper(); + break; + case 3: + running = false; + break; + + default: + break; + } + } +} + +void game_minesweeper(){ + +} + +void options_minesweeper(){ + +} \ No newline at end of file diff --git a/src/main/c/Minesweeper/minesweeper_start.h b/src/main/c/Minesweeper/minesweeper_start.h new file mode 100644 index 0000000..1bb6a7d --- /dev/null +++ b/src/main/c/Minesweeper/minesweeper_start.h @@ -0,0 +1,16 @@ +#ifndef MINESWEEPER_START_H +#define MINESWEEPER_START_H + +typedef struct Minesweeper_Board{ + unsigned int width; + unsigned int height; + char *tiles; + int num_bombs; + unsigned int bombs[]; +}Minesweeper_Board; + + +void minesweeper_start(); + + +#endif // MINESWEEPER_START_H \ No newline at end of file diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index f6b7996..f3ce799 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -5,15 +5,17 @@ #include #include "snake_start.h" #include "get_character.h" +#define WIDTH 15 +#define HEIGHT 15 #define TURN 0.5 #define START_LENGTH 3 #define START_DIRECTION 1 #define START_TILE 8 * 16 + 8 #pragma region Funktion_heads -void main_menu(); -void game(); -void options(); +void main_menu_snake(); +void game_snake(); +void options_snake(); Snake initialize_snake(); void get_next_move(double limit, Snake *snake, bool *running); void move_snake(Snake *snake); @@ -30,10 +32,10 @@ double TIME_TURN = 0.5; void snake_start(){ system("clear"); - main_menu(); + main_menu_snake(); } -void main_menu(){ +void main_menu_snake(){ bool running = true; while (running){ int option = 0; @@ -51,10 +53,10 @@ void main_menu(){ switch (option){ case 1: - game(); + game_snake(); break; case 2: - options(); + options_snake(); break; case 3: running = false; @@ -66,7 +68,7 @@ void main_menu(){ } } -void game(){ +void game_snake(){ Snake snake = initialize_snake(); bool running = true; clock_t t = clock(); @@ -84,12 +86,12 @@ void game(){ } } -void options(){ +void options_snake(){ int difficulty = 1; system("clear"); printf("Please select a difficulty(1 - 10): "); scanf("%d", &difficulty); - TIME_TURN = TURN / difficulty;//(11 - difficulty) * TURN / 10; + TIME_TURN = TURN / difficulty; } Snake initialize_snake(){ diff --git a/src/main/c/Snake/snake_start.h b/src/main/c/Snake/snake_start.h index 1634146..359f548 100644 --- a/src/main/c/Snake/snake_start.h +++ b/src/main/c/Snake/snake_start.h @@ -1,14 +1,10 @@ #ifndef SNAKE_START_H #define SNAKE_START_H -#define HEIGHT 15 -#define WIDTH 15 -#define AREA HEIGHT * WIDTH - -typedef struct{ +typedef struct Snake{ signed char direction; char length; - unsigned char segments[AREA]; + unsigned char segments[225]; }Snake; diff --git a/src/main/c/main.c b/src/main/c/main.c index 922c8d4..11be6b7 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -2,8 +2,8 @@ #include #include -#include "Template/game100.h" #include "Snake/snake_start.h" +#include "Minesweeper/minesweeper_start.h" int main(){ bool running = true; @@ -18,8 +18,8 @@ int main(){ printf("\t2.Spiel2 starten\n"); printf("\t3.Snake starten\n"); printf("\t4.Spiel4 starten\n"); - printf("\t100.Template starten\n"); - printf("\t6.Exit\n"); + printf("\t7.Minesweeper starten\n"); + printf("\t10.Exit\n"); scanf("%d", &option); getchar(); @@ -37,10 +37,10 @@ int main(){ case 4: //start_game4(); break; - case 100: - start_game100(); + case 7: + minesweeper_start(); break; - case 6: + case 10: system("clear"); running = false; break; From 7f81bd04dd2cccac2e941e44a5a8e1102681ca06 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 12:31:57 +0100 Subject: [PATCH 02/18] added game options --- src/main/c/Minesweeper/minesweeper_start.c | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 7b28bd1..2737d4d 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -1,8 +1,16 @@ #include #include +#pragma region defines #define BLOCK 219 #define FLAG '?' #define EMPTY ' ' +#define SMALL 10 +#define MIDDLE 15 +#define LARGE 20 +#define EASY 7 +#define NORMAL 5 +#define HARD 3 +#pragma endregion #pragma region Funktion_heads @@ -12,6 +20,9 @@ void options_minesweeper(); #pragma endregion #pragma region Global +unsigned int width = MIDDLE; +unsigned int height = MIDDLE; +int num_bombs = MIDDLE * MIDDLE / NORMAL; #pragma endregion //Global @@ -58,5 +69,54 @@ void game_minesweeper(){ } void options_minesweeper(){ + bool running = true; + while (running){ + int option = 0; + + system("clear"); + printf("Waehlen Sie eine Option:\n"); + printf("\t1.Schwierigkeit\t\t%s\n", num_bombs < 2 * width * height / (NORMAL + EASY) ? "Einfach" : num_bombs < 2 * width * height / (NORMAL + HARD) ? "Normal" : "Schwer"); + printf("\t2.Groesse\t\t%s\n", width * height < MIDDLE * SMALL ? "Klein" : width * height < MIDDLE * LARGE ? "Mittel" : "Gross"); + printf("\t3.Custom\n"); + printf("\t4.Exit\n"); + + scanf("%d", &option); + getchar(); + switch (option){ + case 1: + printf("Bitte neu Schwierigkeit eingeben (1 - 3):"); + scanf("%d", &num_bombs); + getchar(); + num_bombs = num_bombs == 1 ? EASY : num_bombs == 2 ? NORMAL : HARD; + num_bombs = width * height / num_bombs; + break; + case 2: + num_bombs = num_bombs < width * height / NORMAL ? EASY : num_bombs == width * height / NORMAL ? NORMAL : HARD; + printf("Bitte neu Groesse eingeben (1 - 3):"); + scanf("%d", &width); + getchar(); + width = width == 1 ? SMALL : width == 2 ? MIDDLE : LARGE; + height = width; + num_bombs = width * height / num_bombs; + break; + case 3: + printf("Bitte Breite des Spielfeld eingeben:"); + scanf("%d", &width); + getchar(); + printf("Bitte Hoehe des Spielfeld eingeben:"); + scanf("%d", &height); + getchar(); + printf("Bitte Anzahl der Bomben eingeben:"); + scanf("%d", &num_bombs); + getchar(); + break; + case 4: + running = false; + break; + + default: + break; + } + } } \ No newline at end of file From c1e8a99e7a7b9a7895f0ea396466170a5d49947d Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 12:55:28 +0100 Subject: [PATCH 03/18] initialize minesweeper --- src/main/c/Minesweeper/minesweeper_start.c | 25 ++++++++++++++++++++-- src/main/c/Minesweeper/minesweeper_start.h | 2 +- src/main/c/Snake/snake_start.c | 6 +++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 2737d4d..3f46eec 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -1,5 +1,7 @@ #include +#include #include +#include "minesweeper_start.h" #pragma region defines #define BLOCK 219 #define FLAG '?' @@ -17,6 +19,8 @@ void main_menu_minesweeper(); void game_minesweeper(); void options_minesweeper(); +Minesweeper_Board initialize_minesweeper(); +void draw_minesweeper(); #pragma endregion #pragma region Global @@ -26,7 +30,7 @@ int num_bombs = MIDDLE * MIDDLE / NORMAL; #pragma endregion //Global -int minesweeper_start(){ +void minesweeper_start(){ system("clear"); main_menu_minesweeper(); } @@ -65,7 +69,8 @@ void main_menu_minesweeper(){ } void game_minesweeper(){ - + Minesweeper_Board board = initialize_minesweeper(); + draw_minesweeper(); } void options_minesweeper(){ @@ -119,4 +124,20 @@ void options_minesweeper(){ break; } } +} + +Minesweeper_Board initialize_minesweeper(){ + Minesweeper_Board board; + board.width = width; + board.height = height; + char *tiles = (char*) malloc(width * height * sizeof(char)); + for(int i = 0; i < width * height; i++){tiles[i] = FLAG;} + board.tiles = tiles; + board.num_bombs = num_bombs; + + return board; +} + +void draw_minesweeper(){ + } \ No newline at end of file diff --git a/src/main/c/Minesweeper/minesweeper_start.h b/src/main/c/Minesweeper/minesweeper_start.h index 1bb6a7d..bf0f21a 100644 --- a/src/main/c/Minesweeper/minesweeper_start.h +++ b/src/main/c/Minesweeper/minesweeper_start.h @@ -6,7 +6,7 @@ typedef struct Minesweeper_Board{ unsigned int height; char *tiles; int num_bombs; - unsigned int bombs[]; + int *bombs; }Minesweeper_Board; diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index f3ce799..26c83c4 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -19,7 +19,7 @@ void options_snake(); Snake initialize_snake(); void get_next_move(double limit, Snake *snake, bool *running); void move_snake(Snake *snake); -void draw(Snake *snake, unsigned char fruit); +void draw_snake(Snake *snake, unsigned char fruit); int part_of_snake(Snake *snake, unsigned char tile); bool check_if_dead(Snake *snake); unsigned char spawn_fruit(Snake *snake); @@ -76,7 +76,7 @@ void game_snake(){ while (running){ system("clear"); - draw(&snake, fruit); + draw_snake(&snake, fruit); if(check_if_dead(&snake)){break;} t = clock() - t; get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running); @@ -141,7 +141,7 @@ void move_snake(Snake *snake){ snake->segments[0] += snake->direction; } -void draw(Snake *snake, unsigned char fruit){ +void draw_snake(Snake *snake, unsigned char fruit){ printf("Score:%d Speed:%f\n", snake->length - START_LENGTH, TIME_TURN); printf("+"); for(int i = 0; i < WIDTH; i++){printf("-");} From e1afeb3974b1a2fc4b4a8a254bf091d36e6621f4 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 13:23:22 +0100 Subject: [PATCH 04/18] draw minesweeper --- src/main/c/Minesweeper/minesweeper_start.c | 46 ++++++++++++++++------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 3f46eec..8dcc78f 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -3,7 +3,7 @@ #include #include "minesweeper_start.h" #pragma region defines -#define BLOCK 219 +#define BLOCK '#' #define FLAG '?' #define EMPTY ' ' #define SMALL 10 @@ -20,7 +20,7 @@ void main_menu_minesweeper(); void game_minesweeper(); void options_minesweeper(); Minesweeper_Board initialize_minesweeper(); -void draw_minesweeper(); +void draw_minesweeper(Minesweeper_Board board); #pragma endregion #pragma region Global @@ -69,8 +69,15 @@ void main_menu_minesweeper(){ } void game_minesweeper(){ - Minesweeper_Board board = initialize_minesweeper(); - draw_minesweeper(); + bool running = true; + int q = 0; + while (running){ + Minesweeper_Board board = initialize_minesweeper(); + draw_minesweeper(board); + + scanf("%d", &q); + if (q == 1){break;} + } } void options_minesweeper(){ @@ -90,7 +97,7 @@ void options_minesweeper(){ switch (option){ case 1: - printf("Bitte neu Schwierigkeit eingeben (1 - 3):"); + printf("Bitte neu Schwierigkeit eingeben (1 - 3): "); scanf("%d", &num_bombs); getchar(); num_bombs = num_bombs == 1 ? EASY : num_bombs == 2 ? NORMAL : HARD; @@ -98,7 +105,7 @@ void options_minesweeper(){ break; case 2: num_bombs = num_bombs < width * height / NORMAL ? EASY : num_bombs == width * height / NORMAL ? NORMAL : HARD; - printf("Bitte neu Groesse eingeben (1 - 3):"); + printf("Bitte neu Groesse eingeben (1 - 3): "); scanf("%d", &width); getchar(); width = width == 1 ? SMALL : width == 2 ? MIDDLE : LARGE; @@ -106,13 +113,13 @@ void options_minesweeper(){ num_bombs = width * height / num_bombs; break; case 3: - printf("Bitte Breite des Spielfeld eingeben:"); + printf("Bitte Breite des Spielfeld eingeben: "); scanf("%d", &width); getchar(); - printf("Bitte Hoehe des Spielfeld eingeben:"); + printf("Bitte Hoehe des Spielfeld eingeben: "); scanf("%d", &height); getchar(); - printf("Bitte Anzahl der Bomben eingeben:"); + printf("Bitte Anzahl der Bomben eingeben: "); scanf("%d", &num_bombs); getchar(); break; @@ -131,13 +138,28 @@ Minesweeper_Board initialize_minesweeper(){ board.width = width; board.height = height; char *tiles = (char*) malloc(width * height * sizeof(char)); - for(int i = 0; i < width * height; i++){tiles[i] = FLAG;} + for(int i = 0; i < width * height; i++){tiles[i] = BLOCK;} board.tiles = tiles; board.num_bombs = num_bombs; return board; } -void draw_minesweeper(){ - +void draw_minesweeper(Minesweeper_Board board){ + printf(" "); + for(int i = 0; i < board.width; i++){printf("%d", i / 10);} + printf("\n "); + for(int i = 0; i < board.width; i++){printf("%d", i % 10);} + printf("\n +"); + for(int i = 0; i < board.width; i++){printf("-");} + printf("+\n"); + for(int i = 0; i < board.height; i++){ + printf("%d", i / 10); + printf("%d|", i % 10); + for(int j = 0; j < board.width; j++){printf("%c", board.tiles[i * width + j]);} + printf("|\n"); + } + printf(" +"); + for(int i = 0; i < board.width; i++){printf("-");} + printf("+\n"); } \ No newline at end of file From 50608d06187f4d860ba282586443197adcafcfa6 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 14:00:13 +0100 Subject: [PATCH 05/18] placing bombs --- src/main/c/Minesweeper/minesweeper_start.c | 38 ++++++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 8dcc78f..298f61d 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "minesweeper_start.h" #pragma region defines #define BLOCK '#' @@ -20,6 +21,8 @@ void main_menu_minesweeper(); void game_minesweeper(); void options_minesweeper(); Minesweeper_Board initialize_minesweeper(); +void place_bombs(Minesweeper_Board *board); +bool bomb_in_array(int array[], int bomb, int length); void draw_minesweeper(Minesweeper_Board board); #pragma endregion @@ -70,13 +73,15 @@ void main_menu_minesweeper(){ void game_minesweeper(){ bool running = true; - int q = 0; + int x, y, t; while (running){ Minesweeper_Board board = initialize_minesweeper(); draw_minesweeper(board); - scanf("%d", &q); - if (q == 1){break;} + printf("Next turn (x, y, t(0 = open, 1 = flag)): "); + scanf("%d %d %d", &x, &y, &t); + getchar(); + if (t == 2){break;} } } @@ -137,14 +142,33 @@ Minesweeper_Board initialize_minesweeper(){ Minesweeper_Board board; board.width = width; board.height = height; - char *tiles = (char*) malloc(width * height * sizeof(char)); - for(int i = 0; i < width * height; i++){tiles[i] = BLOCK;} - board.tiles = tiles; + board.tiles = (char*) malloc(width * height * sizeof(char)); + for(int i = 0; i < width * height; i++){board.tiles[i] = BLOCK;} board.num_bombs = num_bombs; - + place_bombs(&board); return board; } +void place_bombs(Minesweeper_Board *board){ + board->bombs = (int*) malloc(board->num_bombs * sizeof(int)); + srand(time(NULL)); + int r; + for(int i = 0; i < board->num_bombs; i++){ + while (1){ + r = rand() % (board->width * board->height); + if(!bomb_in_array(board->bombs, r, board->num_bombs)){break;} + } + board->bombs[i] = r; + } +} + +bool bomb_in_array(int array[], int bomb, int length){ + for(int i = 0; i < length; i++){ + if(array[i] == bomb){return true;} + } + return false; +} + void draw_minesweeper(Minesweeper_Board board){ printf(" "); for(int i = 0; i < board.width; i++){printf("%d", i / 10);} From a910d7d7098296cac7b1db991be35a40942d672f Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 14:16:10 +0100 Subject: [PATCH 06/18] marking and unmarking flags --- src/main/c/Minesweeper/minesweeper_start.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 298f61d..fd58cfe 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -74,13 +74,17 @@ void main_menu_minesweeper(){ void game_minesweeper(){ bool running = true; int x, y, t; + Minesweeper_Board board = initialize_minesweeper(); while (running){ - Minesweeper_Board board = initialize_minesweeper(); + system("clear"); draw_minesweeper(board); printf("Next turn (x, y, t(0 = open, 1 = flag)): "); scanf("%d %d %d", &x, &y, &t); getchar(); + if(t == 1){board.tiles[x + y * board.width] = board.tiles[x + y * board.width] == EMPTY ? EMPTY : board.tiles[x + y * board.width] == FLAG ? BLOCK : FLAG;} + + if (t == 2){break;} } } From c49cdc2e37aa5551297c61fae1ba8919ed51692c Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 14:26:04 +0100 Subject: [PATCH 07/18] refactoring: placing bombs calculating marking tile --- src/main/c/Minesweeper/minesweeper_start.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index fd58cfe..85dc9d1 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -75,6 +75,8 @@ void game_minesweeper(){ bool running = true; int x, y, t; Minesweeper_Board board = initialize_minesweeper(); + place_bombs(&board); + while (running){ system("clear"); draw_minesweeper(board); @@ -82,9 +84,10 @@ void game_minesweeper(){ printf("Next turn (x, y, t(0 = open, 1 = flag)): "); scanf("%d %d %d", &x, &y, &t); getchar(); - if(t == 1){board.tiles[x + y * board.width] = board.tiles[x + y * board.width] == EMPTY ? EMPTY : board.tiles[x + y * board.width] == FLAG ? BLOCK : FLAG;} - - + if(x < board.width && y < board.height){ + x = x + y * board.width; + if(t == 1){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];} + } if (t == 2){break;} } } @@ -149,12 +152,12 @@ Minesweeper_Board initialize_minesweeper(){ board.tiles = (char*) malloc(width * height * sizeof(char)); for(int i = 0; i < width * height; i++){board.tiles[i] = BLOCK;} board.num_bombs = num_bombs; - place_bombs(&board); + board.bombs = (int*) malloc(board.num_bombs * sizeof(int)); + for(int i = 0; i < num_bombs; i++){board.bombs[i] = -1;} return board; } void place_bombs(Minesweeper_Board *board){ - board->bombs = (int*) malloc(board->num_bombs * sizeof(int)); srand(time(NULL)); int r; for(int i = 0; i < board->num_bombs; i++){ From 063eb9ebe040e53d5f170ffd62089f2e847ec2a6 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 14:39:53 +0100 Subject: [PATCH 08/18] Test test if tile has a bomb --- src/main/c/Minesweeper/minesweeper_start.h | 1 + test/Minesweeper/test_bomb_in_array.c | 25 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/Minesweeper/test_bomb_in_array.c diff --git a/src/main/c/Minesweeper/minesweeper_start.h b/src/main/c/Minesweeper/minesweeper_start.h index bf0f21a..a73d944 100644 --- a/src/main/c/Minesweeper/minesweeper_start.h +++ b/src/main/c/Minesweeper/minesweeper_start.h @@ -11,6 +11,7 @@ typedef struct Minesweeper_Board{ void minesweeper_start(); +bool bomb_in_array(int array[], int bomb, int length); #endif // MINESWEEPER_START_H \ No newline at end of file diff --git a/test/Minesweeper/test_bomb_in_array.c b/test/Minesweeper/test_bomb_in_array.c new file mode 100644 index 0000000..710791b --- /dev/null +++ b/test/Minesweeper/test_bomb_in_array.c @@ -0,0 +1,25 @@ +#ifdef TEST +#include "unity.h" +#include +#include "../../src/main/c/Minesweeper/minesweeper_start.h" + + +void setUp(void){} +void tearDown(void){} + + +void test_bomb_in_array(void){ + /* arrange */ + bool result; + int array[] = {5, 9, 42, 6, 87, 95, 202, 13, 45 ,78}; + int bomb = 42; + int length = 10; + + /* act */ + result = bomb_in_array(array, bomb, length); + + /* assert */ + TEST_ASSERT_TRUE(result);//head collides with body +} + +#endif // TEST \ No newline at end of file From 72f8a44cc1d2c5ab3cefe1fa2ab0f679abc44671 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 15:59:03 +0100 Subject: [PATCH 09/18] Test if tile has no bomb --- src/main/c/Minesweeper/minesweeper_start.c | 9 ++++++++- test/Minesweeper/test_bomb_in_array.c | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 85dc9d1..50bb461 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -24,6 +24,7 @@ Minesweeper_Board initialize_minesweeper(); void place_bombs(Minesweeper_Board *board); bool bomb_in_array(int array[], int bomb, int length); void draw_minesweeper(Minesweeper_Board board); +int open_tile(Minesweeper_Board *board, int tile); #pragma endregion #pragma region Global @@ -84,8 +85,9 @@ void game_minesweeper(){ printf("Next turn (x, y, t(0 = open, 1 = flag)): "); scanf("%d %d %d", &x, &y, &t); getchar(); - if(x < board.width && y < board.height){ + if(x < board.width && x > -1 && y < board.height && y > -1){ x = x + y * board.width; + if(t == 0){running = open_tile(&board, x);} if(t == 1){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];} } if (t == 2){break;} @@ -193,4 +195,9 @@ void draw_minesweeper(Minesweeper_Board board){ printf(" +"); for(int i = 0; i < board.width; i++){printf("-");} printf("+\n"); +} + +int open_tile(Minesweeper_Board *board, int tile){ + if(bomb_in_array(board->bombs, tile, board->num_bombs)){return -1;} + } \ No newline at end of file diff --git a/test/Minesweeper/test_bomb_in_array.c b/test/Minesweeper/test_bomb_in_array.c index 710791b..2e97c56 100644 --- a/test/Minesweeper/test_bomb_in_array.c +++ b/test/Minesweeper/test_bomb_in_array.c @@ -22,4 +22,19 @@ void test_bomb_in_array(void){ TEST_ASSERT_TRUE(result);//head collides with body } + +void test_bomb_not_in_array(void){ + /* arrange */ + bool result; + int array[] = {5, 9, 42, 6, 87, 95, 202, 13, 45 ,78}; + int bomb = 0; + int length = 10; + + /* act */ + result = bomb_in_array(array, bomb, length); + + /* assert */ + TEST_ASSERT_FALSE(result);//head collides with body +} + #endif // TEST \ No newline at end of file From 2a8cefa322d5359746f4591563a20c269b933c66 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 17:30:10 +0100 Subject: [PATCH 10/18] Test no bombs araound tile --- src/main/c/Minesweeper/minesweeper_start.c | 19 +++++++++++++++++ src/main/c/Minesweeper/minesweeper_start.h | 2 ++ test/Minesweeper/test_number_of_bombs.c | 24 ++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 test/Minesweeper/test_number_of_bombs.c diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 50bb461..43a1eed 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -25,6 +25,7 @@ void place_bombs(Minesweeper_Board *board); bool bomb_in_array(int array[], int bomb, int length); void draw_minesweeper(Minesweeper_Board board); int open_tile(Minesweeper_Board *board, int tile); +int number_of_bombs(Minesweeper_Board *board, int tile); #pragma endregion #pragma region Global @@ -199,5 +200,23 @@ void draw_minesweeper(Minesweeper_Board board){ int open_tile(Minesweeper_Board *board, int tile){ if(bomb_in_array(board->bombs, tile, board->num_bombs)){return -1;} + int num = number_of_bombs(board, tile); +} + +int number_of_bombs(Minesweeper_Board *board, int tile){ + int sum = 0; + for(int i = 0; i < 8; i++){ + int check_tile = tile; + if(i < 3 && tile % board->width == 0){continue;} + else{check_tile - 1;} + if(i > 4 && tile % board->width + 1 == board->width){continue;} + else{check_tile + 1;} + if(i % 3 == 0 && i != 0 && tile / board->width == 0){continue;} + else{check_tile - board->width;} + if(i % 3 == 1 && tile / board->width + 1 == height){continue;} + else{check_tile + board->width;} + if(bomb_in_array(board->bombs, check_tile, board->num_bombs)){sum++;} + } + return sum; } \ No newline at end of file diff --git a/src/main/c/Minesweeper/minesweeper_start.h b/src/main/c/Minesweeper/minesweeper_start.h index a73d944..c62977b 100644 --- a/src/main/c/Minesweeper/minesweeper_start.h +++ b/src/main/c/Minesweeper/minesweeper_start.h @@ -11,7 +11,9 @@ typedef struct Minesweeper_Board{ void minesweeper_start(); +Minesweeper_Board initialize_minesweeper(); bool bomb_in_array(int array[], int bomb, int length); +int number_of_bombs(Minesweeper_Board *board, int tile); #endif // MINESWEEPER_START_H \ No newline at end of file diff --git a/test/Minesweeper/test_number_of_bombs.c b/test/Minesweeper/test_number_of_bombs.c new file mode 100644 index 0000000..3f899f7 --- /dev/null +++ b/test/Minesweeper/test_number_of_bombs.c @@ -0,0 +1,24 @@ +#ifdef TEST +#include "unity.h" +#include +#include "../../src/main/c/Minesweeper/minesweeper_start.h" + + +void setUp(void){} +void tearDown(void){} + + +void test_no_bombs_placed_around_tile(void){ + /* arrange */ + int result; + Minesweeper_Board board = initialize_minesweeper(); + int tile = 1 + board.width * 1; + + /* act */ + result = number_of_bombs(&board, tile); + + /* assert */ + TEST_ASSERT_EQUAL_INT(0, result);//no bombs placed +} + +#endif // TEST \ No newline at end of file From 6e0233c784d56d4cdfe10d3120ee0159d572a3c0 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 17:39:25 +0100 Subject: [PATCH 11/18] Test bombs near but not around tile --- test/Minesweeper/test_bomb_in_array.c | 4 ++-- test/Minesweeper/test_number_of_bombs.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/test/Minesweeper/test_bomb_in_array.c b/test/Minesweeper/test_bomb_in_array.c index 2e97c56..8aae16d 100644 --- a/test/Minesweeper/test_bomb_in_array.c +++ b/test/Minesweeper/test_bomb_in_array.c @@ -19,7 +19,7 @@ void test_bomb_in_array(void){ result = bomb_in_array(array, bomb, length); /* assert */ - TEST_ASSERT_TRUE(result);//head collides with body + TEST_ASSERT_TRUE(result); } @@ -34,7 +34,7 @@ void test_bomb_not_in_array(void){ result = bomb_in_array(array, bomb, length); /* assert */ - TEST_ASSERT_FALSE(result);//head collides with body + TEST_ASSERT_FALSE(result); } #endif // TEST \ No newline at end of file diff --git a/test/Minesweeper/test_number_of_bombs.c b/test/Minesweeper/test_number_of_bombs.c index 3f899f7..e449f33 100644 --- a/test/Minesweeper/test_number_of_bombs.c +++ b/test/Minesweeper/test_number_of_bombs.c @@ -21,4 +21,21 @@ void test_no_bombs_placed_around_tile(void){ TEST_ASSERT_EQUAL_INT(0, result);//no bombs placed } +void test_searching_for_bombs_on_correct_tiles(void){ + /* arrange */ + int result; + Minesweeper_Board board = initialize_minesweeper(); + int tile = 0 + board.width * 1; + board.bombs[0] = 0 + board.width * 2; + board.bombs[0] = 2 + board.width * 1; + board.bombs[0] = board.width - 1 + board.width * 1; + board.bombs[0] = 0 + board.width * (board.height - 1); + + /* act */ + result = number_of_bombs(&board, tile); + + /* assert */ + TEST_ASSERT_EQUAL_INT(0, result);//no bombs placed around tile +} + #endif // TEST \ No newline at end of file From 79c871c7ff3ead2265e8f9a390d8f829e5924bb1 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 18:13:20 +0100 Subject: [PATCH 12/18] Test 2 bombs around tile --- src/main/c/Minesweeper/minesweeper_start.c | 16 ++++++------- test/Minesweeper/test_number_of_bombs.c | 27 ++++++++++++++++++---- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 43a1eed..98eb0eb 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -208,14 +208,14 @@ int number_of_bombs(Minesweeper_Board *board, int tile){ int sum = 0; for(int i = 0; i < 8; i++){ int check_tile = tile; - if(i < 3 && tile % board->width == 0){continue;} - else{check_tile - 1;} - if(i > 4 && tile % board->width + 1 == board->width){continue;} - else{check_tile + 1;} - if(i % 3 == 0 && i != 0 && tile / board->width == 0){continue;} - else{check_tile - board->width;} - if(i % 3 == 1 && tile / board->width + 1 == height){continue;} - else{check_tile + board->width;} + if(i < 3){if(tile % board->width == 0){continue;} + else{check_tile -= 1;}} + if(i > 4){if(tile % board->width + 1 == board->width){continue;} + else{check_tile += 1;}} + if(i % 3 == 0){if(i != 0 && tile / board->width == 0){continue;} + else{check_tile -= board->width;}} + if(i % 3 == 1){if(tile / board->width + 1 == height){continue;} + else{check_tile += board->width;}} if(bomb_in_array(board->bombs, check_tile, board->num_bombs)){sum++;} } return sum; diff --git a/test/Minesweeper/test_number_of_bombs.c b/test/Minesweeper/test_number_of_bombs.c index e449f33..047c475 100644 --- a/test/Minesweeper/test_number_of_bombs.c +++ b/test/Minesweeper/test_number_of_bombs.c @@ -12,7 +12,7 @@ void test_no_bombs_placed_around_tile(void){ /* arrange */ int result; Minesweeper_Board board = initialize_minesweeper(); - int tile = 1 + board.width * 1; + int tile = 0 + board.width * 1; /* act */ result = number_of_bombs(&board, tile); @@ -26,10 +26,10 @@ void test_searching_for_bombs_on_correct_tiles(void){ int result; Minesweeper_Board board = initialize_minesweeper(); int tile = 0 + board.width * 1; - board.bombs[0] = 0 + board.width * 2; - board.bombs[0] = 2 + board.width * 1; - board.bombs[0] = board.width - 1 + board.width * 1; - board.bombs[0] = 0 + board.width * (board.height - 1); + board.bombs[0] = 0 + board.width * 3; + board.bombs[1] = 2 + board.width * 1; + board.bombs[2] = board.width - 1 + board.width * 1; + board.bombs[3] = 0 + board.width * (board.height - 1); /* act */ result = number_of_bombs(&board, tile); @@ -38,4 +38,21 @@ void test_searching_for_bombs_on_correct_tiles(void){ TEST_ASSERT_EQUAL_INT(0, result);//no bombs placed around tile } +void test_2_bombs_around_tile(void){ + /* arrange */ + int result; + Minesweeper_Board board = initialize_minesweeper(); + int tile = 0 + board.width * 1; + board.bombs[0] = 0 + board.width * 2; + board.bombs[1] = 1 + board.width * 1; + board.bombs[2] = board.width - 1 + board.width * 1; + board.bombs[3] = 0 + board.width * (board.height - 1); + + /* act */ + result = number_of_bombs(&board, tile); + + /* assert */ + TEST_ASSERT_EQUAL_INT(2, result);//2 bombs placed around tile +} + #endif // TEST \ No newline at end of file From 616ffae6b48f45dc917d61e4962d6a54fc43594a Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 18:21:30 +0100 Subject: [PATCH 13/18] Test 5 or 8 bombs around tile --- test/Minesweeper/test_number_of_bombs.c | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/Minesweeper/test_number_of_bombs.c b/test/Minesweeper/test_number_of_bombs.c index 047c475..6a0dd4f 100644 --- a/test/Minesweeper/test_number_of_bombs.c +++ b/test/Minesweeper/test_number_of_bombs.c @@ -55,4 +55,43 @@ void test_2_bombs_around_tile(void){ TEST_ASSERT_EQUAL_INT(2, result);//2 bombs placed around tile } +void test_5_bombs_around_tile(void){ + /* arrange */ + int result; + Minesweeper_Board board = initialize_minesweeper(); + int tile = 1 + board.width * 0; + board.bombs[0] = 0 + board.width * 0; + board.bombs[1] = 2 + board.width * 0; + board.bombs[2] = 0 + board.width * 1; + board.bombs[3] = 1 + board.width * 1; + board.bombs[4] = 2 + board.width * 1; + + /* act */ + result = number_of_bombs(&board, tile); + + /* assert */ + TEST_ASSERT_EQUAL_INT(5, result);//5 bombs placed around tile +} + +void test_8_bombs_around_tile(void){ + /* arrange */ + int result; + Minesweeper_Board board = initialize_minesweeper(); + int tile = 7 + board.width * 7; + board.bombs[0] = 6 + board.width * 6; + board.bombs[1] = 7 + board.width * 6; + board.bombs[2] = 8 + board.width * 6; + board.bombs[3] = 6 + board.width * 7; + board.bombs[4] = 8 + board.width * 7; + board.bombs[5] = 6 + board.width * 8; + board.bombs[6] = 7 + board.width * 8; + board.bombs[7] = 8 + board.width * 8; + + /* act */ + result = number_of_bombs(&board, tile); + + /* assert */ + TEST_ASSERT_EQUAL_INT(8, result);//2 bombs placed around tile +} + #endif // TEST \ No newline at end of file From a004b66a33412dcc9e49ea169d08116a428374e3 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 20:22:53 +0100 Subject: [PATCH 14/18] Minesweeper is playable --- src/main/c/Minesweeper/minesweeper_start.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 98eb0eb..48bf4d9 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -88,7 +88,12 @@ void game_minesweeper(){ getchar(); if(x < board.width && x > -1 && y < board.height && y > -1){ x = x + y * board.width; - if(t == 0){running = open_tile(&board, x);} + if(t == 0){ + int bombs = open_tile(&board, x); + if(bombs == -1){running = false;} + else if(bombs == 0){board.tiles[x] = EMPTY;} + else{board.tiles[x] = bombs + 48;} + } if(t == 1){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];} } if (t == 2){break;} @@ -201,7 +206,7 @@ void draw_minesweeper(Minesweeper_Board board){ int open_tile(Minesweeper_Board *board, int tile){ if(bomb_in_array(board->bombs, tile, board->num_bombs)){return -1;} int num = number_of_bombs(board, tile); - + if(1){return num;} } int number_of_bombs(Minesweeper_Board *board, int tile){ From 2367550137e9c3c30e57e53bb00d4d36e6bba6b8 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 21:03:38 +0100 Subject: [PATCH 15/18] wip batch opening --- src/main/c/Minesweeper/minesweeper_start.c | 54 ++++++++++++++++------ src/main/c/Minesweeper/minesweeper_start.h | 5 +- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 48bf4d9..a16a24b 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -22,10 +22,12 @@ void game_minesweeper(); void options_minesweeper(); Minesweeper_Board initialize_minesweeper(); void place_bombs(Minesweeper_Board *board); -bool bomb_in_array(int array[], int bomb, int length); -void draw_minesweeper(Minesweeper_Board board); +bool bomb_in_array(int *array, int bomb, int length); +bool array_contains_value(int *array, int value, int length); +void draw_minesweeper(Minesweeper_Board *board); int open_tile(Minesweeper_Board *board, int tile); int number_of_bombs(Minesweeper_Board *board, int tile); +void open_empty_space(Minesweeper_Board *board, int tile, int index); #pragma endregion #pragma region Global @@ -77,11 +79,12 @@ void game_minesweeper(){ bool running = true; int x, y, t; Minesweeper_Board board = initialize_minesweeper(); + place_bombs(&board); while (running){ system("clear"); - draw_minesweeper(board); + draw_minesweeper(&board); printf("Next turn (x, y, t(0 = open, 1 = flag)): "); scanf("%d %d %d", &x, &y, &t); @@ -91,7 +94,7 @@ void game_minesweeper(){ if(t == 0){ int bombs = open_tile(&board, x); if(bombs == -1){running = false;} - else if(bombs == 0){board.tiles[x] = EMPTY;} + else if(bombs == 0){open_empty_space(&board, x, 0);} else{board.tiles[x] = bombs + 48;} } if(t == 1){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];} @@ -157,8 +160,10 @@ Minesweeper_Board initialize_minesweeper(){ Minesweeper_Board board; board.width = width; board.height = height; - board.tiles = (char*) malloc(width * height * sizeof(char)); + board.tiles = (int*) malloc(width * height * sizeof(int)); for(int i = 0; i < width * height; i++){board.tiles[i] = BLOCK;} + board.marked = (int*) malloc(width * height * sizeof(int)); + for(int i = 0; i < width * height; i++){board.tiles[i] = 0;} board.num_bombs = num_bombs; board.bombs = (int*) malloc(board.num_bombs * sizeof(int)); for(int i = 0; i < num_bombs; i++){board.bombs[i] = -1;} @@ -177,29 +182,33 @@ void place_bombs(Minesweeper_Board *board){ } } -bool bomb_in_array(int array[], int bomb, int length){ +bool bomb_in_array(int *array, int bomb, int length){ for(int i = 0; i < length; i++){ if(array[i] == bomb){return true;} } return false; } -void draw_minesweeper(Minesweeper_Board board){ +bool array_contains_value(int *array, int value, int length){ + +} + +void draw_minesweeper(Minesweeper_Board *board){ printf(" "); - for(int i = 0; i < board.width; i++){printf("%d", i / 10);} + for(int i = 0; i < board->width; i++){printf("%d", i / 10);} printf("\n "); - for(int i = 0; i < board.width; i++){printf("%d", i % 10);} + for(int i = 0; i < board->width; i++){printf("%d", i % 10);} printf("\n +"); - for(int i = 0; i < board.width; i++){printf("-");} + for(int i = 0; i < board->width; i++){printf("-");} printf("+\n"); - for(int i = 0; i < board.height; i++){ + for(int i = 0; i < board->height; i++){ printf("%d", i / 10); printf("%d|", i % 10); - for(int j = 0; j < board.width; j++){printf("%c", board.tiles[i * width + j]);} + for(int j = 0; j < board->width; j++){printf("%c", board->tiles[i * width + j]);} printf("|\n"); } printf(" +"); - for(int i = 0; i < board.width; i++){printf("-");} + for(int i = 0; i < board->width; i++){printf("-");} printf("+\n"); } @@ -224,4 +233,23 @@ int number_of_bombs(Minesweeper_Board *board, int tile){ if(bomb_in_array(board->bombs, check_tile, board->num_bombs)){sum++;} } return sum; +} + +void open_empty_space(Minesweeper_Board *board, int tile, int index){ + board->tiles[tile] = EMPTY; + board->marked[index++] = tile; + for(int i = 0; i < 8; i++){ + int check_tile = tile; + if(i < 3){if(tile % board->width == 0){continue;} + else{check_tile -= 1;}} + if(i > 4){if(tile % board->width + 1 == board->width){continue;} + else{check_tile += 1;}} + if(i % 3 == 0){if(i != 0 && tile / board->width == 0){continue;} + else{check_tile -= board->width;}} + if(i % 3 == 1){if(tile / board->width + 1 == height){continue;} + else{check_tile += board->width;}} + int sum = open_tile(board, check_tile); + if(sum != 0){board->tiles[check_tile] = sum + 48;} + else if(!array_contains_value(board->marked, check_tile, index)){open_empty_space(board, check_tile, index);} + } } \ No newline at end of file diff --git a/src/main/c/Minesweeper/minesweeper_start.h b/src/main/c/Minesweeper/minesweeper_start.h index c62977b..b577233 100644 --- a/src/main/c/Minesweeper/minesweeper_start.h +++ b/src/main/c/Minesweeper/minesweeper_start.h @@ -4,7 +4,8 @@ typedef struct Minesweeper_Board{ unsigned int width; unsigned int height; - char *tiles; + int *tiles; + int *marked; int num_bombs; int *bombs; }Minesweeper_Board; @@ -12,7 +13,7 @@ typedef struct Minesweeper_Board{ void minesweeper_start(); Minesweeper_Board initialize_minesweeper(); -bool bomb_in_array(int array[], int bomb, int length); +bool bomb_in_array(int *array, int bomb, int length); int number_of_bombs(Minesweeper_Board *board, int tile); From 3203609f1abeef01468d8d5cf5ab837158156a5b Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 21:27:55 +0100 Subject: [PATCH 16/18] refactoring: Minesweeper_Board and bomb_in_array to array_contains_value --- src/main/c/Minesweeper/minesweeper_start.c | 28 ++++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index a16a24b..3025597 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -5,7 +5,7 @@ #include "minesweeper_start.h" #pragma region defines #define BLOCK '#' -#define FLAG '?' +#define FLAG 'x' #define EMPTY ' ' #define SMALL 10 #define MIDDLE 15 @@ -163,7 +163,7 @@ Minesweeper_Board initialize_minesweeper(){ board.tiles = (int*) malloc(width * height * sizeof(int)); for(int i = 0; i < width * height; i++){board.tiles[i] = BLOCK;} board.marked = (int*) malloc(width * height * sizeof(int)); - for(int i = 0; i < width * height; i++){board.tiles[i] = 0;} + for(int i = 0; i < width * height; i++){board.marked[i] = 0;} board.num_bombs = num_bombs; board.bombs = (int*) malloc(board.num_bombs * sizeof(int)); for(int i = 0; i < num_bombs; i++){board.bombs[i] = -1;} @@ -176,21 +176,24 @@ void place_bombs(Minesweeper_Board *board){ for(int i = 0; i < board->num_bombs; i++){ while (1){ r = rand() % (board->width * board->height); - if(!bomb_in_array(board->bombs, r, board->num_bombs)){break;} + if(!array_contains_value(board->bombs, r, board->num_bombs)){break;} } board->bombs[i] = r; } } -bool bomb_in_array(int *array, int bomb, int length){ +bool bomb_in_array(int *array, int value, int length){ for(int i = 0; i < length; i++){ - if(array[i] == bomb){return true;} + if(array[i] == value){return true;} } return false; } bool array_contains_value(int *array, int value, int length){ - + for(int i = 0; i < length; i++){ + if(array[i] == value){return true;} + } + return false; } void draw_minesweeper(Minesweeper_Board *board){ @@ -205,15 +208,20 @@ void draw_minesweeper(Minesweeper_Board *board){ printf("%d", i / 10); printf("%d|", i % 10); for(int j = 0; j < board->width; j++){printf("%c", board->tiles[i * width + j]);} - printf("|\n"); + printf("|%d", i / 10); + printf("%d\n", i % 10); } printf(" +"); for(int i = 0; i < board->width; i++){printf("-");} - printf("+\n"); + printf("+\n "); + for(int i = 0; i < board->width; i++){printf("%d", i / 10);} + printf("\n "); + for(int i = 0; i < board->width; i++){printf("%d", i % 10);} + printf("\n"); } int open_tile(Minesweeper_Board *board, int tile){ - if(bomb_in_array(board->bombs, tile, board->num_bombs)){return -1;} + if(array_contains_value(board->bombs, tile, board->num_bombs)){return -1;} int num = number_of_bombs(board, tile); if(1){return num;} } @@ -230,7 +238,7 @@ int number_of_bombs(Minesweeper_Board *board, int tile){ else{check_tile -= board->width;}} if(i % 3 == 1){if(tile / board->width + 1 == height){continue;} else{check_tile += board->width;}} - if(bomb_in_array(board->bombs, check_tile, board->num_bombs)){sum++;} + if(array_contains_value(board->bombs, check_tile, board->num_bombs)){sum++;} } return sum; } From ad8fc5d7bcb1279df272e1ed3d33f2a4d9d6f5d8 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Sat, 27 Jan 2024 16:05:11 +0100 Subject: [PATCH 17/18] minor fixes --- src/main/c/Minesweeper/minesweeper_start.c | 8 -------- src/main/c/Minesweeper/minesweeper_start.h | 2 +- test/Minesweeper/test_bomb_in_array.c | 4 ++-- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 3025597..fae882a 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -22,7 +22,6 @@ void game_minesweeper(); void options_minesweeper(); Minesweeper_Board initialize_minesweeper(); void place_bombs(Minesweeper_Board *board); -bool bomb_in_array(int *array, int bomb, int length); bool array_contains_value(int *array, int value, int length); void draw_minesweeper(Minesweeper_Board *board); int open_tile(Minesweeper_Board *board, int tile); @@ -182,13 +181,6 @@ void place_bombs(Minesweeper_Board *board){ } } -bool bomb_in_array(int *array, int value, int length){ - for(int i = 0; i < length; i++){ - if(array[i] == value){return true;} - } - return false; -} - bool array_contains_value(int *array, int value, int length){ for(int i = 0; i < length; i++){ if(array[i] == value){return true;} diff --git a/src/main/c/Minesweeper/minesweeper_start.h b/src/main/c/Minesweeper/minesweeper_start.h index b577233..c37d731 100644 --- a/src/main/c/Minesweeper/minesweeper_start.h +++ b/src/main/c/Minesweeper/minesweeper_start.h @@ -13,7 +13,7 @@ typedef struct Minesweeper_Board{ void minesweeper_start(); Minesweeper_Board initialize_minesweeper(); -bool bomb_in_array(int *array, int bomb, int length); +bool array_contains_value(int *array, int bomb, int length); int number_of_bombs(Minesweeper_Board *board, int tile); diff --git a/test/Minesweeper/test_bomb_in_array.c b/test/Minesweeper/test_bomb_in_array.c index 8aae16d..7073857 100644 --- a/test/Minesweeper/test_bomb_in_array.c +++ b/test/Minesweeper/test_bomb_in_array.c @@ -16,7 +16,7 @@ void test_bomb_in_array(void){ int length = 10; /* act */ - result = bomb_in_array(array, bomb, length); + result = array_contains_value(array, bomb, length); /* assert */ TEST_ASSERT_TRUE(result); @@ -31,7 +31,7 @@ void test_bomb_not_in_array(void){ int length = 10; /* act */ - result = bomb_in_array(array, bomb, length); + result = array_contains_value(array, bomb, length); /* assert */ TEST_ASSERT_FALSE(result); From adce56939d6c6b61595291714fa9c4a836a90a63 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 2 Feb 2024 11:49:17 +0100 Subject: [PATCH 18/18] Minor Fixes --- src/main/c/Snake/snake_start.h | 2 ++ test/Snake/test_collision.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/c/Snake/snake_start.h b/src/main/c/Snake/snake_start.h index 359f548..b985421 100644 --- a/src/main/c/Snake/snake_start.h +++ b/src/main/c/Snake/snake_start.h @@ -1,6 +1,8 @@ #ifndef SNAKE_START_H #define SNAKE_START_H +#include + typedef struct Snake{ signed char direction; char length; diff --git a/test/Snake/test_collision.c b/test/Snake/test_collision.c index e5d5a63..df400b0 100644 --- a/test/Snake/test_collision.c +++ b/test/Snake/test_collision.c @@ -1,8 +1,8 @@ #ifdef TEST #include "unity.h" #include -#include "../../src/main/c/Snake/snake_start.h" -#include "../../src/main/c/Snake/get_character.h" +#include "snake_start.h" +#include "get_character.h" void setUp(void){}