Elementary c language: fun minesweeper game

Table of contents

foreword

 make menu

Building a Game Selection Framework

Realize the game function

Modular programming: see the content of the previous section of the three chess

Initialization minefield

​edit

Optimize the board

randomly planted mines

Post-Click Decisions

 Implement this function code

game(); Arrangement


 

foreword

"Minesweeper" is a popular puzzle game released in 1992. The goal of the game is to find out all the grids that are not landmines according to the numbers that appear on the clicked grids in the shortest time, and at the same time avoid stepping on landmines. If you step on a mine, you will lose everything.

Players need to check out all the mines one by one in the minefield: 9x9 specification

      test.c - - - - Test game logic

      game.c - - - - Game code implementation

      game.h - - - - Declaration of game code (function declaration, symbol definition)

 make menu

  When playing games, we will have menu options when we enter the game, choose to start the game, launch the game and other instructions. When it comes to selection, then we can first complete the design of the basic framework based on the loop and branch statements we have learned.

First of all, when we enter the game, we will first display the options, make a choice, and play a game while playing the game. What should we do if we want to play again (think about the structure of our previous knowledge that meets the requirement of entering the game first and then recirculating the menu) ) That must be the loop structure of do...while is more suitable, then we first use the function to print out a menu option

void menu()
{
	printf("*****************************\n");
	printf("*****************************\n");
	printf("**********1.play^************\n");
	printf("**********0.exit^************\n");//菜单
	printf("*****************************\n");
	printf("*****************************\n");
}
 
int main()
{int a = 0;
    do
 {
  menu();
  printf("请选择:");
  scanf("%d",&a);
 
 
 }while();  
return 0;
}

Building a Game Selection Framework

The game menu has been displayed on the screen, now you need to complete the selection, and play a game while playing the game, what should you do if you want to play.

At this time, it is necessary to apply the switch statement explained by the blogger before :


int main()
{
	int input = 0;
	do
	{
		menu();
		printf("PLEASE SELECT:");
		scanf("%d", &input);
		switch(input)
		{
		case 1:
			game();    //以上为界面的选择
			break;
		case 0:
			printf("Exit\n");
			break;
		default:
			printf("ERRO,PLEASE CHOOSE AGAIN\n");
			break;
		}
	} while (input);//while循环可以利用0为假,其余为来实现用户可反复选择
//直到选到合适为止
	return 0;
}              

Realize the game function

Modular programming : see the content of the previous section of the three chess


 test.c: is used to realize the game logic game.c: the function used to realize the game function game.h: used to declare the game function function (can be quoted)

Using modular programming can greatly improve the readability, maintainability, portability, etc. of the code!

This is the game.h file

#pragma once
#define _CRT_SECURE_NO_WARNINGS//使用scanf函数的报错处理方式 
#include <stdio.h>//打印函数的使用工具箱
#include<Windows.h>//颜色函数和清屏指令的工具箱
#include<stdlib.h>//在使用rand的时候需要用到srand,	srand((unsigned int)time(NULL))随机函数,调用一次就可以
#include<time.h>
#define row 9//常量
#define col 9
#define cols col+2//定义常量
#define rows row+2
void initboard(char board[rows][cols],int hang, int lie,char set);//形参数
void displayboard(char board[rows][cols], int hang, int lie);//只设置9*9格子
void setmine(char board[rows][cols], int hang, int lie);//埋入地雷
void panduan(char show[rows][cols], char mine[rows][cols],int hang, int lie);

It will be analyzed one by one later:

Used to reference in the test.c file

Initialization minefield

The first thing that catches the eye must be the 9×9 minefield, the 81 covered grids,

When we randomly click on the grid, the following two situations will appear:

① When the opened grid is a mine, the player is "dead" by the bomb, and the game ends;

② When the opened grid is not a mine, the grid will display the number of mines in the surrounding 8 grids;

From the legend, it can be concluded that to realize a 9×9 minesweeper game, it is not appropriate to create a two-dimensional array with 9 rows and 9 columns.

Since the operation on the boundary elements of a two-dimensional array with 9 rows and 9 columns will lead to array out-of-bounds access, then we simply expand the two-dimensional array by one circle, and include those ranges that will cause out-of-bounds access in the array, from It is a very clever way to solve the problem at the source!

