fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SIZE 5
  6. #define BOMBS 3
  7.  
  8. char board[SIZE][SIZE];
  9. char show[SIZE][SIZE];
  10.  
  11. void init() {
  12. for (int i = 0; i < SIZE; i++)
  13. for (int j = 0; j < SIZE; j++)
  14. board[i][j] = show[i][j] = '.';
  15.  
  16. int placed = 0;
  17. while (placed < BOMBS) {
  18. int r = rand() % SIZE;
  19. int c = rand() % SIZE;
  20. if (board[r][c] != '*') {
  21. board[r][c] = '*';
  22. placed++;
  23. }
  24. }
  25. }
  26.  
  27. void printBoard() {
  28. printf(" ");
  29. for (int i = 0; i < SIZE; i++) printf("%d ", i);
  30. printf("\n");
  31. for (int i = 0; i < SIZE; i++) {
  32. printf("%d ", i);
  33. for (int j = 0; j < SIZE; j++)
  34. printf("%c ", show[i][j]);
  35. printf("\n");
  36. }
  37. }
  38.  
  39. int main() {
  40. srand(time(0));
  41. init();
  42.  
  43. int x, y;
  44. int safe = SIZE * SIZE - BOMBS;
  45.  
  46. while (1) {
  47. printBoard();
  48. printf("Enter position to open (row column): ");
  49. scanf("%d %d", &x, &y);
  50.  
  51. if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
  52. printf("Invalid position!\n");
  53. continue;
  54. }
  55.  
  56. if (board[x][y] == '*') {
  57. printf("Boom! You hit a bomb. Game over.\n");
  58. break;
  59. }
  60.  
  61. if (show[x][y] == '.') {
  62. show[x][y] = 'O';
  63. safe--;
  64. }
  65.  
  66. if (safe == 0) {
  67. printf("Congratulations! You win!\n");
  68. break;
  69. }
  70. }
  71.  
  72. printf("\nActual board:\n");
  73. for (int i = 0; i < SIZE; i++) {
  74. for (int j = 0; j < SIZE; j++)
  75. printf("%c ", board[i][j]);
  76. printf("\n");
  77. }
  78.  
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0.03s 25816KB
stdin
Standard input is empty
stdout
#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;
}