c语言扫雷游戏的模拟

(1)创建game.c文件存放游戏主体

​
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"game.h"
#include<time.h>
#include<stdlib.h>

void init(char arr[ROWS][COLS], char ch)//初始化棋盘函数
{
	int i;
	for (i = 0;i < ROWS;i++)
	{
		int j;
		for (j = 0;j < COLS;j++)
		{
			arr[i][j] = ch;
		}
	}
}

void   display(char arr[ROWS][COLS])//展示棋盘函数
{
	int a,b;
	printf("%-3d ",0);
	for (a = 1;a < COLS - 1;a++)
	{
		printf("%-3d ", a);
	}
	printf("\n");
	for (b = 0;b < COLS - 1;b++)
	{
		printf("----");
	}
	printf("\n");
	int i;
	for (i = 1;i < ROWS - 1;i++)
	{
		printf("%-3d ", i);
		int j;
		for (j = 1;j < COLS - 1;j++)
		{
			printf("%-3c ", arr[i][j]);

		}
		printf("\n\n");
	}
}
void   set(char arr[ROWS][COLS], int count)//设置地雷函数(设置1为地雷)
{
	do
	{
		int x = rand() % ROWS - 1;
		int y = rand() % COLS - 1;
		if (x >= 1 && y >= 1 && arr[x][y] != '1')
		{
			arr[x][y] = '1';
			count--;
		}
	} while (count);
}

char jud(char arr[ROWS][COLS], int x, int y)//判断周围雷的个数函数
{
	return	(arr[x - 1][y - 1] + arr[x][y - 1] + arr[x + 1][y - 1] + arr[x - 1][y] + arr[x + 1][y] + arr[x - 1][y + 1] + arr[x][y + 1] + arr[x + 1][y + 1]) - 8 * '0' + '0';

}

void explore(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)//周围没有雷后分别对周围八个格子进行判断,以此类推
{
	show[x][y] = jud(mine, x, y);
	if (mine[x - 1][y - 1] != '1' && mine[x][y - 1] != '1' && mine[x + 1][y - 1] != '1' && mine[x - 1][y] != '1' && mine[x + 1][y] != '1' && mine[x - 1][y + 1] != '1' && mine[x][y + 1] != '1' && mine[x + 1][y + 1]!= '1')
	{
    
		if (show[x - 1][y - 1] == '*' && x - 1 >= 1 && x - 1 <= ROWS - 2 && y - 1 >= 1 && y - 1 <= COLS - 2)
			explore(mine,show,x-1,y-1);
		if (show[x][y - 1] == '*' && x  >= 1 && x  <= ROWS - 2 && y - 1 >= 1 && y - 1 <= COLS - 2)
			explore(mine, show, x , y - 1);
		if (show[x +1][y - 1] == '*' && x + 1 >= 1 && x + 1 <= ROWS - 2 && y - 1 >= 1 && y - 1 <= COLS - 2)
			explore(mine, show, x +1, y - 1);
		if (show[x - 1][y ] == '*' && x - 1 >= 1 && x - 1 <= ROWS - 2 && y >= 1 && y  <= COLS - 2)
			explore(mine, show, x - 1, y );
		if (show[x +1][y ] == '*' && x +1 >= 1 && x + 1 <= ROWS - 2 && y  >= 1 && y <= COLS - 2)
			explore(mine, show, x + 1, y );
		if (show[x - 1][y + 1] == '*' && x - 1 >= 1 && x - 1 <= ROWS - 2 && y + 1 >= 1 && y + 1 <= COLS - 2)
			explore(mine, show, x - 1, y +1);
		if (show[x][y + 1] == '*' && x  >= 1 && x  <= ROWS - 2 && y + 1 >= 1 && y + 1 <= COLS - 2)
			explore(mine, show, x , y + 1);
		if (show[x + 1][y + 1] == '*' && x +1 >= 1 && x + 1 <= ROWS - 2 && y +1 >= 1 && y + 1 <= COLS - 2)
			explore(mine, show, x + 1, y + 1);

	}
}

void play(char mine[ROWS][COLS],char show[ROWS][COLS],int count)//玩家操作的函数
{
	int x, y;
	int i, j;
	int num=0;
	while (num!=(COLS-2)*(ROWS-2)-count)
	{
		num = 0;
		while (1)
		{
			printf("请输入坐标(行 列)>");
			scanf("%d%d", &x, &y);
			if (x >= 1 && x <= ROWS - 2 && y >= 1 && y <= COLS - 2)
				break;

		}
		if (mine[x][y] == '1')
		{
			system("cls");//清空屏幕
			printf("踩雷 游戏结束\n");
			display(mine);
			printf("1是雷\n");
			break;
		}
		else
		{
			system("cls");
			explore(mine,show,x,y);
			
			display(show);
			
			for (i = 1;i <= ROWS - 2;i++)
			{
				
				for(j=1;j<=COLS-2;j++)
				{
					if (show[i][j] != '*')
						num++;
				}
			}
		}
	}
	if (num == (COLS - 2) * (ROWS - 2) - count)
	{
		system("cls");
		printf("you win!!!\n");
		display(mine);
		printf("\n ------1是雷\n");
	}
}

void game()//游戏主体
{
	system("cls ");
	srand((unsigned)time(NULL));
	char show[ROWS][ROWS] = { 0 };//用于计算的棋盘
	char mine[ROWS][COLS] = { 0 };//展现给玩家的棋盘
	init(mine,'0');//内部用于计算的棋盘‘0’表示没有雷‘1’表示雷
	init(show,'*');//‘*’代表未知

	set(mine,number);//number表示地雷数量
	display(show);
	printf("'*'是未知领域,0为已知,数字为该地周围八个格子中雷的数量\n");
	play(mine,show,number);
	


}

​

(2)创建头文件game.h用来引用上述游戏本体

#pragma once
#define ROW  5//棋盘的行数
#define COL  5//棋盘的列数
#define ROWS  ROW+2
#define COLS  COL+2

#define number 5 //设置雷的个数
void game();//设置game函数的声明

(3)编写main函数主体部分

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"game.h"//包含game.h头文件来引用游戏主体
int main()
{
	int a=2;
	printf("**********************\n");
	printf("*****欢迎来到扫雷*****\n");
	printf("**********************\n");
	printf("**play(1)***exit(0)***\n");
	printf("**********************\n");
	printf("**********************\n");

	while (1)
	{
		printf("please input 1 or 0\n");
		scanf("%d", &a);
		if (a == 1)
		{
			game();
		}
		else
		{
			break;

		}
	}
	return 0;

以上就完成了扫雷的模拟编写,仅供参考,欢迎大家讨论。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值