Elementary C language - minesweeper game

        Not much to say, go directly to the code.


content

1. Create a project environment

2. test.c file

3. Create the game function

4. Create the chessboard

 5. Lay out mines

6. Troubleshoot mines

7. Final Implementation

Conclusion:


1. Create a project environment

There's not much to say here, it's the same as chess.


2. test.c file


#include "game.h"//引用自己创建的头文件,可以把stdio.h这个头文件包括在那里面

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

void game()
{
    //游戏的逻辑在game函数中去实现
}
void test()
{
	int input = 0;
	do
	{
		menu();//打印菜单,1.代表玩游戏,0.代表退出
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1://玩游戏
			game();//游戏的逻辑在game函数中去实现
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
}
int main()
{
	test();
	return 0;
}

3. Create the game function

The logic of the game is explained here, and then we can start writing code. 

So in the game.h header file, the defined global variables can be written like this

 Because not only 9, but also 11 will be used later.


4. Create the chessboard

void game()
{
	//扫雷游戏的实现
	char mine[ROWS][COLS] = { 0 };//初始化成'0'
	char show[ROWS][COLS] = { 0 };//初始化成'*'
	//初始化棋盘
	Init_board(mine, ROWS, COLS, '0');//初始化成'0'
	Init_board(show, ROWS, COLS, '*');//初始化成'*'
	//打印棋盘
	show_board(mine, ROW, COL);//虽然创建的是11*11的棋盘,但我们只想看到的是中间的9*9
	show_board(show, ROW, COL);
}
void Init_board(char arr[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++)
		{
			arr[i][j] = set;
		}
	}
}

void show_board(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("--------扫雷--------\n");
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}

Here first print two chessboards to see the effect.

So when we actually want to print the chessboard is the show array '*'. 


 5. Lay out mines

void game()
{
	//扫雷游戏的实现
	//mine是用来存放布置好的雷的信息
	//char mine[ROWS][COLS] = { 0 };//初始化成'0'
	//show是用来存放排查出的雷的信息
	//char show[ROWS][COLS] = { 0 };//初始化成'*'
	//初始化棋盘
	//Init_board(mine, ROWS, COLS, '0');
	//Init_board(show, ROWS, COLS, '*');
	//打印棋盘
	//show_board(mine, ROW, COL);//虽然创建的是11*11的棋盘,但我们只想看到的是中间的9*9
	
    //布置雷
	set_mine(mine, ROW, COL);
}
void set_mine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	int x = 0;
	int y = 0;
	while (count)
	{
		//这里还是11*11的数组,生成的随机数范围是0~9,加1就可以变成1~10,正好对应需要的中间的9*9的数组
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';//布置雷
		}
		count--;
	}
}

        Random numbers are used here again, so the same routine, the header file, srand( ( unsigned int )time(NULL));. '1' is a randomly generated mine.

       The above picture is just to see the location of the generated mine, but when you actually play the game, you can't see anything, so you need to change the mine in the show_board function under the // layout mine to show, and it will be changed after the change. becomes the chessboard below.


6. Troubleshoot mines

void game()
{
	//扫雷游戏的实现
	//mine是用来存放布置好的雷的信息
	//char mine[ROWS][COLS] = { 0 };//初始化成'0'
	//show是用来存放排查出的雷的信息
	//char show[ROWS][COLS] = { 0 };//初始化成'*'
	//初始化棋盘
	//Init_board(mine, ROWS, COLS, '0');
	//Init_board(show, ROWS, COLS, '*');
	//打印棋盘
	//show_board(mine, ROW, COL);//虽然创建的是11*11的棋盘,但我们只想看到的是中间的9*9
	//布置雷
	//set_mine(mine, ROW, COL);
	//show_board(show, ROW, COL);
	
    //排查雷
	find_mine(mine, show, ROW, COL);
}
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
	return (mine[x - 1][y] +//这里解释一下,如果周围的坐标被放置了雷,该坐标就会被设置成字符'1',不是雷就还是'0',字符1和字符0的ASCII码值相差1,所以返回值就是周围雷的个数
		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 find_mine(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 - EASY_COUNT)//10个雷,那就剩下71个位置,把71个位置都排完,就是排雷成功
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,被炸死了\n");
				show_board(mine, ROW, COL);
				break;
			}
			else
			{
				int count = get_mine_count(mine, x, y);
				show[x][y] = count + '0';
				show_board(show, ROW, COL);
				win++;//每排完一个位置就减少一个需要排的位置
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,排雷成功\n");
		show_board(mine, ROW, COL);
	}
}

7. Final Implementation

All code attached:

game.h file:

#pragma once

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

#define ROW 9
#define COL 9

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

#define EASY_COUNT 10

//初始化棋盘
void Init_board(char arr[ROWS][COLS], int rows, int cols, char set);

//打印棋盘
void show_board(char arr[ROWS][COLS], int row, int col);

//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col);

//排查雷
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

game.c file:

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

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

void show_board(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("--------扫雷--------\n");
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}

void set_mine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASY_COUNT;
	int x = 0;
	int y = 0;
	while (count)
	{
		//这里还是11*11的数组,生成的随机数范围是0~9,加1就可以变成1~10,正好对应需要的中间的9*9的数组
		x = rand() % row + 1;
		y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';//布置雷
			count--;
		}
	}
}

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 find_mine(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 - EASY_COUNT)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,被炸死了\n");
				show_board(mine, ROW, COL);
				break;
			}
			else
			{
				int count = get_mine_count(mine, x, y);
				show[x][y] = count + '0';
				show_board(show, ROW, COL);
				win++;
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你,排雷成功\n");
		show_board(mine, ROW, COL);
	}
}

test.c file:

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"//引用自己创建的头文件,可以把stdio.h这个头文件包括在那里面

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

void game()
{
	//扫雷游戏的实现
	//mine是用来存放布置好的雷的信息
	char mine[ROWS][COLS] = { 0 };//初始化成'0'
	//show是用来存放排查出的雷的信息
	char show[ROWS][COLS] = { 0 };//初始化成'*'

	//初始化棋盘
	Init_board(mine, ROWS, COLS, '0');
	Init_board(show, ROWS, COLS, '*');
	//打印棋盘
	//show_board(mine, ROW, COL);//虽然创建的是11*11的棋盘,但我们只想看到的是中间的9*9
	//布置雷
	set_mine(mine, ROW, COL);
	show_board(show, ROW, COL);
	//排查雷
	find_mine(mine, show, ROW, COL);
}
void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();//打印菜单,1.代表玩游戏,0.代表退出
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1://玩游戏
			game();//游戏的逻辑在game函数中去实现
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
}
int main()
{
	test();
	return 0;
}

Conclusion:

       Then these two games have been written, and it feels very good. I only use the relevant knowledge of C language, and have not used the more complicated ones, so as long as you work hard, you can write more beautiful code.

Guess you like

Origin blog.csdn.net/m0_64607843/article/details/122652624