C language to implement a minesweeper game

//但是对于这个小游戏还不是很完美
//当你点开一个,如果他的周围都没有别的地雷的时候,就会展开一片
//这样就能更加接近真正的扫雷小游戏
//递归方法来实现   1.这个坐标不是雷 2.该坐标的周围也都不是雷
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
    
    
	printf("********************\n");
	printf("***1.play  0.exit***\n");
	printf("********************\n");
}
void game()
{
    
    
	//存放雷的信息  开始的时候布局都是0,当你想要放置雷的时候,雷的信息用1来代替
	//(存放雷的信息  和  排查出来雷的信息) 当你在存放雷的信息界面内如果出现了一个1,就会产生歧义,他到底是周围存在一个雷还是它本身就是一个雷
	//我们给玩家打印的是排查出来雷的信息的界面
	//1.布置好雷的信息
	char mine[ROWS][COLS] = {
    
     0 }; // 11*11
	//2.排查出来雷的信息
	char show[ROWS][COLS] = {
    
     0 };
	//初始化  
	InitBoard(mine, ROWS, COLS,'0');
	InitBoard(show, ROWS, COLS, '*');
	//打印棋盘
	//DisplayBoard(mine, ROW, COL);// 在打印雷的信息的时候我们一定要打印中间的那个9*9的格子
	DisplayBoard(show, ROW, COL);
	//布置雷(在mine(9*9)数组里面随机的找地方放入1)
	SetMine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);  可以在你测试的时候验证代码的正确性
	//找雷
	FindMine(mine, show, ROW, COL); // 我需要从mine数组中找到雷的信息然后再把信息放到show数组中
	

}
void test()
{
    
    
	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);
}
int main()
{
    
    
	test();  
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#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[ROW][COL], int rows, int cols);

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

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

`oid 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)   //对于传过去的mine数组一直都是11*11的所以千万不敢写成ROW , COL
{
    
    
	int i = 0;
	int j = 0;
	//这里我们打印的时候第一行和第一列都省略了,所以初始化的时候i,j=1 开始
	//打印列号
	for (i = 0; i <=col; i++)
	{
    
    
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
    
    
		//行号
		printf("%d ", i);
		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 = EASY_COUNT;
	while (count)
	{
    
    
		int x = rand() % row + 1;   //且在1-9的范围内
		int y = rand() % col + 1;
		if (board[x][y] == '0')
		{
    
    
			board[x][y] = '1';
			count--;
		}
	}
}

int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
    
    
	//'3' - '0' = 0
	//'0' - '0' =0
	//把周围的8个都加起来看是几,但是你得到的是字符
	return mine[x - 1][y] +
		mine[x+1][y] +
		mine[x-1][y+1] +
		mine[x][y+1] +
		mine[x+1][y+1] +
		mine[x-1][y-1] +
		mine[x][y-1] +
		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 count = 0;
	int win = 0;
	//9*9 - 10 = 71 当win=71时就赢了
	while (win<row*col -EASY_COUNT)
	{
    
    
		printf("请输入排查雷的坐标:>");
		scanf("%d%d", &x, &y);
		//这里还要判断用户输入的x和y坐标的有效值
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
    
    
			//坐标合法
			//有两种情况1.这个就是雷,直接炸死了
			//          2.这个不是雷,所以我要统计一下它周围8个位置的地雷总数
			if (mine[x][y] == '1')
			{
    
    
				printf("很遗憾,你被炸死了!\n");
				DisplayBoard(mine, row, col);  //展示出来,避免你不知道自己是怎么死的
				break;
			}
			else
			{
    
    
				//计算x,y坐标周围有几个雷
				int count = get_mine_count(mine, x, y);//在mine 数组的x,y数组周围去寻找
				//之后应该在mine数组对应的show数组中显示这个count数
				show[x][y] = count + '0';//在数组中应该放入字符0
				DisplayBoard(show, row, col);
				//但是你会发现这除非你被炸死了游戏停止,不然就会一直运行下去,那么怎么才算赢了呢
				win++;
			}
		}
		else
		{
    
    
			printf("输入坐标非法,请重新输入!\n");
		}
	}
	if (win = row*col - EASY_COUNT)  // 这里需要判断一下,不然你上面的很遗憾你被炸死了,也会break,直接告诉你成功,这就扯淡了
	{
    
    
		printf("恭喜你,排雷成功\n");
	}
}

But for this mini game, it is not perfect. When you click on one, if there are no other mines around, it will unfold, so that you can get closer to the real minesweeper mini game.
Recursive method to achieve 1. This coordinate is not mine 2. The surrounding of the coordinate is also not mine.
At present, this method is still very difficult for me, and I also need to strengthen this piece of knowledge in the follow-up, and then come back to enrich the integrity of the code.


Guess you like

Origin blog.csdn.net/MEANSWER/article/details/109564234