扫雷-C语言实现
在写代码之前,我们需要先了解几个概念。
数组大小:
我们都知道扫雷是由数组实现的。
假设我们定义为9×9 的方格,那我们的数组也定义为9×9 吗?
必然不是,如果定义9*9 我们在排查边界的时候就会数组越界,因此我们定义的数组大小应该是 行+2 列+2
定义几个数组呢?
如果我们只定义一个数组,很明显是不够的,因为我们要设置雷的位置,还要去排雷,就会冲突,因为我们设定两个数组,一个数组用来放雷,另一个数组就是我们界面显示的,用来排雷。下面我们来实现它。
工程界面如下:
main函数如下:
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
#include <time.h>
void menu()
{
printf("***************************\n");
printf("***** 1.paly 0.exit *****\n");
printf("***************************\n");
}
void game()
{
//雷的信息存储
//1、布置好雷的信息
char mineboard[ROWS][COLS] = { 0 };
//2、排查出雷的信息
char showboard[ROWS][COLS] = { 0 };
//初始化
InitBoard(mineboard, ROWS, COLS, '0');
InitBoard(showboard, ROWS, COLS, '*');
//打印
//DisplayBoard(mineboard, ROWS, COLS);
DisplayBoard(showboard, ROW, COL);
//布置雷
SetMine(mineboard, ROW, COL);
//DisplayBoard(mineboard, ROW, COL);
//扫雷
FindMine(mineboard, showboard, ROW, COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("选择错误请从新选择:\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
game.c文件如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0, j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisCoord(int row, int col)
{
int i = 0;
printf(" ");
for (i = 1; i <= row; i++)
{
printf(" %d ", i);
}
printf("\n");
}
void DisplayLine(int row, int col)
{
int i = 0;
printf(" ");
for (i = 0; i < 2*row; i++)
{
printf("--");
}
printf("\n");
}
void DisplayBoard(char board[ROWS][COLS], int rows, int cols)
{
int i = 0, j = 0;
DisCoord(rows, cols);
DisplayLine(rows, cols);
for (i = 1; i <= rows ; i++)
{
printf("%2d", i);
for (j = 1; j <= cols ; j++)
{
printf("| %c ", board[i][j]);
}
printf("|\n");
DisplayLine(rows, cols);
}
printf("\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1; //1-9
int y = rand() % col + 1; //1-9
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int get_mine_count(char board[ROWS][COLS],int x,int y)
{
return board[x-1][y-1]+board[x-1][y]+board[x-1][y+1]+
board[x][y-1]+board[x][y+1]+
board[x+1][y-1]+board[x+1][y]+board[x+1][y+1]-8*'0';
}
void Findother(char mine[ROWS][COLS],char show[ROWS][COLS], int rows,int cols,int x, int y)
{
int count = get_mine_count(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';
if (x - 1 > 0 && y - 1 > 0 && show[x - 1][y - 1] == '*')
Findother(mine, show, rows, cols, x - 1, y - 1);
if (x - 1 > 0 && y > 0 && show[x - 1][y] == '*')
Findother(mine, show, rows, cols, x - 1, y);
if (x - 1 > 0 && y + 1 > 0 && show[x - 1][y + 1] == '*')
Findother(mine, show, rows, cols, x - 1, y + 1);
if (x > 0 && y - 1 > 0 && show[x][y - 1] == '*')
Findother(mine, show, rows, cols, x, y - 1);
if (x > 0 && y + 1 > 0 && show[x][y + 1] == '*')
Findother(mine, show, rows, cols, x, y + 1);
if (x + 1 > 0 && y - 1 > 0 && show[x + 1][y - 1] == '*')
Findother(mine, show, rows, cols, x + 1, y - 1);
if (x + 1 > 0 && y > 0 && show[x + 1][y] == '*')
Findother(mine, show, rows, cols, x + 1, y);
if (x + 1 > 0 && y + 1 > 0 && show[x + 1][y + 1] == '*')
Findother(mine, show, rows, cols, x + 1, y + 1);
}
else
{
show[x][y] = get_mine_count(mine, x, y) + '0';
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x=0, y=0;
int win = 0;
while (win<row*col-EASY_COUNT)
{
printf("请输入排雷的坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
//踩雷
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
//DisplayBoard(mine[x][y], row, col);
break;
}
else
{
//计算x,y坐标周围有几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
Findother(mine,show, row+2, col+2,x,y);
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("输入的坐标不合法,请重新输入!\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
}
}
game.h文件如下
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 9 //定义扫雷的行数
#define COL 9 //定义扫雷的列数
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10 //定义雷的数量
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); //初始化
void DisCoord(int row, int col); //打印坐标
void DisplayLine(int row, int col); //打印线条
void DisplayBoard(char board[ROWS][COLS], int rows, int cols); //打印
void SetMine(char board[ROWS][COLS], int row, int col); //防雷
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col); //排雷
void Findother(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols, int x, int y); //将周围没有雷的地方显示出来
看一下最终的效果,这样我们的扫雷游戏就制作完成了。如果你想修改扫雷的行和列或者雷的数量就在game.h文件去修改即可。
这里涉及到了一个时间戳产生随机数的概念,如果有不懂的小伙伴,请看随机数、时间戳这篇文章。
在计算坐标周围雷的数量的时候,我们通过把周围的数字加起来,如果数字之和是0,那就表示周围没有雷,如果数字之和为2则表示周围有两颗雷,但是我们定义的是字符数组,因此我们就需要 减去‘0’这样才是真正意义上的数字之后,-8*‘0’ 是因为周围有8个方块。
有不懂的地方可以留言解答。