练习的时候写的一个小游戏-开箱子
#include
#include
#include<conio.h>
#include<unistd.h>
#include
using namespace std;
int jf(int a);
int jifen;
int xianjia;
class lei
{
private:
int a[20][20];
public:
lei()
{
for(int b=0;20>b;b++)
{
for(int c=0;20>c;c++)
{
a[b][c]=0;
}
}
a[0][0]=1;
}
void dayin()
{
system("cls");
for(int d=0;20>d;d++)
{
for(int e=0;20>e;e++)
{
if(a[d][e]==0)
{
cout<<"▪ ";
}
else if(a[d][e]==1)
{
cout<<"▣ ";
}
else if(a[d][e]==2)
{
cout<<"🍌 ";
}
else if(a[d][e]==3)
{
cout<<"🍎 ";
}
else if(a[d][e]==4)
{
cout<<"🍉 ";
}
else if(a[d][e]==5)
{
cout<<"💣 ";
}
}
cout<<endl;
}
cout<<"本次得分:"<<jf(xianjia)<<endl;
cout<<"积分:"<<jifen<<endl;
/* if(jifen<0)
{
system(“cls”);
cout<<“你已输掉本局游戏”<<endl;
}*/
if(jifen>200)
{
system(“cls”);
cout<<“你已赢得本局比赛”<<endl;
}
}
void xieru(int y,int x,int shu)
{
xianjia=shu;
if(shu==5)
{
jifen-=50;
}
else if(shu==4)
{
jifen+=20;
}
else if(shu==3)
{
jifen+=5;
}
else if(shu==2)
{
jifen+=10;
}
a[y][x]=shu;
}
int duqu(int yy,int xx)
{
return a[yy][xx];
}
};
int sjs(int a,int b);
int main()
{
int x=0,y=0;
lei api;
string ch;
api.dayin();
for(;😉
{
if(kbhit())
{
ch=getch();
if(ch==“2” && y!=0)
{
ch="";
y–;
if(api.duqu(y,x)<2)
{
api.xieru(y,x,1);
if(api.duqu(y+1,x)==1)
{
api.xieru(y+1,x,0);
}
api.dayin();
}
else
{
if(api.duqu(y+1,x)==1)
{
api.xieru(y+1,x,0);
}
}
}
if(ch=="4" && x!=0)
{
ch="";
x--;
if(api.duqu(y,x)<2)
{
api.xieru(y,x,1);
if(api.duqu(y,x+1)==1)
{
api.xieru(y,x+1,0);
}
api.dayin();
}
else
{
if(api.duqu(y,x+1)==1)
{
api.xieru(y,x+1,0);
}
}
}
if(ch=="6" && x!=20)
{
ch="";
x++;
if(api.duqu(y,x)<2)
{
api.xieru(y,x,1);
if(api.duqu(y,x-1)==1)
{
api.xieru(y,x-1,0);
}
api.dayin();
}
else
{
if(api.duqu(y,x-1)==1)
{
api.xieru(y,x-1,0);
}
}
}
if(ch=="8" && y!=20)
{
ch="";
y++;
if(api.duqu(y,x)<2)
{
api.xieru(y,x,1);
if(api.duqu(y-1,x)==1)
{
api.xieru(y-1,x,0);
}
api.dayin();
}
else
{
if(api.duqu(y-1,x)==1)
{
api.xieru(y-1,x,0);
}
}
}
if(ch=="5" && api.duqu(y,x)==1)
{
ch="";
api.xieru(y,x,sjs(2,5));
api.dayin();
}
}
}
}
int sjs(int a,int b)
{
srand((unsigned)time(NULL));
return a+(rand()%(b-a+1));
}
int jf(int a)
{
switch(a)
{
case 0:
return 0;
break;
case 1:
return 0;
break;
case 2:
return 10;
break;
case 3:
return 5;
break;
case 4:
return 20;
break;
case 5:
return -50;
break;
}
}