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);}