IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C语言实现扫雷游戏(一步步教你如何写扫雷) -> 正文阅读

[C++知识库]C语言实现扫雷游戏(一步步教你如何写扫雷)

使用工具

Visual Studio 2019

一、基本思路和流程

1. 扫雷游戏的游戏规则

1. 在已经准备好的棋盘中,找出所有没有设有雷的位置,找出所有位置后,既获得胜利
2. 该游戏设有三种难度等级,分别是简单、中级和困难模式,每种模式的棋盘大小和 雷的数目都不一样
3. 踩到雷即游戏失败

2. 代码实现思路

1. 游戏规则界面,给玩家讲述该游戏的游戏规则
2. 菜单界面: 玩家可选择开始游戏或退出游戏
3. 选择游戏难度界面,玩家可选择游戏难度
4. 初始化棋盘和打印棋盘
5. 玩家开始选择排雷坐标
6. 玩家落下排雷坐标后打印棋盘,并判断玩家是否踩到雷或者是否已经找出所有非雷位置
7. 若玩家所排位置周围的雷的个数为0,则将该位置改为空格,并利用递归对周围八个位置分别进行排雷
8. 游戏结束后,玩家可选择继续游戏或者退出游戏

二、实现步骤(具体步骤)

1. 使用多文件形式

1. test.c 文件(源文件): 写整个游戏的大致过程
2. game.h 文件(头文件): 引用所需的头文件以及对各个程序所需函数的声明
3. game.c 文件(源文件):定义在game.h 中声明的函数

2. 代码实现

定义各个难度等级的棋盘大小和雷的数量

在game.h文件中,使用宏定义

//简单
#define EASY_ROW 9
#define EASY_COL 9
#define EASY_COUNT 15

//中级
#define MID_ROW 15
#define MID_COL 15
#define MID_COUNT 30

//困难
#define DIF_ROW 21
#define DIF_COL 21
#define DIF_COUNT 40

//简单
#define EASY_ROWS EASY_ROW+2
#define EASY_COLS EASY_COL+2
//中级
#define MID_ROWS MID_ROW+2
#define MID_COLS MID_COL+2
//困难
#define DIF_ROWS DIF_ROW+2
#define DIF_COLS DIF_COL+2

定义全局变量

在test.c 文件中

int ROW = 0;
int COL = 0;
int ROWS = 0;
int COLS = 0;
int count = 0;

游戏规则界面

  • 参考代码如下:
void game_rule()
{
	printf("***********游戏规则: ***********\n");
	printf("游戏开始前,你可以选择游戏难度,\n");
	printf("设有简单,中级,困难三种难度,每\n");
	printf("种难度棋盘大小和雷的数目都不一样,\n");
	printf("当你把所有非雷位置找出时,既赢下\n");
	printf("游戏,祝您游戏愉快\n");
	printf("\n");
	printf("\n");
}

菜单界面

  • 代考代码如下:
void menu()
{
	printf("********************************\n");
	printf("********   1. play     *********\n");
	printf("********   0. exit     *********\n");
	printf("********************************\n");
}

选择实现

  • 参考代码如下
int main()
{
	game_rule();
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
	return 0;
}

选择难度界面

  • 参考代码如下
void select_diffcult()
{
	printf("************难度系数***********\n");
	printf("**********  1. 简单   *********\n");
	printf("**********  2. 中级   *********\n");
	printf("**********  3. 困难   *********\n");
	int input = 0;
	do {
		printf("请选择:>");
		int select = 0;
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			ROW = EASY_ROW;
			COL = EASY_COL;
			ROWS = EASY_ROWS;
			COLS = EASY_COLS;
			count = EASY_COUNT;
			input = 0;
			break;
		case 2:
			ROW = MID_ROW;
			COL = MID_COL;
			ROWS = MID_ROWS;
			COLS = MID_COLS;
			count = MID_COUNT;
			input = 0;
			break;
		case 3:
			ROW = DIF_ROW;
			COL = DIF_COL;
			ROWS = DIF_ROWS;
			COLS = DIF_COLS;
			count = DIF_COUNT;
			input = 0;
			break;
		default:
			input = 1;
			break;
		}
	} while (input);
}

