c语言小游戏-扫雷的完成

C语言-扫雷游戏

本文将对此游戏做一个大致的概述,此代码适合初学者,编写软件使用了vs2017。

该代码可以实现如下功能:

1.用户可以选择3个难度,分别布置不同个数的雷。

2.随机数设置雷的位置。

3.输入坐标进行排雷(周围没有雷可以展开一片,用0表示)。

4.输入的坐标为雷时,被炸死游戏结束。

5.排除所有的雷后,游戏结束,显示所用时间。

下面展示源代码:

头文件game.h

引用了几个必要的头文件,宏定义了行和列以及雷的个数,方便后续的更改。

 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);

测试模块 text.c

本模块主要针对于控制台窗口的颜色与大小,以及函数的调用。 

#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("请选择难度:\n1.简单\n2.困难\n3.炼狱\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];//存放雷的信息 
	char show[ROWS][COLS];//存放排查出的雷的信息
	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请选择:\n");
	scanf("%d", &input);
		switch (input) {
		case 1:game(); break;
		case 0:printf("退出游戏\n"); break;
		default:printf("输入有误,请重新输入\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-----欢迎进入扫雷游戏-----\n\n\n");
	printf("----------请稍后----------\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.c

此模块为各个函数的实现。

#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("请输入要排查的坐标:\n");
		scanf("%d %d", &x, &y);
		system("cls");
		if (show[x][y] != '*')
		{
			printf("此坐标已经排查过,请重新输入!\n");
			displayboard(show, ROW, COL);
			continue;
		}
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '0' + 1)
			{
				printf("很遗憾,你被炸死了\n");
				displayboard(mine, ROW, COL);
				break;
			}
			else
			{
				Recursion(mine, show, x, y, &count1);
				//displayboard(mine, ROW, COL);
				displayboard(show, ROW, COL);
				
			}
		}
		else
		{
			printf("输入有误,请重新输入\n");

		}end = time(NULL);

}
	
	
	if (difftime(end, start)<=60&&count1 == 0)
	{
		printf("恭喜你排雷成功!\n");
		
		printf("用时:%d秒\n", (int)difftime(end, start));
	}
}

 以上便为此扫雷游戏的简单完成,本文到此结束谢谢!

猜你喜欢

转载自www.cnblogs.com/lienguang/p/12324428.html