c语言实现扫雷

来源:互联网 发布:sql 查询年龄最大的人 编辑:程序博客网 时间:2024/05/17 07:54

game.h

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>#include<stdlib.h> #include<time.h>#include<string.h>#define SUM 10#define row 11#define col 11void print_();void init(char mine[row][col], char show[row][col]);void select(char show, char mine);int play_game(char mine[row][col], char show[row][col]);void setmine(char mine[row][col]);void dayin(char show[row][col]);int caculate(char mine[row][col], int x, int y);int saolei(char mine[row][col], char show[row][col]);void blank_(char mine[row][col], char show[row][col], int x, int y);

test.c

#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"int main(){char mine[row][col];char show[row][col];print_();//调用打印菜单的函数int input = 0;do{printf("请输入你的选择>");//让用户选择玩游戏还是退出游戏scanf("%d", &input);switch (input){case 1:play_game(mine, show);//进入游戏break;case 0:exit(0);break;default:printf("输入错误,请重新输入>");}} while (input);system("system");return 0;}

game.c

#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"void print_()//打印菜单的函数{printf("*************欢迎进入扫雷系统***********\n");printf("*****************请选择*****************\n");printf("**************1.play 2.exit*************\n");}int play_game(char mine[row][col], char show[row][col])//游戏的函数{init(mine, show);setmine(mine);//首先调用布雷函数在雷的数组上面布置雷dayin(mine);//设置好雷以后打印出棋盘(可打印可不打印)dayin(show);//打印显示给玩家看的界面saolei(mine, show);//玩家开始扫雷return 0;}void  setmine(char mine[row][col])//布置雷的函数{int x = 0;int y = 0;int count = SUM;//用count统计雷的数量雷srand((unsigned)time(NULL));//产生随机数的while (count > 0){x = rand() % 9;//y = rand() % 9;if (mine[x][y] == '0'){mine[x][y] = '1';count--;}}}void dayin(char show[row][col])//打印棋盘的函数{int i = 0;int j = 0;printf(" ");for (i = 0; i < 9; i++){printf("   %d", i);}printf("\n");printf("   ___________________________________\n");for (i = 0; i < 9; i++){printf("%d", i);printf(" | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n", show[i][0], show[i][1], show[i][2], show[i][3], show[i][4], \show[i][5], show[i][6], show[i][7], show[i][8], show[i][9]);if (i != 9){printf("  |---|---|---|---|---|---|---|---|---|\n");}}}int caculate(char mine[row][col], int x, int y)//计算x,y周围雷的个数的函数{int count = 0;if (mine[x - 1][y - 1] == '1'){count++;}if (mine[x - 1][y] == '1'){count++;}if (mine[x - 1][y + 1] == '1'){count++;}if (mine[x][y + 1] == '1'){count++;}if (mine[x + 1][y + 1] == '1'){count++;}if (mine[x + 1][y] == '1'){count++;}if (mine[x + 1][y - 1] == '1'){count++;}if (mine[x][y - 1] == '1'){count++;}return count;}int saolei(char mine[row][col], char show[row][col])//扫雷的函数{int x;int y;int count = 0;int z = 0;while (count != ((col - 2)*(row - 2)) - SUM)//当没有扫完时,可以一直玩{do{printf("请输入你的坐标>>\n");scanf("%d%d", &x, &y);z++;if (x<0 || y>8 || x > 8 || (y<0) || (show[x][y] != '*')){printf("输入错误\n");}} while ((x<0 || y>8 || x>8 || y < 0));if (mine[x][y] == '1'){if (z == 1){mine[x][y] = mine[x - 1][x - 1];goto flag1;}printf("对不起你被炸了\n");dayin(mine);printf("还想继续玩吗?****1.play  0.exit****\n");return 0;}else if (mine[x][y] != '1'){flag1:blank_(mine, show, x, y);int ret = caculate(mine, x, y);show[x][y] = ret + '0';if (show[x][y] == '0')show[x][y] = ' ';dayin(show);count++;}else{printf("你赢了");dayin(mine);break;}}}void blank_(char mine[row][col], char show[row][col], int x, int y){if ((x >= 0) && (y >= 0) && (x <= 8) && (y <= 8)){if (caculate(mine, x, y) == 0)//如果它周围没有雷,则它等于0{show[x][y] = ' ';///将它设置为空白if (show[x - 1][y - 1] == '*'){blank_(mine, show, x - 1, y - 1);}if (show[x - 1][y] == '*'){blank_(mine, show, x - 1, y);}if (show[x - 1][y + 1] == '*'){blank_(mine, show, x - 1, y + 1);}if (show[x][y + 1] == '*'){blank_(mine, show, x, y + 1);}if (show[x + 1][y + 1] == '*'){blank_(mine, show, x + 1, y + 1);}if (show[x + 1][y] == '*'){blank_(mine, show, x + 1, y);}if (show[x + 1][y - 1] == '*'){blank_(mine, show, x + 1, y - 1);}if (show[x][y - 1] == '*'){blank_(mine, show, x, y - 1);}}}}void init(char mine[row][col], char show[row][col]){//char mine[row][col];//定义两个数组//char show[row][col];int i = 0;int j = 0;for (i = 0; i < 10; i++){for (j = 0; j < 10; j++)//初始化两个数组,一个扫雷用的数组,一个显示的数组{mine[i][j] = '0';show[i][j] = '*';}}}

此扫雷完成的功能: 1,玩家如果第一个踩中雷,则将它移走
                              2,展示空白区域
                              3,可以循环玩
游戏界面如下图>>>
图一是展示随机布置的雷,其中1为雷,

这个是给玩家看的界面,再这个界面进行扫雷


这个时候我输入坐标5 5,如图显示提醒其坐标位置周围有1个雷


我在输入3 3 坐标,被炸死了,正如图一显示的坐标三三的位置是雷


如果周围没有雷,则显示空白(这两幅图的布置的雷不是图一中的,因为我重新开始了一局)


如果用户第一个踩中雷,则将其移走,

在这里再开始新的一局

雷的布置如下图


如上图1 0 位置是雷,我输入1 0坐标,则并没有中雷,将雷移走了,从而保证玩家第一次不会直接踩雷导致游戏结束


如雷的布置图所示2 2也为雷,我第二次输入2 2坐标,则显示被炸死了


如果有不足和错误的地方,欢迎大家提出宝贵意见,谢谢!

1 0
原创粉丝点击