Detailed explanation of the minesweeping process, how to distribute it, and the code and project have been uploaded to gitee

Analysis on the process of making simple mine sweeping

This chapter specifically talks about ideas and processes

1, Document composition

1. test.c

It is used to design the basic framework of the main functions of the game, including menu, game board, player operation and computer operation

It does not include how to implement this function

2. game.c

It is used for the realization of game functions. How to realize the functions in the framework is basically a function

3. game.h

Contains library functions or custom functions that need to be referenced to implement the statements in game.h and test.c

2, Implementation step analysis

1. Game menu

Just entering the game interface, you need to choose whether to start the game or exit the game. If you want to play the game repeatedly, it must be a cycle. In the selection phase, we use the switch statement to select. The judgment of while cycle exit is the player's choice

code:

void menu()
{
printf("********** 1: Start the game *********");
printf("********** 0: Exit the game *********");
printf("Please enter your choice:");
}

void game()
{

}

int main()
{
int input = 0;

do
{
  menu();//Print menu
  scanf("%d\n", &input);//Player selection

  switch (input)
  {
      case 1:
          game();//Enter the game
      case 0:
          break;//Exit the game
      default: 
          printf("Please enter the correct number!\n"); //Enter other characters
          break;
  }
} while (input == 0);

return 0;
}

2. Prepare the chessboard

Take the chessboard in 9x9 format as an example, we need to place 9 mines at random

Considerations:

Because you need to calculate how many mines are around to prevent the array from crossing the boundary, all four sides must be + 1, so the size of the chessboard is a two-dimensional array of 11x11

After replacing the number of mines, the next time you want to calculate the number of mines around the nearby position, the number of mines around will be calculated last time, so the chessboard for whether there are mines and the chessboard for calculating how many mines around cannot be on the same chessboard

First prepare two chessboards with only a single symbol, so that both chessboards can use this function

code:

void prepare_pan(char pan[ROWS][COLS], int row, int col, char set)//Initialize chessboard
{
	int i = 0;
	int j = 0;

	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			pan[i][j] = set;//First set all to a single symbol, and then set mine
		}
	}
}

Then we need to set up ray

code:

void set_boom(char pan[ROWS][COLS], int row, int col)
{
	int i = 0;

	for (i = 0; i < BOOM; i++)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;//Set random number 1-9

		if (pan[x][y] != '1')
		{
			pan[x][y] = '1';//Lei is set to character 1

		}
		else
		{
			continue;
		}
	}

}

3. Print minesweeping chessboard

Considering that the chessboard we need is printed in the form of symbols, we need to store the chessboard in the form of a two-dimensional array.

Because the chessboard shown to the player is a two-dimensional array with the size of 11x11, after the player selects the position, the number of mines around the position is displayed, and all four sides must be + 1

The edge of the chessboard can be set to 1, 2, 3... To facilitate the player to choose the position

void print_pan(char pan[ROWS][COLS], int row, int col)
    //Print chessboard
{
	int i = 0;
	int j = 0;
	
	for (i = 0; i <= row; i++)
	{
		if (i == 0)//first line
		{
			for (j = 0; j <= col; j++)
			{
				printf("%d ", j);
			}
		}
		else
		{
			for (j = 0; j <= col; j++)
			{
				if (j == 0)//First column
				{
					printf("%d ", i);
				}
				else
				{
					printf("%c ", pan[i][j]);
				}
			}
		}

		printf("\n");
	}
}

4. Players check thunder and judge whether to step on thunder

Calculate the number of thunder around with the coordinates entered by the player

First judge whether the position is thunder. If it is thunder, the player loses

If it is not thunder, count. The count shall be printed in the form of characters. The digital character minus the character zero is equal to the number. It is calculated based on this principle

Count the number of positions viewed by players. The number of views plus the number of thunder is equal to the number of options

void win(char local[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = 0;

	while (1)
	{
		printf("Please enter the coordinates you need to view:");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= 9 && y >= 1 && y <= 9)//Judge coordinate legality
		{
			if (local[x][y] == '1')
			{
				print_pan(show, ROW, COL);
				printf("Unfortunately, you stepped on thunder. The game is over!\n");
				printf("-----------------------------------\n");
				break;
			}
			else
			{
				char ret = judge(local, x, y);//Several mines around this coordinate
				show[x][y] = ret + '0';
				print_pan(show, ROW, COL);//Show the player how many mines are around the location
				printf("-----------------------------------\n");
				count++;//Count the numbers and finish all the places that are not thunder
			}
		}
		else
		{
			printf("Coordinates do not exist, please enter correct coordinates!\n");

		}
		if (count == (row * col - BOOM))
		{
			printf("Congratulations on your victory!");
			break;
		}
	}

}

3, test.c file

Used to complete the test of the whole program

#define _CRT_SECURE_NO_WARNINGS

#include "game.h"

void menu()
{
    printf("********** 1: Start the game *********\n");
    printf("********** 0: Exit the game *********\n");
    printf("Please enter your choice:");
}

void game()
{
    char local[ROWS][COLS] = { 0 };//Array of mineboard
    char show[ROWS][COLS] = { 0 };//Array displayed to players

    prepare_pan(show, ROWS, COLS, '*');
    prepare_pan(local, ROWS, COLS, '0');//Single symbol chessboard

    set_boom(local, ROW, COL);//Set up a bomb

    print_pan(show, ROW, COL);//The initial board shown to the player
    printf("--------------------------------\n");

    win(local, show, ROW, COL);//Judge whether to win or lose

}

int main()
{
    int input = 0;

    do
    {
        menu();//Print menu
        srand((unsigned)time(NULL));//Random number generation, followed by

        scanf("%d", &input);//Player selection

        switch (input)
        {
            case 1:
                game();
                break;//Enter the game
            case 0:
                break;//Exit the game
            default: 
                printf("Please enter the correct number!\n"); //Enter other characters
                break;
        }
    } while (input != 0);

    return 0;
}

Tags: C

Posted on Mon, 20 Sep 2021 17:31:38 -0400 by Stickybomb