C语言详细讲解通过递归实现扫雷的展开

 更新时间:2022年05月19日 11:22:31   作者:头发没有代码多  
windows自带的游戏《扫雷》是陪伴了无数人的经典游戏,本文将利用C语言实现这一经典的游戏,文中的示例代码讲解详细,感兴趣的可以学习一下

用户选择菜单

void menu()
{
	printf("****************************\n");
	printf("********  1.play  **********\n");
	printf("********  0.exit  **********\n");
	printf("****************************\n");
}

用户按1进入游戏

棋盘初始化

void Itnboard(char board[ROWS][COLS], int rows, int cols,char c)
{
	int i, j;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j <cols; j++)
		{
			board[i][j] = c;
		}
	}
}

创建数组,并对其进行初始化

布置雷(随机布置)

void Setboard(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] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}

用time函数产生随机值

打印棋盘

void Displayboard(char board[ROWS][COLS], int row, int col)
{
	int i, j;
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

打印棋盘

玩家下棋

void Player(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x, y;
	int count = 0;
	while (1)
	{
		printf("请排雷:\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
		{
			if (board[x][y] == '0')
			{
				Openboard(show, board, x, y);
				Displayboard(show, ROW, COL);
			}
			else if (board[x][y] == '1')
			{
				printf("你死了\n");
				break;
			}
		}
		else
		{
			printf("请重新输入");
		}
		int i, j;
		for (i = 1; i <= row; i++)
		{
			for (j = 1; j <= col; j++)
			{
				if (show[i][j] == '*')
				{
					count++;
				}
			}
		}
		if (count == Easy_count)
		{
			printf("成功\n");   //这里的判断条件是遍历整个数组,统计雷的个数,如果雷的个数等于所剩余未排的*,说明排雷成功
			break;
		}
	}
}

用户输入值,并进行判断,如果该位置没有雷,我们进入展开函数

棋盘展开

void Openboard(char show[ROWS][COLS], char board[ROWS][COLS], int row, int col)
{
	if (row >= 1 && row <= ROW && col >= 1 && col <= COL)
	{
		int count=sum(board, row, col);
		if (count != 0)
		{
			show[row][col] = count + '0';
		}
		else if (show[row][col] != '_')
		{
			show[row][col] = '_';
			int i = 0, j = 0;
			for (i = row - 1; i <= row + 1; i++)
			{
				for (j = col - 1; j <= col + 1; j++)
				{
					Openboard(show, board, i,j);
				}
			}
		}
		else
		{
			return;
		}
	}
}

如果用户输入的这个位置没有雷,我们对其周围8个位置进行判断是否有雷,若有雷,我们把雷的个数显示在该位置上,若其周围8个位置没有雷并且不是下划线,我们把这个位置赋值为下划线,然后并对其8个位置进行同样的判断,如果周围没雷,而且周围的棋子也不是下划线,我们对其进行返回。

展开部分思维导图

展开函数最后一个else return 作用

这里我们show棋盘有三种情况,

1.该位置是*

2.该位置是下划线

3.该位置是雷的个数

else

return;

这里是作用:如果是下划线,我们就返回上一层函数。因为如果这里不是下划线,我们会在else return 之前的语句中进行判断,并对其周围8个位置进行操作,然后再对这8个棋子各个周围8个位置进行判断并操作,如果这里是下划线,就说明由这个位置为中心的周围8个棋子已经判断过了,并且以这8个位置为中心,已经递归过了,我们不需要再进行判断,所以直接返回就行

周围雷个数判断

int sum(char board[ROWS][COLS], int x, int y)
{
	return (board[x - 1][y - 1] +
		board[x - 1][y] +
		board[x - 1][y + 1] +
		board[x][y - 1] +
		board[x][y + 1] +
		board[x + 1][y - 1] +
		board[x + 1][y] +
		board[x + 1][y + 1] - 8 * '0');
}

test.c

#include"game.h"
void menu()
{
	printf("****************************\n");
	printf("********  1.play  **********\n");
	printf("********  0.exit  **********\n");
	printf("****************************\n");
}
void game()
{
	char board[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	Itnboard(board, ROWS, COLS,'0');  //初始化棋盘
	Itnboard(show, ROWS, COLS, '*');
	Setboard(board, ROW, COL);   
	Displayboard(board, ROW, COL);
	Player(board, show, ROW, COL);            //玩家输入
}
int main()
{
	int input=1;
  srand((unsigned int)time(NULL));
	do{
		menu();
	scanf("%d", &input);
	switch (input)
	{
	case 1:
		game();
		break;
	case 0:
		break;
	default:
		printf("输入错误请重新输入:\n ");
	}
	} while (input);
}

game.c

#include"game.h"
void Itnboard(char board[ROWS][COLS], int rows, int cols,char c)
{
	int i, j;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j <cols; j++)
		{
			board[i][j] = c;
		}
	}
}
void Displayboard(char board[ROWS][COLS], int row, int col)
{
	int i, j;
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}
void Setboard(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] == '0')
		{
			board[x][y] = '1';
			count--;
		}
	}
}
int sum(char board[ROWS][COLS], int x, int y)
{
 
	return (board[x - 1][y - 1] +
		board[x - 1][y] +
		board[x - 1][y + 1] +
		board[x][y - 1] +
		board[x][y + 1] +
		board[x + 1][y - 1] +
		board[x + 1][y] +
		board[x + 1][y + 1] - 8 * '0');
}
void Player(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x, y;
	int count = 0;
	while (1)
	{
		printf("请排雷:\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
		{
			if (board[x][y] == '0')
			{
				Openboard(show, board, x, y);
				Displayboard(show, ROW, COL);
			}
			else if (board[x][y] == '1')
			{
				printf("你死了\n");
				break;
			}
		}
		else
		{
			printf("请重新输入");
		}
		int i, j;
		for (i = 1; i <= row; i++)
		{
			for (j = 1; j <= col; j++)
			{
				if (show[i][j] == '*')
				{
					count++;
				}
			}
		}
		if (count == Easy_count)
		{
			printf("成功\n");
			break;
		}
	}
}
void Openboard(char show[ROWS][COLS], char board[ROWS][COLS], int row, int col)
{
 
	if (row >= 1 && row <= ROW && col >= 1 && col <= COL)
	{
		int count=sum(board, row, col);
		if (count != 0)
		{
			show[row][col] = count + '0';
		}
		else if (show[row][col] != '_')
		{
			show[row][col] = '_';
			int i = 0, j = 0;
			for (i = row - 1; i <= row + 1; i++)
			{
				for (j = col - 1; j <= col + 1; j++)
				{
					
					Openboard(show, board, i,j);
				}
			}
		}
		else
		{
			return;
		}
	}
}

game.h

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define Easy_count 10
void Itnboard(char board[ROWS][COLS], int rows, int cols,char c);
void Displayboard(char board[ROWS][COLS], int row, int col);
void Setboard(char board[ROWS][COLS], int row, int col);
void Player(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void Openboard(char show[ROWS][COLS], char board[ROWS][COLS], int row, int col);

到此这篇关于C语言详细讲解通过递归实现扫雷的展开的文章就介绍到这了,更多相关C语言扫雷内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • c++只保留float型的小数点后两位问题

    c++只保留float型的小数点后两位问题

    这篇文章主要介绍了c++只保留float型的小数点后两位问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • 关于C++中由于字节对齐引起内存问题定位分析

    关于C++中由于字节对齐引起内存问题定位分析

    前几天遇到一个稀奇古怪的问题,在创建对象的时候程序异常退出,查找代码发现结构体数组问题,最终把问题简化得到解决方法,下面小编把我的问题及解决方案分享到脚本之家平台供大家参考下
    2021-06-06
  • getdate()函数的用法实例

    getdate()函数的用法实例

    getdate()函数的用法实例,需要的朋友可以参考一下
    2013-03-03
  • 用C语言实现二分查找算法

    用C语言实现二分查找算法

    大家好,本篇文章主要讲的是c语言实现二分查找法,感性的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2022-01-01
  • C++设计模式之观察者模式

    C++设计模式之观察者模式

    这篇文章主要介绍了C++设计模式之观察者模式,本文讲解了什么是观察者模式、观察者模式的UML类图、观察者模式的使用场合等内容,需要的朋友可以参考下
    2014-10-10
  • C++动态规划算法实现矩阵链乘法

    C++动态规划算法实现矩阵链乘法

    动态规划算法通常用于求解具有某种最优性质的问题。在这类问题中,可能会有许多可行解。每一个解都对应于一个值,我们希望找到具有最优值的解
    2022-06-06
  • C++中求旋转数组中的最小数字(经典面试题)

    C++中求旋转数组中的最小数字(经典面试题)

    这篇文章主要介绍了C++中求旋转数组中的最小数字(经典面试题)的相关资料,需要的朋友可以参考下
    2017-03-03
  • C++直接初始化与复制初始化的区别深入解析

    C++直接初始化与复制初始化的区别深入解析

    这篇文章主要介绍了C++直接初始化与复制初始化的区别深入解析,是很多C++初学者需要深入了解的重要概念,需要的朋友可以参考下
    2014-09-09
  • C++中register关键字举例详解

    C++中register关键字举例详解

    register用来声明变量,然后声明出来的变量是直接放在cpu的寄存器当中,而非就是通过内存寻址访问,这样效率更高,下面这篇文章主要给大家介绍了关于C++中register关键字的相关资料,需要的朋友可以参考下
    2023-03-03
  • C语言实现简易扫雷游戏详解

    C语言实现简易扫雷游戏详解

    这篇文章主要为大家详细介绍了C语言实现简易扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08

最新评论