初始化和打印棋盘

棋盘
  • 使用二维数组,由于定于数组设置大小只能用常量,所有为了避免越界,我们将棋盘大小设置为三种游戏模式中棋盘最大的那一类
  • 为了方便后续打印,我们将设置两个一样大小的数组,一个用来存放雷,一个用来打印
	char mine[DIF_ROWS][DIF_COLS];//存放雷
	char show[DIF_ROWS][DIF_COLS];
初始化棋盘

mine[DIF_ROWS][DIF_COLS] 和 show[DIF_ROWS][DIF_COLS]都需要进行初始化

  • 参考代码如下
void InitBoard(char board[DIF_ROWS][DIF_COLS], int rows, int cols, char ch)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ch;
		}
	}
}
打印棋盘
  • 参考代码如下
void DisPlayBoard(char board[DIF_ROWS][DIF_COLS], int row, int col)
{
	int i = 0;
	printf("   ");
	for (i = 1; i <= row; i++)
	{
		printf("%2d ", i);
	}
	printf("\n");
	printf("  |");
	for (i = 1; i <= row; i++)
	{
		printf("---");
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%2d|", i);
		int j = 0;
		for (j = 1; j <= col; j++)
		{
			printf(" %c ", board[i][j]);
		}
		printf("\n");
	}
}

  • 实现后效果
    在这里插入图片描述

存放雷

  • 存放雷可能出现的问题

1. 随机生成雷的过程中,需要判断雷的位置是否已被占用

可以使用时间戳生成随机数,随机生成雷的坐标,利用循环解决位置是否被占用的问题

  • 参考代码如下
//cnt是雷的数量
void SetMine(char mine[DIF_ROWS][DIF_COLS], int row, int col, int cnt)
{
	while (cnt > 0)
	{
		int x = 0;
		int y = 0;
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			cnt--;
		}
	}
}

排查放雷

如果玩家排查的位置的周围雷的数量为0, 那么久要利用递归来对周围的八个位置进行排查,为了防止递归的栈溢出问题,需要将已经排查过得位置进行标记

  • 参考代码如下
