推箱子

发布时间 2023-08-08 20:34:32作者: 爱吃泡面的皮卡
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
    int c,d,h,s,u,t;
    char f;
    cout<<"请输入棋盘的大小"<<endl; 
    cin>>c>>d;
    cout<<"请输入棋子的坐标"<<endl;
    cin>>h>>s;
    cout<<"请输入箱子的坐标"<<endl;
    cin>>u>>t;
    while(1){
        for(int i=0;i<c;i++){
            for(int j=0;j<d;j++){
                if(i==h&&j==s){
                    cout<<setw(3)<<"A";
                }else if(i==u&&j==t){
                    cout<<setw(3)<<"@";
                }else{
                    cout<<setw(3)<<".";
                }
            }
            cout<<endl;
        }
        cout<<"wasd 四个方向移动"<<endl;
        cin>>f;
        if(f=='W'||f=='w'){
            h--;
            if(h<=0)
                h=0;
            if(s==t&&h==u){
                u--;
            }
            }else if(f=='S'||f=='s'){
                h++;
                if(h>=c)
                    h-=1;
                    if(s==t&&h==u){
                    u++;
                }
            }else if(f=='a'||f=='A'){
            s--;
            if(s<0)
                s=0;
                if(s==t&&h==u){
                    t--;
                }
            }else if(f=='D'||f=='d'){
                s++;
                if(s>=d)
                s-=1;
                if(s==t&&h==u){
                    t++;
                }
            }else if(f=='q'||f=='Q'){
                    break;
            }
            system("cls");
        }
    return 0;
}