用c语言实现经典小游戏———扫雷

  扫雷游戏是我们很多人都知道的一款经典小游戏,事实上,用c语言其实也可以实现扫雷游戏的玩法。一个扫雷游戏基本的功能有哪些呢?
  0> 如果第一次排雷恰好是雷,保证玩家不死
  1> 如果输入坐标的点不是雷,将周围不是雷的点展开
  e> 如果玩家被雷炸死,可以看到雷的分布
  2> 可以判断玩家是否排除所有雷
  3> 如果周围有雷,显示周围雷数
  如何实现一个函数可以展开周围的无雷区呢?我们可以考虑用递归,如果周围雷数为0,就一直遍历,且向上下左右四个方向,注意一定要记录该坐标是否被排查过,否则最后无法判断输赢。
  如何保证第一次为雷不被炸死?我们可以设置一个局部变量,记录排查的次数,如果值为1且输入坐标为雷,那么生成一个随即坐标让该坐标生成雷,当前输入坐标改为无雷。
  除此之外,我们应当考虑的是,遍历一个边界坐标周围雷数时,会存在越界,所以我们要将雷盘设置成11×11,且为了判断某坐标是否有雷和坐标周围雷数方便,我们需要两个相同的雷盘,一个用于存放雷,一个用来展示。
  
  代码如下:
#ifndef __GAME__H__
#define __GAME__H__
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROWS 11
#define COLS 11	
#define ROW  9
#define COL 9
#define count 10
//初始化雷阵
void Init(char arr[ROWS][COLS], int rows, int cols, char set);
//随机出雷
void Setbomb(char arr[ROW][COL], int row, int col);
//打印雷阵
void Displaybomb(char arr[ROWS][COLS], int row, int col);
//排查雷阵
void Findbomb(char show[ROWS][COLS],char bomb[ROWS][COLS],int row, int col);
//周围雷数
char  Bombcount(int x, int y,char bomb [ROWS][COLS]);
//排查周围雷数
void Expandbomb(int x, int y, char show[ROWS][COLS], char bomb[ROWS][COLS], int col, int row);
//判断输赢
int IS_win(char show[ROWS][COLS], int row, int col);
#endif // !__GAME__H__


#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Init( char arr[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = set;
		}
	}
}
void Setbomb(char arr[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int k = count;
	while (k)
	{
		x = rand()% row + 1;
	    y = rand()% col + 1;
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			k--;
		}
	}
}void Displaybomb(char arr[ROWS][COLS], int row, int col) {

	int i = 0;
	int j = 0;
	for (i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);

		}
		printf("\n");
	}
}

void Findbomb(char show[ROWS][COLS],char bomb[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int safecount = 0;
	while (IS_win( show, ROW,COL) - count)
	{
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			int i = 0;
			int j = 0;
			safecount++;
			if (bomb[x][y] == '1')
			{
				if (safecount == 1)
				{
					while (1)
					{
						i = rand() % 9 + 1;
						j = rand() % 9 + 1;
						if (bomb[i][j] == '0')
						{
							bomb[x][y] = '0';
							bomb[i][j] = '1';
							show[x][y] = Bombcount(x, y, bomb);
							Expandbomb(x, y, show, bomb, col, row);
							Displaybomb(show, ROW, COL);
							break;
						}
					}
					
				}
				else
				{
					printf("很遗憾,你被炸死\n");
					Displaybomb(bomb, ROW, COL);
					return;
				}
			}
			else
			{
			
				Expandbomb(x , y , show, bomb, col, row);
				Displaybomb(show, ROW, COL);
				printf("请继续输入你要排查的坐标:\n");

			}
		}
		else
		{
			printf("输入坐标有误,请重新输入:\n");
		}
	
	} 
	printf("恭喜你赢了\n");
	
}
char Bombcount(int x, int y,char bomb[ROWS][COLS])
{
	return (bomb[x - 1][y - 1] + bomb[x - 1][y] + bomb[x - 1][y + 1] + bomb[x][y - 1] +
		bomb[x][y + 1] + bomb[x + 1][y - 1] + bomb[x + 1][y] + bomb[x + 1][y + 1]) - 7*'0';
}
void Expandbomb(int x, int y, char show[ROWS][COLS], char bomb[ROWS][COLS],int col,int row)
{
	char a = Bombcount(x, y, bomb);
	
	if (bomb[x][y] == '1' )
	{
		return;
	}
	if (a == '0') {
		show[x][y] = a;
		if (x - 1 > 0 && show[x-1][y]=='*')Expandbomb(x-1, y, show, bomb, col, row);
		if (y - 1 > 0 && show[x][y - 1] == '*')Expandbomb(x , y - 1, show, bomb, col, row);
		if (x + 1 < ROWS-1 && show[x + 1][y] == '*')Expandbomb(x + 1, y, show, bomb, col, row);
		if (y + 1 < COLS-1 && show[x][y + 1] == '*')Expandbomb(x , y + 1, show, bomb, col, row);
	}
	else {
		show[x][y] = a;
	}
}
int  IS_win(char show[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	int minecount = 0;
	for (i = 1; i <= row; i++)
	{
		for (j = 1; j <= col; j++)
		{
			if (show[i][j] == '*')
			{
				minecount++;
			}
		}
	}
	return minecount;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
	printf("           - - - - - - - - - - - - - - - - - -\n");
	printf("           - - - - -    1. play      - - - - -\n"); 
	printf("           - - - - -    0. exit      - - - - -\n");
	printf("           - - - - - - - - - - - - - - - - - -\n");
}
void game()
{
	char bomb[ROWS][COLS] = { 0 }; //存放雷阵
	char show[ROWS][COLS] = { 0 }; //展示雷阵
	Init(bomb, ROWS, COLS, '0');
	Init(show, ROWS, COLS, '*');
	Setbomb(bomb, ROW, COL);
	//Displaybomb(bomb, ROW, COL);
	Displaybomb(show, ROW, COL);
	printf("请输入想要排查的坐标:\n");
	Findbomb(show,bomb, ROW, COL);
}
int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		menu();
		printf("请输入你的选择:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			    game();
				break;
		case 0:
			printf("退出游戏:\n");
			break;
		default:
			printf("输入有误,请重新输入");
			printf("\n");
		}
	} while (input);
	return 0;
}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值