通过函数递归,实现简单的扫雷小游戏

主函数:

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 MATLAB 扫雷小游戏的步骤: 1. 创建一个空白的图形窗口。你可以使用 MATLAB 的 "figure" 函数来创建一个新的图形窗口。 2. 创建一个矩阵来表示扫雷棋盘。你可以使用 MATLAB 的 "zeros" 函数创建一个矩阵,然后将某些元素设置为 "1" 来表示雷的位置。 3. 在棋盘上随机放置一些雷。你可以使用 MATLAB 的 "randperm" 函数来随机选择一些位置,并将这些位置设置为 "1"。 4. 计算每个格子周围的雷的数量。你可以使用 MATLAB 的 "conv2" 函数来计算每个格子周围的 8 个格子中有多少个雷,然后将这个数量保存在相应的格子中。 5. 循环显示棋盘,并等待用户点击一个格子。你可以使用 MATLAB 的 "while" 循环来不断地显示棋盘,并等待用户输入。每当用户点击一个格子时,你需要检查这个格子是否是雷,如果是,游戏结束;如果不是,你需要显示这个格子周围的雷的数量,并标记这个格子已经被点击过。 6. 如果用户点击的格子周围没有雷,你需要递归地显示周围的格子。你可以使用 MATLAB 的 "find" 函数来搜索周围的格子,并递归地显示这些格子。 7. 当用户点击所有非雷的格子时,游戏胜利。你可以使用一个计数器来记录用户点击的非雷格子的数量,当这个计数器等于总格子数减去雷的数量时,游戏胜利。 这些是实现 MATLAB 扫雷小游戏的一些基本步骤,你可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值