写一个python扫雷程序
时间: 2023-05-12 12:04:05 浏览: 55
很高兴为您服务!我可以为您提供一个简单的Python扫雷程序,它可以帮助您在短时间内完成扫雷任务。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
//定义雷区大小
int row, col;
printf("请输入雷区的行数:");
scanf("%d", &row);
printf("请输入雷区的列数:");
scanf("%d", &col);
int mine_field[row][col];
//初始化雷区
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
mine_field[i][j] = 0;
}
}
//随机放置雷
srand((unsigned)time(NULL));
int mine_num = row * col / 5;
int x, y;
for(int i = 0; i < mine_num; i++){
x = rand() % row;
y = rand() % col;
mine_field[x][y] = 1;
}
//扫雷
int x1, y1;
int mine_count;
while(1){
//输入坐标
printf("请输入要排查的坐标:");
scanf("%d %d", &x1, &y1);
//判断是否越界
if(x1 < 0 || x1 >= row || y1 < 0 || y1 >= col){
printf("输入坐标越界!\n");
continue;
}
//判断是否是雷
if(mine_field[x1][y1] == 1){
printf("Game Over!\n");
break;
}
//计算周围雷的数量
mine_count = 0;
for(int i = x1-1; i <= x1+1; i++){
for(int j = y1-1; j <= y1+1; j++){
if(i >= 0 && i < row && j >= 0 && j < col){
if(mine_field[i][j] == 1){
mine_count++;
}
}
}
}
printf("周围有%d颗雷!\n", mine_count);
//判断是否胜利
if(mine_count == 0){
int flag = 1;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(mine_field[i][j] == 0){
flag = 0;
break;
}
}
}
if(flag == 1){
printf("You Win!\n");
break;
}
}
}
return 0;
}
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)