当前位置:网站首页>C language to achieve minesweeping games

C language to achieve minesweeping games

2022-06-11 07:31:00 ∞ big understand

Minesweeping games

Through a period of C Language learning , Presumably, the little friends are also eager to write some small programs , This simple minesweeping game , Very suitable C Language beginners to practice .

Achieve mine clearance , First there must be two chessboards , A chessboard holds the information of thunder , The other one is for display on the screen ; Then the player enters the coordinates to clear the mine , If the coordinates entered are thunder , The game is over , If there is no thunder, the corresponding position of the chessboard will be displayed , Show the number of surrounding mines ; Finally, check all the coordinates and avoid being killed by mines , Is victory .


The above is the basic logic of mine clearance , now , Step by step .

1. Initialize chessboard

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

mine It's the chess board that releases thunder ,show Is to show the chessboard . We put mine Are initialized to ‘0’, Lei Wei ‘1’,show

All initialized to ‘*’.

2. Arrange thunder

void SetMine(char board[ROWS][COLS], int row, int col)
{
    
	int count = EASY_COUNT;//10 A thunder 

	while (count)
	{
    
		//1.  Generate random subscripts 
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] != '1')
		{
    
			board[x][y] = '1';
			count--;
		}
	}
}

3. Check the thunder

Checking thunder is the key to this game , It's also a little difficult , First , You need to pass both chessboards , Every time I input the coordinates, I will look in the thunder pan , If it's ray, the game is over , It is not ray that will make the position of the corresponding display panel Show the number of thunder around .

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 be checked :>");
		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");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
    
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
    
			printf(" Illegal coordinates , Re input \n");
		}
	}

	if (win == row * col - EASY_COUNT)
	{
    
		printf(" congratulations , Mine clearance is successful \n");
		DisplayBoard(mine, ROW, COL);
	}
}

see (x,y) A function of the surrounding ray number :

int GetMineCount(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');
}

The above steps make the minesweeping game complete step by step , Next is the running code of the game :

1. The header file

#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

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

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

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

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

2. Game code

#include "game.h"

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

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    
	int i = 0;
	printf("------------------------\n");
	for (i = 0; i <= 9; i++)
	{
    
		printf("%d ", i);
	}
	printf("\n");

	for (i = 1; i <= row; i++)
	{
    
		int j = 0;
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
    
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("------------------------\n");
}


void SetMine(char board[ROWS][COLS], int row, int col)
{
    
	int count = EASY_COUNT;

	while (count)
	{
    
		//1.  Generate random subscripts 
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] != '1')
		{
    
			board[x][y] = '1';
			count--;
		}
	}
}

int GetMineCount(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 be checked :>");
		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");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
    
				int count = GetMineCount(mine, x, y);
				show[x][y] = count + '0';
				DisplayBoard(show, ROW, COL);
				win++;
			}
		}
		else
		{
    
			printf(" Illegal coordinates , Re input \n");
		}
	}

	if (win == row * col - EASY_COUNT)
	{
    
		printf(" congratulations , Mine clearance is successful \n");
		DisplayBoard(mine, ROW, COL);
	}
}

3. General procedure

#include "game.h"

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

void game()
{
    
	char mine[ROWS][COLS] = {
     0 };// Store Ray's information 
	char show[ROWS][COLS] = {
     0 };// Store the information of the detected mine 
	// Initialize the chessboard 
	InitBoard(mine, ROWS, COLS, '0');//'0'
	InitBoard(show, ROWS, COLS, '*');//'*'

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

	// Check the thunder 
	FindMine(mine, show, ROW, COL);
}

int main()
{
    
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
    
		menu();
		printf(" Please select :>");
		scanf("%d", &input);
		switch (input)
		{
    
		case 1:
			game();
			break;
		case 0:
			printf(" Quit the game \n");
			break;
		default:
			printf(" Wrong choice , To choose !\n");
			break;
		}
	} while (input);

	return 0;
}

That's what this issue is about

原网站

版权声明
本文为[∞ big understand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020520334982.html