How to implement minesweeper only with language?

Table of contents

Ideas for game design:

Implementation of the code:

1. Entering the game is printing the menu:

code:

2. Array creation and initialization:

code:

3. Printing of the chessboard

code:

4. Arrange mines (here is simple, use time stamps, the computer actually arranges 10 randomly, the character '1' in the running result as shown in the figure below)

code:

5. Realization of demining

code:

Full code:

1. Declaration of game functions

Game.h file

2. Realization of game functions

Game.c file

3. Game code implementation

test.c file


Ideas for game design :

As shown in the minesweeper board of 9*9

First of all, we can use a two-dimensional array to store the data, and distinguish it by the difference between the data stored by mine and non mine (here I use the characters '1' and '0' )

For convenience, we use two two-dimensional arrays, one is used to arrange mines, and the other is used to show players

Here we choose to use a two-dimensional array of  char type

In order to prevent the range of the array from being exceeded when counting the number of mines around, for example, when counting the position of coordinates (1, 1), the upper and left sides are out of bounds, so we can use a two-dimensional array of 11*11, and then give the player Display a 9*9 chessboard.



Implementation of the code:

(The front is the implementation of part of the code, and the complete code is at the end)

1. Entering the game is printing the menu:

code :

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

int main()
{
	srand((unsigned int)time(NULL));
	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);

	return 0;
}



2. Array creation and initialization:

code:

//存放布置好的的雷
char Mine[ROWS][COLS];

//存放排查好的雷
char Show[ROWS][COLS];

//初始化棋盘
//Mine 最开始全部放'0'
//Show 最开始全部放'*'
InitBoard(Mine, ROWS, COLS,'0');
InitBoard(Show, ROWS, COLS,'*');


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


3. Printing of the chessboard

code:

//打印棋盘
void DisplayBoard(char Board[ROWS][COLS], int row, int col)
{
	int i = 0;
	printf("------扫雷游戏-----\n");
	for (i = 0; i <= col; ++i)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ",i);
		int j = 0;

		for (j = 1; j <= col; j++)
		{
			printf("%c ",Board[i][j]);
		}
		printf("\n");
	}
}



4. Arrange mines (here is simple, use time stamps, the computer actually arranges 10 randomly, the character '1' in the running result as shown in the figure below)

code:

//布置雷
void SetMine(char Board[ROWS][COLS], int row, int col)
{
	int count = MineCount;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;

		if (Board[x][y] == '0')
		{
			Board[x][y] = '1';
			count--;
		}
	}
}



5. Realization of demining

code:

//排雷

int CountMine(char Mine[ROWS][COLS],int x,int y)
{
	return (Mine[x-1][y-1]+ Mine[x-1][y]+ Mine[x-1][y+1]+ Mine[x][y-1]
		+Mine[x][y+1]+ Mine[x+1][y-1]+Mine[x+1][y]+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 = ROW*COL-MineCount;

	while (Win>0)
	{
		printf("请输入你要排查雷的坐标:>\n");
		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 count = CountMine(Mine, x, y);
				Show[x][y] = count + '0';
				DisplayBoard(Show, ROW, COL);
				Win--;
			}
		}
		else
		{
			printf("输入坐标已超出棋盘,请吃重新输入\n");
		}
	}
	if (Win == 0)
	{
		printf("恭喜你,赢了\n");
		DisplayBoard(Mine, ROW, COL);
	}

}



Full code:

1. Declaration of game functions

Game.h file

//Game.h文件



#pragma once

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

#define ROW 9
#define COL 9
#define MineCount 10

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

void InitBoard(char Board[ROWS][COLS],int rows,int cols,char set);

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

2. Realization of game functions

Game.c file

//Game.c文件



#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//初始化棋盘
void 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)
{
	int i = 0;
	printf("------扫雷游戏-----\n");
	for (i = 0; i <= col; ++i)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ",i);
		int j = 0;

		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 = MineCount;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;

		if (Board[x][y] == '0')
		{
			Board[x][y] = '1';
			count--;
		}
	}
}
//排雷

int CountMine(char Mine[ROWS][COLS],int x,int y)
{
	return (Mine[x-1][y-1]+ Mine[x-1][y]+ Mine[x-1][y+1]+ Mine[x][y-1]
		+Mine[x][y+1]+ Mine[x+1][y-1]+Mine[x+1][y]+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 = ROW*COL-MineCount;

	while (Win>0)
	{
		printf("请输入你要排查雷的坐标:>\n");
		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 count = CountMine(Mine, x, y);
				Show[x][y] = count + '0';
				DisplayBoard(Show, ROW, COL);
				Win--;
			}
		}
		else
		{
			printf("输入坐标已超出棋盘,请吃重新输入\n");
		}
	}
	if (Win == 0)
	{
		printf("恭喜你,赢了\n");
		DisplayBoard(Mine, ROW, COL);
	}

3. Game code implementation

test.c file

//test.c文件


#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

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

void game()
{
	//存放布置好的的雷
	char Mine[ROWS][COLS];

	//存放排查好的雷
	char Show[ROWS][COLS];

	//初始化棋盘
	//Mine 最开始全部放'0'
	//Show 最开始全部放'*'
	InitBoard(Mine, ROWS, COLS, '0');
	InitBoard(Show, ROWS, COLS, '*');

	//打印棋盘
	DisplayBoard(Mine, ROW, COL);
	DisplayBoard(Show, ROW, COL);

	//布置雷,有雷放'1'
	SetMine(Mine,ROW,COL);
	DisplayBoard(Mine, ROW, COL);
	
	排雷
	//FindMine(Mine,Show,ROW,COL);

}

int main()
{
	srand((unsigned int)time(NULL));
	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);

	return 0;
}

Guess you like

Origin blog.csdn.net/2301_77509762/article/details/130601898