-
Notifications
You must be signed in to change notification settings - Fork 5
/
minesweeper.c
150 lines (138 loc) · 3.48 KB
/
minesweeper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 10
#define NUM_MINES 10
int board[BOARD_HEIGHT][BOARD_WIDTH];
int revealed[BOARD_HEIGHT][BOARD_WIDTH];
void init_board()
{
// Initialize the game board with random mine locations
for (int i = 0; i < BOARD_HEIGHT; i++)
{
for (int j = 0; j < BOARD_WIDTH; j++)
{
board[i][j] = 0;
revealed[i][j] = 0;
}
}
int num_mines_placed = 0;
while (num_mines_placed < NUM_MINES)
{
int x = rand() % BOARD_WIDTH;
int y = rand() % BOARD_HEIGHT;
if (board[y][x] == 0)
{
board[y][x] = 9; // 9 represents a mine
num_mines_placed++;
}
}
}
void display_board()
{
// Display the game board using ASCII characters
printf(" ");
for (int j = 0; j < BOARD_WIDTH; j++)
{
printf("%d ", j);
}
printf("\n");
for (int i = 0; i < BOARD_HEIGHT; i++)
{
printf("%d ", i);
for (int j = 0; j < BOARD_WIDTH; j++)
{
if (revealed[i][j])
{
if (board[i][j] == 9)
{
printf("* ");
}
else
{
printf("%d ", board[i][j]);
}
}
else
{
printf("- ");
}
}
printf("\n");
}
}
int count_adjacent_mines(int x, int y)
{
// Count the number of adjacent mines to a cell
int count = 0;
for (int i = y - 1; i <= y + 1; i++)
{
for (int j = x - 1; j <= x + 1; j++)
{
if (i >= 0 && i < BOARD_HEIGHT && j >= 0 && j < BOARD_WIDTH && board[i][j] == 9)
{
count++;
}
}
}
return count;
}
void reveal_cell(int x, int y)
{
// Recursively reveal cells and their neighbors
if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT || revealed[y][x])
{
return;
}
revealed[y][x] = 1;
if (board[y][x] == 0)
{
// Reveal neighbors recursively
for (int i = y - 1; i <= y + 1; i++)
{
for (int j = x - 1; j <= x + 1; j++)
{
reveal_cell(j, i);
}
}
}
}
int main()
{
// Set up the game board and initialize the random number generator
srand(time(NULL));
init_board();
// Set up the game loop
int game_over = 0;
int num_cells_revealed = 0;
while (!game_over)
{
// Display the game board and prompt for input
display_board();
printf("Enter x and y coordinates to reveal (e.g. '3 4'): ");
int x, y;
scanf("%d %d", &x, &y);
// Reveal the selected
if (board[y][x] == 9)
{
// Game over if a mine is revealed
game_over = 1;
}
else
{
// Otherwise, reveal the selected cell and update the number of cells revealed
reveal_cell(x, y);
num_cells_revealed++;
}
// Check for win condition
if (num_cells_revealed == BOARD_WIDTH * BOARD_HEIGHT - NUM_MINES)
{
printf("You win!\n");
game_over = 1;
}
}
// Display the final game board
display_board();
return 0;
}