So you need to set up two arrays (two-dimensional arrays)

(1) One is for storing mine data (used to judge whether to step on mines)

(2) A chessboard for display to the player (similar to the blue unknown square above, which will display 8 gray numbers around it when clicked)

Two arrays need to be initialized at the same time initboard subfunction

void initboard(char board[rows][cols], int hang, int lie,char set)
{
	int i = 0;

	int j = 0;

	for (i = 0; i < hang; i++)
	{
		for (j = 0; j < lie; j++)
		{
			board[i][j] = set;
		}
	}
}//initboard(mine, rows, cols, '0');//在test.c中引用的函数
//initboard(show, row, col, '*');

Optimize the board

Due to the long line number in the minefield, it is necessary to mark the serial number displayboard subfunction for each row and column

void displayboard(char board[rows][cols], int hang, int lie)
{
	int i = 0;
	int j = 0; 
		printf("---------------------------------------\n");
	for(j = 0; j <=lie; j++)
	{
		printf("%d ",j);
	}
	printf("\n");
	for (i = 1; i <=hang; i++)
	{
		printf("%d ", i);
		for (j = 1; j <=lie; j++)
		{
			printf("%c ", board[i-1][j-1]);
		}
		printf("\n");
	}
	    printf("---------------------------------------\n");
}	

 renderings

 

randomly planted mines

This should be a 9x9 minefield, so 10 mines are enough. But how to implement randomly planting mines in a suitable area?

How to generate random numbers in C language :

void setmine(char board[rows][cols], int hang, int lie)//随机埋入地雷
{
	int count = 10;
	while (count)
	{
		int x = rand() % hang ;
		int y = rand() % lie ;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
		}
		count--;
	}

}//srand((unsigned int)time(NULL));//布置雷的随机时间函数,在test.c主函数
中引用一次就够

Post-Click Decisions

How many mines around after unfolding 

In the minesweeper game, when the square we click is not a mine and there are no mines in the surrounding area, a minefield will be directly expanded. The specific effect is as shown in the figure below

 

In the minesweeper game, when the player flips a square, if the square is not a mine, the number of mines in the 8 surrounding squares will be displayed. If there are no mines in the 8 coordinates around the square, all the 9 squares will be expanded, and so on until there are mines in 8 squares around a square, and the expansion will stop. The two cases are shown in the figure below

 In order to realize this function, we need to traverse the 8 coordinates around the coordinates input by the player, and count the number of mines around the coordinates.

int get_mine(char board[rows][cols],int x, int y)
{
	return (board[x - 1][y - 1]
		+ board[x - 1][y]
		+ board[x - 1][y + 1]
		+ board[x][y + 1]
		+ board[x][y - 1]
		+ board[x + 1][y + 1]
		+ board[x + 1][y]
		+ board[x + 1][y - 1] - 8* '0');//周围8个坐标相加
}

 

 

 Implement this function code

void Mark(char board[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("\n请输入要标记的坐标>>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)       //判断玩家输入坐标是否合法
		{
			if (board[x][y] == '!')       //若坐标元素为 '!' 则表示该坐标已被标记过
			{
				printf("\n<该坐标已标记,无需重复标记,请重新输入>\n");
			}
			else       //该坐标未被标记
			{
				board[x][y] = '!';       //将 '!' 赋值给该坐标元素
				DisplayBoard(board, row, col);       //将标记地雷后的雷区展示于玩家
				break;
			}
		}
		else
		{
			printf("\n<坐标非法,请重新输入>\n\n");
		}
	}
}

game(); Arrangement

void game()
{
	char mine[rows][cols] = { 0 };//存放雷的信息
	char show[rows][cols] = { 0 };//显示给玩家的游戏界面信息
	initboard(mine, rows, cols, '0');
	initboard(show, row, col, '*');
	//displayboard(mine, row, col);
	setmine(mine, row,col);
	displayboard(show, row, col);
	panduan(show,mine, row, col);
}

Well, the above content is over today, I hope you can support me a lot!

Some of the content is borrowed from some bloggers, I hope everyone understands.

Guess you like

Origin blog.csdn.net/fcccfffg/article/details/132504711