目录
游戏设计前的准备
思路:我们要想设计好一个扫雷游戏,就要提前准备两个二位数组,一个用来存放雷的信息这个数组一般进行展示,另外一个是用来给玩家展示扫雷的界面,为了代码更好的可读性我们一般把这两个数组命名为
char mine[ROWS][COLS]; //存放雷的信息
char show[ROWS][COLS]; //展示的数组
1、玩家输入坐标,如果是雷,游戏结束,如果不是雷,游戏继续。
2、如果玩家选的坐标周围没有雷,则递归展开,直到展开到雷的附近;若周围有雷,那么就显示周围雷的个数。
3、当玩家将棋盘内非雷的区域全部找到,那么游戏结束玩家胜利。
游戏设计
1.菜单的设计
void menu()
{
printf("************************************\n");
printf("********* 0.exit *********\n");
printf("********* 1.play *********\n");
printf("************************************\n");
}
2.对两个数组经行初始化
为了防止数组访问越界,显示数组为9*9,而实际将初始化的数组设置为11*11。把mine数组全部初始化为字符'0',把show数组全部初始化为字符'*'。
InitBorad(mine, ROWS, COLS, '0'); //储存雷的棋盘 初始化全为'0'
InitBorad(show, ROWS, COLS, '*'); //展示的棋盘 初始化全为'*'
void InitBorad(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
3.打印棋盘来检查初始化是否正确
DisPlayBoard(show, ROW, COL);
void DisPlayBoard(char board[ROWS][COLS], int row, int col)
{
printf("-----------扫雷游戏-----------\n");
for (int i = 0; i <= row ; i++)
{
printf("%d ", i); //打印列列号
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i); //打印行号
for (int j = 1; j <= col; j++)
{
printf("%c ",board[i][j]);
}
printf("\n");
}
}
4.在初始化好的数组上埋雷
使用rand()函数生成随机数,但在使用之前要在主函数中调用srand()函数。
SetMine(mine, ROW, COL);
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EasyMine; //用宏定义的雷的个数以后方便更改
while (count)
{
int i = rand() % row + 1;
int j = rand() % col + 1;
if (board[i][j] == '0')
{
board[i][j] = '1';
count--;
}
}
}
5.查找所选坐标周围雷的个数
//循环查找雷的个数
int Findout(char board[ROWS][COLS], int x, int y)
{
int count = 0;
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
if (board[x - 1 + i][y - 1 + j] != '0')
{
count++; //雷的个数
}
}
}
return count + '0'; //加'0'使其变为字符放入到字符数组中
}
//int Findout(char board[ROWS][COLS], int x, int y)
//{
// int count = 0;
// count = 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] +
// board[x - 1][y + 1] - 8 * '0';
//
// return count;
//}
6.排雷
void FindMine(char Mboard[ROWS][COLS], char Sboard[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int flag = 0;
while (flag < row * col - EasyMine)
{
printf("请输入要查询的坐标:>");
scanf("%d %d", &x, &y);
if ((x >= 1 && x <= row) && (y >= 1 && y <= col)) //判断坐标是否正确
{
if (Sboard[x][y] != '*')
{
printf("该地区已被排查请重新输入!\n");
continue;
}
else if (Mboard[x][y]=='1')
{
system("cls");
printf("您进入了雷区,被炸死了\n");
break;
}
else
{
//Sleep(5000);
system("cls"); //清空界面
OpenMine(Mboard, Sboard, ROW, COL, x, y);//对棋盘经行展开
//判断胜利
flag = is_win(Sboard, ROW, COL);
DisPlayBoard(Sboard, ROW, COL); //打印棋盘
}
}
}
if (flag == row * col - EasyMine)
{
printf("恭喜你获得胜利\n");
DisPlayBoard(Mboard, ROW, COL); //打印棋盘
}
}
7.用递归实现大范围展开
void OpenMine(char Mboard[ROWS][COLS], char Sboard[ROWS][COLS], int row, int col, int x ,int y)
{
if ((x >= 1 && x <= row) && (y >= 1 && y <= col)) //判断坐标是否正确
{
int count = Findout(Mboard, x, y);
if ((count - '0') == 0) //当周围没有雷的时候才将选中的坐标变为空格
{
Sboard[x][y] = ' ';
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (Sboard[i][j] == '*'&&(i!=x||j!=y)) //防止出现死递归
{
OpenMine(Mboard, Sboard, ROW, COL, i, j);
}
}
}
}
else
{
Sboard[x][y] = count ;
}
}
}
8.判断胜利
int is_win(char board[ROWS][COLS], int row, int col)
{
int count = 0;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if (board[i][j] != '*')
{
count++; //查找棋盘中被查找过的个数
}
}
}
return count;
}
游戏主体
1.game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EasyMine 10 //简单模式雷的数量
//初始化棋盘
void InitBorad(char board[ROWS][COLS], int rows, int cols , char set);
//打印棋盘
void DisPlayBoard(char board[ROWS][COLS], int row, int col);
//埋雷
void SetMine(char board[ROWS][COLS], int row, int col);
//排雷
void FindMine(char Mboard[ROWS][COLS], char Sboard[ROWS][COLS], int row, int col);
//找到周围雷的个数
int Findout(char board[ROWS][COLS], int rows, int cols);
//自动展开周围没有雷区的空间
void OpenMine(char Mboard[ROWS][COLS], char Sboard[ROWS][COLS], int row, int col, int x, int y);
//判断胜利
int is_win(char board[ROWS][COLS], int row, int col);
2.游戏函数的实现game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//初始化棋盘
void InitBorad(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
//打印棋盘
void DisPlayBoard(char board[ROWS][COLS], int row, int col)
{
printf("-----------扫雷游戏-----------\n");
for (int i = 0; i <= row ; i++)
{
printf("%d ", i);//打印列列号
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i); //打印行号
for (int j = 1; j <= col; j++)
{
printf("%c ",board[i][j]);
}
printf("\n");
}
}
//埋雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EasyMine;
while (count)
{
int i = rand() % row + 1;
int j = rand() % col + 1;
if (board[i][j] == '0')
{
board[i][j] = '1';
count--;
}
}
}
//显示出周围雷的个数
//int Findout(char board[ROWS][COLS], int x, int y)
//{
// int count = 0;
// count = 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] +
// board[x - 1][y + 1] - 8 * '0';
//
// return count;
//}
//循环查找雷的个数
int Findout(char board[ROWS][COLS], int x, int y)
{
int count = 0;
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
if (board[x - 1 + i][y - 1 + j] != '0')
{
count++;
}
}
}
return count + '0';
}
//自动展开周围没有雷区的空间
void OpenMine(char Mboard[ROWS][COLS], char Sboard[ROWS][COLS], int row, int col, int x ,int y)
{
if ((x >= 1 && x <= row) && (y >= 1 && y <= col)) //判断坐标是否正确
{
int count = Findout(Mboard, x, y);
if ((count - '0') == 0)
{
Sboard[x][y] = ' ';
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (Sboard[i][j] == '*'&&(i!=x||j!=y))
{
OpenMine(Mboard, Sboard, ROW, COL, i, j);
}
}
}
}
else
{
Sboard[x][y] = count ;
}
}
}
//判断胜利
int is_win(char board[ROWS][COLS], int row, int col)
{
int count = 0;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if (board[i][j] != '*')
{
count++;
}
}
}
return count;
}
//排雷
void FindMine(char Mboard[ROWS][COLS], char Sboard[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int flag = 0;
while (flag < row * col - EasyMine)
{
printf("请输入要查询的坐标:>");
scanf("%d %d", &x, &y);
if ((x >= 1 && x <= row) && (y >= 1 && y <= col)) //判断坐标是否正确
{
if (Sboard[x][y] != '*')
{
printf("该地区已被排查请重新输入!\n");
continue;
}
else if (Mboard[x][y]=='1')
{
system("cls");
printf("您进入了雷区,被炸死了\n");
break;
}
else
{
//Sleep(5000);
system("cls");
OpenMine(Mboard, Sboard, ROW, COL, x, y);//对棋盘经行展开
//int count = 0; //计算周围雷区的个数
//count = Findout(Mboard, x, y);
//Sboard[x][y] = count;
//flag++;
//判断胜利
flag = is_win(Sboard, ROW, COL);
DisPlayBoard(Sboard, ROW, COL); //打印棋盘
}
}
}
if (flag == row * col - EasyMine)
{
printf("恭喜你获得胜利\n");
DisPlayBoard(Mboard, ROW, COL); //打印棋盘
}
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("************************************\n");
printf("********* 0.exit *********\n");
printf("********* 1.play *********\n");
printf("************************************\n");
}
//
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBorad(mine, ROWS, COLS, '0'); //储存雷的棋盘 初始化全为'0'
InitBorad(show, ROWS, COLS, '*'); //展示的棋盘 初始化全为'*'
//DisPlayBoard(mine, ROW, COL); //打印棋盘
DisPlayBoard(show, ROW, COL);
//埋雷
SetMine(mine, ROW, COL);
//DisPlayBoard(mine, ROW, COL); //打印棋盘
// 排雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int )time(NULL));
do
{
menu();
printf("请输入:>");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
game();
break;
default:
printf("输入错误请重新输入!\n");
break;
}
} while (input);
return 0;
}