C语言实现扫雷程序

所属分类: 软件编程 / C 语言 阅读数: 86
收藏 0 赞 0 分享

使用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++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多