扫雷游戏——附源代码

扫雷游戏的源代码比较简单,不设计比较复杂的代码,主要是多个函数的组合,每个函数执行自己的功能,最终支持游戏的完成。

1.菜单

我们需要一个提醒信息来让用户进行选择。

cpp 复制代码
void menu()
{
	printf("***********************\n");
	printf("******1. play     *****\n");
	printf("******0.exit      *****\n");
	printf("***********************\n");
}
void test()
{
	int input = 0;
	do
	{
		menu();
		printf("请选择:>\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("扫雷\n");
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
}

2.游戏代码的实现

运用二维数组来制定表格,当是雷的时候,我们填补空格为*,当不是雷的时候,我们填补空格为0,再创建一个相同的表格,将我们在玩游戏过程中排查到的雷的情况显示出来。在实际创建表格的过程中,行和列均比所需要的多一行(例如:如果棋盘的大小是9*9,则数组的大小就为11*11)。这样有利于我们对于雷的排查(雷的排查需要以排查的对象为中心,知道所有周围雷的情况)。

游戏代码我们用两个文件来实现,game.c和game.h

在game.h 中主要包含的是所需要函数的声明,在game.c中是对各个函数的实现。

首先,我们需要对两个表格进行初始化。

2.1 表格初始化

函数的声明为:

cpp 复制代码
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);

函数的代码实现:

cpp 复制代码
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;
		}
	}
}

2.2 打印棋盘

为了方便后续对于表盘中位置的确定,在打印棋盘的时候,我们将坐标也标了出来。

cpp 复制代码
DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	printf("--------扫雷--------\n");
	int i = 0;
	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");
	}
}

2.3 布置雷

在布置雷的时候,我们要确定雷的个数,除此之外,我们还要确保雷位置的随机性。

cpp 复制代码
void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand()%row+1;
		y = rand()%col+1;
		if (mine[x][y] != '1')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

2.4 排查雷

在排查雷时,当我们所确定的位置不是雷,那么就要计算其周围的雷的个数,这里就需要一个函数来进行实现。

cpp 复制代码
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	int i = 0;
	int j = 0;
	int sum = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
		for (j = y - 1; j <= y + 1; j++)
		{
			sum += (mine[i][j] - '0');
		}
	}
	return sum;
}

由于只有八个数相加,因此,除了这个办法之外,我们还可以直接让它们进行相加即可。

接下来,我们就进行了排查雷的工作,要注意的是,当经历每个循环时,我们要判断用户是否把每个雷都排查了出来,所以,我们还要有一个判断的步骤。

cpp 复制代码
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 < col*row - EASY_COUNT)
	{
		printf("请输入要排查的坐标:\n");
		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] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("输入的坐标有误,请重新输入!\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,排雷成功!\n");
		DisplayBoard(mine, ROW, COL);
	}
}

3.源代码展示

3.1 test.c 中的代码

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#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');
	InitBoard(show, ROWS, COLS, '*');

	//打印棋盘
	DisplayBoard(show, ROW, COL);
	printf("\n");
	
	//布置雷
	SetMine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);

	//排查雷
	FindMine(mine, show, ROW, COL);
}

void test()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请选择:>\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("扫雷\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
}

int main()
{
	test();
	return 0;
}

3.2 game.h 中的代码

cpp 复制代码
#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);
//打印棋盘
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);

3.3 game.c 中的代码

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#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;
		}
	}
}

DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	printf("--------扫雷--------\n");
	int i = 0;
	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");
	}
}

//在布置雷时,要注意随机
void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand()%row+1;
		y = rand()%col+1;
		if (mine[x][y] != '1')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
	int i = 0;
	int j = 0;
	int sum = 0;
	for (i = x - 1; i <= x + 1; i++)
	{
		for (j = y - 1; j <= y + 1; j++)
		{
			sum += (mine[i][j] - '0');
		}
	}
	return sum;
}

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 < col*row - EASY_COUNT)
	{
		printf("请输入要排查的坐标:\n");
		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] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("输入的坐标有误,请重新输入!\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,排雷成功!\n");
		DisplayBoard(mine, ROW, COL);
	}
}

总体来说,代码实现简单,思路也比较清晰,今天就到这里,我们下一个知识点见!

相关推荐
DisonTangor17 小时前
Ruffle 继续在开源软件中支持 Adobe Flash Player
游戏·adobe
VMOS云手机19 小时前
《仙境传说RO:新启航》游戏攻略,VMOS云手机辅助高效挂机助攻!
游戏·云手机·游戏辅助·黑科技·免费云手机
PC端游爱好者20 小时前
战神诸神黄昏9月19日登录PC端! 手机怎么玩战神诸神黄昏
游戏·智能手机·电脑·远程工作·玩游戏
疑惑的杰瑞1 天前
[C语言]连子棋游戏
c语言·开发语言·游戏
文宇炽筱1 天前
《黑神话:悟空》:中国游戏界的新篇章
游戏·游戏程序·玩游戏
星毅要努力1 天前
【C语言编程】【小游戏】【俄罗斯方块】
c语言·开发语言·学习·游戏
宇宙第一小趴菜2 天前
中秋节特别游戏:给玉兔投喂月饼
python·游戏·pygame·中秋节
这是我582 天前
C++掉血迷宫
c++·游戏·visual studio·陷阱·迷宫·生命·
zhooyu2 天前
C++和OpenGL实现3D游戏编程【目录】
开发语言·游戏·游戏程序
niaoma2 天前
剑灵服务端源码(c#版本+数据库+配套客户端+服务端)
游戏·c#·游戏程序·游戏策划