#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 5
#define BOMBS 3

char board[SIZE][SIZE];
char show[SIZE][SIZE];

void init() {
    for (int i = 0; i < SIZE; i++)
        for (int j = 0; j < SIZE; j++)
            board[i][j] = show[i][j] = '.';

    int placed = 0;
    while (placed < BOMBS) {
        int r = rand() % SIZE;
        int c = rand() % SIZE;
        if (board[r][c] != '*') {
            board[r][c] = '*';
            placed++;
        }
    }
}

void printBoard() {
    printf("  ");
    for (int i = 0; i < SIZE; i++) printf("%d ", i);
    printf("\n");
    for (int i = 0; i < SIZE; i++) {
        printf("%d ", i);
        for (int j = 0; j < SIZE; j++)
            printf("%c ", show[i][j]);
        printf("\n");
    }
}

int main() {
    srand(time(0));
    init();

    int x, y;
    int safe = SIZE * SIZE - BOMBS;

    while (1) {
        printBoard();
        printf("Enter position to open (row column): ");
        scanf("%d %d", &x, &y);

        if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
            printf("Invalid position!\n");
            continue;
        }

        if (board[x][y] == '*') {
            printf("Boom! You hit a bomb. Game over.\n");
            break;
        }

        if (show[x][y] == '.') {
            show[x][y] = 'O';
            safe--;
        }

        if (safe == 0) {
            printf("Congratulations! You win!\n");
            break;
        }
    }

    printf("\nActual board:\n");
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++)
            printf("%c ", board[i][j]);
        printf("\n");
    }

    return 0;
}
