#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define GRID_SIZE 6
#define NUM_MINES 6
// Function to initialize the board with empty spaces
void initializeBoard(char board[GRID_SIZE][GRID_SIZE], char display[GRID_SIZE][GRID_SIZE]) {
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
board[i][j] = ' ';
display[i][j] = '-'; // # represents hidden cells
}
}
}
// Function to place mines randomly on the board
void placeMines(char board[GRID_SIZE][GRID_SIZE]) {
int count = 0;
while (count < NUM_MINES) {
int row
= rand() % GRID_SIZE
; int col
= rand() % GRID_SIZE
; if (board[row][col] != 'M') { // If it's not already a mine
board[row][col] = 'M'; // Place a mine
count++;
}
}
}
// Function to count the number of adjacent mines around a cell
int countAdjacentMines(char board[GRID_SIZE][GRID_SIZE], int row, int col) {
int count = 0;
int directions[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
for (int i = 0; i < 8; i++) {
int r = row + directions[i][0];
int c = col + directions[i][1];
if (r >= 0 && r < GRID_SIZE && c >= 0 && c < GRID_SIZE) {
if (board[r][c] == 'M') {
count++;
}
}
}
return count;
}
// Function to reveal a cell
int revealCell(char board[GRID_SIZE][GRID_SIZE], char display[GRID_SIZE][GRID_SIZE], int row, int col) {
if (display[row][col] != '|') {
return 0; // Cell is already revealed
}
display[row][col] = board[row][col]; // Reveal the cell
if (board[row][col] == 'M') {
return 1; // Game Over
}
// If no adjacent mines, recursively reveal neighboring cells
if (board[row][col] == ' ') {
int directions[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
for (int i = 0; i < 8; i++) {
int r = row + directions[i][0];
int c = col + directions[i][1];
if (r >= 0 && r < GRID_SIZE && c >= 0 && c < GRID_SIZE && display[r][c] == '#') {
revealCell(board, display, r, c);
}
}
}
return 0; // Continue game
}
// Function to check if all non-mine cells are revealed
int checkWin(char board[GRID_SIZE][GRID_SIZE], char display[GRID_SIZE][GRID_SIZE]) {
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
if (board[i][j] != 'M' && display[i][j] == '- |') {
return 0; // Not all non-mine cells are revealed
}
}
}
return 1; // Player wins
}
// Function to print the board
void printBoard(char display[GRID_SIZE][GRID_SIZE]) {
for (int i = 0; i < GRID_SIZE; i++) {
}
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
}
}
}
int main() {
srand(time(NULL
)); // Seed for random number generator
char board[GRID_SIZE][GRID_SIZE];
char display[GRID_SIZE][GRID_SIZE];
initializeBoard(board, display);
placeMines(board);
// Fill board with numbers indicating adjacent mines
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
if (board[i][j] != 'M') {
int adjacentMines = countAdjacentMines(board, i, j);
if (adjacentMines > 0) {
board[i][j] = '0' + adjacentMines;
}
}
}
}
int gameOver = 0;
int row, col;
// Main game loop
while (!gameOver) {
printBoard(display);
printf("Enter row and column (0-5): "); scanf("%d %d", &row
, &col
);
if (row < 0 || row >= GRID_SIZE || col < 0 || col >= GRID_SIZE) {
printf("Invalid input! Please enter valid row and column.\n"); continue;
}
// Reveal the selected cell
gameOver = revealCell(board, display, row, col);
// Check for win condition
if (checkWin(board, display)) {
printBoard(display);
printf("You won! Congratulations!\n"); break;
}
if (gameOver) {
printBoard(display);
printf("Game Over! You hit a mine.\n"); }
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCiNkZWZpbmUgR1JJRF9TSVpFIDYKI2RlZmluZSBOVU1fTUlORVMgNgoKLy8gRnVuY3Rpb24gdG8gaW5pdGlhbGl6ZSB0aGUgYm9hcmQgd2l0aCBlbXB0eSBzcGFjZXMKdm9pZCBpbml0aWFsaXplQm9hcmQoY2hhciBib2FyZFtHU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CB7LTEsIDB9LCB7LTEsIDF9LCB7MCwgLTF9LCB7MCwgMX0sIHsxLCAtMX0sIHsxLCAwfSwgezEsIDF9fTsKICAgIAogICAgZm9yIChpbnQgaSA9IDA7IGkgPCA4OyBpKyspIHsKICAgICAgICBpbnQgciA9IHJvdyArIGRpcmVjdGlvbnNbaV1bMF07CiAgICAgICAgaW50IGMgPSBjb2wgKyBkaXJlY3Rpb25zW2ldWzFdOwogICAgICAgIAogICAgICAgIGlmIChyID49IDAgJiYgciA8IEdSSURfU0laRSAmJiBjID49IDAgJiYgYyA8IEdSSURfU0laRSkgewogICAgICAgICAgICBpZiAoYm9hcmRbcl1bY10gPT0gJ00nKSB7CiAgICAgICAgICAgICAgICBjb3VudCsrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICByZXR1cm4gY291bnQ7Cn0KCi8vIEZ1bmN0aW9uIHRvIHJldmVhbCBhIGNlbGwKaW50IHJldmVhbENlbGwoY2hhciBib2FyZFtHUklEX1NJWkVdW0dSSURfU0laRV0sIGNoYXIgZGlzcGxheVtHUklEX1NJWkVdW0dSSURfU0laRV0sIGludCByb3csIGludCBjb2wpIHsKICAgIGlmIChkaXNwbGF5W3Jvd11bY29sXSAhPSAnfCcpIHsKICAgICAgICByZXR1cm4gMDsgLy8gQ2VsbCBpcyBhbHJlYWR5IHJldmVhbGVkCiAgICB9CgogICAgZGlzcGxheVtyb3ddW2NvbF0gPSBib2FyZFtyb3ddW2NvbF07IC8vIFJldmVhbCB0aGUgY2VsbAoKICAgIGlmIChib2FyZFtyb3ddW2NvbF0gPT0gJ00nKSB7CiAgICAgICAgcmV0dXJuIDE7IC8vIEdhbWUgT3ZlcgogICAgfQoKICAgIC8vIElmIG5vIGFkamFjZW50IG1pbmVzLCByZWN1cnNpdmVseSByZXZlYWwgbmVpZ2hib3JpbmcgY2VsbHMKICAgIGlmIChib2FyZFtyb3ddW2NvbF0gPT0gJyAnKSB7CiAgICAgICAgaW50IGRpcmVjdGlvbnNbOF1bMl0gPSB7ey0xLCAtMX0sIHstMSwgMH0sIHstMSwgMX0sIHswLCAtMX0sIHswLCAxfSwgezEsIC0xfSwgezEsIDB9LCB7MSwgMX19OwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgODsgaSsrKSB7CiAgICAgICAgICAgIGludCByID0gcm93ICsgZGlyZWN0aW9uc1tpXVswXTsKICAgICAgICAgICAgaW50IGMgPSBjb2wgKyBkaXJlY3Rpb25zW2ldWzFdOwogICAgICAgICAgICAKICAgICAgICAgICAgaWYgKHIgPj0gMCAmJiByIDwgR1JJRF9TSVpFICYmIGMgPj0gMCAmJiBjIDwgR1JJRF9TSVpFICYmIGRpc3BsYXlbcl1bY10gPT0gJyMnKSB7CiAgICAgICAgICAgICAgICByZXZlYWxDZWxsKGJvYXJkLCBkaXNwbGF5LCByLCBjKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIAogICAgcmV0dXJuIDA7IC8vIENvbnRpbnVlIGdhbWUKfQoKLy8gRnVuY3Rpb24gdG8gY2hlY2sgaWYgYWxsIG5vbi1taW5lIGNlbGxzIGFyZSByZXZlYWxlZAppbnQgY2hlY2tXaW4oY2hhciBib2FyZFtHUklEX1NJWkVdW0dSSURfU0laRV0sIGNoYXIgZGlzcGxheVtHUklEX1NJWkVdW0dSSURfU0laRV0pIHsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgR1JJRF9TSVpFOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IEdSSURfU0laRTsgaisrKSB7CiAgICAgICAgICAgIGlmIChib2FyZFtpXVtqXSAhPSAnTScgJiYgZGlzcGxheVtpXVtqXSA9PSAnLSB8JykgewogICAgICAgICAgICAgICAgcmV0dXJuIDA7IC8vIE5vdCBhbGwgbm9uLW1pbmUgY2VsbHMgYXJlIHJldmVhbGVkCiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMTsgLy8gUGxheWVyIHdpbnMKfQoKLy8gRnVuY3Rpb24gdG8gcHJpbnQgdGhlIGJvYXJkCnZvaWQgcHJpbnRCb2FyZChjaGFyIGRpc3BsYXlbR1JJRF9TSVpFXVtHUklEX1NJWkVdKSB7CiAgICBwcmludGYoIiAgIik7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IEdSSURfU0laRTsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCIlZCAiLCBpKTsKICAgIH0KICAgIHByaW50ZigiXG4iKTsKCiAgICBmb3IgKGludCBpID0gMDsgaSA8IEdSSURfU0laRTsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCIlZCAiLCBpKTsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IEdSSURfU0laRTsgaisrKSB7CiAgICAgICAgICAgIHByaW50ZigiJWMgIiwgZGlzcGxheVtpXVtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBzcmFuZCh0aW1lKE5VTEwpKTsgLy8gU2VlZCBmb3IgcmFuZG9tIG51bWJlciBnZW5lcmF0b3IKCiAgICBjaGFyIGJvYXJkW0dSSURfU0laRV1bR1JJRF9TSVpFXTsKICAgIGNoYXIgZGlzcGxheVtHUklEX1NJWkVdW0dSSURfU0laRV07CgogICAgaW5pdGlhbGl6ZUJvYXJkKGJvYXJkLCBkaXNwbGF5KTsKICAgIHBsYWNlTWluZXMoYm9hcmQpOwoKICAgIC8vIEZpbGwgYm9hcmQgd2l0aCBudW1iZXJzIGluZGljYXRpbmcgYWRqYWNlbnQgbWluZXMKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgR1JJRF9TSVpFOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IEdSSURfU0laRTsgaisrKSB7CiAgICAgICAgICAgIGlmIChib2FyZFtpXVtqXSAhPSAnTScpIHsKICAgICAgICAgICAgICAgIGludCBhZGphY2VudE1pbmVzID0gY291bnRBZGphY2VudE1pbmVzKGJvYXJkLCBpLCBqKTsKICAgICAgICAgICAgICAgIGlmIChhZGphY2VudE1pbmVzID4gMCkgewogICAgICAgICAgICAgICAgICAgIGJvYXJkW2ldW2pdID0gJzAnICsgYWRqYWNlbnRNaW5lczsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBpbnQgZ2FtZU92ZXIgPSAwOwogICAgaW50IHJvdywgY29sOwoKICAgIC8vIE1haW4gZ2FtZSBsb29wCiAgICB3aGlsZSAoIWdhbWVPdmVyKSB7CiAgICAgICAgcHJpbnRCb2FyZChkaXNwbGF5KTsKCiAgICAgICAgcHJpbnRmKCJFbnRlciByb3cgYW5kIGNvbHVtbiAoMC01KTogIik7CiAgICAgICAgc2NhbmYoIiVkICVkIiwgJnJvdywgJmNvbCk7CgogICAgICAgIGlmIChyb3cgPCAwIHx8IHJvdyA+PSBHUklEX1NJWkUgfHwgY29sIDwgMCB8fCBjb2wgPj0gR1JJRF9TSVpFKSB7CiAgICAgICAgICAgIHByaW50ZigiSW52YWxpZCBpbnB1dCEgUGxlYXNlIGVudGVyIHZhbGlkIHJvdyBhbmQgY29sdW1uLlxuIik7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIH0KCiAgICAgICAgLy8gUmV2ZWFsIHRoZSBzZWxlY3RlZCBjZWxsCiAgICAgICAgZ2FtZU92ZXIgPSByZXZlYWxDZWxsKGJvYXJkLCBkaXNwbGF5LCByb3csIGNvbCk7CgogICAgICAgIC8vIENoZWNrIGZvciB3aW4gY29uZGl0aW9uCiAgICAgICAgaWYgKGNoZWNrV2luKGJvYXJkLCBkaXNwbGF5KSkgewogICAgICAgICAgICBwcmludEJvYXJkKGRpc3BsYXkpOwogICAgICAgICAgICBwcmludGYoIllvdSB3b24hIENvbmdyYXR1bGF0aW9ucyFcbiIpOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CgogICAgICAgIGlmIChnYW1lT3ZlcikgewogICAgICAgICAgICBwcmludEJvYXJkKGRpc3BsYXkpOwogICAgICAgICAgICBwcmludGYoIkdhbWUgT3ZlciEgWW91IGhpdCBhIG1pbmUuXG4iKTsKICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIDA7Cn0=