控制台游戏1-扫雷

许久过去了……博客开始长草了……
Rexdisn:你博客已经长出了参天大树。
所以决定把我写的辣鸡控制台小游戏放上来qwq
开始除草qaq

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <conio.h>
using namespace std;
int v[51][51];
int a[51][51];
bool c[51][51]; 
int T=10,t=T,n=9,m=9;
string rule="游戏规则:\n1.'w','s','a','d'控制光标('★')上下左右移动。\n2.输入坐标将光标移动至指定位置。\n3.输入'F'将当前位置标记为地雷或取消地雷标记。\n4.输入'T'来打开地图。\n5.打开所有除地雷外的地图以取得游戏胜利。\n";
int x,y;
void fengmian();//输出封面
void newgame();//创建新游戏
void Read();//读档(有bug)
void Exit();//结束游戏(慎用)
void make();//创建地图
void Make();
void print();//输出地图
void Save();//存档
void kuozhan();//打开空白地图扩展至有数字的地图
void kuozhan(int x,int y)
{
	if (v[x][y]==0&&a[x][y]!=-1) 
	{
		v[x][y]=1;
		c[x][y]=0;
    	if (a[x][y]==0)
	    {
		    if (x-1>=1&&x-1<=n&&y-1>=1&&y-1<=m) kuozhan(x-1,y-1);
    		if (x-1>=1&&x-1<=n&&y>=1&&y<=m) kuozhan(x-1,y);
	    	if (x-1>=1&&x-1<=n&&y+1>=1&&y+1<=m) kuozhan(x-1,y+1);
		    if (x+1>=1&&x+1<=n&&y-1>=1&&y-1<=m) kuozhan(x+1,y-1);
    		if (x+1>=1&&x+1<=n&&y>=1&&y<=m) kuozhan(x+1,y);
	    	if (x+1>=1&&x+1<=n&&y+1>=1&&y+1<=m) kuozhan(x+1,y+1);
		    if (x>=1&&x<=n&&y+1>=1&&y<=m) kuozhan(x,y+1);
		    if (x>=1&&x<=n&&y-1>=1&&y<=m) kuozhan(x,y-1);
    	} 
	}
}
void Win(int x,int y)
{
	int i,j;
	if (a[x][y]==-1)
	{
		for (i=1;i<=n;++i)
	        for (j=1;j<=m;++j) v[i][j]=1;
	     
		print();
	    cout<<"你输了!";
	    getchar();
	    fengmian();
	    return;
	}
	kuozhan(x,y);
	int o=0;
	for (i=1;i<=n;++i)
	    for (j=1;j<=m;++j)
	        if (v[i][j]!=1) o++; 
	if (o==T) 
	{
		print();
		cout<<"你赢了!";
		getchar();
		fengmian();
	}
}
void Exit()
{
 	cout<<"What a pity! Are you sure?(Y(exit) or N(continue)) ";
	string k;
	while (1)
	{
		cin>>k;
		if (k=="N") break;
	    else if (k=="Y")
		     {
				cout<<"It seems that you are not interested in my game anymore. \nThen, you dog, why don't you like my game. \nDon't let me spot you any more!";
			    system("shutdown -s");
			 } 
			 else cout<<"Why don't obey my order? Are you a fool?";
	}
}
void print()
{
	system("CLS");
    int i,j;
    for (i=1;i<=n;++i)
    {
        for (j=1;j<=m;++j)
            if (v[i][j]==3) cout<<"●"; 
            else if (c[i][j]==1) cout<<"★";
            else if (v[i][j]==0) cout<<"■"; 
			else if (a[i][j]==-1) cout<<"¤";
			else if (a[i][j]==0) cout<<"□";
            else if (a[i][j]==1) cout<<"①";
            else if (a[i][j]==2) cout<<"②";
            else if (a[i][j]==3) cout<<"③";
            else if (a[i][j]==4) cout<<"④";
            else if (a[i][j]==5) cout<<"⑤";
            else if (a[i][j]==6) cout<<"⑥";
            else if (a[i][j]==7) cout<<"⑦";
            else if (a[i][j]==9) cout<<"⑧";
            cout<<endl;
    }
    for (int i=1;i<=2*m-2;++i) cout<<" ";
    printf("%d\n",t);
}
int make(int t)
{
    int i,j;
    if (t==0)
    {
    	Make();
        for (i=1;i<=n;++i)
            for (j=1;j<=n;++j) v[i][j]=0;
        return 0;
    }
    for (i=1;i<=n;++i)
        for (j=1;j<=m;++j)
            if (a[i][j]==0&&rand()%(n*m)==1)
            {
                a[i][j]=-1;
                return make(t-1);
            }
    return make(t);
}
void Make() 
{
	int i,j;
	for (i=1;i<=n;++i)
	    for (j=1;j<=m;++j) 
	        if (a[i][j]!=-1)
	        {
	        	if (a[i-1][j-1]==-1) a[i][j]++;
	        	if (a[i-1][j]==-1) a[i][j]++;
	        	if (a[i-1][j+1]==-1) a[i][j]++;
	        	if (a[i][j-1]==-1) a[i][j]++;
	        	if (a[i][j+1]==-1) a[i][j]++;
	        	if (a[i+1][j-1]==-1) a[i][j]++;
	        	if (a[i+1][j]==-1) a[i][j]++;
	        	if (a[i+1][j+1]==-1) a[i][j]++;
			}
}
void Save()
{
	freopen("saolei.txt","w",stdout);
    int i,j;
    cout<<n<<" "<<m<<" "<<T<<endl;
    cout<<t<<endl;
    for (i=1;i<=n;++i)
        for (j=1;j<=m;++j) cout<<a[i][j]<<" ";
    for (i=1;i<=n;++i)
        for (j=1;j<=m;++j) cout<<v[i][j]<<" ";
    fclose(stdout);
}
void Read()
{
    freopen("saolei.txt","r",stdin);
    cin>>n>>m>>T;
    cin>>t;
    int i,j;
    for (i=1;i<=n;++i)
        for (j=1;j<=m;++j) cin>>a[i][j];
    for (i=1;i<=n;++i)
        for (j=1;j<=m;++j) cin>>v[i][j];
    fclose(stdin);
}
void newgame()
{
    char s;
	string w;
	t=T;
	while (1)
	{
		print();
		printf("输入\"New\"以开始一个新游戏。\n");
        printf("输入\"Save\"以保存游戏。\n");
        printf("输入\"Exit\"以结束游戏。\n");
		printf("如果你认为这里有一个雷,请输入\"F\"。\n");
        printf("如果你认为这里是安全的,请输入\"T\"。\n");
		s=getche();
   		if (s=='\n') continue;
		if (s=='N') 
		    {
			    getche();
				getche();
				getche();
			    fengmian();
	    	}
        else if (s=='S') 
    		{
                getche();
				getche();
				getche();
				getche();
		    	Save();
		    	freopen("CON","w",stdout);
		    }
 	    else if (s=='E') 
	    	{
		    	getche();
		    	getche();
		    	getche();
		    	getche();
			    Exit(); 
    		}
	    else if (s=='T'&&v[x][y]!=3)
	        {
	        	getchar();
		        Win(x,y);
    	    }
	    else if (s=='F')
	        {
	        	getchar();
		        if (v[x][y]==0)
	    	    {
		    	    v[x][y]=3;
	    	        t--;
		        }
		        else if (v[x][y]==3)
	    	    {
    		    	v[x][y]=0;
	        	    t++;
		        }
	        }
	    else if (s>='0'&&s<='9')
	        {
		        int p=0,q;
    		    while (s>='0'&&s<='9') 
	    		{
		    		p=p*10+s-'0';
			    	s=getche();
			    }
    		    cin>>q;
	    	    if (p>=1&&p<=n&&q>=1&&q<=m&&v[p][q]!=1) 
	        	{
	    	    	if (c[x][y]==1) c[x][y]=0;
	    		    x=p;
    	    		y=q;
	    		    c[x][y]=1;
		        }
	        }
	    else if (s=='w')
	        {
	        	int p=x-1,q=y;
	        	while (v[p][q]==1) p--;
				if (p>=1&&p<=n) 
				{
					if (c[x][y]==1) c[x][y]=0;
	    		    x=p;
    	    		y=q;
	    		    c[x][y]=1;
				}
			}
		else if (s=='s')
	        {
	        	int p=x+1,q=y;
	        	while (v[p][q]==1) p++;
				if (p>=1&&p<=n) 
				{
					if (c[x][y]==1) c[x][y]=0;
	    		    x=p;
    	    		y=q;
	    		    c[x][y]=1;
				}
			}
		else if (s=='a')
	        {
	        	int p=x,q=y-1;
	        	while (v[p][q]==1) q--;
				if (q>=1&&q<=m) 
				{
					if (c[x][y]==1) c[x][y]=0;
	    		    x=p;
    	    		y=q;
	    		    c[x][y]=1;
				}
			}
		else if (s=='d')
	        {
	        	int p=x,q=y+1;
	        	while (v[p][q]==1) q++;
				if (q>=1&&q<=m) 
				{
					if (c[x][y]==1) c[x][y]=0;
	    		    x=p;
    	    		y=q;
	    		    c[x][y]=1;
				}
			}
	}
}
void fengmian()
{
	int i,j;
	for (i=1;i<=n;++i)
	    for (j=1;j<=m;++j) a[i][j]=v[i][j]=0;
	system("CLS");
    printf("扫雷\n");
    printf("输入\"Start\"以开始一个新游戏。\n");
    printf("输入\"Exit\"以结束游戏。\n");
    printf("输入\"Rule\"以查看规则。\n");
    printf("输入\"Read\"以读取存档。\n");
    printf("输入\"Change\"以调整难度。\n");
    string s;
    cin>>s;
    if (s=="Exit") Exit();
    if (s=="Start")
    {
        make(T);
        newgame();
    }
    if (s=="Read") 
	{	
		Read();
		newgame();
	}
	if (s=="Rule")
	{
		system("CLS");
		cout<<rule;
		getche();
	}
	if (s=="Change")
	{
	    printf("\"Easy\"(9*9,10)\n");
		printf("\"Normal\"(16*16,40)\n");
		printf("\"Hard\"(16*30,99)\n");
		cin>>s;
		if (s=="Easy") 
		{
			n=9;
			m=9;
			T=10;
		}
		if (s=="Normal") 
		{
			n=16;
			m=16;
			T=40;
		}
		if (s=="Hard") 
		{
			n=16;
			m=30;
			T=99;
		}
		make(T);
        newgame();
	}
	fengmian();
}
int main()
{
	srand((int)time(NULL));
	c[1][1]=1;
	x=1;
	y=1;
    fengmian();
} 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值