前言
通过学习C语言中的部分知识,我们可以实现一个简单的扫雷小游戏,具体步骤如下。
准备工作
在创建文件的时候,我们可以分为三个文件:1.game.h文件(头文件的包含和函数的声明)2.game.c文件(游戏函数的具体实现)3,test.c文件(游戏测试部分)
游戏的具体实现
扫雷游戏介绍:
通过点击格子出现的数字找到所有非雷格子,同时避免踩雷,踩到一个雷就会全盘皆输。
(1)菜单的创建
菜单的创建是为了让玩家选择是否开始玩游戏的关键,我们可以设定当玩家输入1时开始游戏,输入0时退出游戏,输入其他数字会显示输入错误。代码如下:
void menu()
{
printf("***** 1.play *****\n");
printf("***** 0.exit *****\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;
}
通过创建菜单,其实大部分游戏的初步框架已经搭建好了,剩下就剩下游戏的具体核心如何实现了。
(2)棋盘的创建和初始化
我们要使用到两个棋盘(实际就是数组),分别为mine数组和show数组。mine数组是有关埋雷的数组,没有雷的位置是0,有雷的位置是1。show数组是展示给玩家的,就是查找雷的数组,还未查找的位置值*,查找之后的位置是周围雷的数量。经过分析,我们需要创建两个数组。并且根据它们不同的功能初始化。代码如下:
char mine[ROWS][COLS] = { 0 };//创建
char show[ROWS][COLS] = { 0 };//创建
InitBoard(mine, ROWS, COLS, '0');//初始化
InitBoard(show, ROWS, COLS, '*');//初始化
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char n )
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = n;
}
}
}
其中的ROWS和COLS是在game.h文件中利用#define定义的常量。
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
其中棋盘边长为9,但是为了更好的查找在棋盘边的雷周围数量,所以加大棋盘范围。
(3)打印棋盘
为了使玩家有更好的游玩体验,每次在玩家找完雷之后需要让玩家看到棋盘变成了什么样子,所以我们需要打印棋盘。函数起名为DisplayBoard()。代码如下:
//打印棋盘
18 void DisplayBoard(char board[ROWS][COLS], int row, int col)
19 {
20 int i = 0;
21 int j = 0;
22 for (j = 0; j <= col; j++)
23 {
24 printf("%d ", j);
25 }
26 printf("\n");
27 for (i = 1; i <= row; i++)
28 {
29 printf("%d ", i);
30 for (j = 1; j <= col; j++)
31 {
32 printf("%c ", board[i][j]);
33 }
34 printf("\n");
35 }
36 }
(4)布置雷
由于棋盘的大小是9X9的大小,我们确定雷的数量为10个,我们在头文件中利用#define定义EASY_COUNT为10,代表雷的数量。但是雷的出现位置是随机的,我们需要用到rand()和srand()函数来产生随机数。布置雷的函数起名SetMine()。代码如下:
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = EASY_COUNT;
while (count)
{
x = rand() % 9 + 1;
y = rand() % 9 + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
棋盘产生雷的范围就在9X9的大小中,所以坐标X和Y产生的随机数取除以9的余数加1就在棋盘所属的范围了。
(4)排查雷
在排查雷的时候我们需要找雷和计数。代码如下:
//排雷
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 - 1][y] +
mine[x - 1][y] +
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");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = '0' + count;
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("输入位置超出范围,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,游戏结束!\n");
DisplayBoard(mine, ROW, COL);
}
}
最后把这些函数整合到一起就OK了!!!
完整代码如下:
game.h:
#pragma on
2
3 #include<stdio.h>
4 #include<stdlib.h>
5 #include<time.h>
6
7 #define ROW 9
8 #define COL 9
9 #define ROWS ROW+2
10 #define COLS COL+2
11 #define EASY_COUNT 10
12
13 void InitBoard(char board[ROWS][COLS], int row, int col);//初始化
14 void DisplayBoard(char board[ROWS][COLS], int row, int col);//打印棋盘
15 void SetMine(char mine[ROWS][COLS], int row, int col);//布置雷
16 void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//排雷
game.c:
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char n )
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = n;
}
}
}
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
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");
}
}
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = EASY_COUNT;
while (count)
{
x = rand() % 9 + 1;
y = rand() % 9 + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//排雷
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 - 1][y] +
mine[x - 1][y] +
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");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = '0' + count;
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("输入位置超出范围,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,游戏结束!\n");
DisplayBoard(mine, ROW, COL);
}
}
test.c:
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void game()
{
char mine[ROWS][COLS] = { 0 };//创建
char show[ROWS][COLS] = { 0 };//创建
InitBoard(mine, ROWS, COLS, '0');//初始化
InitBoard(show, ROWS, COLS, '*');//初始化
//打印棋盘
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
//排雷
FindMine(mine, show, ROW, COL);
}
void menu()
{
printf("***** 1.play *****\n");
printf("***** 0.exit *****\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
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;
}
今天分享就到这里,希望大家一起提高!