Mine sweeping interface is relatively good-looking version C (1.0)

Posted by pahunrepublic on Tue, 25 Jan 2022 10:12:03 +0100

First, I'll show you the beautiful black frame=

Come on, do it!

catalogue

1: Components of the game

2: Special section summary

(1) Create two 2D arrays:

mine array:

show array:

2: Game implementation logic

3: Array size creation problem

3: code implementation

(1)game.h

(2)game.c

(3)test.c

4: Test game

1: Components of the game

I really don't want to repeat this ==

Forgive me, I have an article I wrote about Sanzi chess with the same frame

Implementation of Sanzi chess (C)_ lihua777 blog - Implementation of CSDN blog Sanzi chess (C)https://blog.csdn.net/lihua777/article/details/122647702?spm=1001.2014.3001.5501

2: Special section summary

(1) Create two 2D arrays:

A two-dimensional array mine is a two-dimensional array of buried mines

A two-dimensional array show is a two-dimensional array presented to players

mine array:

It didn't show up until you were killed by a bomb

1 indicates that there is thunder in this place, and 0 indicates that there is no thunder in this place

show array:

(the picture actually seen by the user during the game)

The function we need to do is to input the coordinates of Troubleshooting:

If there are no mines in this location, we will take this location as the center and display the number of mines in 8 locations around it

If there is thunder in the selected location, you will be killed. Let me show you the burial point of the bomb and let you die in peace

2: Game implementation logic

1: Initialize two 2D arrays

2: Print chessboard (note that the fillings of the two chessboards are different)

3: Burying mines in the mine array

4: show the chessboard to the user

5: Start demining (pass mine and show arrays at this time)

6: Judgment result

3: Array size creation problem

To judge the number of mines around the 8 positions, if we define a 9 * 9 two-dimensional array, we also need to judge whether the 8 positions around the elements in the edge zone are out of bounds. Therefore, for convenience, we define an 11 * 11 array, but we show players a 9 * 9 array

3: code implementation

(1)game.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10

void Init_board(char board[ROWS][COLS], int rows, int cols, char set);
void Set_mine(char board[ROWS][COLS],int row,int col,int count);
void Display_board(char board[ROWS][COLS],int row,int col);
void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols);

(2)game.c

#include"game.h"

void Init_board(char board[ROWS][COLS], int rows, int cols, char set)
//Initialize chessboard
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}

void Set_mine(char mine[ROWS][COLS], int row, int col, int count)
{//Buried mine
	while (count)
	{
		int x = rand() % 9 + 1;//Coordinate range: [1,9];
		int y = rand() % 9 + 1;
		if (mine[x][y] == '0')//If no mine has been buried here, mine here
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

void Display_board(char board[ROWS][COLS], int row, int col)
{
	int i = 1;
	int j = 1;

	printf("0    ");
	//Print column number
	for (i = 1; i <= col; i++)
	{
		printf("%d   ", i);
	}
	printf("\n");//Line break
	for (i = 0; i <= col; i++)
	{
		printf("----");
	}
	printf("\n");//Line feed board
	//Real printing chessboard
	for (i = 1; i <= row; i++)
	{
		printf("%d   ", i);//Print line number
		for (j = 1; j <= col; j++)
		{
			printf(" %c ", board[i][j]);//Print the real elements in the two-dimensional array
			if (j <= col)//Range of j [1:9]
			{
				printf("|");//Vertical line
			}
		}
		printf("\n");//Line feed
		if (i < row )//Row = 9 I < 8 print | - "if the number of lines is less than 9, the vertical line will be printed
		{
			for (j = 0; j <= col; j++)
			{
				printf("---");
				if (j <= col)
				{
					printf("|");
				}
			}
		}
		printf("\n");
	}
}

static int count_num(char mine[ROWS][COLS], int x, int y)//Calculate the number of mines at 8 locations around
{
	return mine[x - 1][y - 1] + mine[x - 1][y] + 
		mine[x + 1][y] +mine[x + 1][y - 1] + 
		mine[x + 1][y + 1] + mine[x - 1][y + 1] + 
		mine[x][y + 1] +mine[x][y - 1] - 8 * '0';
}

void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;//Use win to record the number of deminers
	while (win < (row * col - EASY_COUNT))//When the number of cleared mines = = total number of cells - number of mines, the cycle will jump out
	{
		printf("Enter the coordinates to check:");
		scanf("%d %d", &x, &y);
		if (x > row || x< 0 || y>col || y < 0)
		{
			printf("Illegal coordinates, please re-enter");
		}
		else
		{
			if (mine[x][y] == '1')
			{
				printf("I'm sorry you were killed\n");
				Display_board(mine, ROW, COL);
				break;
			}
			else
			{
				show[x][y] = count_num(mine, x, y) + '0';
				Display_board(show, ROW, COL);
				win++;
			}
		}
	}
	if (win == (row * col - EASY_COUNT))//Jump out of cycle and print successfully
	{
		printf("Demining succeeded!\n");
	}
}

(3)test.c

#include"game.h"
void game()
{
	char mine[ROWS][COLS] = { 0 };//Storage of mine burial information
	char show[ROWS][COLS] = { 0 };//Chessboard displayed to users

	Init_board(mine, ROWS, COLS, '0');//Initialize minefield checkerboard
	Init_board(show, ROWS, COLS, '*');//Initialize the chessboard for users
	
	Set_mine(mine, ROW, COL, EASY_COUNT);//Buried mine

	Display_board(show, ROW, COL);//Show the chessboard to the user
	//printf("_______ perspective cheater _______________\ n");
	//Display_board(mine, ROW, COL);// Show the chessboard to the user
	Find_mine(mine, show, ROW, COL);//start to rehearse
}

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

void test()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("Please enter->");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Selection error, please re select\n");
			break;
		}
	} while (input);
}
int main()
{
	test();
	return 0;
}

4: Test game

You can display the buried mine map and adjust the number of mines to 80. In this way, we can only look at the map and find one position, which is also the advantage of defining global variables

Topics: C Back-end