c language games - Minesweeper is completed

C language - Minesweeper

This article will make this game a general overview of this code is suitable for beginners, to write software using vs2017.

The code can achieve the following functions:

1. The user may select three difficulty, a different number of mine are arranged.

2. random number set position of mine.

3. Enter the coordinates of mine (mine does not expand around one, is represented by 0).

4. Enter the coordinates for the Lightning, he was killed the game ends.

5. exclude all mine, the game is over, displaying the elapsed time.

The following shows the source code:

Header game.h

He cites several necessary header files, macro defines the number of rows and columns as well as mine, to facilitate subsequent changes.

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #include<time.h>
 6 #include<windows.h>
 7 #define ROW 9
 8 #define COL 9
 9 
10 #define ROWS ROW+2
11 #define COLS COL+2
12 
13 #define EASY_COUNT  10
14 
15 void Initboard(char board[ROWS][COLS],int rows, int cols,char set);
16 void displayboard(char board[ROWS][COLS],int row,int col);
17 void setmine(char mine[ROWS][COLS], int row, int col,int count);
18 void findmine(char mine[ROWS][COLS],char show[ROWS][COLS],int  row,int col,int count);

The test module text.c

This module is mainly for the color and size of the console window, and call functions. 

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"
void menu()
{

	printf("***********************************\n");
	printf("***************1.play**************\n");
	printf("***************0.exit**************\n");
	printf("***********************************\n");

}
void game()
{
	int input = 0;
	printf ( "Please select the difficulty:... \ n1 simple \ n2 difficult \ n3 purgatory \ n");
	scanf("%d", &input);
	int count = input * EASY_COUNT; 
	//sprintf(stCmd, "mode con cols=%d lines=%d", a,b);
	//system(stCmd);
	char mine [ROWS] [COLS]; // store the information of mine 
	char show [ROWS] [COLS]; // store the information out of Ray's investigation
	Initboard(mine, ROWS, COLS,'0');  //'0'
	Initboard(show, ROWS, COLS,'*');//'*'
	//displayboard(mine, ROW, COL);
	setmine(mine, ROW, COL,count);
	//displayboard(mine, ROW, COL);
	displayboard(show, ROW, COL);
	findmine(mine, show, ROW, COL,count);
}
void test()
{
	srand((unsigned int)time(NULL));
	int input = 0;	
	do
	{
	menu();
	printf ( "\ n Please choose: \ n");
	scanf("%d", &input);
		switch (input) {
		case 1:game(); break;
		case 0: printf ( "quit the game \ n"); break;
		default: printf ( "input is incorrect, please re-enter \ n"); break;
		}
	} while (input);

}

int main ()
{
	int a = 35; int b = 16;
	char stCmd[128];
	system("color 4A");
	system("mode con: cols=30 lines=12");
	printf ( "\ n \ n \ n ----- Welcome to the minesweeper ----- \ n \ n \ n");
	printf ( "---------- Please wait ---------- \ n \ n \ n");
	Sleep(1000);
	system("cls");
	system("color 1A");
	sprintf(stCmd, "mode con cols=%d lines=%d", a, b);
	system(stCmd);
	test();
	return 0;
}

  Game module game.c

This module to implement various functions.

#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 show[ROWS][COLS], int row, int col)
{
	
	int i = 0;
	int j = 0;
	printf("\n  ");
	for(i=0;i<=row;i++)
	{
		printf("%d  ", i);	
	}
	printf("\n");
	printf("    ===========================\n");
	for (i = 1; i <=row; i++)
	{
		printf("%d ||", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c  ",show[i][j]);
		}
		printf("||");
		printf("\n");
	}
	printf("    ============================\n");

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

}
static int judgemine(char mine[ROWS][COLS], int x,int y,int* count1)
{
	(*count1)--;
		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 Recursion(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int *count1)
{
	int res = judgemine(mine, x, y, count1);
	if (res == 0 && show[x][y] == '*')
	{
		show[x][y] = '0';
		int arr[8][2] = { {-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1} };
		for (int i = 0; i < 8; ++i)
		{
			if (x + arr[i][0] >= 1 && x + arr[i][0] <= ROW && y + arr[i][1] >= 1 && y + arr[i][1] <= COL
				&& show[x + arr[i][0]][y + arr[i][1]] == '*')
				Recursion(mine, show, x + arr[i][0], y + arr[i][1], count1);
		}
	}
	else
		show [x] [y] = res + '0';
}

void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int  row, int col,int count)
{
	int x = 0;
	int y = 0;
	int count1 = ROW * COL-count;
	time_t start, end;
	start = time(NULL);
	while (count1)
	{
		
		printf ( "Please enter the investigation coordinates: \ n");
		scanf("%d %d", &x, &y);
		system("cls");
		if (show[x][y] != '*')
		{
			printf ( "This investigation has been coordinates, please re-enter \ n!");
			displayboard(show, ROW, COL);
			continue;
		}
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '0' + 1)
			{
				printf ( "I'm sorry, you are killed \ n");
				displayboard(mine, ROW, COL);
				break;
			}
			else
			{
				Recursion(mine, show, x, y, &count1);
				//displayboard(mine, ROW, COL);
				displayboard(show, ROW, COL);
				
			}
		}
		else
		{
			printf ( "input error, please re-enter \ n");

		}end = time(NULL);

}
	
	
	if (difftime(end, start)<=60&&count1 == 0)
	{
		printf ( "Congratulations on your success mine \ n!");
		
		printf("用时:%d秒\n", (int)difftime(end, start));
	}
}

 To this end it more than a simple game to complete the de-mining, the paper concludes Thank you!

Guess you like

Origin www.cnblogs.com/lienguang/p/12324428.html