Using C Language to Realize Minesweeper

Inspirational session

We should dare to try, dare to work silently on this lonely and hesitant road.


Table of contents

1 Enter the game, exit the game and the menu section

2 ideas

3 codes

1 Enter the game, exit the game and the menu section

Code display:

#include <stdio.h>

void game()
{
	printf("扫雷\n");
}

void menu()
{
	printf("********************\n");
	printf("****** 1.play ******\n");
	printf("****** 0.exit ******\n");
	printf("********************\n");
}

void test()
{
	int input = 0;
	do
	{
		menu();
		printf("请选择:->\n");
		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;
}

Knowledge points: 

(1) As long as we don't exit the game, we can continue to play, so use the do...while structure

(2) Pay attention to break, don’t forget to write

(3) For the sake of beauty and convenience, \n is used in many places, pay attention

2 ideas

Minesweeper game: First of all, there is a fixed number of mines in it. When you click a grid randomly, the number of mines in the surrounding 8 grids will be displayed. When the player clicks a grid with mines, you will lose the game. For example: There are 10 mines in a 9*9 grid.

(1) 9*9 grid, use a two-dimensional array.

(1) Arrange the positions of 10 mines. If the int type 0 and 1 are used to indicate whether there are mines, then when a grid is clicked, when there are no mines around and there is a mine (0 and 1 should be displayed in the grid), a conflict will occur at this time.

Therefore, using the char type, you can arrange the mine information on one chessboard, and arrange the detected information on one chessboard.

The first board (mine): store mine

The second checkerboard (show): Put '*' in the unchecked position, and display the corresponding number in the checked position.

Check the grid at the edge position, and the checked area is out of bounds, so change the array to 11*11, and add a line at the top, bottom, left, and right. char board[11][11]

(3) Number + '0' to convert the number into a character

3 codes

game.h code display

#pragma once

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2
#define easycount 10

//初始化棋盘
void Initboard(char board[ROWS][COLS], int rows, int cols, char set);//两个数组 mine show 都可以应用
//打印棋盘
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);

game.c code display

#include "game.h"

void Initboard(char mine[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++)
		{
			mine[i][j] = set;
		}
	}
}

void displayboard(char board[ROWS][COLS], int row, int col)
{
	//打印中间9*9
	int i = 0;
	int j = 0;
	//打印列号
	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");
	}
}
//布置雷
void Setmine(char board[ROWS][COLS], int row, int col)
{
	int count = easycount;
	while (count)
	{
		int x = (rand() % row) + 1;
		int y = (rand() % col) + 1;
		if (board[x][y] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + 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 win = 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
			{
				//计算周围有多少个雷
				int n = get_mine_count(mine, x, y);
				show[x][y] = n + '0';
				displayboard(show, row, col);
				win++;
			
			}
		}
		else
		{
			printf("输入坐标违法,请重新输入!\n");
		}
		if (win == row * col - easycount)
		{
			printf("恭喜你,成功了!\n");
			break;
		}

	}
}

test.c code display

#include <stdio.h>
#include "game.h"

void game()
{
	//创建数组
	char mine[ROWS][COLS] = { 0 };//存放布置好雷的信息
	char show[ROWS][COLS] = { 0 };//存放排查出雷的信息

	//初始化mine数组全为'0'
	Initboard(mine, ROWS, COLS,'0');
	//初始化show数组全为'*'
	Initboard(show,ROWS, COLS, '*');
	//打印数据
	//displayboard(show, ROW, COL);//打印9*9
	//displayboard(mine, ROW, COL);
	//布置雷
	Setmine(mine, ROW,  COL);
	displayboard(show, ROW, COL);//显示show
	//排雷
	Findmine(mine, show, ROW, COL);
}

void menu()
{
	printf("********************\n");
	printf("****** 1.play ******\n");
	printf("****** 0.exit ******\n");
	printf("********************\n");
}

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;
}

 

 

Guess you like

Origin blog.csdn.net/m0_57388581/article/details/125794463