扫雷游戏
一.主函数流程
- 写在最开始的一些预定义量,作用是方便以后游戏内容的更改。
注:调试行列数+2便于对玩家显示棋盘周边一圈的雷数进行统计。
#pragma once
#define EASY 15//布置雷的个数
#include<stdio.h>
#include<time.h>//时间戳头文件引用
#include<stdlib.h>
#define ROW 9//玩家界面show显示的行数
#define COL 9//玩家界面show显示的列数
#define ROWS ROW+2//调试界面mine显示的行数
#define COLS COL+2//调试界面mine显示的列数
- 最开始先初始化随机值用作之后雷的布置,显示菜单后根据玩家选择是否进入游戏。
int main()
{
srand((unsigned int)time(NULL));//利用时间戳初始化随机值起点(每局游戏只需设置一次所以放在最开始)
int input = 0;//玩家输入值
do
{
meau();//显示菜单栏
printf("请选择:>");
scanf("%d", &input);
switch(input)//根据玩家输入判断是否进入游戏
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
default:
printf("输入错误,请重新选择!\n");
break;
}
} while (input);
return 0;
}
- 菜单栏的打印
void meau()
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("$$$$$$$$$$$$$$$$$$$$ 1.play $$$$$$$$$$$$$$$$$$$$$$$\n");
printf("$$$$$$$$$$$$$$$$$$$$ 0.exit $$$$$$$$$$$$$$$$$$$$$$$\n");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}
- 进入游戏后首先创建并初始化棋盘,mine用于自己观测,show用于展示给玩家。
void game()
{
//创建棋盘对应数组
char mine[ROWS][COLS];//存放布置好的雷的信息
char show[ROWS][COLS];//存放的是排查出雷的信息
InitBoard(mine, ROWS, COLS,'0');//对棋盘进行初始化
InitBoard(show, ROWS, COLS,'*');
//打印棋盘
DisplayBoard(mine ,ROW,COL);//显示初始化后的棋盘
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL,EASY);//布置雷函数
DisplayBoard(mine, ROW, COL);//布置之后显示雷的位置便于测试
//排查雷
FindMine(mine ,show,ROW,COL);//进行扫雷操作
}
二.各个函数详解
1.初始化棋盘
void InitBoard(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;
//传值可自定,玩家棋盘显示为'*',调试棋盘显示为'0'
}
}
}
2.显示棋盘
- 除正常打印棋盘外,增添横纵坐标序号,便于玩家选择及测试人员调试。
void DisplayBoard(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");
}
3.利用随机数布置雷
- rand()函数产生一个随机值,起点在主函数开头利用时间戳设定确保了随机性,(rand()% row )范围在0到8之间,+1使范围与行列序号1到9的范围吻合;count为可布置的雷数,取值在头文件预定义处可以修改。
void SetMine(char mine[ROWS][COLS], int row, int col, int count)
{
while (count)//可布置雷数>0时进入循环,反之跳出
{
int x = rand() % row + 1;//1-9
int y = rand() % col + 1;
if (mine[x][y] == '0')//判断布雷处是否已经有雷
{
mine[x][y] = '1';
count--;//剩余可布置雷数-1
}
}
}
4.玩家扫雷的具体执行语句
- win是除踩雷判断胜利的条件:玩家扫描完所有非雷坐标后
- (win<row*col-EASY)//EASY为雷的数量
- 仍未踩雷,玩家胜利。
int win = 0;
while (win<row*col-EASY)
{
......
}
(1)判断输入坐标合法性
- 坐标需满足在棋盘内部即为合法,否则重新输入
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//......此处省略扫雷执行语句
}
else
{
printf("坐标非法,请重新输入!");
}
}
(2)判断玩家输入坐标是不是雷?不是雷统计周围雷的个数
- count(数字)+‘0’(字符零) = char型字符数字
- 例如:
- (字符数字‘4’-字符‘0’=数字4; 数字5+字符‘0’=字符‘5’)
if (mine[x][y] == '1')//踩雷,游戏结束
{
printf("YOU LOSE!\n");
DisplayBoard(mine, row, col);
break;
}
else if (mine[x][y] == '0' && count == 0)
//不是雷并且周围无雷
{
automatic(mine,show,x,y,row,col,win);
//自动展开函数,具体内容在下一小节
DisplayBoard(show, row, col);
}
else
{
show[x][y] = count+'0';
//数字字符(字符数字‘4’-字符‘0’=数字4;
//数字5+字符‘0’=字符‘5’)
DisplayBoard(show, row, col);
win++;
}
扫雷执行语句总代码
void FindMine(char mine[ROWS][COLS],
char show[ROWS][COLS],
int row, int col)
{
int win = 0;
while (win<row*col-EASY)
{
printf("请输入要排查的坐标:>");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
int count = GetMineCont(mine, x, y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("YOU LOSE!\n");
DisplayBoard(mine, row, col);
break;
}
else if (mine[x][y] == '0' && count == 0)
{
automatic(mine,show,x,y,row,col,win);
DisplayBoard(show, row, col);
}
else
{
show[x][y] = count+'0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("坐标非法,请重新输入!");
}
}
if (win == row * col - EASY)
{
printf("YOU WIN!\n");
}
}
5.自动展开周围无雷坐标
- 这里利用了函数的嵌套调用,逻辑如下:
- 1.若坐标在棋盘范围内,并且未被扫描(显示为’*’)则显示函数的值,已扫描且安全坐标win++;
- 2.对接收坐标位置周围八个坐标的四周分别进行一次遍历,若周围八个坐标存在自身为’0’且自身周围安全,则将此安全坐标位置作为参数再次传给自动展开函数,以此实现爆炸式的展开。
void automatic(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y,int row,int col,int win)
{
if (mine[x][y] == 0,x >= 1 && x <= row && y >= 1 && y <= col && show[x][y] == '*')
{
show[x][y] = GetMineCont(mine, x, y) + '0';
win++;
}
if (mine[x][y+1] == 0, x >= 1 && x <= row && y+1 >= 1 && y+1 <= col && show[x][y+1] == '*')//判断坐标是否在棋盘内,是否未被扫描
{
show[x][y + 1] = GetMineCont(mine, x, y+1) + '0';
//扫描该坐标并显示其周围雷数
win++;//已安全扫描的坐标+1
if (GetMineCont(mine, x, y + 1) == 0)
//若该坐标周围无雷,则将此坐标作为参数,嵌套调用自动展开函数。
{
automatic(mine, show, x, y + 1, row, col,win);
}
}
if (mine[x][y-1] == 0, x >= 1 && x <= row && y - 1 >= 1 && y -1 <= col && show[x][y - 1] == '*')
{
show[x][y - 1] = GetMineCont(mine, x, y - 1) + '0';
win++;
if (GetMineCont(mine, x, y - 1) == 0)
{
automatic(mine, show, x, y - 1, row, col, win);
}
}
if (mine[x+1][y+1] == 0, x+1 >= 1 && x+1 <= row && y + 1 >= 1 && y + 1 <= col && show[x+1][y + 1] == '*')
{
show[x+1][y + 1] = GetMineCont(mine, x+1, y + 1) + '0';
win++;
if (GetMineCont(mine, x+1, y + 1) == 0)
{
automatic(mine, show, x+1, y + 1, row, col, win);
}
}
if (mine[x-1][y+1] == 0, x-1 >= 1 && x-1 <= row && y + 1 >= 1 && y + 1 <= col && show[x-1][y + 1] == '*')
{
show[x-1][y + 1] = GetMineCont(mine, x-1, y + 1) + '0';
win++;
if (GetMineCont(mine, x-1, y + 1) == 0)
{
automatic(mine, show, x-1, y + 1, row, col, win);
}
}
if (mine[x+1][y] == 0, x+1 >= 1 && x+1 <= row && y >= 1 && y <= col && show[x+1][y] == '*')
{
show[x+1][y] = GetMineCont(mine, x+1, y) + '0';
win++;
if (GetMineCont(mine, x+1, y ) == 0)
{
automatic(mine, show, x+1, y , row, col, win);
}
}
if (mine[x-1][y] == 0, x-1 >= 1 && x-1 <= row && y >= 1 && y <= col && show[x-1][y] == '*')
{
show[x-1][y] = GetMineCont(mine, x-1, y) + '0';
if (GetMineCont(mine, x-1, y) == 0)
{
automatic(mine, show, x-1, y, row, col, win);
}
}
if (mine[x+1][y-1] == 0, x+1 >= 1 && x+1 <= row && y - 1 >= 1 && y - 1 <= col && show[x+1][y - 1] == '*')
{
show[x+1][y - 1] = GetMineCont(mine, x+1, y - 1) + '0';
if (GetMineCont(mine, x+1, y + 1) == 0)
{
automatic(mine, show, x+1, y - 1, row, col, win);
}
}
if (mine[x-1][y-1] == 0, x-1 >= 1 && x-1 <= row && y - 1 >= 1 && y - 1 <= col && show[x-1][y -1] == '*')
{
show[x-1][y - 1] = GetMineCont(mine, x-1, y -1) + '0';
if (GetMineCont(mine, x-1, y - 1) == 0)
{
automatic(mine, show, x-1, y - 1, row, col,win);
}
}
}
三.附录
1.统计坐标周围雷的个数
- 简单的计数函数
int GetMineCont(char mine[ROWS][COLS],int x,int y)
{
int count = 0;
if (mine[x - 1][y - 1] == '1')
count++;
if (mine[x - 1][y] == '1')
count++;
if (mine[x - 1][y + 1] == '1')
count++;
if (mine[x][y - 1] == '1')
count++;
if (mine[x][y + 1] == '1')
count++;
if (mine[x + 1][y - 1] == '1')
count++;
if (mine[x + 1][y] == '1')
count++;
if (mine[x + 1][y + 1] == '1')
count++;
return count;
}
总代码——头文件
#pragma once
#define EASY 15
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows,int cols,char set);
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
//count-布置雷的个数
void SetMine(char mine[ROWS][COLS], int row,int col,int count);
//排查雷
void FindMine(char mine[ROWS][COLS],
char show[ROWS][COLS],
int row,
int col);
//自动翻牌
void automatic(char mine[ROWS][COLS],char show[ROWS][COLS], int x,int y,int row,int col,int win);
总代码——主函数文件
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
void meau()
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("$$$$$$$$$$$$$$$$$$$$ 1.play $$$$$$$$$$$$$$$$$$$$$$$\n");
printf("$$$$$$$$$$$$$$$$$$$$ 0.exit $$$$$$$$$$$$$$$$$$$$$$$\n");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}
void game()
{
//创建棋盘对应数组
char mine[ROWS][COLS];//存放布置好的雷的信息
char show[ROWS][COLS];//存放的是排查出雷的信息
InitBoard(mine, ROWS, COLS,'0');//对棋盘进行初始化
InitBoard(show, ROWS, COLS,'*');
//打印棋盘
DisplayBoard(mine ,ROW,COL);//显示初始化后的棋盘
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL,EASY);//布置雷函数
DisplayBoard(mine, ROW, COL);//布置之后显示雷的位置便于测试
//排查雷
FindMine(mine ,show,ROW,COL);//进行扫雷操作
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
meau();
printf("请选择:>");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
default:
printf("输入错误,请重新选择!\n");
break;
}
} while (input);
return 0;
}
总代码——游戏函数
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(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 DisplayBoard(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 SetMine(char mine[ROWS][COLS], int row, int col, int count)
{
while (count)
{
int x = rand() % row + 1;//1-9
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
//x,y处没有雷
mine[x][y] = '1';
count--;
}
}
}
//统计mine数组的x,y坐标周围有几个雷
int GetMineCont(char mine[ROWS][COLS],int x,int y)
{
int count = 0;
if (mine[x - 1][y - 1] == '1')
count++;
if (mine[x - 1][y] == '1')
count++;
if (mine[x - 1][y + 1] == '1')
count++;
if (mine[x][y - 1] == '1')
count++;
if (mine[x][y + 1] == '1')
count++;
if (mine[x + 1][y - 1] == '1')
count++;
if (mine[x + 1][y] == '1')
count++;
if (mine[x + 1][y + 1] == '1')
count++;
return count;
}
void automatic(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y,int row,int col,int win)
{
if (mine[x][y] == 0,x >= 1 && x <= row && y >= 1 && y <= col && show[x][y] == '*')
{
show[x][y] = GetMineCont(mine, x, y) + '0';
win++;
}
if (mine[x][y+1] == 0, x >= 1 && x <= row && y+1 >= 1 && y+1 <= col && show[x][y+1] == '*')
{
show[x][y + 1] = GetMineCont(mine, x, y+1) + '0';
win++;
if (GetMineCont(mine, x, y + 1) == 0)
{
automatic(mine, show, x, y + 1, row, col,win);
}
}
if (mine[x][y-1] == 0, x >= 1 && x <= row && y - 1 >= 1 && y -1 <= col && show[x][y - 1] == '*')
{
show[x][y - 1] = GetMineCont(mine, x, y - 1) + '0';
win++;
if (GetMineCont(mine, x, y - 1) == 0)
{
automatic(mine, show, x, y - 1, row, col, win);
}
}
if (mine[x+1][y+1] == 0, x+1 >= 1 && x+1 <= row && y + 1 >= 1 && y + 1 <= col && show[x+1][y + 1] == '*')
{
show[x+1][y + 1] = GetMineCont(mine, x+1, y + 1) + '0';
win++;
if (GetMineCont(mine, x+1, y + 1) == 0)
{
automatic(mine, show, x+1, y + 1, row, col, win);
}
}
if (mine[x-1][y+1] == 0, x-1 >= 1 && x-1 <= row && y + 1 >= 1 && y + 1 <= col && show[x-1][y + 1] == '*')
{
show[x-1][y + 1] = GetMineCont(mine, x-1, y + 1) + '0';
win++;
if (GetMineCont(mine, x-1, y + 1) == 0)
{
automatic(mine, show, x-1, y + 1, row, col, win);
}
}
if (mine[x+1][y] == 0, x+1 >= 1 && x+1 <= row && y >= 1 && y <= col && show[x+1][y] == '*')
{
show[x+1][y] = GetMineCont(mine, x+1, y) + '0';
win++;
if (GetMineCont(mine, x+1, y ) == 0)
{
automatic(mine, show, x+1, y , row, col, win);
}
}
if (mine[x-1][y] == 0, x-1 >= 1 && x-1 <= row && y >= 1 && y <= col && show[x-1][y] == '*')
{
show[x-1][y] = GetMineCont(mine, x-1, y) + '0';
if (GetMineCont(mine, x-1, y) == 0)
{
automatic(mine, show, x-1, y, row, col, win);
}
}
if (mine[x+1][y-1] == 0, x+1 >= 1 && x+1 <= row && y - 1 >= 1 && y - 1 <= col && show[x+1][y - 1] == '*')
{
show[x+1][y - 1] = GetMineCont(mine, x+1, y - 1) + '0';
if (GetMineCont(mine, x+1, y + 1) == 0)
{
automatic(mine, show, x+1, y - 1, row, col, win);
}
}
if (mine[x-1][y-1] == 0, x-1 >= 1 && x-1 <= row && y - 1 >= 1 && y - 1 <= col && show[x-1][y -1] == '*')
{
show[x-1][y - 1] = GetMineCont(mine, x-1, y -1) + '0';
if (GetMineCont(mine, x-1, y - 1) == 0)
{
automatic(mine, show, x-1, y - 1, row, col,win);
}
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;
while (win<row*col-EASY)
{
printf("请输入要排查的坐标:>");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
//1.坐标合法性
//2.判断改坐标是不是雷?不是雷统计周围雷的个数
int count = GetMineCont(mine, x, y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("YOU LOSE!\n");
DisplayBoard(mine, row, col);
break;
}
else if (mine[x][y] == '0' && count == 0)
{
automatic(mine,show,x,y,row,col,win);
DisplayBoard(show, row, col);
}
else
{
show[x][y] = count+'0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("坐标非法,请重新输入!");
}
}
if (win == row * col - EASY)
{
printf("YOU WIN!\n");
}
}
总结
小白的第一篇博客,其中可能会有很多瑕疵的地方欢迎大家指正。