Realize primary minesweeper

Through the learning of two-dimensional arrays and some knowledge previously learned, the primary minesweeper (9×9) mini-game is realized.

                                                                                                           
 

 the whole idea

menu menu (one is not enough, another one)
Use a two-dimensional array to create two (11×11) chessboards, so why not (9×9), the following code has an explanation.
initboard initializes the chessboard ( char/int )
4 diaplayboard prints the chessboard
5 ​​​​​​​setmine arranges mines
6 findmine counts the number of mines around the
function implementation in game.c, and function declaration in game.h. Function calls and partial function implementations are performed in test.c.
Define macros ROW, COL, ROWS, COLS for easy calculation.

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


1 Implementation of the menu

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"
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("请选择》\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误\n");
			break;
		}//switch用不惯的也可以用多条件if语句


	} while (input);
}

2.1  The first chessboard
is used to store the information of laying mines, mines are '1', not mines are '0', why should the characters '1' and '0' be placed? Let's look down

2.2  The second chessboard
Every time the player takes a step, it is used to display information about the number of mines around. Put "*" in the coordinates of the mines that have not been rowed, and the number of mines around them will be displayed if the mines have been rowed.

2.3  Chessboard selection ( char )
1 and 0 are originally integers, we can consider using int type to create, but since the chessboard will be printed with a function later, we will use char as a whole. 1 and 0 are replaced by '0' and '1'.

 

 

Connected to the above chessboard creation
When the above situation occurs, we will generate out-of-bounds access when calculating the number of surrounding mines. (11×11) can avoid this problem.

3 Initialize the board 

Select char type to initialize the chessboard, a function can print two chessboards

//test.c
initboard(mine, ROWS, COLS, '0');
initboard(show, ROWS, COLS, '*');
//game.c中的函数实现
void initboard(char board[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++)
		{
			board[i][j]=set;
		}
	}
}

 4 Print the chessboard

//test.c
displayboard(mine, ROW, COL);//这里我们打印棋盘是(9*9)
displayboard(show, ROW, COL);
//game.c
void displayboard(char board[ROWS][COLS], int row, int col)
{
	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");
	}
}

 5 Place mines (random)

//game.h
#include<time.h>
#include<stdlib.h>

srand((unsigned int)time(NULL));//test.c
//game.c
void setmine(char mine[ROWS][COLS], int row, int col)
{
	int count = 10;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}

	}
}

 After laying out the thunder, we can call the displayboard function to print the chessboard for viewing.

6 Find mines ('0' and '1' only need to convert the surrounding numbers into integer numbers and add them when calculating the number of surrounding mines)

void findmine(char mine[ROWS][COLS], char show[ROWS][COLS],int row,int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-10)
	{
		printf("请输入要排查的坐标");
		scanf("%d %d", &x, &y);
		if (x > 0 && x <= row&&y > 0 && y <= col)
		{
			if (mine[x][y] != '1')
			{
				int m =getmine(mine,x,y);//用来计算周围雷的数量
				show[x][y] = m+'0';//整型数字转换成字符数字
				displayboard(show, ROW, COL);
				win++;
			}
			else
			{
				printf("您被发往一趟去西天取经的航班,祝你旅途愉快\n");
				displayboard(mine, ROW, COL);
				break;
			}
		}
		else
		{
			printf("输入坐标非法,请重新输入\n");
		}
	}
	if (win == row*col - 10)
	{
		printf("你有当工兵的好天赋\n");
		displayboard(mine, ROW, COL);
	}
}

 getmine

int getmine(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';
//这里返回的是int类型,将字符数字转化为整型数字(减去字符0):eg:'1'-'0'=1(感兴趣的同学可以去查查ascall码表)
}

 Complete code display

game.h

#pragma once
#include<stdio.h>
#define ROW 9
#define COL 9
#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 mind[ROWS][COLS], int row, int col);

void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);


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

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;
	int j = 0;
	
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j]=set;
		}
	}
}
void setmine(char mine[ROWS][COLS], int row, int col)
{
	int count = 10;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}

	}
}
int getmine(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';
//这里返回的是int类型,将字符数字转化为整型数字(减去字符0):eg:'1'-'0'=1(感兴趣的同学可以去查查ascall码表)
}
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS],int row,int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win<row*col-10)
	{
		printf("请输入要排查的坐标");
		scanf("%d %d", &x, &y);
		if (x > 0 && x <= row&&y > 0 && y <= col)
		{
			if (mine[x][y] != '1')
			{
				int m =getmine(mine,x,y);
				show[x][y] = m+'0';
				displayboard(show, ROW, COL);
				win++;
			}
			else
			{
				printf("您被发往一趟去西天取经的航班,祝你旅途愉快\n");
				displayboard(mine, ROW, COL);
				break;
			}
		}
		else
		{
			printf("输入坐标非法,请重新输入\n");
		}
	}
	if (win == row*col - 10)
	{
		printf("你有当工兵的好天赋\n");
		displayboard(mine, ROW, COL);
	}
}


void displayboard(char board[ROWS][COLS], int row, int col)
{
	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");
	}
}

 

test.c

#define _CRT_SECURE_NO_WARNINGS 1

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

}
void game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	//初始化棋盘
	initboard(mine, ROWS, COLS, '0');
	initboard(show, ROWS, COLS, '*');
	setmine(mine, ROW, COL);
	displayboard(mine, ROW, COL);
	displayboard(show, ROW, COL);

	
	findmine(mine, show, ROW, COL);
	
	
}

void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	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;
}

This is the end of today's content. If you think it is helpful to you, please like and follow. This is also my first blog. Your support will be my biggest motivation. Finally, thank you for watching. Let's See you next time.

                                                        

Guess you like

Origin blog.csdn.net/weixin_63451038/article/details/121479206
Recommended