请用C语言写一个15*15的扫雷小游戏
时间: 2023-03-13 17:41:20 浏览: 4
扫雷是一个非常有趣的游戏,可以使用 C 语言编写。下面是一个简单的扫雷代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 15
#define COL 15
#define MINE_COUNT 99
int map[ROW][COL];
int state[ROW][COL];
void init() {
int i, j;
int x, y;
int count;
srand(time(0));
for (i = 0; i < MINE_COUNT; i++) {
x = rand() % ROW;
y = rand() % COL;
if (map[x][y] == -1) {
i--;
continue;
}
map[x][y] = -1;
for (j = -1; j <= 1; j++) {
for (k = -1; k <= 1; k++) {
if (x + j >= 0 && x + j < ROW && y + k >= 0 && y + k < COL && map[x + j][y + k] != -1) {
map[x + j][y + k]++;
}
}
}
}
}
void show() {
int i, j;
printf(" ");
for (i = 0; i < COL; i++) {
printf("%d ", i);
}
printf("\n");
for (i = 0; i < ROW; i++) {
printf("%2d ", i);
for (j = 0; j < COL; j++) {
if (state[i][j] == 1) {
printf("%d ", map[i][j]);
} else {
printf("* ");
}
}
printf("\n");
}
}
void open(int x, int y) {
if (x < 0 || x >= ROW || y < 0 || y >= COL) {
return;
}
if (state[x][y] == 1) {
return;
}
if (map[x][y] == -1) {
printf("you lose\n");
exit(0);
}
state[x][y] = 1;
if (map[x][y] == 0) {
open(x - 1, y - 1);
open(x - 1, y);
open(x - 1, y + 1);
open(x, y - 1);
open(x, y + 1);
open(x + 1, y - 1);
open(x + 1, y);
open(x + 1, y + 1);
}
}
int main() {
int x, y;
init();
while (1) {
show();
printf("input x y:");
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)