current position:Home>Minesweeper implemented in C language

Minesweeper implemented in C language

2022-08-06 09:23:16ex-boyfriend in C

目录

一.整体设计逻辑

二.加载逻辑

 三.设置雷盘,and display panel

四.设置雷 

 五.排雷

 最后:


The birth of minesweeper actually originated from1989年的时候,一位名叫Robert DonnerIn order to exercise their programming skills,One written with “穿越雷区”A small game for the purpose,After some fine-tuning,It has become a well-known minesweeper game.随后,随着计算机的普及,Windows95、98Two generations of operating systems were a huge success,The free minesweeper game has also become one of the most popular computer games.然而,When minesweeper was a smash hit,But it was targeted by the international anti-landmine organization,I think games like this oversimplify the process of minesweeping,It is disrespectful to mine clearance and its staff.So Microsoft finally launched a flower field game,to replace the original minesweeper page,But the gameplay remains the same.然而,In the end, the packaged minesweeper game was taken off the assembly line under the arguments of the International Anti-Mine Organization,But this classicwindowsMinesweeper games have long become an important pastime in some people's lives.

Today we will simply restore this game.

一.整体设计逻辑

We use two 2D arrays,mine[ROW][LIN],和show[ROW][LIN]They are used to represent thunder disks, respectively,and display panel.Set mines in a minefield.In demining, the demining results are displayed in the storageshow[ROW][LIN]中,Print it once if it is not arranged onceshow[ROW][LIN]显示盘.Until every place without mines has been checked.

二.加载逻辑

The loading logic of the game,We want the option to start or quit the game,It's not that it's gone after one game,Instead, you can choose to continue to play or not to play after a game,Only when our election group exits,程序才会结束.A menu is also required to display options.

加载逻辑代码:

int main()
{
	int input = 0;
	do 
	{
		mnue();
		printf("请输入选项:>");
		scanf("%d", &input);
		switch (input)
		{
			case 1:
				game();
			    break;
			case 0:
				printf("退出游戏\n");
				break;
			default:  
				printf("输入错误,请重新选择\n");
				break;
		}
	} while (input);
	return 0;
}

 三.设置雷盘,and display panel

 The macro defines the ranks of the thunder disk and the display disk

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
//行
#define ROW 11
//列
#define LIN 11
//雷数
#define MINE_COUNT 10

 Two two-dimensional arrays are defined here,mine[ROW][LIN]初始化为‘0’,show[ROW][LIN]初始化为‘*’.And print thunder disk and display disk.

In order to prevent us from demining later, we need to query the Jiugongge,But at the corners each will cross the line,所以我们直接11*11的雷盘,But we just use 1~9.就不会越界了. 

void game()
{
	//设置随机数生成器
	srand((unsigned int)time(NULL));
	//Create thunder disks and display disks
	char mine[ROW][LIN] = { 0 };
	char show[ROW][LIN] = { 0 };

	//Initialize the thunder disk and display disk
	init_board(mine, ROW, LIN,'0');
	init_board(show, ROW, LIN,'*');

	//打印显示盘
	display_show(mine, ROW, LIN);
	printf("\n");
	display_show(show, ROW, LIN);


}
//初始化数组
void init_board(char board[ROW][LIN],int row,int lin,char set)
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < lin; j++)
		{
			board[i][j] = set;
		}
	}
}

//
void display_show(char board[ROW][LIN], int row, int lin)
{
    //打印行号
    
	for (int i = 0; i <= lin-2; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
    //打印数组
    //这里linRepresents columns however11行,We only use printing1~9.   
	for (int i = 1; i < row-1; i++)
	{
        //打印列号
		printf("%d ", i);
        //这里linRepresents columns however11行,We only use printing1~9
		for (int j = 1; j < lin-1; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

运行展示:

四.设置雷 

Generate random coordinates with random number settings,在1~9之间.

void set_mine(char board[ROW][LIN], int row, int lin)
{
	for (int i = 0; i < MINE_COUNT; i++)
	{
		while (1)
		{
			//32767%10
			//0----9
			int x = rand() % 9+1;  //1~9;
			int y = rand() % 9+1;
            //Only the position in the coordinates is‘0’,才可以以,防止重复
			if (board[x][y]=='0')
			{
				board[x][y] = '1';
				break;
			}
		}
	}
}
void game()
{
	//设置随机数生成器
	srand((unsigned int)time(NULL));
	//Create thunder disks and display disks
	char mine[ROW][LIN] = { 0 };
	char show[ROW][LIN] = { 0 };

	//Initialize the thunder disk and display disk
	init_board(mine, ROW, LIN,'0');
	init_board(show, ROW, LIN,'*');

	打印显示盘
	//display_show(mine, ROW, LIN);
	//printf("\n");
	//display_show(show, ROW, LIN);

	//布置雷
	set_mine(mine, ROW, LIN);
	display_show(mine, ROW, LIN);
	printf("\n");
	display_show(show, ROW, LIN);

}

运行结果:

 五.排雷

输入坐标,First check if the coordinates are mine,If it is, the game ends immediately,If not, check how many mines there are in the eight squares around him,And write the thunder number to the display disk.A respective count was not checkedwin就加一,直到win ==(row - 2) * (lin - 2) - MINE_COUNT游戏结束,排雷成功.

//Get the nine-square Gray number
int get_mine(char mine[ROW][LIN], int x, int y)
{
	int sum = 0;
	for (int i = -1; i <= 1; i++)
	{
		for (int j = -1; j <= 1; j++)
		{
            //Because there is no class in the Nine Palaces‘0’,there is thunder‘1’,
            //So just add it together,因为字符1~9Convert to a number to subtract48.
			sum += mine[x+i][y+j] - 48;
		}
	}
	return sum;
}

void find_mine(char mine[ROW][LIN],char show[ROW][LIN],int row,int lin)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < (row - 2) * (lin - 2) - MINE_COUNT)
	{
		printf("请输入需要排查的坐标:>");
		scanf("%d %d", &x, &y);
        //判断是否为雷
		if (mine[x][y] == '0')
		{
            //判断是否被排查过
			if (show[x][y] == '*')
			{
				int count = get_mine(mine, x, y);
                //Numbers to characters need to be added48
				show[x][y] = count + 48;
				display_show(show, ROW, LIN);
				win++;
			}
			else
			{
				printf("It has been checked here\n");
			}
			
		}
		else
		{
			printf("此处是雷,游戏结束!!!\n");
			display_show(mine, ROW, LIN);
			return;
		}
	}
	if (win == (row - 2) * (lin - 2) - MINE_COUNT)
	{
		printf("排雷成功,游戏通关!!!\n");
	}
}

Here is my setup for the convenience of demonstration80个雷,并且显示出来了,雷盘,Everyone can display the thunder disk when testing,以便测试,After the completion, just print the thunder disk.

 最后:

决定今天的不是今天,而是昨天对人生的态度;决定明天的不是明天,而是今天对事业的作为.我们的今天由过去决定,我们的明天由今天决定;Victory does not depend on physical strength and intelligence,Success does not depend on miracles on trajectory.Success is not about getting good cards,The key is whether you can play the bad cards in your hand well.人生最重要的是,Know where you're going.加油,诸君,山顶见!!!

copyright notice
author[ex-boyfriend in C],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/218/202208060919556779.html

Random recommended