C language - personal questions (and answers) in mine clearance + error prone points + mine clearance code

Posted by Adeus on Thu, 11 Nov 2021 12:05:51 +0100

Personal questions:

  1. This minesweeping function calculates several surrounding mines. Can the return value be a character? Why do you have to subtract 8 characters from zero to become a number?

         A: what you want is the number of Lei, and the array is all character 1. Subtracting character 0 from character 1 can get the actual digital difference. Each position is reduced by one, so a total of 8 are subtracted. You return one character. How do you know how many there are outside

  2. Can this n returned character be directly assigned to show[x][y]?

         A: No, because you need to subtract 8 characters 0 internally, but you only add one externally. If you don't subtract internally, it's equivalent to an additional 7 characters 0  , It is equivalent to converting numbers into characters here as the display value of mine disk

3. The previous array is the characters 0 and 1? (personal answers below)

         Because the second one stores the information of the checked mine   The function show of is initialized with the character '*'. In order to be consistent with it (they use the same function to initialize the chessboard), the first function needs to be initialized with the character '0'

4. What is the board in void initboard (char Board [rows] [cols], int rows, int cols, char set)?

         The board in the formal parameter of the initialization checkerboard function is a receiving array, which receives the mine or show function in the actual parameter

Error prone points in mine clearance procedures:

1. When printing a chessboard, 0 rows and 0 columns are marked with several columns and rows. The chessboard shall be printed from 1 row and 1 column

2. In setmine, the number of buried mines (10) is given to i. each time one mine is buried, it will be reduced by 1. After 10 times, it will be reduced by 10. If while (0) is not established, it will jump out of the cycle and the buried mine will end.

Do not mistakenly write% as /, = = in the function=

  3. Function for calculating the number of surrounding mines. In the mine finding function, the input coordinate x   Only when y is passed to the function for calculating the number of mines can the number of mines in a circle around this coordinate be calculated

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 - EASY_COUNT)
	{
		printf("Please enter the coordinates to check:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("I'm sorry you were killed\n");
				DisplayBoard(mine, row, col);
				break;
			}
			else
			{
				//How many mines are there around the X and Y coordinates
				int n = get_mine_count(mine, x, y);
				show[x][y] = n + '0';
				DisplayBoard(show, row, col);
				win++;
			}
		}
		else
		{
			printf("The coordinates entered are illegal and cannot be cleared. Please re-enter\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("Congratulations on your success\n");
		DisplayBoard(mine, row, col);
	}

}

  The following are mine clearance Codes:

test.c

#define _CRT_SECURE_NO_WARNINGS

#include "game.h"

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


void game()
{
	//Create array
	char mine[ROWS][COLS] = { 0 };//Store the information of the arranged mine
	char show[ROWS][COLS] = { 0 };//Store the information of the detected mine

	//Initialize mine array to all '0'
	InitBoard(mine, ROWS, COLS, '0');
	//Initialize the show array to all '*'
	InitBoard(show, ROWS, COLS, '*');
	//DisplayBoard(mine, ROW, COL);

	//Print chessboard
	//DisplayBoard(mine, ROW, COL);

	//Lay thunder
	SetMine(mine, ROW, COL);
	DisplayBoard(show, ROW, COL);

	//DisplayBoard(mine, ROW, COL);
	//mine clearance
	FindMine(mine, show, ROW, COL);
}

void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));

	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			//mine clearance
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Selection error\n");
			break;
		}
	} while (input);
}

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

game.c

#define _CRT_SECURE_NO_WARNINGS
#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)
{
	//1~9
	int i = 0;
	int j = 0;

	//Printing of column numbers
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");

	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);//Print line number

		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 = EASY_COUNT;

	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;

		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

static 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 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 - EASY_COUNT)
	{
		printf("Please enter the coordinates to check:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("I'm sorry you were killed\n");
				DisplayBoard(mine, row, col);
				break;
			}
			else
			{
				//How many mines are there around the X and Y coordinates
				int n = get_mine_count(mine, x, y);
				show[x][y] = n + '0';
				DisplayBoard(show, row, col);
				win++;
			}
		}
		else
		{
			printf("The coordinates entered are illegal and cannot be cleared. Please re-enter\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("Congratulations on your success\n");
		DisplayBoard(mine, row, col);
	}

}

 game.h

#pragma once

//Inclusion of header file
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

//Declaration of symbols
#define ROW 9
#define COL 9

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

#define EASY_COUNT 10

//Declaration of function

//Initialize chessboard
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);

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

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

//Check thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

Topics: C Back-end