C language to implement minesweeping (with source code)

foreword

Minesweeper is the same as backgammon, which can exercise our logical analysis ability well, and better understand and apply the knowledge we have learned ( arrays, loops and branches, etc. )

game rules

I believe that many people have been exposed to this game in elementary school, but the subject also knows how to play it recently, let us look at the rules of the game

Click the square with the mouse, if it is a mine, the game will fail;
if it is not a mine, the number of mines in the adjacent 8 grids will be counted

Realization of the game

Need to use, two .c files and one .h file
insert image description here

The minesweeper file contains our main calling function, the game function contains the main part of implementing the game, the header file is used to store function declarations, header file inclusion and macro definitions

This is clear and clear, laying a certain foundation for future multi-person cooperation

calling function

After many times of use, I believe you already know this part well

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"//使用时记得包含头文件,用“”’。
menu()
{
    
    
	printf("******************************\n");
	printf("***********1.play ************\n");
	printf("******************************\n");
	printf("***********0.exit ************\n");
	printf("******************************\n");

}
game()
{
    
    
   
}
int main()
{
    
    
	srand((unsigned int)time(NULL));
	int n;
	do
	{
    
    
		menu();
		printf("请输入你的选择\n");
		scanf("%d", &n);
		switch(n)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("你已退出游戏\n");
			break;
		default :
			printf("输入错误\n");
			break;
		}
	} while (n);
	return 0;
}

design of game()

This part is also the core of the game
and is mainly divided into three parts

1. Creation, initialization and display of chessboard
2. Arrangement of mines
3. Players input mines

1. Chessboard

(1) Creation of the chessboard

The chessboard needs a two-dimensional array, one array is used to store data, and the other is used to display.
Therefore, to create it in game(),
first define a macro (in the game header file)
so that our chessboard can be customized in size, without dragging move the whole body

#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2

We plan to make a 9*9 chessboard
, so create it in game()

    char board_mine[ROWS][COLS] = {
    
     0 };
	char board_show[ROWS][COLS] = {
    
     0 };

There will definitely be friends wondering
why ROWS COLS is used?

insert image description here

(2) Chessboard initialization
is created in game()

init_board(board_mine, board_show, ROWS, COLS)

Set the function in the game.c file

void init_board(char board_mine[ROWS][COLS], char board_show[ROWS][COLS], int rows, int cols)
{
    
    
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
    
    
		for (j = 0; j < cols; j++)
		{
    
    
			board_mine[i][j] = '0';
			board_show[i][j] = '*';

		}
	}
}

(3) Print the chessboard
created in game()

display_board(board_show, ROW, COL);

Set the function in the game.c file

void display_board(char board[ROWS][COLS], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	printf("--------扫雷--------\n");
	for (i = 0; i <= row; 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");
	}
	printf("--------扫雷--------\n");

}

2. Arrangement of Ray

Created in game()

set_board(board_mine, ROW, COL);

Set in the game.c file

EASY is defined by #define, the number of mines used to arrange is carried out in the game header file, and
the seed of the random number is in the main calling function

	srand((unsigned int)time(NULL));//随机数种子
void set_board(char board_mine[ROWS][COLS], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	
	int count = EASY;
	while (count)
	{
    
    
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board_mine[x][y] == '0')
		{
    
    
			board_mine[x][y] = '1';
			count--;
		}
	}
}

3. Check mine

Created in game()

find_mine(board_mine, board_show, ROW, COL);

implemented in game.c

int cal(char board_mine[ROWS][COLS], int x, int y)
{
    
    
	return board_mine[x + 1][y - 1] +
		board_mine[x - 1][y - 1] + 
		board_mine[x][y - 1] + 
		board_mine[x + 1][y] + 
		board_mine[x - 1][y]
		+board_mine[x + 1][y + 1] + 
		board_mine[x][y + 1] + 
		board_mine[x - 1][y + 1] - 8 * '0';
}
void find_mine(char board_mine[ROWS][COLS], char board_show[ROWS][COLS], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-EASY)
	{
    
    
		printf("输入要排查的坐标\n");
		scanf("%d %d", &x, &y);
		if (x <= row && x >= 1 && y <= col && y >= 1)
		{
    
    
			if (board_mine[x][y] == '1')
			{
    
    
				printf("你被炸死了\n");
				display_board(board_mine, ROW, COL);
				break;
			}
			else 
			{
    
    
				int ret = cal(board_mine, x, y);
				board_show[x][y] = ret+'0';
				display_board(board_show, ROW, COL);
				win++;
			}
		}
		else
		{
    
    
			printf("坐标非法,重新输入\n");
		}
		
	}
	if (win == row * col - EASY)
	{
    
    
		printf("恭喜你,排雷成功\n");
		display_board(board_mine, ROW, COL);
	}
}

