推箱子一关(熟悉的一关)



如果要改地图,只要改字符串部分、人与出口的位置和showmap那里的i和j就好了

#include<iostream>
#include<windows.h>
using namespace std;

int irow=1,icol=1;//人的位置
int c1row=3,c1col=7,c2row=4,c2col=7,c3row=5,c3col=7;//出口位置

char A[9][10]={
{"#########"},
{"#i  #####"},
{"# oo#####"},
{"# o ###c#"},
{"### ###c#"},
{"###    c#"},
{"##   #  #"},
{"##   ####"},
{"#########"}
};

void showmap();
void step(char n);
int iswin();

//恢复被删去的c
void renew()
{
 if(A[c1row][c1col]==' ')
 {
  A[c1row][c1col]='c';
 }
 if(A[c2row][c2col]==' ')
 {
  A[c2row][c2col]='c';
 }
 if(A[c3row][c3col]==' ')
 {
  A[c3row][c3col]='c';
 }
}

//显示地图
void showmap()
{
 int i,j;
 for(i=0;i<=8;i++)
 {
  for(j=0;j<=8;j++)
  {
   cout<<A[i][j];
  }
  cout<<endl;
 }
}

//一步判断
void step(char n)
{
 int r,c;
 r=irow;c=icol;
 if(n=='w')
 {
  r=r-1;
 }
 else if(n=='a')
 {
  c=c-1;
 }
 else if(n=='d')
 {
  c=c+1;
 }
 else if(n=='s')
 {
  r=r+1;
 }
 if(A[r][c]==' '||A[r][c]=='c')
 {
  A[irow][icol]=' ';
  A[r][c]='i';
  irow=r;icol=c;
 }
 else if(A[r][c]=='o')
 {
  if(A[2*r-irow][2*c-icol]==' '||A[2*r-irow][2*c-icol]=='c')  //人隔着箱子正对的地方
  {
   A[irow][icol]=' ';
   A[r][c]='i';
   A[2*r-irow][2*c-icol]='o';
   irow=r;icol=c;
  }
 }
}

//胜利
int iswin()
{
 if(A[c1row][c1col]=='o'&&A[c2row][c2col]=='o'&&A[c3row][c3col]=='o')
 {
  return 1;
 }
 return 0;
}

//主控
int main()
{
 char n;
 while(1)
 {
  renew();
  showmap();
  cin>>n;
  step(n);
  if(iswin())
  {
   cout<<"You Win!"<<endl;
   break;
  }
  system("cls");
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值