扫雷的完成(展开和第一步不被炸死)
程序员文章站
2022-04-07 21:54:05
...
扫雷是类似于三子棋在C语言初阶就能写成的游戏,要用到的知识点和三子棋基本一样,do...while()循环,while循环,for循环,if...else语句,switch语句,二维数组,函数等等。
扫雷相对于三子棋来说步骤变多了,但是万变不离其宗, 每一步的过程都有其对应的知识点,掌握这些知识点可以说是完成扫雷最重要的步骤了。
一般的扫雷可以分成6步:
(1)初始化棋盘,
(2)打印出棋盘
(3)布雷
(4)扫雷(核心)
(5)标记地雷
(6)取消标记
扫雷代码:
(1)game.h
#ifndef _Game_H_
#define _Game_H_
#define ROW 9
#define ROWS ROW+2
#define COL 9
#define COLS ROW+2
#define Easy_count 10
void InitBoard(char board[ROWS][COLS], int row, int col, char flag);//初始化
void DisplayBoard(char board[ROWS][COLS], int row, int col);//打印
void SetMine(char board[ROWS][COLS], int row, int col, int count);//布雷
void Sweep(char board1[ROWS][COLS], char board2[ROWS][COLS], int x, int y, int *Num);//排雷
int GetCount(char board[ROWS][COLS], int x, int y);//计算坐标周围雷数
void sign(char board[ROWS][COLS], int x, int y);//标记地雷
void unsign(char board[ROWS][COLS], int x, int y);//取消标记
#endif
(2)game.c
#include<stdio.h>
#include<windows.h>
#include"game.h"
#include<time.h>
#include<stdlib.h>
void InitBoard(char board[ROWS][COLS], int row, int col, char flag)
{
memset(board, flag, row*col*sizeof(board[0][0]));//将字符串'flag'直接把board中的元素替换。头文件为#include<string.h>
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)//打印棋盘
{
int i = 0;
int j = 0;
printf(" ");
for (i = 1; i <= row; i++)
printf("%d ", i);
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%-2d", i);//提供两个字符的空间用来输出i.
for (j= 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char board[ROWS][COLS], int row, int col, int count)
{
while (count)
{
int x = rand() % row+1;
int y = rand() % col+1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;//没生成一个雷雷的数量减一
}
}
}
void Sweep(char board1[ROWS][COLS], char board2[ROWS][COLS], int x, int y, int *Num)
{
int i = 0;
int j = 0;
int AroundCount = 0;
if (board2[x][y] == '*')
{
(*Num)++;
AroundCount = GetCount(board1, x, y);//坐标周围雷的数量。
if (AroundCount != 0)
{
board2[x][y] = '0' + AroundCount;
}
else
{
board2[x][y] = '0';
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
if (x + i >= 1 && x + i <= ROW && y + j >= 1 && y + j <= COL)
{
if (i != 0 || j != 0)
{
Sweep(board1, board2, x + i, y + j,Num);//对此坐标周围的八个坐标依次判断是否为0,只要有0,继续在相应的坐标判断并且扫雷,直到没有坐标等于0为止。
} //这也是递归的思想。
}
}
}
}
}
}
int GetCount(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 + 1][y + 1] +
board[x + 1][y] +
board[x + 1][y - 1] +
board[x][y - 1] - 8 * '0';
}
void sign(char board[ROWS][COLS], int x, int y)//标记地雷
{
if (board[x][y] == '*')
{
board[x][y] = '@';
}
}
void unsign(char board[ROWS][COLS], int x, int y)//取消标记
{
if (board[x][y] == '@')
{
board[x][y] = '*';
}
}
(3)test.c
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include"game.h"
void game()
{
int x = 0;
int y = 0;
int xuanxiang = 0;
int safenum = 0;
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitBoard(mine, ROWS, COLS,'0');
InitBoard(show, ROWS, COLS,'*');
DisplayBoard(show, ROW, COL);
SetMine(mine, ROW, COL,Easy_count);
while (safenum < ((ROW*COL) - Easy_count))
{
printf("+++++++++++++++++++++++++++++++++\n");
printf("+1.排雷 2.标记 3.取消标记 0.退出+\n");
printf("+++++++++++++++++++++++++++++++++\n");
printf("请选择\n");
scanf_s("%d", &xuanxiang);
if (xuanxiang == 1)
{
printf("请输入坐标\n");
scanf_s("%d %d",&x,&y);
if (x>=1 && x<= 9 && y>=1 && y<= 9)
{
if (mine[x][y] == '1')
{
if (safenum == 0)//是否是第一次扫雷
{
mine[x][y] = '0';
SetMine(mine, ROW, COL, 1);
Sweep(mine, show, x, y, &safenum);//如果第一次扫到雷,避免第一次被炸死,将1变成0,并且将一个雷重新随机生成到mine里边去,让雷的数量不变。
}
else
{
printf("很抱歉,你被炸死了!!!!!!!!\n");
DisplayBoard(mine, ROW, COL);
return;
}
}
else
{
Sweep(mine, show, x, y, &safenum);
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("输入错误,请重新输入\n");
}
}
else if (xuanxiang == 2)
{
printf("请输入坐标\n");
scanf_s("%d %d", &x, &y);
sign(show,x,y);
DisplayBoard(show, ROWS, COLS);
}
else if (xuanxiang == 3)
{
printf("请输入坐标\n");
scanf_s("%d %d", &x, &y);
unsign(show, x, y);
DisplayBoard(show, ROWS, COLS);
}
else if (xuanxiang == 0)
{
return ;
}
else
{
printf("输入错误,请重新输入\n");
}
}
printf("恭喜你扫雷成功!!!!!!!!\n");
DisplayBoard(mine, ROW, COL);
}
void mune()
{
printf("*******************\n");
printf("***** 扫雷 *****\n");
printf("*******************\n");
printf("***** 1.play *****\n");
printf("***** 0.exit *****\n");
printf("*******************\n");
}
void test()
{
mune();
srand((unsigned)time(NULL));
int input = 0;
do
{
scanf_s("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
default:
printf("输入有错,请重新输入\n");
break;
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}
最终的实现:
推荐阅读