Browse Source

refactoring: replacing more magic numbers

remotes/origin/David
David Moeller 11 months ago
parent
commit
8a3292f2d9
  1. 35
      src/main/c/Minesweeper/minesweeper_start.c

35
src/main/c/Minesweeper/minesweeper_start.c

@ -15,6 +15,10 @@
#define NORMAL 5
#define HARD 3
#define OFFSET_NUM_CHARS 48
#define OPEN_TILE 0
#define FLAG_TILE 1
#define QUIT_GAME 2
#define ERROR -1
#pragma endregion
#pragma region Funktion_heads
@ -91,15 +95,18 @@ void game_minesweeper(){
getchar();
if(x < board.width && x > -1 && y < board.height && y > -1){
x = x + y * board.width;
if(t == 0){
if(t == OPEN_TILE){
int bombs = open_tile(&board, x);
if(bombs == -1){printf("You Lose!"); getchar(); break;}
else if(bombs == 0){open_empty_space(&board, x, 0);}
if(bombs == ERROR){printf("You Lose!"); getchar(); break;}
else if(bombs == 0){
open_empty_space(&board, x, 0);
for(int i = 0; i < board.width * board.height; i++){board.marked[i] = ERROR;}
}
else{board.tiles[x] = bombs + OFFSET_NUM_CHARS;}
}
if(t == 1){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];}
if(t == FLAG_TILE){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];}
}
if (t == 2){break;}
if (t == QUIT_GAME){break;}
int test = 1;
for (int i = 0; i < board.height * board.width; i++){if(board.tiles[i] == BLOCK){test = 0; break;}}
@ -168,10 +175,10 @@ 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.marked[i] = -1;}
for(int i = 0; i < width * height; i++){board.marked[i] = ERROR;}
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;}
for(int i = 0; i < num_bombs; i++){board.bombs[i] = ERROR;}
return board;
}
@ -222,7 +229,7 @@ void draw_minesweeper(Minesweeper_Board *board){
}
int open_tile(Minesweeper_Board *board, int tile){
if(array_contains_value(board->bombs, tile, board->num_bombs)){return -1;}
if(array_contains_value(board->bombs, tile, board->num_bombs)){return ERROR;}
return number_of_bombs(board, tile);
}
@ -230,7 +237,7 @@ int number_of_bombs(Minesweeper_Board *board, int tile){
int sum = 0;
for(int i = 0; i < 8; i++){
int check_tile = is_Valid_tile(board, tile, i);
if(check_tile == -1){continue;}
if(check_tile == ERROR){continue;}
if(array_contains_value(board->bombs, check_tile, board->num_bombs)){sum++;}
}
return sum;
@ -241,7 +248,7 @@ void open_empty_space(Minesweeper_Board *board, int tile, int index){
board->marked[index] = tile;
for(int i = 0; i < 8; i++){
int check_tile = is_Valid_tile(board, tile, i);
if(check_tile == -1){continue;}
if(check_tile == ERROR){continue;}
int sum = open_tile(board, check_tile);
if(sum != 0){board->tiles[check_tile] = sum + OFFSET_NUM_CHARS;}
else if(!array_contains_value(board->marked, check_tile, board->height * board->width)){open_empty_space(board, check_tile, index + 1);}
@ -252,13 +259,13 @@ void open_empty_space(Minesweeper_Board *board, int tile, int index){
int is_Valid_tile(Minesweeper_Board *board, int tile, int direction){
int check_tile = tile;
if(direction < 3){if(tile % board->width == 0){return -1;}
if(direction < 3){if(tile % board->width == 0){return ERROR;}
else{check_tile -= 1;}}
if(direction > 4){if(tile % board->width + 1 == board->width){return -1;}
if(direction > 4){if(tile % board->width + 1 == board->width){return ERROR;}
else{check_tile += 1;}}
if(direction % 3 == 0){if(tile / board->width == 0){return -1;}
if(direction % 3 == 0){if(tile / board->width == 0){return ERROR;}
else{check_tile -= board->width;}}
if(direction % 3 == 1){if(tile / board->width + 1 == height){return -1;}
if(direction % 3 == 1){if(tile / board->width + 1 == height){return ERROR;}
else{check_tile += board->width;}}
return check_tile;
Loading…
Cancel
Save