Browse Source

placing bombs

remotes/origin/David
David Moeller 11 months ago
parent
commit
50608d0618
  1. 38
      src/main/c/Minesweeper/minesweeper_start.c

38
src/main/c/Minesweeper/minesweeper_start.c

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h>
#include "minesweeper_start.h" #include "minesweeper_start.h"
#pragma region defines #pragma region defines
#define BLOCK '#' #define BLOCK '#'
@ -20,6 +21,8 @@ void main_menu_minesweeper();
void game_minesweeper(); void game_minesweeper();
void options_minesweeper(); void options_minesweeper();
Minesweeper_Board initialize_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); void draw_minesweeper(Minesweeper_Board board);
#pragma endregion #pragma endregion
@ -70,13 +73,15 @@ void main_menu_minesweeper(){
void game_minesweeper(){ void game_minesweeper(){
bool running = true; bool running = true;
int q = 0;
int x, y, t;
while (running){ while (running){
Minesweeper_Board board = initialize_minesweeper(); Minesweeper_Board board = initialize_minesweeper();
draw_minesweeper(board); 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; Minesweeper_Board board;
board.width = width; board.width = width;
board.height = height; 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; board.num_bombs = num_bombs;
place_bombs(&board);
return 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){ void draw_minesweeper(Minesweeper_Board board){
printf(" "); 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);}

Loading…
Cancel
Save