[C language] Implementing simple mine sweeping (imitation of mine sweeping under Windows)

background

        Personally, I like to play minesweeper, and I happen to be learning the C language, so I want to use my weak C language level to implement a simple minesweeper game. One is to improve your programming level, and the other is to get a different sense of achievement by writing your favorite games.

frame

        The realization of mine clearance is different from the three pieces of chess I wrote before. The function of mine clearance is more complex and more logical.

        I'll start by listing all the modules I can think of:

               Game Implementation Function Menu Initialize Minefield Place Minefield Print Minefield Count Remaining Blocks in Minefield    

               No-mine square expansion function Novice protection function

       Here I have written the outline, and then start to write the supplementary function content.

running result

                                  

        

        But you must have seen it, this small game is only suitable for display, not suitable for playing. (because it's too eye-catching)

       (Also, friends who are interested in improving, please pay attention to my next improvement in minesweeping. I will eventually implement this simple version of minesweeping as a classic minesweeping style under the windows system.)

source code

        The following is the code I wrote. If there are any deficiencies, you are very welcome to point out.
                        (If there is something you don't understand, please ask questions in the comment area)
       

        head File

#ifndef __SAOLEI_H__
#define __SAOLEI_H__
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

#define ROW 10
#define COL 10

void game(void); //The main body of the game

void menu(void); //Menu

void set_mine(char mine[ROW + 2][COL + 2], int row, int col, int num);  //布雷

int mine_num(char mine[ROW + 2][COL + 2], int x, int y); //Count the number of mines around a certain location

int mine_count(char board[ROW + 2][COL + 2], int row, int col); //Count the number of remaining *

void spread_board(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y); //Expand without minefield

void print_board(char board[ROW + 2][COL + 2], int row, int col); //print minefield

void new_help(char mine[ROW + 2][COL + 2], int x, int y); //novice protection (the first mine sweep will not be killed)

#endif

        function function

#define _CRT_SECURE_NO_WARNINGS

#include "saolei.h"
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

#define ROW 10
#define COL 10

void print_board(char board[ROW + 2][COL + 2], int row, int col) //print minefield
{
	printf("\n");
	int i = 0;
	printf(" 0  1 2 3 4 5 6 7 8 9 10\n");
	for (i = 1; i < (row - 1); i++)
	{
		int j = 0;
		printf("%2d ", i);
		for (j = 1; j < (col - 1); j++)
		{
			printf("%2c", board[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

int mine_count(char board[ROW + 2][COL + 2], int row, int col) // count the number of remaining *
{
	int count = 0;
	for (int i = 1; i < (row - 1); i++)
	{
		for (int j = 1; j < (col - 1); j++)
		{
			if (board[i][j] == '*')
				count++;
		}
	}
	return count;
}

int mine_num(char mine[ROW + 2][COL + 2], int x, int y) //Count the number of mines around a certain location
{
	return ((mine[x - 1][y - 1] - '0') + (mine[x - 1][y] - '0')
		+ (mine[x - 1][y + 1] - '0') + (mine[x][y + 1] - '0')
		+ (mine[x + 1][y + 1] - '0') + (mine[x + 1][y] - '0')
		+ (mine[x + 1][y - 1] - '0') + (mine[x][y - 1] - '0'));
}


void spread_board(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y) //Expand without minefield
{
	if ((1 <= x && x <= 10) && (1 <= y && y <= 10) && (mine[x][y] != '1') && (board[x][y] == '*'))
	{
		if (mine_num(mine, x, y) > 0)
		{
			board[x][y] = mine_num(mine, x, y) + '0';
		}
		else
		{
			board[x][y] = ' ';
			spread_board(board, mine, x - 1, y - 1);
			spread_board(board, mine, x - 1, y);
			spread_board(board, mine, x - 1, y + 1);
			spread_board(board, mine, x, y + 1);
			spread_board(board, mine, x + 1, y + 1);
			spread_board(board, mine, x + 1, y);
			spread_board(board, mine, x + 1, y - 1);
			spread_board(board, mine, x, y - 1);
		}
	}
}

void set_mine(char mine[ROW + 2][COL + 2], int row, int col, int num)  //布雷
{
	int i = 0;
	srand((unsigned)time(NULL));
	for (i = 0; i < num; i++)
	{
		int x = rand() % (row - 2) + 1;
		int y = rand() % (col - 2) + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
		}
		else
		{
			i--;
		}
	}
}

void new_help(char mine[ROW+2][COL+2], char board[ROW+2][COL+2]) //novice protection (the first mine sweep will not be killed)
{
	int x = 0;
	int y = 0;

	srand((unsigned)time(NULL));
	
	while (1)
	{
		//Enter the mine clearance coordinates (while judging the legality of the coordinates)
		printf("Please enter the coordinates (X,Y):>");
		scanf("%d,%d", &x, &y);

		if ((1 <= x && x <= 10) && (1 <= y && y <= 10))
		{
			if (mine[x][y] == '1')
			{
				mine[x][y] = '0';
				while (1)
				{
					int m = rand() % ROW + 1;
					int n = rand() % COL + 1;
					if ((m != x) && (n != y) && (mine[m][n] == '0'))
					{
						mine[m][n] = '1';
						spread_board(board, mine, x, y);
						print_board(board, (ROW + 2), (COL + 2));
						break;
					}
				}
			}
			else
			{
				spread_board(board, mine, x, y);
				print_board(board, (ROW + 2), (COL + 2));
			}
			break;
		}
		else
		{
			printf("The coordinates you entered may point to other galaxies.\n\n");
		}
	}
}

void menu(void) //menu
{
	printf("*********************\n");
	printf("** 1. Start the game**\n");
	printf("** 0. Exit the game**\n");
	printf("*********************\n");
	printf("Please select the action to be performed:>");
}

void game(void) //The main body of the game
{
	int x = 0;
	int y = 0;
	int count = 0; //Define the number of remaining mines
	int num = 0;
	//Define minefield, fog area
	char mine[ROW + 2][COL + 2];
	char board[ROW + 2][COL + 2];

	//Initialize minefield, fog area
	memset(mine, '0', sizeof(mine));
	memset(board, '*', sizeof(board));
	// Bray
	printf("Please enter the number of mines (recommended 10~30) : >");
	scanf("%d", &num);
	set_mine(mine, (ROW + 2), (COL + 2), num);
	// print fog area
	print_board(board, (ROW + 2), (COL + 2));
	// Novice protection
	new_help(mine, board);

	while (1)
	{
		// determine whether to win
		count = mine_count(board, (ROW + 2), (COL + 2));
		if (count == num)
		{
			printf("Congratulations, you successfully cleared all the mines!\n\n");
			break;
		}
		//Enter the mine clearance coordinates (while judging the legality of the coordinates)
		printf("Please enter the coordinates (X,Y):>");
		scanf("%d,%d", &x, &y);

		if ((1 <= x && x <= 10) && (1 <= y && y <= 10))
		{
			if (mine[x][y] == '1')
			{
				printf("Oh! You have stepped on the thunder, keep trying!!!\n\n");
				break;
			}
			else
			{
				spread_board(board, mine, x, y);
				print_board(board, (ROW + 2), (COL + 2));
			}
		}
		else
		{
			printf("The coordinates you entered may point to other galaxies.\n");
		}
	}
}

        main function

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "saolei.h"


intmain()
{
	int input = 0;
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1: game();
			break;
		case 0: printf("\nExit the game.\n\n");
			break;
		default: printf("\nIncorrect input!\n\nPlease re-enter.\n\n");
			break;
		}
	} while (input);
	return 0;
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325562678&siteId=291194637