c语言实现扫雷

该博客介绍了如何使用C语言编写扫雷游戏。通过定义矩阵大小、初始化棋盘、设置地雷、显示棋盘、查找地雷和判断游戏状态等功能,实现了经典的扫雷游戏逻辑。玩家可以输入坐标进行游戏,直到成功排除所有非地雷格子或踩到地雷结束游戏。
摘要由CSDN通过智能技术生成

今天我展示的是c语言版的

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 15
#define COL 15
#define ROWS ROW+2
#define COLS COL+2
#define easy_count 20
void Initboard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row,int col);
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 getmine(char mine[ROWS][COLS], int x, int y);
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y);
int iswine(char show[ROWS][COLS], int row, int col);
void menu()
{
	printf("*********************************\n");
	printf("*************1.play**************\n");
	printf("*************0.exit**************\n");
	printf("*********************************\n");
}
void Initboard(char board[ROWS][COLS], int rows, int cols,char set)
{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	for (i = 1; i <= row; i++)
	{
		int j = 0;
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

void setMine(char board[ROWS][COLS], int row, int col)
{
	int count = easy_count;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] != '1')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

void FindMINE(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	
	while (iswine(show,row,col)!=easy_count) 
	{
		printf("请输入一个坐标");
		int x, y;
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾\n");
				DisplayBoard(mine, ROW, COL);
				break;
			}
			else
			{
				int count = getmine(mine, x, y);
				show[x][y] = '0'+count;
				OpenMine(mine, show, row, col,x,y);
				DisplayBoard(show, ROW, COL);
			
			}
			
		}
		else
		{
			printf("请入坐标有误\n");
		}
	}
	if (iswine(show,row,col)==easy_count)
	{
		printf("恭喜你,排雷成功\n");
	}
}
int getmine(char mine[ROWS][COLS], int x, int y)
{
	 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 OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)  //展开雷阵
{
	int ret = 0;
	ret = getmine(mine, x, y);
	if (ret == 0)
	{
		show[x][y] = ' ';
		if (x - 1 > 0 && y > 0&&x-1<=row&&y<=col && show[x - 1][y] == '*')
			OpenMine(mine, show, row, col, x - 1, y);

		if (x - 1 > 0 && y + 1 <= col && show[x - 1][y + 1] == '*')
			OpenMine(mine, show, row, col, x - 1, y + 1);


		if (x > 0 && y + 1 <= col && show[x][y + 1] == '*')
			OpenMine(mine, show, row, col, x, y + 1);

		if (x + 1 <= row && y + 1 <= col && show[x + 1][y + 1] == '*')
			OpenMine(mine, show, row, col, x + 1, y + 1);

		if (x + 1 <= row && y > 0 && show[x + 1][y] == '*')
			OpenMine(mine, show, row, col, x + 1, y);

		if (x + 1 <= row && y - 1 > 0 && show[x + 1][y - 1] == '*')
			OpenMine(mine, show, row, col, x + 1, y - 1);

		if (x > 0 && y - 1 > 0 && show[x][y - 1] == '*')
			OpenMine(mine, show, row, col, x, y - 1);


		if (x - 1 > 0 && y - 1 > 0 && show[x - 1][y - 1] == '*')
			OpenMine(mine, show, row, col, x - 1, y - 1);
		
	}
	else
	{
		show[x][y] = getmine(mine, x, y) + '0';
	}
}

int iswine(char show[ROWS][COLS], int row, int col)
{ 
	int count = 0;
	int i = 0;
	for (i = 1; i <= row; i++)
	{
		int j;
		for (j = 1; j <= col; j++)
		{
			if (show[i][j] == '*')
				count++;
		}
	}
	return count;
}
void game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	//初始化棋盘
	Initboard(mine, ROWS, COLS,'0');
	Initboard(show, ROWS, COLS,'*');
	//布雷
	setMine(mine, ROW, COL);

        //DisplayBoard(mine, ROW, COL);
		DisplayBoard(show, ROW, COL);
        //排雷
       FindMINE(mine,show, ROW, COL);
		


}
int main()
{
	srand((unsigned int)time(NULL));
	int input;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出系统\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值