扫雷游戏
目录
图示
代码实现
本次代码实现:
1、打印棋盘;
2、输入坐标显示周围雷的数量。
需要创建:
1、test.c:测试游戏逻辑
2、game.c:游戏实现
3、game.h:游戏函数的声明
一、打印游戏进入界面,以及实现无限次输入。
在test.c文件下,菜单;游戏;输入。
代码如下
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
//菜单
void mneu(){
printf("***********************\n");
printf("******* 1.play *******\n");
printf("******* 0.exit *******\n");
printf("***********************\n");
}
//游戏实现
void game() {
}
//输入
int main() {
int input = 0;
do {
mneu();
printf("请输入:");
scanf("%d", &input);
switch (input) {
case 1:game();
break;
case 0:printf("<退出游戏>");
break;
default:printf("输入错误,请重新输入:");
break;
}
} while (input);//input=0退出
return 0;
}
代码呈现
***********************
******* 1.play *******
******* 0.exit *******
***********************
请输入:1
***********************
******* 1.play *******
******* 0.exit *******
***********************
请输入:0
<退出游戏>
二、需要两个数组。
数组1:记录雷的位置;
数组2:隐秘棋盘展示给玩家。
代码如下
void game()
{
//数组的创建
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBoard(mine,ROWS,COLS,'0');//数组初始化‘0’
InitBoard(show, ROWS, COLS,'*');//数组初始化‘*’
//棋盘的打印
Displayboard(mine, ROW, COL);
Displayboard(show, ROW, COL);
}
在game.h文件下,函数的声明、定义语句。
代码如下
#pragma once//声明、定义语句
#include <stdio.h>
//实现置雷随机头文件
#include <stdlib.h>
#include <time.h>
//雷数
#define EASY_COUNT 10
//定义行列数
#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);//打印棋盘
在game.c文件下,棋盘的初始化与打印。
代码如下
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;
printf("-----扫雷 游戏-----\n");
for (i = 0; i <= col; i++)//显示列
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)//显示行
{
printf("%d ", i);
int j = 0;
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-------------------\n");
}
代码实现
*********************
****** 1.play *****
****** 2.exit *****
*********************
请输入:1
-----扫雷 游戏-----
0 1 2 3 4 5 6 7 8 9
1 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0
-------------------
-----扫雷 游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
-------------------
三、布置雷的位置。
在game.c文件下,使用随机数,用以随机布置雷。
代码如下
void Setmine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count != 0)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
在test.c文件下,加入布置雷函数。
代码如下
void game()
{
//数组的创建
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBoard(mine,ROWS,COLS,'0');//数组初始化‘0’
InitBoard(show, ROWS, COLS,'*');//数组初始化‘*’
//棋盘的打印
//Displayboard(mine, ROW, COL);//隐秘棋盘
Displayboard(show, ROW, COL);
//布置雷
Setmine(mine,ROW,COL);
}
代码实现
*********************
****** 1.play *****
****** 2.exit *****
*********************
请输入:1
-----扫雷 游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
-------------------
-----扫雷 游戏-----
0 1 2 3 4 5 6 7 8 9
1 1 0 0 1 1 0 0 0 0
2 0 0 1 0 0 0 0 0 0
3 0 0 0 0 0 1 0 0 0
4 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 1 0 0 0
6 0 0 0 0 0 0 0 0 1
7 0 0 0 0 0 1 0 0 0
8 0 0 0 1 0 0 0 0 0
9 0 0 0 0 0 1 0 0 0
-------------------
四、排查雷。
在game.c文件下,计算坐标周围的雷数之和。
逻辑:
首先输入坐标,若不是雷,则显示周围8格雷的数量之和,打印show数组,所有雷都找到,重新打印棋盘mine数组;若是雷,则游戏结束,重新打印棋盘mine数组。
代码如下
// (x-1,y-1) (x,y-1) (x+1,y-1)
// (x-1,y) (x,y) (x+1,y)
// (x-1,y+1) (x,y+1) (x+1,y+1)
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x - 1][y] +
mine[x + 1][y] +
mine[x - 1][y + 1] +
mine[x][y + 1] +
mine[x + 1][y + 1] - 8 * '0';//减去8个0的ACSII码值
}
五、游戏逻辑。
定义一个变量win,win为当前空白数量,若win小于总空白数,则游戏继续;若win=总白数,游戏获胜。踩雷则失败。
在game.c文件下,游戏逻辑。
代码如下
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 (show[x][y] != '*') {
printf("<该位置已排查过请重新输入>\n");
}
else if (mine[x][y] == '1') {
printf("很遗憾,你被炸死了!\n");
Displayboard(mine, ROW, COL);
break;
}
else {
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
Displayboard(show, ROW, COL);
win++;
}
}
else {
printf("<坐标非法,请重新输入>\n");
}
}
if (win == row * col - EASY_COUNT) {
printf("恭喜你,排雷成功!\n");
Displayboard(mine, ROW, COL);
}
}
六、 更新game函数,以及更新声明。
在test.c文件下
代码如下
void game() {
//数组
char mine[ROWS][COLS];//‘0’
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);
//Displayboard(mine, ROW, COL);
//排查雷
FindMine(mine,show, ROW, COL);
}
在game.h文件下
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.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 mine[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
七、开始游戏
*******************
***** 1.play ****
***** 0.exit ****
*******************
请输入:1
-----扫雷 游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
-------------------
请输入要排查的坐标(行,列):5 5
-----扫雷 游戏-----
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * 0 * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
-------------------
请输入要排查的坐标(行,列):1 1
很遗憾,你被炸死了!
—————————————————————————
完
本次代码未实现:
1、标记;
2、递归扩展没有雷的位置
有问题请指正