#include<stdio.h>
#include<stdlib.h>
#define ROW 11
#define COL 11
#define COUNT 10
//扫雷
//1.初始化地图(main_map,show_map),布置雷阵
//2.先打印show_map。
//3.提示用户进行输入坐标[1,ROW][1,COL]
//4.对用户输入进行合法性判定,如果不合法,请提示用户进行重新输入
//5.判定玩家当前是否踩雷,如果踩雷,则游戏结束
//6.如果没踩雷,判断玩家是否胜利,胜利条件是地图所有没有雷的方格都被翻开了
//7.如果游戏没有胜利,就需要统计当前位置周围有几个雷,然后返回第三步
//先打印一个游戏菜单
void menu(){
printf("*****************************\n");
printf("****** 欢迎来到扫雷游戏 ******\n");
printf("*****************************\n");
printf("****** 1.开始游戏 **********\n");
printf("****** 2.结束游戏 **********\n");
}
//创造一个地图;用俩个二维数组表示
//第一个二维数组表示雷阵,第二个二维数组表示用户看到的地图
//扫雷地图大小为9*9,但是我们把这个二维数组定义成11*11
//此时我们的看到的有效地图范围是[1,9][1,9]
//对于main_map用'X'表示地雷 用'*'表示不是地雷
char main_map[ROW][COL];
char show_map[ROW][COL];
//将数组初始化
//对于main_map,先把所有元素都设置成'0'
//对于show_map, 先把所有元素都设置成'*'
void Init(char main_map[ROW][COL],char show_map[ROW][COL]){
int row = 0;
int col = 0;
for (row = 0; row < ROW - 1; row++){
for (col = 0; col < COL - 1; col++){
main_map[row][col] = '0';
show_map[row][col] = '*';
}
}
}
#include <time.h>
//设置雷的位置
void Set_main_map(char main_map[ROW][COL]){
int row = 0;
int col = 0;
int count = COUNT;
srand((unsigned)time(NULL));
while(count){
row = rand() % 9 + 1;
col = rand() % 9 + 1;
if( main_map[row][col]=='0'){
main_map[row][col]='X';
count--;
}
}
}
//打印下棋完后显示的界面
void DisPlayshow_map(char show_map[ROW][COL]){
int row = 0;
int col = 0;
printf(" ");
for(row = 1;row<ROW-1;row++){
printf("%d",row);
}
printf("\n");
for(row = 1;row < ROW - 1;++row ){
printf("%d",row);
for(col = 1;col < COL - 1;++col){
printf("%c",show_map[row][col]);
}
printf("\n");
}
}
//计算雷的个数
int Update_show_map(char show_map[ROW][COL],int row,int col){
int count = 0;
if(main_map[row - 1][col - 1] == 'X'){//左上方
count++;
}
if(main_map[row - 1][col] == 'X'){//左边
count++;
}
if(main_map[row - 1][col + 1] == 'X'){//左下方
count++;
}
if(main_map[row][col - 1] == 'X'){//上边
count++;
}
if(main_map[row][col + 1] == 'X'){//下边
count++;
}
if(main_map[row + 1][col + 1] == 'X'){//右下
count++;
}
if(main_map[row + 1][col] == 'X'){//右边
count++;
}
if(main_map[row + 1][col - 1] == 'X'){//右上
count++;
}
return count;
}
//扫雷
int Sweep(char main_map[ROW][COL],char show_map[ROW][COL]){
int count = 0;
int row = 0;
int col = 0;
while(1){
printf("请您输入您所想扫的位置:");
scanf("%d,%d",&row,&col);
//对用户输入进行合法性判定,如果不合法,请提示用户进行重新输入
if(row <= 0||row > ROW-2||col <= 0||col > COL-2){
printf("您输入的位置不合法,请重新输入");
continue;
}
//判定玩家当前是否踩雷,如果踩雷,则游戏结束
if(main_map[row][col] == 'X'){
printf("您踩到雷了,游戏结束");
break;
}
//如果没踩雷,判断玩家是否胜利,胜利条件是地图所有没有雷的方格都被翻开了
if(count == (ROW-2)*(COL-2)-COUNT){
printf("扫雷成功!");
}else{//需要统计当前位置周围有几个雷,然后返回第三步
int ret = Update_show_map(main_map, row, col);
show_map[row][col] = ret + '0';
DisPlayshow_map(show_map);
}
count++;
}
return 0;
}
//建一个游戏函数
void Game(char main_map[ROW][COL],char show_map[ROW][COL]){
Set_main_map(main_map);
DisPlayshow_map(show_map);
Sweep(main_map, show_map);
}
int main(){
int input = 0;
Init(main_map, show_map);
menu();
while(1){
printf("请选择:");
scanf("%d",&input);
if(input == 1){
printf("开始游戏,祝您玩的愉快");
Game(main_map,show_map);
break;
}
if(input == 0){
printf("结束游戏");
}
}
system("pause");
return 0;
}
扫雷初阶版
最新推荐文章于 2022-05-12 16:49:28 发布