//排查位置周围雷的数量
int MineCount(char mine[DIF_ROWS][DIF_COLS], int x, int y)
{
	int cnt = 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';
	return cnt;

}
//对排查位置周围八个位置进行排查
void Expend(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int x, int y, int visted[DIF_ROWS][DIF_COLS])
{
	//visted[x][y] == 1;
	int vist[DIF_ROWS][DIF_COLS] = { 0 };
	int cnt = MineCount(mine, x, y);
	if (cnt > 0)
		show[x][y] = cnt + '0';
	else
	{
		show[x][y] = ' ';
		if (visted[x - 1][y - 1] == 0)
		{
			vist[x	- 1][y - 1] = 1;
			visted[x - 1][y - 1] = 1;
		}
		if (visted[x - 1][y] == 0)
		{
			vist[x - 1][y] = 1;
			visted[x - 1][y] = 1;
		}
		if (visted[x - 1][y + 1] == 0)
		{
			vist[x - 1][y + 1] = 1;
			visted[x - 1][y + 1] = 1;
		}
		if (visted[x][y - 1] == 0)
		{
			vist[x][y - 1] = 1;
			visted[x][y - 1] = 1;
		}
		if (visted[x][y + 1] == 0)
		{
			vist[x ][y + 1] = 1;
			visted[x][y + 1] = 1;
		}
		if (visted[x + 1][y - 1] == 0)
		{
			vist[x + 1][y - 1] = 1;
			visted[x + 1][y - 1] = 1;
		}
		if (visted[x + 1][y] == 0)
		{
			vist[x + 1][y] = 1;
			visted[x + 1][y] = 1;
		}
		if (visted[x + 1][y + 1] == 0)
		{
			vist[x + 1][y + 1] = 1;
			visted[x + 1][y + 1] = 1;
		}


		if (vist[x - 1][y - 1] == 1)
		{
			Expend(mine, show, x - 1, y - 1, visted);
		}
		if (vist[x - 1][y] == 1)
		{
			Expend(mine, show, x - 1, y, visted);
		}
		if (vist[x - 1][y + 1] == 1)
		{
			Expend(mine, show, x - 1, y + 1, visted);
		}
		if (vist[x][y - 1] == 1)
		{
			Expend(mine, show, x, y - 1, visted);
		}
		if (vist[x][y + 1] == 1)
		{
			Expend(mine, show, x, y + 1, visted);
		}
		if (vist[x + 1][y - 1] == 1)
		{
			Expend(mine, show, x + 1, y - 1, visted);
		}
		if (vist[x + 1][y] == 1)
		{
			Expend(mine, show, x + 1, y, visted);
		}
		if (vist[x + 1][y + 1] == 1)
		{
			Expend(mine, show, x + 1, y + 1, visted);
		}
	}
}
//排查雷
void FindMine(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int row, int col, int cnt, int visted[DIF_ROWS][DIF_COLS])
{
	while (1)
	{
		int x = 0;
		int y = 0;
		printf("请输入:>");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (1 == visted[x][y])
			{
				printf("该位子已被占用\n");
			}
			else if (mine[x][y] == '1')
			{
				show[x][y] = '$';
				DisPlayBoard(show, row, col);
				printf("你踩到雷了\n");
				break;
			}
			else
			{
				visted[x][y] == 1;
				Expend(mine, show, x, y, visted);
				DisPlayBoard(show, row, col);
			}
		}
		else
		{
			printf("超出范围\n");
		}
		int count = 0;
		int i = 0;
		for (i = 1; i <= row; i++)
		{
			int j = 0;
			for (j = 1; j <= col; j++)
			{
				if (show[i][j] == '*')
				{
					count++;
				}
			}
		}
		if (count == cnt)
		{
			printf("恭喜你,你赢了\n");
			break;
		}
	}
}

三、整体游戏流程

  • 参考代码如下
void game()
{
	select_diffcult();
	char mine[DIF_ROWS][DIF_COLS];
	char show[DIF_ROWS][DIF_COLS];
	int visted[DIF_ROWS][DIF_COLS];
	int i = 0;
	for (i = 0; i < ROWS; i++)
	{
		int j = 0;
		for (j = 0; j < COLS; j++)
		{
			if (i == 0)
				visted[i][j] = 1;
			else if (j == 0)
				visted[i][j] = 1;
			else if (i == ROWS - 1)
				visted[i][j] = 1;
			else if (j == COLS - 1)
				visted[i][j] = 1;
			else
				visted[i][j] = 0;
		}
	}
	//初始化棋盘
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	//打印棋盘
	DisPlayBoard(show, ROW, COL);
	//存放雷
	SetMine(mine, ROW, COL, count);
	//排查雷
	FindMine(mine, show, ROW, COL, count, visted);
}

附上所有代码

test.c 文件

#include "game.h"

int ROW = 0;
int COL = 0;
int ROWS = 0;
int COLS = 0;
int count = 0;

void game_rule()
{
	printf("***********游戏规则: ***********\n");
	printf("游戏开始前,你可以选择游戏难度,\n");
	printf("设有简单,中级,困难三种难度,每\n");
	printf("种难度棋盘大小和雷的数目都不一样,\n");
	printf("当你把所有非雷位置找出时,既赢下\n");
	printf("游戏,祝您游戏愉快\n");
	printf("\n");
	printf("\n");
}

void menu()
{
	printf("********************************\n");
	printf("********   1. play     *********\n");
	printf("********   0. exit     *********\n");
	printf("********************************\n");
}

