Browse Source

refactoring: Minesweeper_Board and bomb_in_array to array_contains_value

remotes/origin/David
David Moeller 12 months ago
parent
commit
3203609f1a
  1. 28
      src/main/c/Minesweeper/minesweeper_start.c

28
src/main/c/Minesweeper/minesweeper_start.c

@ -5,7 +5,7 @@
#include "minesweeper_start.h" #include "minesweeper_start.h"
#pragma region defines #pragma region defines
#define BLOCK '#' #define BLOCK '#'
#define FLAG '?'
#define FLAG 'x'
#define EMPTY ' ' #define EMPTY ' '
#define SMALL 10 #define SMALL 10
#define MIDDLE 15 #define MIDDLE 15
@ -163,7 +163,7 @@ Minesweeper_Board initialize_minesweeper(){
board.tiles = (int*) malloc(width * height * sizeof(int)); board.tiles = (int*) malloc(width * height * sizeof(int));
for(int i = 0; i < width * height; i++){board.tiles[i] = BLOCK;} for(int i = 0; i < width * height; i++){board.tiles[i] = BLOCK;}
board.marked = (int*) malloc(width * height * sizeof(int)); 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.num_bombs = num_bombs;
board.bombs = (int*) malloc(board.num_bombs * sizeof(int)); board.bombs = (int*) malloc(board.num_bombs * sizeof(int));
for(int i = 0; i < num_bombs; i++){board.bombs[i] = -1;} 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++){ for(int i = 0; i < board->num_bombs; i++){
while (1){ while (1){
r = rand() % (board->width * board->height); 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; 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++){ for(int i = 0; i < length; i++){
if(array[i] == bomb){return true;}
if(array[i] == value){return true;}
} }
return false; return false;
} }
bool array_contains_value(int *array, int value, int length){ 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){ void draw_minesweeper(Minesweeper_Board *board){
@ -205,15 +208,20 @@ void draw_minesweeper(Minesweeper_Board *board){
printf("%d", i / 10); printf("%d", i / 10);
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("|%d", i / 10);
printf("%d\n", i % 10);
} }
printf(" +"); printf(" +");
for(int i = 0; i < board->width; i++){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){ 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); int num = number_of_bombs(board, tile);
if(1){return num;} if(1){return num;}
} }
@ -230,7 +238,7 @@ int number_of_bombs(Minesweeper_Board *board, int tile){
else{check_tile -= board->width;}} else{check_tile -= board->width;}}
if(i % 3 == 1){if(tile / board->width + 1 == height){continue;} if(i % 3 == 1){if(tile / board->width + 1 == height){continue;}
else{check_tile += board->width;}} 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; return sum;
} }

Loading…
Cancel
Save