初阶扫雷
1.游戏介绍
2.基本思路
3.实现前的准备
4.实现步骤
4.1 打印菜单
打印菜单和三子棋一样。如果我们想要多次游玩,则菜单要放进循环里,在菜单里选择开始游戏或者退出游戏。菜单中的选择我们通常用switch语句,菜单的循环我们通常用do … while循环。
//test.c
#include <stdio.h>
//菜单
void menu()
{
printf("*******************\n");
printf("******1.play ******\n");
printf("******0.exit ******\n");
printf("*******************\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请输入>:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
4.2 初始化扫雷棋盘
主程序框架写好了,我们现在开始写游戏具体程序。因为棋盘有很多行和列,我们想到二维数组可以表现多行多列。
注意: 为了可以方便修改棋盘的大小,我们可以用#define定义行和列,并将他们放到game.h头文件,只要在主程序中声明一下gane.h就可以使用头文件中的所有内容。
我们用1表示雷,用0表示不是雷的格。如果数组只设置9行9列,那边上和角上的格子在计算周围8个格子有几个雷时就会越界。
为了防止越界,我们要在周围再加一圈变成11*11。
因为在创建二维数组时全都默认为0,所以这里最外面一圈在初始化时放不放0都行,但一定不能放 1(雷),这里并不属于真正的棋盘,只是为了防越界。
如果我们在这个棋盘中的某一个格子里显示周围8个格中有几个雷。若周围只有一个雷,那这个格子上显示的1就会和是雷的那些格子上的1相混淆,就分不清哪个是雷,哪个是统计的周围的雷的数量。
- 因此,我们要再初始化一个棋盘,专门用来存放一个格子周围8个格子中的雷的数量,用来展示给玩家看(那就用9 * 9就行了),先把它全都初始化为 *,我们把这个棋盘称为展示棋盘(show) 。
- 存放雷的棋盘是给我们程序员看的,用来让程序排雷的,先把它全初始化为0,我们称它为排雷棋盘(mine) 。
为了方便和排雷棋盘共用一个初始化函数,将展示棋盘初始化11 * 11也可以,只要打印时打印的是9 * 9就行。
//game.c
#include <stdio.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);
//game.c
#include "game.h"
//初始化扫雷棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
//test.c
#include "game.h"
//游戏
void game()
{
char mine[ROWS][COLS] ;//定义
char show[ROWS][COLS] ;
//初始化排雷棋盘
InitBoard(mine, ROWS, COLS, '0');
//打印排雷棋盘
//初始化展示棋盘
InitBoard(show, ROWS, COLS, '*');
//打印展示棋盘
}
4.3 打印扫雷棋盘
那么多的行和列,玩家在玩时不容易看坐标是哪行哪列,所以我们可以把行和列对应的数字也打印出来。打印时也可以自由发挥一下给棋盘加一些装饰。
//game.h
//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//game.c
#include "game.h"
//打印扫雷棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("******* 扫雷 ******\n");
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
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");
}
//test.c
#include "game.h"
//游戏
void game()
{
char mine[ROWS][COLS] ;//定义
char show[ROWS][COLS] ;
//初始化排雷棋盘
InitBoard(mine, ROWS, COLS, '0');
//打印排雷棋盘
DisplayBoard(mine, ROW, COL);
//初始化展示棋盘
InitBoard(show, ROWS, COLS, '*');
//打印展示棋盘
DisplayBoard(show, ROW, COL);
}
4.4 布置雷
我们制作的是9 * 9版本的扫雷,所以我们希望能在排雷棋盘中随机生成10个雷,就是找到10个随机的坐标,那么就需要产生随机数。用rand可以产生随机数。
让电脑下棋需要先让电脑产生随机的坐标,那我们需要用rand产生随机数。
注意: 只用rand产生的是伪随机数,要想让rand产生真随机数,就需要先用srand为rand产生随机的种子,给srand()的()中输入的是随机数,srand产生的就是随机的种子。时间戳(需要头文件time.h)是一个随着时间的变化而变化的值,给srand()中输入时间戳->srand( (unsigne int)time(NULL) ),得到的就是随机的种子。
rand和srand都需要头文件stdlib.h
//game.h
#include <time.h>
#include <stdlib.h>
//雷的总数
#define EASY_COUNT 10
//布置雷
void SetMine(char board[ROWS][COLS], int rows, int cols);
//game.c
#include "game.h"
//布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
int c = EASY_COUNT;
while (c)
{
int a = rand() % row + 1;
int b = rand() % col + 1;
board[a][b] = '1';
c--;
}
}
//test.c
#include "game.h"
//产生随机的种子,用于rand产生真随机数
srand((unsigned int)time(NULL));
//布置雷
SetMine(mine, ROW, COL);
//打印排雷棋盘
DisplayBoard(mine, ROW, COL);
4.5 扫雷
-
玩家输入的坐标一定要在我们设置的二维数组的范围内,如果不在则重新输入;
-
玩家输入的坐标一定要是没有排查过的,若是排查过的,则需重新输入;
-
如果输入的坐标在排雷棋盘中是1,则排雷失败,本局游戏结束;
-
如果排查雷的次数正好等于不是雷的格子的数量,则排雷成功,本局游戏结束;
//game.h
//扫雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols);
//game.c
#include "game.h"
//通用方法:计算坐标周围8个格中雷的数量
//static int GetMineCount(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;
//}
//计算坐标周围8个格中雷的数量
static int GetMineCount(char mine[ROWS][COLS], int x, int y)//把函数放到静态库,使函数失去外部连接属性,只能在本源文件中使用。
{
return 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';
}
//扫雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int 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");
break;
}
else if (show[x][y] != '*')
{
printf("该坐标已被排查了,请重新输入\n");
}
else
{ //不是雷,就统计x,y有多少雷?
int c = GetMineCount(mine, x, y);
show[x][y] = c + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
printf("坐标非法,请重新输入\n");
}
if (win == row * col - EASY_COUNT)
{
printf("排雷成功\n");
}
}
//test.c
#include "game.h"
//扫雷
FindMine(mine, show, ROW, COL);
4.6 优化棋盘显示
玩家每次排雷时,上一次的棋盘并未消失。这会使打印的棋盘越来越多,不美观。
我们可以在每次排雷(输入坐标)后都清空一次屏幕,这样屏幕就只会显示一个棋盘,更加美观。
使用system(“cls”)可以清空屏幕,需要头文件stdlib.h
5.游戏代码
game.h
#define _CRT_SECURE_NO_WARNINGS
#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 DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char board[ROWS][COLS], int row, int col);
//扫雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols);
game.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
//初始化扫雷棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
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 (j = 0; j <= col; j++)
{
printf("%d ", j);
}
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 board[ROWS][COLS], int row, int col)
{
int c = EASY_COUNT;
while (c)
{
int a = rand() % row + 1;
int b = rand() % col + 1;
board[a][b] = '1';
c--;
}
}
//通用方法:计算坐标周围8个格中雷的数量
//static int GetMineCount(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;
//}
//计算坐标周围8个格中雷的数量
static int GetMineCount(char mine[ROWS][COLS], int x, int y)//把函数放到静态库,使函数失去外部连接属性,只能在本源文件中使用。
{
return 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';
}
//扫雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int 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");
break;
}
else if (show[x][y] != '*')
{
printf("该坐标已被排查了,请重新输入\n");
}
else
{ //不是雷,就统计x,y有多少雷?
int c = GetMineCount(mine, x, y);
show[x][y] = c + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
printf("坐标非法,请重新输入\n");
}
if (win == row * col - EASY_COUNT)
{
printf("排雷成功\n");
}
}
排雷棋盘打印出来是给我们程序员看的,方便我们调试,给玩家玩时可以注释掉。
随机数的种子不需要每局都获取,所以我们可以把它放进main函数里。
test.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
//菜单
void menu()
{
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');
//打印排雷棋盘
//DisplayBoard(mine, ROW, COL);
//初始化展示棋盘
InitBoard(show, ROWS, COLS, '*');
//打印展示棋盘
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));//产生随机的种子,用于rand产生真随机数
do
{
menu();
printf("请输入>:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
6.总结
点赞收藏加关注,C语言学习不迷路!