温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C语言代码如何实现扫雷游戏

发布时间:2021-05-27 12:16:19 来源:亿速云 阅读:210 作者:小新 栏目:编程语言

这篇文章主要介绍了C语言代码如何实现扫雷游戏,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

该游戏我们也是利用数组完成,设置俩个数组一个mine数组,一个show数组。

mine数组存放雷,show数组显示棋盘并且显示该位置是否有雷或者显示该位置周围有几个雷。

数组大小有讲究,我们宏定义变量 ROW COL 为9 定义ROWS COLS为11,我们显示的是9X9的棋盘,也是将雷设置在9X9的位置内,但是我们设置数组是设置11X11,因为这样方便我们遍历9X9棋盘四边位置上某位置四周雷的数目,不然的话会发生越界错误。

对于雷的符号,我们设置空位置为 0,有雷的位置为1,这样易于我们统计某一位置周围有多少雷的数目。

重点在于扫雷函数,玩家输入x y位置,我们判断该位置是否有雷,否的话判断该位置周围有多少雷并在存于show数组display给玩家。并且我们输入一个位置当该位置不是雷的时候,计数器count++;若果该计数器count==col*row-EAXY_COUNT;我们就判断玩家赢。

#include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 //函数声明 void ChushiBoard(char board[ROWS][COLS],int rows,int cols,char set); void Dayinboard(char board[ROWS][COLS], int row, int col); void BuzhiBoard(char mine[ROWS][COLS], int row, int col); void CaoleiBoard(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col); void menu() {  printf("*****************************************\n");  printf("*****************************************\n");  printf("**** 输入1开始游戏 输入0退出游戏 *****\n");  printf("*****************************************\n");  printf("*****************************************\n\n\n"); } void game() {  char mine[ROWS][COLS];//存放雷  char show[ROWS][COLS];//显示排查出来的雷  //初始化  ChushiBoard(mine, ROWS, COLS,'0');//'0'  ChushiBoard(show, ROWS, COLS,'*');//'*'  //布置雷  BuzhiBoard(mine,ROW,COL);  //Dayinboard(mine, ROW, COL);  //打印棋盘  Dayinboard(show, ROW, COL);  //扫雷  CaoleiBoard(mine,show,ROW,COL); } void test() {  int input = 0;  do  {  menu();  printf("请输入:>");  scanf("%d", &input);  switch (input)  {  case 1:  game();  break;  case 0:  printf("退出游戏\n\n");  break;  default:  printf("输入错误,重新输入\n\n");  break;  }  } while (input); } int main() {  test();  system("pause");  return 0; } void ChushiBoard(char board[ROWS][COLS], int rows, int cols,char set) {  int i = 0;  int j = 0;  for (i = 0; i < rows; i++)  {  for (j = 0; j < cols; j++)  {  board[i][j] = set;  }  } } void Dayinboard(char board[ROWS][COLS], int row, int col) {  int i = 0;  int j = 0;  printf("------------------------------\n");  for (i = 0; i <= col; i++)  {  printf(" %d ", i);  }  printf("\n");  for (i = 1; i <= row; i++)  {  printf(" %d ", i);  for (j = 1; j <= col; j++)  {  printf(" %c ", board[i][j]);  }  printf("\n");  }  printf("------------------------------\n"); } void BuzhiBoard(char mine[ROWS][COLS], int row, int col) {  int count = EASY_COUNT;  while (count)  {  int x = rand()%row+1;  int y = rand()%col+1;  if (mine[x][y] == '0')  {  mine[x][y] = '1';  count--;  }  } } static int GetMineCount(char mine[ROWS][COLS], int x, int y) {  return mine[x - 1][y] +  mine[x - 1][y - 1] +  mine[x][y - 1] +  mine[x + 1][y - 1] +  mine[x + 1][y] +  mine[x + 1][y + 1] +  mine[x][y + 1] +  mine[x - 1][y + 1] - 8 * '0'; } void CaoleiBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {  int x = 0;  int y = 0;  int win = 0;  while (win<row*col-EASY_COUNT)  {  printf("请输入要排查的坐标(格式:X空格X回车):>");  scanf("%d%d", &x, &y);  if (x >= 1 && x <= 9 && y >= 1 && y <= 9)  {  if (mine[x][y] == '1')  {  printf(" 很遗憾,你被炸死了!!!\n");  Dayinboard(mine, row, col);  break;  }  else  {  int count = GetMineCount(mine,x,y);  show[x][y] = count+'0';  Dayinboard(show, row, col);  win++;  }  }  else  {  printf("输入的坐标非法\n");  }  }  if (win == row*col - EASY_COUNT)  {  printf(" 恭喜你,你排雷成功了!!!\n");  } }

运行结果:

C语言代码如何实现扫雷游戏

C语言代码如何实现扫雷游戏

C语言代码如何实现扫雷游戏

感谢你能够认真阅读完这篇文章,希望小编分享的“C语言代码如何实现扫雷游戏”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI