温馨提示×

温馨提示×

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

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

C语言如何实现扫雷程序

发布时间:2021-05-28 12:36:39 来源:亿速云 阅读:174 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关C语言如何实现扫雷程序的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

使用C语言实现简单的扫雷程序,主要是对二维数组的运用,我们需要一个头文件,两个源文件来实现。

game.h //包含函数的声明,宏定义
test.c //包含主函数,函数调用
game.c //包含函数的定义

整体思路

1.要完成一个简单的扫雷程序,我们需要创建两个二维数组,一个保存我们随机生成的雷,另外一个向外界展示。

//使用宏定义定义常量,方便之后对数组的使用
#define ROW 11 //雷
#define COL 11
#define ROWS 9 //棋盘
#define COLS 9
#define THUNDER 10 //雷数

char mine[ROW][COL] = { 0 }; //存雷数组
char show[ROWS][COLS] = { 0 }; //展示数组
Arr_init(mine, show, ROW, COL, ROWS, COLS); //数组初始化

2.完成对数组的初始化后,我们需要对雷进行放置

void Col_thu(char mine[ROW][COL], int row, int col, int thunder) //布置雷
{
 int x, y;
 int i = 0;
 while (i < thunder) //存放雷的个数等于雷的个数
 {
 x = rand() % (row-2) + 1;
 y = rand() % (col-2) + 1;
 if (mine[x][y] == '0')
 {
 mine[x][y] = '1';
 i++;
 } 
 }
}

3.布置完雷后,我们需要打印所需要的棋盘

存雷棋盘

void Print_che1(char mine[ROW][COL], int row, int col) //打印存雷棋盘
{
 int i, j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
 printf("%2c", mine[i][j]);
 }
 printf("\n");
 }
}

展示棋盘

void Print_che2(char show[ROWS][COLS], int rows, int cols) //打印展示棋盘
{
 int i, j;
 for (i = 0; i <= rows; i++) //方便我们输入坐标
 {
 printf("%2d", i);
 }
 printf("\n");
 for (i = 0; i < rows; i++)
 {
 printf("%2d", i+1);
 for (j = 0; j < cols; j++)
 {
 printf("%2c", show[i][j]);
 }
 printf("\n");
 }
}

4.打印完棋盘后,我们开始扫雷了。

在扫雷的过程中,我们需要在没有找到雷时展示输入坐标周围的雷数并进行展开,同时,为了增加游戏的可玩性,当第一次就找到雷时,我们需要将雷转移到其他位置。

统计周围雷数

void Num_mines(char mine[ROW][COL],char show[ROWS][COLS], int x, int y) //计算输入坐标周围的雷数
{
 int ch;
 ch = mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';
 show[x - 1][y - 1] = ch + 48; //数字对应的ASCLL与数字字符相差48
}

展开

void open_show(char mine[ROW][COL], char show[ROWS][COLS], int rows, int x, int y) //计算输入坐标及周围的雷数(展开)
{
 if (mine[x][y - 1] == '0')//中 x,y
 {
 if (x - 1 >= 0 && y - 1 >= 0 && x + 1 <= rows + 1 && y + 1 <= rows + 1) //防止越界情况(所有的坐标+1<=rows+1,-1>=0)
 {
 Num_mines(mine, show, x, y); //返回坐标周围的雷数
 }
 }
 if (mine[x - 1][y - 1] == '0')//左上角x-1,y-1
 {
 if (x - 2 >= 0 && y - 2 >= 0 && x <= rows + 1 && y <= rows + 1) 
 {
 Num_mines(mine, show, x - 1, y - 1);
 }
 }

 if (mine[x - 1][y] == '0')//上x-1, y
 {
 if (x - 2 >= 0 && y - 1 >= 0 && x <= rows + 1 && y + 1 <= rows + 1)
 {
 Num_mines(mine, show, x - 1, y);
 }
 }
 if (mine[x - 1][y + 1] == '0')//右上角 x-1, y+1
 {
 if (x - 2 >= 0 && y >= 0 && x <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x - 1, y + 1);
 }
 }
 if (mine[x][y - 1] == '0')//左 x,y-1
 {
 if (x - 1 >= 0 && y - 2 >= 0 && x + 1 <= rows + 1 && y <= rows + 1)
 {
 Num_mines(mine, show, x, y - 1);
 }
 }
 if (mine[x][y + 1] == '0')//右 x,y+1
 {
 if (x - 1 >= 0 && y >= 0 && x + 1 <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x, y + 1);
 }
 }

 if (mine[x + 1][y - 1] == '0')//左下角 x+1,y-1
 {
 if (x >= 0 && y - 2 >= 0 && x + 2 <= rows + 1 && y <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y - 1);
 }
 }

 if (mine[x + 1][y] == '0')//下 x+1,y
 {
 if (x >= 0 && y - 1 >= 0 && x + 2 <= rows + 1 && y + 1 <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y);
 }
 }
 if (mine[x + 1][y + 1] == '0')//右下角 x+1,y+1
 {
 if (x >= 0 && y >= 0 && x + 2 <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y + 1);
 }
 }
}

扫雷

char Find_thu(char mine[ROW][COL], char show[ROWS][COLS], int row, int col, int i) //找雷
{
 int x, y;
flag1:
 printf("请玩家输入坐标");
 scanf("%d %d", &x, &y);
flag:
 if ((x > 0 && x <= row - 2) && (y > 0 && y <= col - 2)) //判断输入坐标的正确性
 {
 if (mine[x][y] == '0')//没找到雷
 {
 open_show(mine, show, ROWS, x, y); //计算输入坐标及周围的雷数(展开)
 return '0';
 }
 else //找到雷
 {
 if (i == 0) //第一个就找到雷
 {
 mine[x][y] = '0';
 while (1)
 {
  int a, b;
  a = rand() % (row - 2) + 1;
  b = rand() % (col - 2) + 1;
  if (mine[a][b] == '0')
  {
  mine[a][b] = '1';
  goto flag;
  }
 }
 }
 else
 {
 show[x - 1][y - 1] = '1';
 return '1';
 } 
 }
 }
 else
 {
 printf("输入错误\n");
 goto flag1;
 }
}

确定大致思路后,我们完成程序的流程部分,并放入我们所创建的文件中。

代码如下:

game.h //包含函数的声明,宏定义

#ifndef __GAME_H__
#define __GAME_H__

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>

#define ROW 11//雷
#define COL 11
#define ROWS 9 //棋盘
#define COLS 9
#define THUNDER 10 //雷数

void Arr_init(char mine[ROW][COL], char show[ROWS][COLS], int row, int col, int rows, int cols); //数组初始化
void Col_thu(char mine[ROW][COL], int row, int col, int thunder); //布置雷
void Print_che1(char mine[ROW][COL], int row, int col); //打印存雷棋盘
void Print_che2(char show[ROWS][COLS], int rows, int cols); //打印展示棋盘
char Find_thu(char mine[ROW][COL], char show[ROWS][COLS], int row, int col, int i); //找雷
void Num_mines(char mine[ROW][COL], char show[ROWS][COLS], int x, int y);//计算输入坐标周围的雷数
void open_show(char mine[ROW][COL], char show[ROWS][COLS], int rows, int x, int y);////计算输入坐标及周围的雷数(展开)
#endif // __GAME_H__

test.c //包含主函数,函数调用

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void menu() //菜单函数
{
 printf("********************\n");
 printf("**** 1.play ****\n");
 printf("**** 0.exit ****\n");
 printf("********************\n");
}

void game() //游戏函数
{
 int i;
 char mine[ROW][COL] = {0};//存雷数组
 char show[ROWS][COLS] = { 0 };//展示数组
 Arr_init(mine, show, ROW, COL, ROWS, COLS);//数组初始化
 Col_thu(mine, ROW, COL, THUNDER); //布置雷
 Print_che2(show, ROWS, COLS); //打印展示棋盘
 for (i = 0; i < ROWS * COLS - THUNDER; i++)
 {
 char n;
 n = Find_thu(mine, show, ROW, COL, i); //找雷
 Print_che2(show, ROWS, COLS); //打印展示棋盘
 if (n == '0')
 {
 printf("_______________________\n");
 }
 else
 {
 printf("你踩到雷了,游戏结束\n");
 Print_che1(mine, ROW, COL); //打印存雷棋盘
 break;
 }
 }
 if (i == ROWS * COLS - THUNDER)
 printf("游戏成功\n");
}

void test() //游戏流程函数
{
 int input;
 srand((unsigned)time(NULL));
 do
 {
 menu();
 printf("请输入选择:");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 game();
 break;
 case 0:
 break;
 default:
 printf("输入错误,请重新输入\n");
 }
 } while (input);
}

int main()
{
 test();
 system("pause");
 return 0;
}

game.c //包含函数的定义

#include "game.h"

void Arr_init(char mine[ROW][COL], char show[ROWS][COLS], int row, int col, int rows, int cols)//数组初始化
{
 int i, j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
 mine[i][j] = '0';//改变存雷数组
 }
 }
 for (i = 0; i < rows; i++)
 {
 for (j = 0; j < cols; j++)
 {
 show[i][j] = '*';//改变展示数组
 }
 }
}

void Col_thu(char mine[ROW][COL], int row, int col, int thunder) //布置雷
{
 int x, y;
 int i = 0;
 while (i < thunder) //存放雷的个数等于雷的个数
 {
 x = rand() % (row-2) + 1;
 y = rand() % (col-2) + 1;
 if (mine[x][y] == '0')
 {
 mine[x][y] = '1';
 i++;
 } 
 }
}

void Print_che1(char mine[ROW][COL], int row, int col) //打印存雷棋盘
{
 int i, j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
 printf("%2c", mine[i][j]);
 }
 printf("\n");
 }
}

void Print_che2(char show[ROWS][COLS], int rows, int cols) //打印展示棋盘
{
 int i, j;
 for (i = 0; i <= rows; i++)//方便我们输入坐标
 {
 printf("%2d", i);
 }
 printf("\n");
 for (i = 0; i < rows; i++)
 {
 printf("%2d", i+1);
 for (j = 0; j < cols; j++)
 {
 printf("%2c", show[i][j]);
 }
 printf("\n");
 }
}

void open_show(char mine[ROW][COL], char show[ROWS][COLS], int rows, int x, int y) //计算输入坐标及周围的雷数(展开)
{
 if (mine[x][y - 1] == '0')//中 x,y
 {
 if (x - 1 >= 0 && y - 1 >= 0 && x + 1 <= rows + 1 && y + 1 <= rows + 1) //防止越界情况(所有的坐标+1<=rows+1,-1>=0)
 {
 Num_mines(mine, show, x, y); //返回坐标周围的雷数
 }
 }
 if (mine[x - 1][y - 1] == '0')//左上角x-1,y-1
 {
 if (x - 2 >= 0 && y - 2 >= 0 && x <= rows + 1 && y <= rows + 1) 
 {
 Num_mines(mine, show, x - 1, y - 1);
 }
 }

 if (mine[x - 1][y] == '0')//上x-1, y
 {
 if (x - 2 >= 0 && y - 1 >= 0 && x <= rows + 1 && y + 1 <= rows + 1)
 {
 Num_mines(mine, show, x - 1, y);
 }
 }
 if (mine[x - 1][y + 1] == '0')//右上角 x-1, y+1
 {
 if (x - 2 >= 0 && y >= 0 && x <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x - 1, y + 1);
 }
 }
 if (mine[x][y - 1] == '0')//左 x,y-1
 {
 if (x - 1 >= 0 && y - 2 >= 0 && x + 1 <= rows + 1 && y <= rows + 1)
 {
 Num_mines(mine, show, x, y - 1);
 }
 }
 if (mine[x][y + 1] == '0')//右 x,y+1
 {
 if (x - 1 >= 0 && y >= 0 && x + 1 <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x, y + 1);
 }
 }

 if (mine[x + 1][y - 1] == '0')//左下角 x+1,y-1
 {
 if (x >= 0 && y - 2 >= 0 && x + 2 <= rows + 1 && y <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y - 1);
 }
 }

 if (mine[x + 1][y] == '0')//下 x+1,y
 {
 if (x >= 0 && y - 1 >= 0 && x + 2 <= rows + 1 && y + 1 <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y);
 }
 }
 
 if (mine[x + 1][y + 1] == '0')//右下角 x+1,y+1
 {
 if (x >= 0 && y >= 0 && x + 2 <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y + 1);
 }
 }
}
char Find_thu(char mine[ROW][COL], char show[ROWS][COLS], int row, int col, int i)//找雷
{
 int x, y;
flag1:
 printf("请玩家输入坐标");
 scanf("%d %d", &x, &y);
flag:
 if ((x > 0 && x <= row - 2) && (y > 0 && y <= col - 2))//判断输入坐标的正确性
 {
 if (mine[x][y] == '0')//没找到雷
 {
 open_show(mine, show, ROWS, x, y);//计算输入坐标及周围的雷数(展开)
 return '0';
 }
 else //找到雷
 {
 if (i == 0)//第一个就找到雷
 {
 mine[x][y] = '0';
 while (1)
 {
  int a, b;
  a = rand() % (row - 2) + 1;
  b = rand() % (col - 2) + 1;
  if (mine[a][b] == '0')
  {
  mine[a][b] = '1';
  goto flag;
  }
 }
 }
 else
 {
 show[x - 1][y - 1] = '1';
 return '1';
 }
 
 }
 }
 else
 {
 printf("输入错误\n");
 goto flag1;
 }
}

到这里,我们的程序就完成了,我们看看程序的效果

C语言如何实现扫雷程序

C语言如何实现扫雷程序

感谢各位的阅读!关于“C语言如何实现扫雷程序”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

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

AI