source code

calling function

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
menu()
{
    
    
	printf("******************************\n");
	printf("***********1.play ************\n");
	printf("******************************\n");
	printf("***********0.exit ************\n");
	printf("******************************\n");

}
game()
{
    
    
    //棋盘的创建
	char board_mine[ROWS][COLS] = {
    
     0 };
	char board_show[ROWS][COLS] = {
    
     0 };
	//棋盘初始化
	init_board(board_mine, board_show, ROWS, COLS);
	//展示棋盘
	/*display_board(board_mine, ROW, COL);*/
	display_board(board_show, ROW, COL);
	//布置雷
	set_board(board_mine, ROW, COL);
	/*display_board(board_mine, ROW, COL);*/
	//排查雷
	find_mine(board_mine, board_show, ROW, COL);

}
int main()
{
    
    
	srand((unsigned int)time(NULL));
	int n;
	do
	{
    
    
		menu();
		printf("请输入你的选择\n");
		scanf("%d", &n);
		switch(n)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("你已退出游戏\n");
			break;
		default :
			printf("输入错误\n");
			break;
		}
	} while (n);
	return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"
void init_board(char board_mine[ROWS][COLS], char board_show[ROWS][COLS], int rows, int cols)
{
    
    
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
    
    
		for (j = 0; j < cols; j++)
		{
    
    
			board_mine[i][j] = '0';
			board_show[i][j] = '*';

		}
	}
}

void display_board(char board[ROWS][COLS], int row, int col)
{
    
    
	int i = 0;
	int j = 0;
	printf("--------扫雷--------\n");
	for (i = 0; i <= row; 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");
	}
	printf("--------扫雷--------\n");

}

void set_board(char board_mine[ROWS][COLS], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	
	int count = EASY;
	while (count)
	{
    
    
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board_mine[x][y] == '0')
		{
    
    
			board_mine[x][y] = '1';
			count--;
		}
	}
}
int cal(char board_mine[ROWS][COLS], int x, int y)
{
    
    
	return board_mine[x + 1][y - 1] +
		board_mine[x - 1][y - 1] + 
		board_mine[x][y - 1] + 
		board_mine[x + 1][y] + 
		board_mine[x - 1][y]
		+board_mine[x + 1][y + 1] + 
		board_mine[x][y + 1] + 
		board_mine[x - 1][y + 1] - 8 * '0';
}
void find_mine(char board_mine[ROWS][COLS], char board_show[ROWS][COLS], int row, int col)
{
    
    
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-EASY)
	{
    
    
		printf("输入要排查的坐标\n");
		scanf("%d %d", &x, &y);
		if (x <= row && x >= 1 && y <= col && y >= 1)
		{
    
    
			if (board_mine[x][y] == '1')
			{
    
    
				printf("你被炸死了\n");
				display_board(board_mine, ROW, COL);
				break;
			}
			else 
			{
    
    
				int ret = cal(board_mine, x, y);
				board_show[x][y] = ret+'0';
				display_board(board_show, ROW, COL);
				win++;
			}
		}
		else
		{
    
    
			printf("坐标非法,重新输入\n");
		}
		
	}
	if (win == row * col - EASY)
	{
    
    
		printf("恭喜你,排雷成功\n");
		display_board(board_mine, ROW, COL);
	}
}

game.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define EASY 10

#define ROW 9
#define COL 9
#define ROWS 11
#define COLS 11
void init_board(char board_mine[ROWS][COLS], char board_show[ROWS][COLS], int rows, int cols);
void display_board(char board[ROWS][COLS], int row, int col);
void set_board(char board_mine[ROWS][COLS], int row, int col);
void find_mine(char board_mine[ROWS][COLS], char board_show[ROWS][COLS], int row, int col);

I believe that after reading the article, you will gain something, let's make progress together

Guess you like

Origin blog.csdn.net/2301_78636079/article/details/132003263