void select_diffcult()
{
	printf("************难度系数***********\n");
	printf("**********  1. 简单   *********\n");
	printf("**********  2. 中级   *********\n");
	printf("**********  3. 困难   *********\n");
	int input = 0;
	do {
		printf("请选择:>");
		int select = 0;
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			ROW = EASY_ROW;
			COL = EASY_COL;
			ROWS = EASY_ROWS;
			COLS = EASY_COLS;
			count = EASY_COUNT;
			input = 0;
			break;
		case 2:
			ROW = MID_ROW;
			COL = MID_COL;
			ROWS = MID_ROWS;
			COLS = MID_COLS;
			count = MID_COUNT;
			input = 0;
			break;
		case 3:
			ROW = DIF_ROW;
			COL = DIF_COL;
			ROWS = DIF_ROWS;
			COLS = DIF_COLS;
			count = DIF_COUNT;
			input = 0;
			break;
		default:
			input = 1;
			break;
		}
	} while (input);
}
void game()
{
	select_diffcult();
	char mine[DIF_ROWS][DIF_COLS];
	char show[DIF_ROWS][DIF_COLS];
	int visted[DIF_ROWS][DIF_COLS];
	int i = 0;
	for (i = 0; i < ROWS; i++)
	{
		int j = 0;
		for (j = 0; j < COLS; j++)
		{
			if (i == 0)
				visted[i][j] = 1;
			else if (j == 0)
				visted[i][j] = 1;
			else if (i == ROWS - 1)
				visted[i][j] = 1;
			else if (j == COLS - 1)
				visted[i][j] = 1;
			else
				visted[i][j] = 0;
		}
	}
	//初始化棋盘
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	//打印棋盘
	DisPlayBoard(show, ROW, COL);
	//存放雷
	SetMine(mine, ROW, COL, count);
	//排查雷
	FindMine(mine, show, ROW, COL, count, visted);
}

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

game.h 文件

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//简单
#define EASY_ROW 9
#define EASY_COL 9
#define EASY_COUNT 15

//中级
#define MID_ROW 15
#define MID_COL 15
#define MID_COUNT 30

//困难
#define DIF_ROW 21
#define DIF_COL 21
#define DIF_COUNT 40

//简单
#define EASY_ROWS EASY_ROW+2
#define EASY_COLS EASY_COL+2
//中级
#define MID_ROWS MID_ROW+2
#define MID_COLS MID_COL+2
//困难
#define DIF_ROWS DIF_ROW+2
#define DIF_COLS DIF_COL+2

//初始化棋盘
extern void InitBoard(char board[DIF_ROWS][DIF_COLS], int rows, int cols,char ch);
//打印棋盘
extern void DisPlayBoard(char board[DIF_ROWS][DIF_COLS], int row, int col);
//存放雷
extern void SetMine(char mine[DIF_ROWS][DIF_COLS], int row, int col, int cnt);
//排查雷
extern void FindMine(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int row, int col, int cnt, int visted[DIF_ROWS][DIF_COLS]);
extern int MineCount(char mine[DIF_ROWS][DIF_COLS], int x, int y);
extern void Expend(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int x, int y, int visted[DIF_ROWS][DIF_COLS]);

game.c 文件

#include "game.h"

void InitBoard(char board[DIF_ROWS][DIF_COLS], int rows, int cols, char ch)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ch;
		}
	}
}

void DisPlayBoard(char board[DIF_ROWS][DIF_COLS], int row, int col)
{
	int i = 0;
	printf("   ");
	for (i = 1; i <= row; i++)
	{
		printf("%2d ", i);
	}
	printf("\n");
	printf("  |");
	for (i = 1; i <= row; i++)
	{
		printf("---");
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%2d|", i);
		int j = 0;
		for (j = 1; j <= col; j++)
		{
			printf(" %c ", board[i][j]);
		}
		printf("\n");
	}
}

