求大家帮忙看看将军部分
查看原帖
求大家帮忙看看将军部分
961351
nightwatch.ryan楼主2024/8/6 16:51
#include<iostream>
#include<cmath>
int chess_board[10][9]={/*棋盘*/
    {5,4,3,2,1,2,3,4,5},
    {0,0,0,0,0,0,0,0,0},
    {6,0,0,0,0,0,0,0,6},
    {7,0,7,0,7,0,7,0,7},
    {0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0},
    {-7,0,-7,0,-7,0,-7,0,-7},
    {-6,0,0,0,0,0,0,0,-6},
    {0,0,0,0,0,0,0,0,0},
    {-5,-4,-3,-2,-1,-2,-3,-4,-5}
};
int operator_people=1;/*当前是谁在操作,1=red,-1=blue*/
bool Game_Over;/*判断游戏是否结束,0=no,1=yes*/
namespace Checker{
    bool checkCaptain(int sx,int sy,int fx,int fy){
        if(sx-1==fx&&sy==fy){
            return true;/*{-1,0}*/
        }else if(sx+1==fx&&sy==fy){
            return true;/*{+1,0}*/
        }else if(sy-1==fy&&sx==fx){
            return true;/*{0,-1}*/
        }else if(sy+1==fy&&sx==fx){
            return true;/*{0,+1}*/
        }
        return false;
    }
    bool checkGuard(int sx,int sy,int fx,int fy){
        if(sx-1==fx&&sy-1==fy){
            return true; /*{-1,-1}*/
        }else if(sx+1==fx&&sy+1==fy){
            return true;/*{+1,+1}*/
        }else if(sx-1==fx&&sy+1==fy){
            return true;/*{-1,+1}*/
        }else if(sx+1==fx&&sy-1==fy){
            return true;/*{+1,-1}*/
        }
        return false;
    }
    bool checkElephant(int sx,int sy,int fx,int fy){
        for(int x=-1;x<=1;x+=2){
            for(int y=-1;y<=1;y+=2){
                if(chess_board[sx+x][sy+y]==0){/*这个上面没有棋子*/
                    int nx=sx+x*2,ny=sy+y*2;
                    if(nx==fx&&ny==fy){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    bool checkHorse(int sx,int sy,int fx,int fy){
        for(int x=-1;x<=1;x+=2){
            for(int y=-1;y<=1;y+=2){
                if(chess_board[x+sx][y]==0){
                    int nx=sx+x*2,ny=sy+y;
                    if(nx==fx&&ny==fy){
                        return true;
                    }
                }
                if(chess_board[sx][y+sy]==0){
                    int nx=x+sx,ny=sy+y*2;
                    if(nx==fx&&ny==fy){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    bool checkDuck(int sx,int sy,int fx,int fy){
        for(int x=-1;x<=1;x+=2){
            for(int y=-1;y<=1;y+=2){
                if(chess_board[sx+x*2][sy+y*2]==0&&
                   chess_board[sx+x][sy]==0){
                    int nx=sx+x*3,ny=sy+y*2;
                    if(nx==fx&&ny==fy){
                        return true;
                    }
                }
                if(chess_board[sx+x][sy+y*2]==0&&
                   chess_board[sx][y+sy]==0){
                    int nx=x*2+sx,ny=y*3+sy;
                    if(nx==fx&&ny==fy){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    bool checkSoldier(int sx,int sy,int fx,int fy){
        /**
         * {-1,0}
         * {+1,0}
         * {0,-1}
         * {0,+1}
         * {-1,+1}
         * {-1,-1}
         * {+1,-1}
         * {+1,+1}
         */
        if(sx-1==fx&&sy==fy){
            return true;
        }else if(sx+1==fx&&sy==fy){
            return true;
        }else if(sx==fx&&sy-1==fy){
            return true;
        }else if(sx==fx&&sy+1==fy){
            return true;
        }else if(sx-1==fx&&sy+1==fy){
            return true;
        }else if(sx-1==fx&&sy-1==fy){
            return true;
        }else if(sx+1==fx&&sy-1==fy){
            return true;
        }else if(sx+1==fx&&sy+1==fy){
            return true;
        }
        return false;
    }
    bool checkCar(int sx,int sy,int fx,int fy){
        if(sx!=fx&&sy!=fy){
            return false;
        }else if(sx==fx&&sy<fy){
            for(int i=sy+1;i<fy;i++){
                if(chess_board[sx][i]){
                    return false;
                }
            }
            return true;
        }else if(sx==fx&&sy>fy){
            for(int i=fy+1;i<sy;i++){
                if(chess_board[sx][i]){
                    return false;
                }
            }
            return true;
        }else if(sx<fx&&sy==fy){
            for(int i=sx+1;i<fx;i++){
                if(chess_board[i][sy]){
                    return false;
                }
            }
            return true;
        }else if(sx>fx&&sy==fy){
            for(int i=fx+1;i<sx;i++){
                if(chess_board[i][sy]){
                    return false;
                }
            }return true;
        }
        return false;
    }
}
using namespace Checker;
/*    Red:          Blue:
      1.captain     -1.captain
      2.guard       -2.guard
      3.elephant    -3.elephant
      4.horse       -4.horse
      5.car         -5.car
      6.duck        -6.duck
      7.soldier     -7 soldier

      Captain:     Guard:       Elephant:          Horse:             Car:
      ----------   ----------   ----------------   ----------------   ----------------
      |  | *|  |   | *|  | *|   | *|  |  |  | *|   |  | *|  | *|  |   |  |  | *|  |  |
      |---------   ----------   ----------------   ----------------   ----------------
      | *| C| *|   |  | G|  |   |  | M|  | M|  |   | *|  | M|  | *|   |  |  | *|  |  |
      |---------   ----------   ----------------   ----------------   ----------------
      |  | *|  |   | *|  | *|   |  |  | E|  |  |   |  | M| H| M|  |   | *| *| C| *| *|
      ----------   ----------   ----------------   ----------------   ----------------
      Rules:                    |  | M|  | M|  |   | *|  | M|  | *|   |  |  | *|  |  |
      1.* = can go position     ----------------   ----------------   ----------------
      2.(C,G,E,H,D,S) = origin  | *|  |  |  | *|   |  | *|  | *|  |   |  |  | *|  |  |
      3.M = must no chess       ----------------   ----------------   ----------------

      Duck:                    Soldier:
      ----------------------   ----------------------
      |  | *|  |  |  | *|  |   |  |  |  |  |  |  |  |
      ----------------------   ----------------------
      | *|  | M|  | M|  | *|   |  |  |  |  |  |  |  |
      ----------------------   ----------------------
      |  | M|  | M|  | M|  |   |  |  | *| *| *|  |  |
      ----------------------   ----------------------
      |  |  | M| D| M|  |  |   |  |  | *| S| *|  |  |
      ----------------------   ----------------------
      |  | M|  | M|  | M|  |   |  |  | *| *| *|  |  |
      ----------------------   ----------------------
      | *|  | M|  | M|  | *|   |  |  |  |  |  |  |  |
      ----------------------   ----------------------
      |  | *|  |  |  | *|  |   |  |  |  |  |  |  |  |
      ----------------------   ----------------------
*/
int main(){
    int operator_sequence_length;/*操作序列长度*/
    std::cin>>operator_sequence_length;
    for(int i=1;i<=operator_sequence_length;i++){
        int x_s,y_s;/*起始位置*/
        std::cin>>x_s>>y_s;
        int x_t,y_t;/*目标位置*/
        std::cin>>x_t>>y_t;
        if(Game_Over){/*游戏结束后所有指令作废*/
            puts("Invalid command");
            continue;
        }
        if(chess_board[x_s][y_s]==0){/*如果起始位置是空的,说明该操作不合法*/
            puts("Invalid command");
            continue;
        }
        if(chess_board[x_s][y_s]<0&&chess_board[x_t][y_t]<0||
           chess_board[x_s][y_s]>0&&chess_board[x_t][y_t]>0){/*不能将己方棋子挪到己方棋子上*/
            puts("Invalid command");
            continue;                   
        }
        if(chess_board[x_s][y_s]<0&&operator_people>0||
           chess_board[x_s][y_s]>0&&operator_people<0){/*不是当前这个人的棋子*/
            puts("Invalid command");                             
            continue;
        }
        int chess_type=std::abs(chess_board[x_s][y_s]);
        bool can_get=false;
        std::string name;
        if(chess_type==1){
            can_get=checkCaptain(x_s,y_s,x_t,y_t);  
            name="captain";                      
        }else if(chess_type==2){                                                                                 
            can_get=checkGuard(x_s,y_s,x_t,y_t);           
            name="guard";                                                        
        }else if(chess_type==3){                                                                                                    
            can_get=checkElephant(x_s,y_s,x_t,y_t);
            name="elephant";
        }else if(chess_type==4){
            can_get=checkHorse(x_s,y_s,x_t,y_t);
            name="horse";
        }else if(chess_type==5){                                                                                            
            can_get=checkCar(x_s,y_s,x_t,y_t);
            name="car";
        }else if(chess_type==6){
            can_get=checkDuck(x_s,y_s,x_t,y_t);     
            name="duck";                 
        }else{
            can_get=checkSoldier(x_s,y_s,x_t,y_t); 
            name="soldier";                      
        }
        if(can_get==false){/*不能到达*/
            puts("Invalid command");
            continue;                              
        }
        if(std::abs(chess_board[x_t][y_t])==1){
        	Game_Over=true;
		}
        if(operator_people==1){
            std::cout<<"red ";
        }else{
            std::cout<<"blue ";
        }
        std::cout<<name<<";";
        if(chess_board[x_t][y_t]==0){
            std::cout<<"NA;";
        }else{
            chess_type=std::abs(chess_board[x_t][y_t]);
            if(chess_board[x_t][y_t]<0){
            	std::cout<<"blue ";
			}else{
				std::cout<<"red ";
			}
            if(chess_type==1){
                name="captain";                      
            }else if(chess_type==2){                                                                                 
                name="guard";                                                        
            }else if(chess_type==3){                                                                                                    
                name="elephant";
            }else if(chess_type==4){
                name="horse";
            }else if(chess_type==5){                                                                                            
                name="car";
            }else if(chess_type==6){
                name="duck";                 
            }else{
                name="soldier";                      
            }
            std::cout<<name<<";";
        }
        chess_board[x_t][y_t]=chess_board[x_s][y_s];
        chess_board[x_s][y_s]=0;
        int redKing_x=0,redKing_y=0;
        int blueKing_x=0,blueKing_y=0;
        for(int i=0;i<10;i++){
            for(int j=0;j<9;j++){
                if(chess_board[i][j]==1){
                    redKing_x=i;
                    redKing_y=j;
                }
                if(chess_board[i][j]==-1){
                    blueKing_x=i;
                    blueKing_y=j;
                }
            }
        }
        bool Judge=false;
        for(int i=0;i<10;i++){
            for(int j=0;j<9;j++){
                if(chess_board[i][j]==0){
                    continue;
                }
                if(chess_board[i][j]==-1){
                	continue;
				}
				if(chess_board[i][j]==1){
                	continue;
				}
                int _type=std::abs(chess_board[i][j]);
                bool _get=0;/*red*/
                bool __get=0;/*blue*/
                if(_type==1){
                    if(!chess_board[i][j]>0)_get=checkCaptain(i,j,redKing_x,redKing_y);     
                    else __get=checkCaptain(i,j,blueKing_x,blueKing_y);
                }else if(_type==2){                                                                                 
                    if(!chess_board[i][j]>0)_get=checkGuard(i,j,redKing_x,redKing_y);    
                    else __get=checkGuard(i,j,blueKing_x,blueKing_y);                                                     
                }else if(_type==3){                                                                                                    
                    if(!chess_board[i][j]>0)_get=checkElephant(i,j,redKing_x,redKing_y);
                    else __get=checkElephant(i,j,blueKing_x,blueKing_y);
                }else if(_type==4){
                    if(!chess_board[i][j]>0)_get=checkHorse(i,j,redKing_x,redKing_y);
                    else __get=checkHorse(i,j,blueKing_x,blueKing_y);
                }else if(_type==5){                                                                                            
                    if(!chess_board[i][j]>0)_get=checkCar(i,j,redKing_x,redKing_y);
                    else __get=checkCar(i,j,blueKing_x,blueKing_y);
                }else if(_type==6){
                    if(!chess_board[i][j]>0)_get=checkDuck(i,j,redKing_x,redKing_y); 
                    else __get=checkDuck(i,j,blueKing_x,blueKing_y);
                }else{
                    if(!chess_board[i][j]>0)_get=checkSoldier(i,j,redKing_x,redKing_y);  
                    else __get=checkSoldier(i,j,blueKing_x,blueKing_y);                 
                }
                if(_get||__get){
                    Judge=true;
                    break;
                }
            }
        }
        if(Judge){
            std::cout<<"yes;";
        }else{
            std::cout<<"no;";
        }
        if(Game_Over){
            puts("yes");
        }else{
            puts("no");
        }
        if(operator_people>0){
        	operator_people=-1;
		}else{
			operator_people=1;
		}
    }
}
2024/8/6 16:51
加载中...