当前位置:网站首页>Simple realization of mine sweeping

Simple realization of mine sweeping

2022-06-25 13:20:00 LIn_ jt

Simple implementation of mine sweeping

The mine sweeping characteristics :

  • The first click must not be ray
  • Easy to implement

First , We are still like a three piece chess game , Create two source files and a header file :

 Please add a picture description

Then there is our main function part :

Because it's the test part , Write a test function to place our code :
 Insert picture description here

Next is test Implementation of function :

 Insert picture description here

Here menu The function is the print menu function , That is to remind the player what to choose , From here into our switch case Statement to determine what statement should be executed next

Input 1 After entering the game :( The minesweeping game is temporarily replaced by printing ), In this case, we have written the basic logic , Now let's focus on the implementation of mine sweeping game

take printf Change statement to game() Function to implement , Now we're going to play Minesweeper , What about the minesweeping game , Now take a written program to see

 Insert picture description here

Now it's a chessboard , And we have to store information , therefore , We need to create a two-dimensional array to implement , But now we want to put the characters 1 It's our ray , What to do , therefore , Let's create two arrays , One for storing thunder , An array used to store demining information , But when we mine , Mine clearance needs to be carried out in eight directions , But what about the coordinates of the four corners ? towards 8 Judging in one direction is likely to cross the line . therefore , We don't create 9,9 Array of , And then create a 11,11 Array of , But we still use 9*9 Array of , So we define four identifier constants .

Now that it has been defined in the header file , Otherwise, let's just put all the contents of the header file .
 Insert picture description here

therefore , Now come to our game Internal function :

The first is our initialization function :>
 Insert picture description here

take mine and show Initialize the array to the elements we want , Initialize the array of layout mines to 0, Initialize the displayed array to ’*‘, So we can achieve the effect we want ,InitBoard Function definition part : Insert picture description here

Now that the chessboard has been initialized , Let's print it out and see how it works , Write a DisplayBoard Function to implement , Please look at the chart below.
 Insert picture description here

first for The loop prints out the column number of each column , It's printed in the next cycle . Now let's look at the printing effect
 Insert picture description here

 Insert picture description here

Now that it has been printed , So now we're going to set up ray , Arrange thunder , We use one SetMine Function to implement , Please look at the chart below.

 Insert picture description here

Because we're going to place ten mines , use count = 10 To count , And generate random coordinates to place the mine

Ray has put it all away , It's time to start minesweeping now , Next, let's start our minesweeping part , Write a SweepMine Function to implement
 Insert picture description here

It's on the inner floor here CountMine function , It is used to calculate the number of mines around the coordinates of the point of the discharged mine , We do that :>

 Insert picture description here

So the subroutine can run , Let's take a look at the effect :>
 Insert picture description here

Here are the improvements to the program :>

In case it's ray for the first time , And we're easy to debug , We set the number of Lei as the identifier constant , The following is the added code :>

 Insert picture description here

The number of mines is defined as the identifier constant

Then we create a safe minesweeping function
 Insert picture description here

In this way, you can avoid being the first to thunder , Now let's look at the effect :

 Insert picture description here
 Insert picture description here
Let's release the source code :
test.c File function code :

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

#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');// Here we pass in the element we want to initialize , So you can simply initialize... With a function .
	InitBoard(show, ROWS, COLS, '*');

	//DisplayBoard(show, ROW, COL);
	SetMine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);
    DisplayBoard(show, ROW, COL);
	SafeMine(mine, show, ROW, COL);
	SweepMine(mine, show, ROW, COL);
}


void test()
{
    
	srand((unsigned int)time(NULL));
	// The game is executed at least once , So with do while loop 
	int input = 0;
	do
	{
    
		menu();// Here menu Function to create a menu function for yourself , Prompt the player 
		printf(" Please select a number :>");// This is a choice , Then you have to enter a number ?
		scanf("%d", &input);
		switch (input)
		{
    
		case 1:
			game();
			break;
		case 0:
			printf(" Quit the game  \n");
			break;
		default:
			printf(" Input error , Please re-enter \n");
			break;
		}
	} while (input);
}

int main()
{
    
	test();
	return 0;
}

Here is game.c The contents of the document

#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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    
	int i = 0;
	int j = 0;
	for (i = 0; i <= col; i++)
	{
    
		printf("%d ", i);      // Print column number , So that players can see 
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
    
		printf("%d ", i);  // Print line number , So that players can see 
		for (j = 1; j <= col; j++)
		{
    
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}


void SetMine(char mine[ROWS][COLS], int row, int col)
{
    
	int count = EASYCOUNT;
	while (count)
	{
    
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
    
			mine[x][y] = '1';
			count--;
		}
	}
}




int CountMine(char mine[ROWS][COLS], int x, int y)
{
    
	return mine[x - 1][y] +
		mine[x + 1][y] +
		mine[x - 1][y - 1] +
		mine[x][y - 1] +
		mine[x + 1][y - 1] +
		mine[x - 1][y + 1] +
		mine[x][y + 1] +
		mine[x + 1][y + 1] - 8 * '0';
}

void SafeMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    
	int x = 0;
	int y = 0;
	int newx = 0;
	int newy = 0;
	printf(" Please enter the coordinates of the investigation :>");
	scanf("%d%d", &x, &y);
	while (1)
	{
    
		if (x >= 1 && x <= row && y >= 1 && y <= col && mine[x][y] == '0')
		{
    
			int ret = CountMine(mine, x, y);
			show[x][y] = ret + '0';
			DisplayBoard(show, ROW, COL);

			break;
		}
		newx = rand() % row + 1;
		newy = rand() % col + 1;
		if (x >= 1 && x <= row && y >= 1 && y <= col && mine[x][y] == '1')
		{
    
				if (mine[newx][newy] == '0')
				{
    
					mine[newx][newy] = '1';
					mine[x][y] = '0';
					int ret = CountMine(mine, x, y);
					show[x][y] = ret + '0';
					DisplayBoard(show, ROW, COL);
					DisplayBoard(mine, ROW, COL);
					break;
				}
		}
	}
}



void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    
	int x = 0;
	int y = 0;
	int count = row * col - EASYCOUNT - 1;
	while (count)
	{
    
		printf(" Please enter the coordinates of the investigation :>");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
    
			if (mine[x][y] == '1')
			{
    
				printf(" unfortunately , You're killed in the blast \n");
				break;
			}
			if (mine[x][y] == '0')
			{
    
				int ret = CountMine(mine, x, y);
				show[x][y] = ret + '0';
				DisplayBoard(show, ROW, COL);
				count--;
			}
		}
		else
		{
    
			printf(" Incorrect input , Please re-enter \n");
		}
	}
	if (count == 0)
	{
    
		printf(" congratulations , Mine clearance is successful \n");
		DisplayBoard(mine, ROW, COL);
	}
}

game.h The code in :

#pragma once

#define ROW 9
#define COL 9

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

#define EASYCOUNT 10

#include <stdio.h>

#include <stdlib.h>

#include <time.h>
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);

void DisplayBoard(char board[ROWS][COLS], int row, int col);

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

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

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

The end of this paper

原网站

版权声明
本文为[LIn_ jt]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251235204364.html