扫雷游戏的拓展

扫雷游戏拓展版

  • 拓展内容:
  • 1>第一次选中坐标,不会被炸死。
  • 2>坐标周围没雷,可以实现展开,并且坐标显示周围雷的个数。
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>

#define ROW 9    //显示的雷盘
#define COL 9

#define ROWS ROW+2   //实际操作的雷盘
#define COLS COL+2

#define EASY_COUNT 10

void Init(char arr[ROWS][COLS],int rows,int cols,char syl);//初始化雷盘
void Display(char arr[ROWS][COLS],int row,int col);//打印雷盘
void Setboard(char arr[ROWS][COLS],int row,int col);//随机布雷
int Findboard(char board[ROWS][COLS],char show[ROWS][COLS],int row,int col);//扫雷
int Getboard(char board[ROWS][COLS],int x,int y);//统计x y周围的雷数


#endif
#include"game.h"

void Init(char arr[ROWS][COLS],int rows,int cols,char syl)
{
	int i=0;
	int j=0;
	for(i=0;i<rows;i++)
	{
		for(j=0;j<cols;j++)
		{
			arr[i][j]=syl;
		}
	}
}
void Display(char arr[ROWS][COLS],int row,int col)
{
	int i=0;
	int j=0;
	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  ",arr[i][j]);
		}
		printf("\n");
	}
	printf("_________________________\n");
}
void Setboard(char arr[ROWS][COLS],int row,int col)
{
	int count=EASY_COUNT;
	while(count)
	{
    int x=rand()%9+1;
	int y=rand()%9+1;
	if(arr[x][y]=='0')
	{
		arr[x][y]='1';
	    count--;
	}
	}
}
int Getboard(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+1][y+1]+
	board[x+1][y]+
	board[x+1][y-1]+
	board[x][y-1]-8*'0');
}
void move(char board[ROWS][COLS],int x,int y)	//如果第一次点的坐标有雷,
         //把它重新置无雷,并且,在别的没有雷的地方布一个雷				
{
		int a=rand()%9+1;
		int b=rand()%9+1;

		if((board[a][b]=='0')&&((x!=a)||(y!=b)))
			board[a][b]='1';
}
int ret_s(char show[ROWS][COLS],int row,int col)
{
	int i=0;
	int j=0;
	int ret=0;
	for(i=1;i<=ROW;i++)
	{
		for(j=1;j<=COL;j++)
		{
			if(show[i][j]=='*')
				ret++;
		}
	}
	return ret;
}
int Findboard(char board[ROWS][COLS],char show[ROWS][COLS],int row,int col)
{
	int x=0;
	int y=0;
	int ret=0;
	int flag=0;
	int count=0;
	while(ret_s(show,row,col)>EASY_COUNT)
	{
	printf("请输入要排查的坐标:>");
	scanf_s("%d %d",&x,&y);
	if(x>=1&&x<=row&&y>=1&&y<=col)
	{
		flag++;
		if(board[x][y]=='1'&&flag==1)
			{	
				board[x][y]= '0';
				move(board,x,y);
				 expand(board ,show,x,y);
			    Display(show,ROW,COL);
				// Display(board,ROW,COL);
				} 
		else if(board[x][y]=='1')
         {
			printf("很遗憾,你被雷炸了\n");
			return 0;
		}
		else 
		{
          expand(board ,show,x,y);
		Display(show,ROW,COL);
	   }
	}
	else
		printf("坐标非法\n");
  }
	
		printf("恭喜你,游戏胜利!\n");
		Display(board,ROW,COL);
	
}
int expand(char board[ROWS][COLS] ,char show[ROWS][COLS],int x,int y)
{
	int i=0;
	int j=0;
	//int count=0;
	if(((x>=1)&&(x<=ROWS-2)&&(y>=1)&&(y<=COLS-2))&&show[x][y]=='*')
	{	
		int count=Getboard(board,x,y);
		if(count==0)
		{
			show[x][y]=' ';
			for(i=-1;i<=1;i++)
			{
				for(j=-1;j<=1;j++)
				{
					 if(i!=0||j!=0)
						expand(board ,show,x+i,y+j);
				}
			}
	    }
	else
		{
			show[x][y]=count+'0';
			return 0;
		}
	}
	return 0;
}
#include"game.h"
void menu()
{
	printf("**************************\n");
	printf("****   1 : piay       ****\n");
	printf("****   0 : exit       ****\n");
	printf("**************************\n");

}
void game()
{
	char board[ROWS][COLS]={0};
	char show[ROWS][COLS]={0};
	Init(board,ROWS,COLS,'0');
	Init(show,ROWS,COLS,'*');
	Setboard(board,ROW,COL);
	Display(board,ROW,COL);
	Display(show,ROW,COL);
	Findboard(board,show,ROW,COL);

}
int main()
{
	
	int input=0;
     srand((unsigned int)time(NULL));
	 
	do
	{
		menu();
		printf("请选择>");
		scanf("%d",&input);
		switch(input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	}
	while(input);
	//game();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/WZL995/article/details/82846872