Simple application of array application in C language - minesweeper game

1. Minesweeper game ————— Simple version for beginners in C

        Realization: ① Difficulty selection, different calculation methods of the number of thunders produce different difficulties;

                   ②The first time you step on the thunder, you will not jump out;

                   ③ If there is no thunder around the coordinates, expand the coordinates.


Source code:

game.h

//*Copyright(c) 2018, Kwai Si
//*All rights reserved.
//*
//* File name: mine clearance game
//*
//*Current version: 1.1
//*Author: Kwai Division
//* Completion date: April 23, 2018
//*
//* supersedes version: 1.0
//*Author: Kwai Division
//*


#ifndef  _GAME_H_
#define  _GAME_H_


#include <stdio.h>
#include <stdlib.h>
#include <time.h>


enum OPTION
{
	PLAY = 1,
	EXIT = 2
};


#define ROW 10
#define COL 10
#define ROWS ROW+2
#define COLS COL+2
extern int number ;//There is a difference between setting 0 and not setting 0
extern int Spacenumber;

void interface();
int  Choose(int a);
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void PrintBoard(char board[ROWS][COLS], int rows, int cols);
void SetMine(char board[ROWS][COLS],int row,int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int GetMineCount(char mine[ROWS][COLS], int x, int  y);
void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y);


#endif//_GAME_H_


game.c

#include "game.h"
int number;//Global variable definition
int Spacenumber;//Global variable definition


//initialize the array
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;
		}
	}
}


// print the array
void PrintBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (j = 0; j <= row; j++)
	{
		printf("%-2d ", j);
	}
	printf("\n");
	for (i = 0; i < row; i++)
	{
		printf("%-2d", i+1);
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}


//set the bomb
void SetMine(char board[ROWS][COLS])
{
	int count = name;
	int x = 0;
	int y = 0;
	while (count)
	{
		x = rand() % ROW + 1;
		y = rand() % COL + 1;
		if ('0' == board[x][y])
		{
			board[x][y] = '1';//The place with thunder is set to character 1
			count--;
		}
	}
}



//Return the number of mines around the coordinates entered by the player
static int GetMineCount(char mine[ROWS][COLS], int x, int y)
{

	int minecount = 0;


	if (mine[x - 1][y] == '1')
		minecount++;
	if (mine[x - 1][y + 1] == '1')
		minecount++;
	if (mine[x - 1][y + 1] == '1')
		minecount++;
	if (mine[x][y + 1] == '1')
		minecount++;
	if (mine[x + 1][y + 1] == '1')
		minecount++;
	if (mine[x + 1][y] == '1')
		minecount++;
	if (mine[x + 1][y - 1] == '1')
		minecount++;
	if (mine[x][y - 1] == '1')
		minecount++;
	if (mine[x - 1][y - 1] == '1')
		minecount++;
	return minecount;
}



//Extension function implemented by recursive function call
void expand(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{
	if (0 == GetMineCount(mine, x, y))
		{
			show[x][y] = ' ';
			Spacenumber++;

			if (show[x][y - 1] == '*')
			{
				expand(mine, show, row, col, x, (y - 1));
			}

			if (show[x][y + 1] == '*')
			{
				expand(mine,show, row, col, x, (y + 1));
			}

			if (show[x - 1][y] == '*')
			{
				expand(mine, show, row, col, (x - 1), y);
			}

			if (show[x + 1][y] == '*')
			{
				expand(mine, show, row, col, (x + 1), y);
			}
		}
	else
	{
		show[x][y] = GetMineCount(mine, x, y) + '0';
	}
}



//find thunder function
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int sum = 0;
	int count = 0;
	int ret = 0;

	while (row*col-sum>nume)
	{
		printf("Please enter your coordinates: ");
		scanf("%d%d", &x, &y);
		printf("\n");
		count++;

		while (1 == (x >= 1 && x <= row && y >= 1 && y <= col))
		{
			sum++;
			x--;
			and--;
			if ('1' == mine[x][y])
			{
				if (1 == count)//Determine whether you stepped on the thunder for the first time
				{
					printf("Break! Give you another chance!\n");
					printf("\n");
					break;
				}
				else
				{
					printf("Congratulations you won the lottery, you stepped on the thunder! GAME OVER!\n");
					printf("\n");
					return 0;
				}
			}
			else
			{
				expand(mine, show, row, col, x, y);
				PrintBoard(show, ROW, COL);
				break;
			}

			if (0 == (x >= 1 && x <= row && y >= 1 && y <= col))
			{
				printf("Illegal input! Please re-enter!\n");
				printf("\n");
			}
		}
	}
	if (row*col-sum == nume)
	{
				printf("Congratulations, mine clearance is successful!\n");
				printf("\n");
	}
}


find.c

#include "game.h"
int number;//Global variable definition
int Spacenumber;//Global variable definition


// menu function
void MENU()
{
		printf("                          #####                             \n");
		printf("                   ######       ######                      \n");
		printf("              #######    1.PLAY     #######                 \n");
		printf("              #######    2.EXIT     #######                 \n");
		printf("                   ######       ######                      \n");
		printf("                          #####                             \n");
}


// select interface
void interface()
{
	printf("*********************************************************************\n");
	printf("******************** 1. Rookie ************************ *************\n");
	printf("******************** 2. Master ************************ *************\n");
	printf("******************** 3. Old bird ************************ ****************\n");
	printf("*********************************************************************\n");
	printf("\n");
}


//Difficulty selection, returns the number of different mines
int  Choose(int a)
{
	switch (a)
	{
	case 1:
		return ROW;
	case 2:
		return ROW + ROW / 2;
	case 3:
		return ROW * 2 ;
	default:
	{
			   printf("Incorrect input, please try again!");
			   break;
	}
	}
}



// game function
void game()
{
	char mine[ROWS][COLS] = { '0' };  
	char show[ROWS][COLS] = { '0' };



	InitBoard(mine, ROW, COL, '0');//Bray's function is initialized to '0'
	InitBoard(show, ROWS, COLS, '*');//The displayed function is initialized to '0'



	//PrintBoard(mine, ROW, COL);
	PrintBoard(show, ROW, COL);


	SetMine(mine,ROW,COL);//Generate mines at random locations
	//PrintBoard(mine, ROW, COL);
	FindMine(mine, show, ROW, COL);//Find mine function
}


// execute function
void TEST()
{
	srand((unsigned)time(NULL));
	int input = 0;
	int i = 0;
	do
	{
		MENU();
		printf("Please select->");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
		{
				  interface();
				  printf("Please select difficulty");
				  scanf("%d", &i);
				  number= Choose(i);//The number of mines assigned to the global variable
				  game();
				  break;
		}
		case 2:
			printf("You will quit the game!\n");
			break;
		default:
			printf("You made a mistake, please try again!\n");
			break;
		}
	} while (input);
}



// main function
intmain()
{
	TEST();
	system("pause");
	return 0;
}

Note: The code has been run on VS2013 without error.


Known bug: (I also want to change it, but I won't, so I will continue to optimize -_+)

                ①: When the game is 9*9, if I set the number of mines to 80, the compilation is successful, but there will be problems in running

                ②..............

problem dicovered:

                ①: The effect of global variables being initialized to 0 in the header file is different from that of not being 0. To be resolved,

                ②............

    


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324839243&siteId=291194637