【C语言】扫雷(简易版)

实现扫雷:

  • game.h —— 游戏代码的申明(函数明,符号定义)
  • test.c —— 测试游戏逻辑
  • game.c —— 游戏代码的实现

游戏具体过程实现

打印菜单

test.c

#include <stdio.h>
void menu()
{
    
    
	printf("*************************\n");
	printf("******    1.play   ******\n");
	printf("******    0.esit   ******\n");
	printf("*************************\n");
}
int main()
{
    
    
	int input = 0;
	do
	{
    
    
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			printf("扫雷\n");
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
	return 0;
}

创建雷盘

创建两个雷盘分别为布置好雷的信息和排查出雷的信息。假设我们建立 9 * 9 的游戏棋盘,那我们真正就要建立 11 * 11 的棋盘。之所以要建立大一圈的棋盘,是为了,后面检测扫雷地址周围雷数目时不越界。
创建一个mine数组放布置好雷的信息,show数组放存放排查出雷的信息。

  • game.h
#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2
  • test.c
	char mine[ROWS][COLS] = {
    
     0 };//布置好雷的信息
	char show[ROWS][COLS] = {
    
     0 };//存放排查出雷的信息

初始化雷盘

初始化数组的内容为指定内容
mine 数组在没有布置雷的时候,都是’0’
show 数组在没有排查出雷的时候,都是’*’

  • game.h
void DisplayBoard(char board[ROWS][COLS], int row, int col);
  • test.c
//初始化数组的内容为指定内容
	//mine 数组在没有布置雷的时候,都是'0'
	InitBoard(mine, ROWS, COLS,'0');
   // show 数组在没有排查出雷的时候,都是'*'
	InitBoard(show, ROWS, COLS,'*');
	DisplayBoard(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);
  • game.c
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	printf("---------扫雷游戏---------\n");
	for (j = 0; j <= col; j++)
	{
    
    
		printf("%d ", j);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
    
    
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
    
    
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("---------扫雷游戏---------\n");

}

设置雷

我们的雷设置在中间的99格子里面,所以我们传的是ROW,COL,我们在99里面随机生成雷。设置简单版本的雷为10个。随机生成坐标的rand函数的种子srand函数只需要在main函数中声明一次即可。

game.h

void SetMine(char board[ROWS][COLS], int row, int col);

test.c

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

game.c

void SetMine(char board[ROWS][COLS], int row, int col)
{
    
    
	int count = EASY_COUNT;
	while (count)
	{
    
    
		int x = rand() % row + 1;  //随机生成坐标
		int y = rand() % col + 1;
		if (board[x][y] == '0')   //检查该位置是否已有雷
		{
    
    
			board[x][y] = '1';
			count--;
		}
	}
}

排查雷

排查雷的时候我们首先需要让用户输入需要排查的坐标,然后判断坐标的合法性及该坐标是否已被排查,其次再判断该坐标是否有雷,如果没有,就检查它周围的坐标,直到遇到有雷的坐标才停止。如果输入的坐标不是雷,要判断它周围八个坐标是不是雷。
注意:棋盘放的是字符,不能简简单单计算。我们知道,‘1’ - ‘0’->1,‘0’ - ‘0’->0。把周围的数字都加起来再一起减去个数*‘0’,我们这里计算的是字符的ASCII值,得到的数是几周围就有几个雷。
在这里插入图片描述

game.h

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

test.c

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

game.c

int get_mine_count(char board[ROWS][COLS], int x, int y)
{
    
    
	return (board[x - 1][y] +
		board[x - 1][y - 1] +
		board[x][y - 1] +
		board[x + 1][y - 1] +
		board[x + 1][y] +
		board[x + 1][y + 1] +
		board[x][y + 1] +
		board[x - 1][y + 1] - 8 * '0');//这里算的是'0'的ASCII值
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	while (1)
	{
    
    
		printf("请输入要排查的坐标:>");
		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
			{
    
    
				//统计mine数组中x,y坐标周围有几个雷
				int count = get_mine_count(mine, x, y);
				show[x][y] = count + '0';//转换成数字字符
				DisplayBoard(show, ROW, COL);
			}
		}
		else
		{
    
    
			printf("输入坐标非法,请重新输入\n");
		}
	}
}

游戏完整代码

game.h

#include <stdio.h>
#include <time.h>
#include <stdlib.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 board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

test.c

#include "game.h"

void menu()
{
    
    
	printf("*************************\n");
	printf("******    1.play   ******\n");
	printf("******    0.esit   ******\n");
	printf("*************************\n");
}
void game()
{
    
    
	char mine[ROWS][COLS] = {
    
     0 };//布置好雷的信息
	char show[ROWS][COLS] = {
    
     0 };//存放排查出雷的信息
	//初始化数组的内容为指定内容
	//mine 数组在没有布置雷的时候,都是'0'
	InitBoard(mine, ROWS, COLS,'0');
   // show 数组在没有排查出雷的时候,都是'*'
	InitBoard(show, ROWS, COLS,'*');
	//设置雷
	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("选择错误\n");
			break;
		}
	} while (input);
	return 0;
}

game.c

#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
    
    
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
    
    
		for (j = 0; j < cols; j++)
		{
    
    
			board[i][j] = set;
		}
	}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	printf("---------扫雷游戏---------\n");
	for (j = 0; j <= col; j++)
	{
    
    
		printf("%d ", j);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
    
    
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
    
    
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("---------扫雷游戏---------\n");

}
void SetMine(char board[ROWS][COLS], int row, int col)
{
    
    
	int count = EASY_COUNT;
	while (count)
	{
    
    
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
    
    
			board[x][y] = '1';
			count--;
		}
	}
}
int get_mine_count(char board[ROWS][COLS], int x, int y)
{
    
    
	return (board[x - 1][y] +
		board[x - 1][y - 1] +
		board[x][y - 1] +
		board[x + 1][y - 1] +
		board[x + 1][y] +
		board[x + 1][y + 1] +
		board[x][y + 1] +
		board[x - 1][y + 1] - 8 * '0');//这里算的是'0'的ASCII值
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	while (1)
	{
    
    
		printf("请输入要排查的坐标:>");
		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
			{
    
    
				//统计mine数组中x,y坐标周围有几个雷
				int count = get_mine_count(mine, x, y);
				show[x][y] = count + '0';//转换成数字字符
				DisplayBoard(show, ROW, COL);
			}
		}
		else
		{
    
    
			printf("输入坐标非法,请重新输入\n");
		}
	}
}

在扫雷时,当该坐标没有雷,有一大片的空白时,需全部打印出来,奈何本人技术不足,难以实现,只能将周围的8个坐标显示出来,找到雷了做上标记这个也没有实现,待我学习学习后补上。

本章到这里就结束啦,如果有哪里写的不好的地方,请指正。
如果觉得不错并且对你有帮助的话请给个三连支持一下吧!
Fighting!!!✊

猜你喜欢

转载自blog.csdn.net/qq_68661624/article/details/124740794