IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 通过函数递归,实现简单的扫雷小游戏 -> 正文阅读

[C++知识库]通过函数递归,实现简单的扫雷小游戏

主函数:

#define _CRT_SECURE_NO_WARNINGS 1


#include "gamefounction.h"

void game()
{
	while (1)
	{
		//打印菜单
		menu();
		int choice = 0;
		printf("请输入:");
		scanf("%d", &choice);

		switch (choice)
		{
		case 1:
			gameplay();
			printf("是否继续游戏?\n");
			break;
		case 0:
			break;
		default:
			printf("输入不合法,请重新输入!\n");
			break;
		}
		if (0 == choice)
		{
			break;
		}
	}
	
}

int main()
{
	srand(time(NULL));
	game();

}

gameplay()函数为扫雷功能的实现函数:

其头文件:

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 19
#define COL 19

#define ROWS ROW+2
#define COLS COL+2

#define Count 5


void menu();
void gameplay();

//初始化
void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set);
//设置雷的位置
void SetLocation(char location[ROWS][COLS], int row, int col);
//打印界面
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//玩家选择位置
void UserMove(char location[ROWS][COLS], char board[ROWS][COLS], int row, int col);
//判断是否结束
int Count_geshu(char location[ROWS][COLS], char board[ROWS][COLS], int x, int y, int row, int col);

?源文件:

#define _CRT_SECURE_NO_WARNINGS 1

#include "gamefounction.h"


void menu()
{
	printf("********************************\n");
	printf("********   1.进行游戏   ********\n");
	printf("********   0.退出游戏   ********\n");
	printf("********************************\n");
}


void gameplay()
{
	char location[ROWS][COLS] = {0};
	char board[ROWS][COLS] = {0};

	//初始化雷的位置数组与棋盘数组
	InitBoard(location, ROWS, COLS, '0');
	InitBoard(board, ROWS, COLS, '*');
	//设置雷的位置
	SetLocation(location, ROW, COL);

	//打印棋盘
	DisplayBoard(location, ROW, COL);
	DisplayBoard(board, ROW, COL);

	//开始扫雷
	UserMove(location, board, ROW, COL);
}


void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			arr[i][j] = set;
		}
	}
}

void SetLocation(char location[ROWS][COLS], int row, int col)
{
	int cou = Count;
	while (cou)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if ('0' == location[x][y])
		{
			location[x][y] = '1';
			cou--;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	printf("   ");
	for (int i = 1; i <= row; i++)
	{
		printf(" %2d", i);
	}
	printf("\n");
	printf("   ");
	for (int i = 1; i <= row; i++)
	{
		printf("___");
	}
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%2d|", i);
		for (int j = 1; j <= col; j++)
		{
			printf("  %c", board[i][j]);
		}
		printf("\n");
		if (i < row)
			printf("  |\n");
		else
			printf("\n");
	}
}

void UserMove(char location[ROWS][COLS], char board[ROWS][COLS], int row, int col)
{
	int count = 0;
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("请输入所选择的位置:");
		scanf("%d%d", &x, &y);
		if (1 <= x && x <= row && 1 <= y && y <= col)
		{
			if ('1' == location[x][y])
			{
				printf("你踩到雷了,gg\n");
				DisplayBoard(location, ROW, COL);
				break;
			}
			else
			{
				count += Count_geshu(location, board, x, y, row, col);
				//printf("count = %d\n", count);
				if (count == ROW * COL - Count)
				{
					printf("恭喜你扫雷成功!\n");
					DisplayBoard(board, ROW, COL);
					/*printf("\n地雷位置如下:\n");
					DisplayBoard(location, ROW, COL);*/
					break;
				}
				DisplayBoard(board, ROW, COL);
			}
		}
		else
		{
			printf("输入不合法,请重新输入:");
		}
	}
	
}

int Count_geshu(char location[ROWS][COLS], char board[ROWS][COLS], int x, int y, int row, int col)
{

	if (1 <= x && x <= row && 1 <= y && y <= col)
	{
		if ('*' == board[x][y])
		{
			int shuliang = 0;
			/*int shuliang =
				location[x - 1][y] +
				location[x - 1][y + 1] +
				location[x][y + 1] +
				location[x + 1][y + 1] +
				location[x + 1][y] +
				location[x + 1][y - 1] +
				location[x][y - 1] +
				location[x - 1][y - 1] - 8 * '0';
			board[x][y] = shuliang + '0';*/
			for (int i = x - 1; i <= x + 1; i++)
			{
				for (int j = y - 1; j <= y + 1; j++)
				{
					shuliang += (location[i][j] - '0');
				}
			}
			board[x][y] = shuliang + '0';
			if ('0' == board[x][y])
			{
				board[x][y] = ' ';
				return 1 + Count_geshu(location, board, x-1, y, row, col) + Count_geshu(location, board, x-1, y+1, row, col)
						+ Count_geshu(location, board, x, y+1, row, col) + Count_geshu(location, board, x+1, y+1, row, col)
						+ Count_geshu(location, board, x+1, y, row, col) + Count_geshu(location, board, x+1, y-1, row, col)
						+ Count_geshu(location, board, x, y-1, row, col) + Count_geshu(location, board, x-1, y-1, row, col);
			}
			return 1;
		}
		return 0;
	}
	return 0;
}

????????其中Count_geshu函数为扫雷中实现用户操作并且对指定位置周围一圈雷的个数进行计算,如果计算得出周围存在雷,则将雷的个数赋予棋盘,如果没有雷,则将‘ ’赋予棋盘,同时递归,对其周围一圈棋盘上的雷个数进行计算。

? ? ? ? 首先判定这个坐标的合法性,然后在确定位置是否被计算,如果没被计算,则进行计算。

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-08 11:36:46  更:2021-10-08 11:37:56 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 3:53:32-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码