ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-10-07 21:30:31  阅读:166  来源: 互联网

标签:ROWS 递归 int void COLS char 小游戏 扫雷 location


主函数:

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

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

标签:ROWS,递归,int,void,COLS,char,小游戏,扫雷,location
来源: https://blog.csdn.net/MLuhuihui/article/details/120641937

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有