#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE_EU 10
#define BLANK_EU 0
#define BOMB_EU 9
void initGrid(int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU);
void placeBombs(int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU, int nBombs_EU);
void printGrid(const int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU);
int getRand(int first_EU, int last_EU);
void printBombCounts(const int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU);
int countSurroundingBombs(const int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU, int loc_row_EU, int loc_col_EU);
void takeMove(int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU, int loc_row_EU, int loc_col_EU);
int main() {
int grid_EU[MAX_SIZE_EU][MAX_SIZE_EU] = {{0}};
int n_rows_EU = 5;
int n_cols_EU = 5;
int guess_r_EU, guess_c_EU;
// Initialize the grid
initGrid(grid_EU, n_rows_EU, n_cols_EU);
// Place bombs randomly on the grid
placeBombs(grid_EU, n_rows_EU, n_cols_EU, 6);
// Print the initial grid
printGrid(grid_EU, n_rows_EU, n_cols_EU);
// Game loop
while (1) {
// Ask the user for input
printf("\nEnter your guess (row and col) or -1 to exit: ");
scanf("%d", &guess_r_EU);
// Check if the user wants to exit
if (guess_r_EU == -1) {
printf("Exiting the game.\n");
break;
}
scanf("%d", &guess_c_EU);
// Check if the guessed cell contains a bomb
if (grid_EU[guess_r_EU][guess_c_EU] == BOMB_EU) {
printf("You hit a bomb! Game over.\n");
break;
} else {
// If no bomb, reveal the cell and its surroundings
takeMove(grid_EU, n_rows_EU, n_cols_EU, guess_r_EU, guess_c_EU);
}
// Print the updated grid
printGrid(grid_EU, n_rows_EU, n_cols_EU);
}
return 0;
}
// Function to initialize the grid with blank cells
void initGrid(int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU) {
for (int i = 0; i < rows_EU; ++i) {
for (int j = 0; j < cols_EU; ++j) {
theGrid_EU[i][j] = BLANK_EU;
}
}
}
// Function to place bombs randomly on the grid
void placeBombs(int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU, int nBombs_EU) {
for (int i = 0; i < nBombs_EU; ++i) {
int r_EU, c_EU;
do {
// Generate random coordinates for bomb placement
r_EU = getRand(0, rows_EU - 1);
c_EU = getRand(0, cols_EU - 1);
} while (theGrid_EU[r_EU][c_EU] == BOMB_EU); // Ensure not placing bomb on existing bomb
theGrid_EU[r_EU][c_EU] = BOMB_EU;
}
}
// Function to print the grid
void printGrid(const int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU) {
for (int i = 0; i < rows_EU; ++i) {
for (int j = 0; j < cols_EU; ++j) {
if (theGrid_EU[i][j] == BOMB_EU) {
printf(" * ");
} else {
printf("%3d", theGrid_EU[i][j]);
}
}
printf("\n");
}
}
// Function to print the bomb counts
void printBombCounts(const int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU) {
for (int i = 0; i < rows_EU; ++i) {
for (int j = 0; j < cols_EU; ++j) {
if (theGrid_EU[i][j] == BOMB_EU) {
printf(" * ");
} else {
// Count surrounding bombs and print the count
int count_EU = countSurroundingBombs(theGrid_EU, rows_EU, cols_EU, i, j);
printf("%3d", count_EU);
}
}
printf("\n");
}
}
// Function to generate random numbers
int getRand(int first_EU, int last_EU) {
srand(time(NULL));
int amountOfNumbers_EU = last_EU - first_EU + 1;
return (rand() % amountOfNumbers_EU + first_EU);
}
// Function to count surrounding bombs for a given cell
int countSurroundingBombs(const int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU, int loc_row_EU, int loc_col_EU) {
int count_EU = 0;
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
int new_row_EU = loc_row_EU + i;
int new_col_EU = loc_col_EU + j;
if (new_row_EU >= 0 && new_row_EU < rows_EU && new_col_EU >= 0 && new_col_EU < cols_EU &&
theGrid_EU[new_row_EU][new_col_EU] == BOMB_EU) {
++count_EU;
}
}
}
return count_EU;
}
// Function to reveal cells recursively
void takeMove(int theGrid_EU[][MAX_SIZE_EU], int rows_EU, int cols_EU, int loc_row_EU, int loc_col_EU) {
int count_EU = countSurroundingBombs(theGrid_EU, rows_EU, cols_EU, loc_row_EU, loc_col_EU);
theGrid_EU[loc_row_EU][loc_col_EU] = count_EU;
if (count_EU == 0) {
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
int new_row_EU = loc_row_EU + i;
int new_col_EU = loc_col_EU + j;
if (new_row_EU >= 0 && new_row_EU < rows_EU && new_col_EU >= 0 && new_col_EU < cols_EU &&
theGrid_EU[new_row_EU][new_col_EU] == BLANK_EU) {
takeMove(theGrid_EU, rows_EU, cols_EU, new_row_EU, new_col_EU);
}
}
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: