简易推箱子代码

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
    //用于用户输入   
    char sr;
    //地图可以更改
    char a[11][20]={ 
     "###################",        
     "###################",        
     "###     $ $     ###",        
     "###    #####    ###",        
     "###             ###",        
     "#### ####  ### ####",        
     "###   #  @   #  ###",        
     "###   #  @   #  ###",        
     "###      ^      ###",        
     "###################",        
     "###################"};
    //定义坐标
    int i=1,e=8,f=9,m=7,n=9,x=6,y=9;        
    while(i)
    {
         //清屏此代码是在苹果电脑上运行的,如果是用vc++的话应该换成system("cls");
        system("clear");               
        printf("欢迎使用推箱子\n^就代表你,而你的工作就是把@推到$的位置,好好努力吧!\n");
        printf("对了w代表上,s代表下,a代表左,d代表右,q表示退出,开始吧!\n");
        //打印地图
        for(int b=0;b<11;b++)           
        {
            for(int c=0;c<20;c++)
            {
                printf("%c",a[b][c]);
            }
            printf("\n");
        }
        //判断是否推到正确位置
       if(m== 2 && n==8)               
        {
            if(x==2 && y==10)
            {
                printf("恭喜你挑战成功!\n");
                break;
            }
        }
        else if(m==2 && n==8)
        {
           if(x==2 && y==10)
           {
                printf("恭喜你挑战成功!\n");
                break;
           }
        }
        //清除缓冲区,字符必须写,因为回车也是一个字符,所以用scanf(“%c”)必须写
        fpurge(stdin);
        scanf("%c",&sr);
        switch(sr)//判断上下左右
        {
            case 'w':
            case 'W':
                if(a[e-1][f]==' ')//上面是否可以走
                {
                    a[e-1][f]='^';
                    a[e][f]=' ';
                    e-=1;
                }
                else if(a[e-1][f]=='@' && a[e-2][f]==' ')//上面是否是箱子而且箱子上面是否有墙或者箱子
                {
                    if(e-m==1 && f==n)//判断人和箱子是否在一条线上
                    {
                        m-=1;
                        a[m][n]='@';
                        a[e-1][f]='^';
                        a[e][f]=' ';
                        e-=1;
                    }
                    else if(e-x==1 && f==y)//判断人和箱子是否在一条线上因为这个上面有两个
                    {
                       x-=1;
                      a[x][y]='@';
                      a[e-1][f]='^';
                      a[e][f]=' ';
                      e-=1; 
                    }
                }
                break;
            case 's':
            case 'S':
                if(a[e+1][f]==' ')//下面是否可以走
                {
                    a[e+1][f]='^';
                    a[e][f]=' ';
                    e+=1;
                }
                else if(a[e+1][f]=='@' && a[e+2][f]==' ' )//下面是否是箱子而且箱子上面是否有墙或者箱子
                {
                    if(m-e==1 && f==n)//判断人和箱子是否在一条线上
                    {
                        m+=1;
                        a[m][n]='@';
                        a[e+1][f]='^';
                        a[e][f]=' ';
                        e+=1;
                    }
                    else if(x-e==1 && y==f)//判断人和箱子是否在一条线上因为这个上面有两个
                    {
                        x+=1;
                        a[x][y]='@';
                        a[e+1][f]='^';
                        a[e][f]=' ';
                        e+=1;
                    }
                }
                break;
            case 'a':
            case 'A':
                if(a[e][f-1]==' ')//上面是否可以走
                {
                    a[e][f-1]='^';
                    a[e][f]=' ';
                    f-=1;
                }
                else if(a[e][f-1]=='@' && a[e][f-2]==' ')//左面是否是箱子而且箱子左面是否有墙或者箱子
                {
                    if(e==m && f-n==1)//判断人和箱子是否在一条线上
                    {
                        n-=1;
                        a[m][n]='@';
                        a[e][f-1]='^';
                        a[e][f]=' ';
                        f-=1;
                    }
                    else if(e==x && f-y==1)//判断人和箱子是否在一条线上因为这个上面有两个
                    {
                        y-=1;
                        a[x][y]='@';
                        a[e][f-1]='^';
                        a[e][f]=' ';
                        f-=1;
                    }
                }
                break;
            case 'd':
            case 'D':
                if(a[e][f+1]==' ')//右面是否可以走
                {
                    a[e][f+1]='^';
                    a[e][f]=' ';
                    f+=1;
                }
                else if(a[e][f+1]=='@' && a[e][f+2]==' ')//右面是否是箱子而且箱子右面是否有墙或者箱子
                {
                    if(e==m && n-f==1)//判断人和箱子是否在一条线上
                    {
                        n+=1;
                        a[m][n]='@';
                        a[e][f+1]='^';
                        a[e][f]= ' ';
                        f+=1;
                    }
                    else if(e==x && y-f==1)//判断人和箱子是否在一条线上因为这个上面有两个
                    {
                        y+=1;
                        a[x][y]='@';
                        a[e][f+1]='^';
                        a[e][f]=' ';
                        f+=1;
                    }
                }
              break;  
            case 'q':
            case 'Q':exit(0);//退出
        }
    }
    return 0;
}

你可能感兴趣的:(C语言,推箱子。)