void SetMine(char mine[DIF_ROWS][DIF_COLS], int row, int col, int cnt)
{
	while (cnt > 0)
	{
		int x = 0;
		int y = 0;
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			cnt--;
		}
	}
}

int MineCount(char mine[DIF_ROWS][DIF_COLS], int x, int y)
{
	int cnt = 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';
	return cnt;

}

void Expend(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int x, int y, int visted[DIF_ROWS][DIF_COLS])
{
	//visted[x][y] == 1;
	int vist[DIF_ROWS][DIF_COLS] = { 0 };
	int cnt = MineCount(mine, x, y);
	if (cnt > 0)
		show[x][y] = cnt + '0';
	else
	{
		show[x][y] = ' ';
		if (visted[x - 1][y - 1] == 0)
		{
			vist[x	- 1][y - 1] = 1;
			visted[x - 1][y - 1] = 1;
		}
		if (visted[x - 1][y] == 0)
		{
			vist[x - 1][y] = 1;
			visted[x - 1][y] = 1;
		}
		if (visted[x - 1][y + 1] == 0)
		{
			vist[x - 1][y + 1] = 1;
			visted[x - 1][y + 1] = 1;
		}
		if (visted[x][y - 1] == 0)
		{
			vist[x][y - 1] = 1;
			visted[x][y - 1] = 1;
		}
		if (visted[x][y + 1] == 0)
		{
			vist[x ][y + 1] = 1;
			visted[x][y + 1] = 1;
		}
		if (visted[x + 1][y - 1] == 0)
		{
			vist[x + 1][y - 1] = 1;
			visted[x + 1][y - 1] = 1;
		}
		if (visted[x + 1][y] == 0)
		{
			vist[x + 1][y] = 1;
			visted[x + 1][y] = 1;
		}
		if (visted[x + 1][y + 1] == 0)
		{
			vist[x + 1][y + 1] = 1;
			visted[x + 1][y + 1] = 1;
		}


		if (vist[x - 1][y - 1] == 1)
		{
			Expend(mine, show, x - 1, y - 1, visted);
		}
		if (vist[x - 1][y] == 1)
		{
			Expend(mine, show, x - 1, y, visted);
		}
		if (vist[x - 1][y + 1] == 1)
		{
			Expend(mine, show, x - 1, y + 1, visted);
		}
		if (vist[x][y - 1] == 1)
		{
			Expend(mine, show, x, y - 1, visted);
		}
		if (vist[x][y + 1] == 1)
		{
			Expend(mine, show, x, y + 1, visted);
		}
		if (vist[x + 1][y - 1] == 1)
		{
			Expend(mine, show, x + 1, y - 1, visted);
		}
		if (vist[x + 1][y] == 1)
		{
			Expend(mine, show, x + 1, y, visted);
		}
		if (vist[x + 1][y + 1] == 1)
		{
			Expend(mine, show, x + 1, y + 1, visted);
		}
	}
}

void FindMine(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int row, int col, int cnt,int visted[DIF_ROWS][DIF_COLS])
{
	while (1)
	{
		int x = 0;
		int y = 0;
		printf("请输入:>");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (1 == visted[x][y])
			{
				printf("该位子已被占用\n");
			}
			else if (mine[x][y] == '1')
			{
				show[x][y] = '$';
				DisPlayBoard(show, row, col);
				printf("你踩到雷了\n");
				break;
			}
			else
			{
				visted[x][y] == 1;
				Expend(mine, show, x, y, visted);
				DisPlayBoard(show, row, col);
			}
		}
		else
		{
			printf("超出范围\n");
		}
		int count = 0;
		int i = 0;
		for (i = 1; i <= row; i++)
		{
			int j = 0;
			for (j = 1; j <= col; j++)
			{
				if (show[i][j] == '*')
				{
					count++;
				}
			}
		}
		if (count == cnt)
		{
			printf("恭喜你,你赢了\n");
			break;
		}
	}
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-19 11:52:27  更:2021-08-19 11:52:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 13:00:33-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码