C语言实现扫雷游戏

来源:互联网 发布:ncut算法代码 编辑:程序博客网 时间:2024/05/17 06:52

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 11  void 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] = '*';}}}
下图为运行结果:



原创粉丝点击