扫雷游戏 ||C语言实现

一.扫雷游戏的思路

1)布置雷

2)排查雷

如果这个位置是雷,就炸死了,游戏结束。

不是雷,就告诉坐标周围有几个雷。

把所有不是雷的地方都找到,剩下的都是雷。游戏结束。

二.扫雷游戏的设计

设计三个文件:

test.c用来测试扫雷游戏,设计游戏菜单。

game.c  game.h用来扫雷游戏的实现

1.布置雷

1)设计棋盘

由于要在棋盘上布置雷和排查雷,只在一个数组里面存储这么多的信息有些杂乱。

为了防止混淆雷和非雷的信息以及布置雷的信息

我们就需要创建两个二维数组

一个用来存放雷,一个存放排查出雷的信息。

第一个数组存放雷,开始全部初始化成0,设置十个雷,将雷设置成1。

第二个数组开始全部初始化为*,保持神秘,之后排查时调用,展示排查出雷的信息。

所以为了方便统一数组类型,都将数组设置为字符数组0,1放入‘’中。

 注意:

由于棋盘大小为9*9,防止越界访问,应创建11*11的数组。(遇到拐角的时候显示雷附近的雷数出错),如图

9b1b3590335d4c728618b8169846b213.png

2)  设计四个函数

分别用于初始化,打印棋盘,布置雷和排查雷

其中用set表示字符数组内存放的初始的字符内容。

#define TOTAL 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);//打印棋盘
void Setmine(char board[ROWS][COLS], int rows, int cols);//布置雷
void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int rows, int cols);//排查雷

2.排查雷

1)布置十个雷放在随机位置

如何产生随机数?

 #include <stdlib.h>
 #include <time.h>
 int main()
 {
 //使⽤time函数的返回值设置种⼦
 
srand((unsigned int)time(NULL))
}

要生成a-b之间的随机数:a + rand()%(b-a+1)

eg.生成100-200:100+rand()%101

那么布置随机十个雷的位置就很容易了

void Setmine(char board[ROWS][COLS],int row,int col)
	{
		int count = TOTAL;
		while (count)
		{
			int x = rand() % row + 1;
			int y = rand() % col + 1;
			if (board[x][y] == '0')
			{
				board[x][y] = '1';
				count--;
			}
		}
	}
2)统计雷个数的函数的设计

如下图 ,想要统计雷的个数,需要找到周围八个各自的坐标所存储的字符,并减去‘0’转为数字,相加计算就得到了个数。

三.扫雷游戏的实现

1.game.h
#define _CRT_SECURE_NO_WARNINGS
#pragma once //让头文件只包含一次
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define TOTAL 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);//打印棋盘
void Setmine(char board[ROWS][COLS], int rows, int cols);//布置雷
void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int rows, int cols);//排查雷
2.test.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;
		}

	}
}
void Displayboard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	printf("-------扫雷游戏-------\n");
	for (i = 0; i <= row; 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 board[ROWS][COLS],int row,int col)
	{
		int count = TOTAL;
		while (count)
		{
			int x = rand() % row + 1;
			int y = rand() % col + 1;
			if (board[x][y] == '0')
			{
				board[x][y] = '1';
				count--;
			}
		}
	}
	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 + 1] +mine[x][y + 1] +mine[x - 1][y + 1] +mine[x - 1][y]) - (8 * '0');
		}

	void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
	{
		int x = 0;
		int y = 0;
		int flag = 0;
		while (flag < row * col - TOTAL)
		{
			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);
					flag++;
				}
			}
			else
			{
				printf("坐标输入错误,请重新输入\n");
			}

		}
		if (flag = row * col - TOTAL)
		{
			printf("恭喜你,排雷成功\n");
			Displayboard(mine, row, col);
		}
	}

​
3.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;
	printf("-------扫雷游戏-------\n");
	for (i = 0; i <= row; 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 board[ROWS][COLS],int row,int col)
	{
		int count = TOTAL;
		while (count)
		{
			int x = rand() % row + 1;
			int y = rand() % col + 1;
			if (board[x][y] == '0')
			{
				board[x][y] = '1';
				count--;
			}
		}
	}
	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 + 1] +mine[x][y + 1] +mine[x - 1][y + 1] +mine[x - 1][y]) - (8 * '0');
		}

	void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
	{
		int x = 0;
		int y = 0;
		int flag = 0;
		while (flag < row * col - TOTAL)
		{
			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);
					flag++;
				}
			}
			else
			{
				printf("坐标输入错误,请重新输入\n");
			}

		}
		if (flag = row * col - TOTAL)
		{
			printf("恭喜你,排雷成功\n");
			Displayboard(mine, row, col);
		}
	}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值