c++制作小游戏——扫雷

#include<ctime>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;
int map[12][12]; 
int derection[3]={0,1,-1};
int calculate(int x,int y)
{
	int counter=0;
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			if (map[x+derection[i]][y+derection[j]]==9 )
				counter++;
			return counter;
}
void game(int x,int y)
{
    if(calculate(x,y)==0)
	{
		map[x][y]=0;
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
				if(x+derection[i]<=9&&y+derection[j]<=9&&x+derection[i]>=1&&y+derection[j]>=1&&!(derection[i]==0&&derection[j]==0)&&map[x+derection[i]][y+derection[j]]==-1) 
					game(x+derection[i],y+derection[j]); 
		}                 
	}
	else
		map[x][y] = calculate(x,y);
}
void print()
{
	for(int i=1;i<10;i++)
	{
		for(int j=1;j<10;j++)
		{
			if(map[i][j]==-1||map[i][j]==9)
				cout<<"#"<<" ";
			else
			{
				if(map[i][j]==0) cout<<"o ";
				else
				cout<<map[i][j]<<" ";
			}
		}
		cout<<endl;
	}
}
bool check ()
{
	int counter=0;
	for (int i=1;i<10;i++)
		for (int j=1;j<10;j++)
			if(map[i][j]!=-1)
				counter++;
			if(counter==10)
				return true;
			else
				return false;
}
int main ()
{
	int i,j,x,y;
	char ch;
	srand(time(0));
	do
	{
		memset(map,-1,sizeof(map));
		for(i=0;i<10;)
		{
			x=rand()%9+1;
			y=rand()%9+1;
			if ( map[x][y]!=9 )
			{
				map[x][y]=9;
				i++;
			}
		}
		for(i=1;i<10;i++ )
		{
			for(j=1;j<10;j++ )
				cout<<"#"<<" ";
			cout<<"\n";
		}
		cout<<"\n";
		cout<<"Please enter a coordinate:(x,y)\n";
		while(cin>>x>>y)
		{
			if(map[x][y]==9)
			{
				cout<<"GAME OVER"<<endl;   
				for(i=1;i<10;i++)
				{
					for(j=1;j<10;j++ )
					{
						if(map[i][j]==9 )
							cout<<"@ ";
						else
							cout<<"#"<<" ";
					}
					cout<<endl;
				}
				break;
			}
			game(x,y);
			print();
			cout<<"Please enter a coordinate:(x,y)\n";
			if(check())
			{
				cout<<"YOU WIN"<<endl;
				break;
			}
			cout<<"\n";
		}	
		cout<<"Do you want to play again?\nenter Y or N"<<endl;
		cin>>ch;
		cout<<"\n";
	}while(ch=='Y');	
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值