扫雷的简单实现

目录

#前言

一、整体思路和代码的实现

主函数和菜单函数实现代码的主题功能,循环游戏

游戏主题函数,实现游戏的功能,具体功能分模块来写,其中传参数mine或者show,表示两个不同的数组地址,mine数组表示存放地雷的数组,show数组表示予人展示的数组

游戏模块具体实现

初始化棋盘,其中ret用来字符,例如接收到字符'*’,就在棋盘上打印‘*’,若是接收到字符‘0’,就在棋盘上打印字符‘0’

打印棋盘

设置地雷,位置和个数

排地雷,首先判断选中位置是否为雷,是雷则结束游戏,否则变通过GetMineCount函数讲选中位置周围雷的数目展示到show数组中,供人继续游戏

二、所有代码

测试代码区

游戏代码区

头文件区

三、思考与总结

熟练使用二维数组和传参使用,宏的定义,函数的声明,随机数的使用


#前言

相信同学们应该玩过Windows系统自带的扫雷游戏,今天为大家带来扫雷游戏代码的简单实现,同样的,分为三个文件test.c测试文件,game.c游戏函数文件,game.h存放所用到的头文件和宏的定义,本游戏采用两个二维数组,mine和show,mine存放地雷真实存放位置,show则是展示给人看的,是为了供玩家继续游戏。

一、整体思路和代码的实现

主函数和菜单函数实现代码的主题功能,循环游戏

//菜单函数
void menu()
{
	printf("*******************\n");
	printf("*****  1.play  ****\n");
	printf("*****  0.exit  ****\n");
	printf("*******************\n");
}


//主函数区
int main()
{
	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("输入错误,请重新选择:>");
			break;
		}

	} while (input);
}

游戏主题函数,实现游戏的功能,具体功能分模块来写,其中传参数mine或者show,表示两个不同的数组地址,mine数组表示存放地雷的数组,show数组表示予人展示的数组

//游戏函数
void game()
{
	//创建数组
	char mine[ROWS][COLS];
	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);
	DisplayBoard(show, ROW, COL);


	//扫雷
	FindMine(mine,show, ROW, COL);


}

游戏模块具体实现

初始化棋盘,其中ret用来字符,例如接收到字符'*’,就在棋盘上打印‘*’,若是接收到字符‘0’,就在棋盘上打印字符‘0’

//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ret;
		}
	}
}

打印棋盘

//打印棋盘
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 = MINE_NUM;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		
			if (board[x][y] == '0')
			{
				board[x][y] = '1';
				count--;
			}
		
	}
}

排地雷,首先判断选中位置是否为雷,是雷则结束游戏,否则变通过GetMineCount函数讲选中位置周围雷的数目展示到show数组中,供人继续游戏

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][y - 1] + mine[x][y + 1] + 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-MINE_NUM)
		{
			printf("请输入排雷坐标:>");
			scanf("%d %d", &x,&y);
			if (x >= 1 && x <= row && y >= 1 && y <= col)
	        {
			if (mine[x][y] == '1')
			{
				printf("你被炸死了");
				DisplayBoard(mine, ROW, COL);
				break;

			}
			else
			{
				int count = GetMineCount(mine , x , y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;

			}
		    }
			else
			{
				printf("坐标非法,请重新输入:>");
			}
	}
		if (win == row * col - MINE_NUM)
		{
			printf("排雷成功");
			DisplayBoard(mine, ROW, COL);
		}

}

二、所有代码

测试代码区

#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(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);

	//设置地雷
	SetMine(mine, ROW, COL);
	DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);


	//扫雷
	FindMine(mine,show, ROW, COL);


}

//主函数区
int main()
{
	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("输入错误,请重新选择:>");
			break;
		}

	} while (input);
}

游戏代码区

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"


//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = ret;
		}
	}
}


//打印棋盘
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 = MINE_NUM;
	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 - 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');
}

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-MINE_NUM)
		{
			printf("请输入排雷坐标:>");
			scanf("%d %d", &x,&y);
			if (x >= 1 && x <= row && y >= 1 && y <= col)
	        {
			if (mine[x][y] == '1')
			{
				printf("你被炸死了");
				DisplayBoard(mine, ROW, COL);
				break;

			}
			else
			{
				int count = GetMineCount(mine , x , y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;

			}
		    }
			else
			{
				printf("坐标非法,请重新输入:>");
			}
	}
		if (win == row * col - MINE_NUM)
		{
			printf("排雷成功");
			DisplayBoard(mine, ROW, COL);
		}

}

头文件区

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MINE_NUM 80     //设置的雷的个数
#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 ret);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

运行样例 

三、思考与总结

熟练使用二维数组和传参使用,宏的定义,函数的声明,随机数的使用

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值