爪哇国王

时间:2014-12-18 22:48:55

标签: java chess

我让所有其他作品顺利移动,但国王正在移动,并且能够一次移动多个地方,任何了解国际象棋的人都知道错误可以帮助任何人吗?

这是我的国王代码

    else if(pieceName.contains("King")){
    int newY = e.getY()/75;
    int newX = e.getX()/75;         
    boolean inTheWay = false;       
    int distance = Math.abs(startX-newX);


    if(((newX < 0) || (newX > 7))||((newY < 0)||(newY > 7))){
        validMove = false;          
    }
    else{

        validMove = true;               

        //Check if the move is a diagonal move

        if(Math.abs(startX-newX)==Math.abs(startY-newY))
        {
            // If there are any pieces along the diagonal in the way the move cannot be made.
            if((startX-newX < 0)&&(startY-newY < 0)){
                for(int i=0; i < distance;i++){
                    if(piecePresent((initialX+(i*75)), (initialY+(i*75)))){
                        inTheWay = true;
                    }
                }                       
            }
            else if((startX-newX < 0)&&(startY-newY > 0)){
                for(int i=0; i < distance;i++){
                    if(piecePresent((initialX+(i*75)), (initialY-(i*75)))){                     
                        inTheWay = true;
                    }
                }                       
            }
            else if((startX-newX > 0)&&(startY-newY > 0)){
                for(int i=0; i < distance;i++){
                    if(piecePresent((initialX-(i*75)), (initialY-(i*75)))){
                        inTheWay = true;
                    }
                }                       
            }
            else if((startX-newX > 0)&&(startY-newY < 0)){
                for(int i=0; i < distance;i++){
                    if(piecePresent((initialX-(i*75)), (initialY+(i*75)))){
                        inTheWay = true;
                    }
                }                       
            }                                       
            if(inTheWay){
                validMove = false;
            }
            else{
                if(piecePresent(e.getX(), (e.getY()))){
                    if(pieceName.contains("White")){
                        if(checkWhiteOponent(e.getX(), e.getY())){
                            validMove = true;
                        }
                        else{
                            validMove = false;
                        }
                    }
                    else{
                        if(checkBlackOponent(e.getX(), e.getY())){
                            validMove = true;
                        }
                        else{
                            validMove = false;
                        }
                    }                                               
                }
                else{
                    validMove = true;
                }                                               
            }                                       
        }
        else if(((Math.abs(startX-newX)!=0)&&(Math.abs(startY-newY) == 0))|| ((Math.abs(startX-newX)==0)&&(Math.abs(newY-startY)!=0)))
        {
            if(Math.abs(startX-newX)!=0){
                //we have movement along the x axis
                int xMovement = Math.abs(startX-newX);
                if(startX-newX > 0){
                    //movement in the left direction....
                    for(int i=0;i < xMovement;i++){
                        if(piecePresent(initialX-(i*75), e.getY())){
                            inTheWay = true;
                            break;
                        }
                        else{
                            inTheWay = false;
                        }
                    }                           
                }
                else{
                    for(int i=0;i < xMovement;i++){                             
                        if(piecePresent(initialX+(i*75), e.getY())){
                            inTheWay = true;
                            break;
                        }
                        else{
                            inTheWay = false;
                        }
                    }
                }
            }
            else{
                //we have movement along the y axis
                int yMovement = Math.abs(startY-newY);
                    if(startY-newY > 0){
                        //movement in the left direction....
                        for(int i=0;i < yMovement;i++){                                 
                            if(piecePresent(e.getX(),initialY-(i*75))){
                                inTheWay = true;
                                break;
                            }
                            else{
                                inTheWay = false;
                            }
                        }                           
                    }
                    else{
                        for(int i=0;i < yMovement;i++){
                            if(piecePresent(e.getX(),initialY+(i*75))){
                                inTheWay = true;
                                break;
                            }
                            else{
                                inTheWay = false;
                            }
                        }
                    }
            }
            if(inTheWay){
                validMove = false;
            }
            else{
                if(piecePresent(e.getX(), (e.getY()))){
                    if(pieceName.contains("White")){
                        if(checkWhiteOponent(e.getX(), e.getY())){
                            validMove = true;
                        }
                        else{
                            validMove = false;
                        }
                    }
                    else{
                        if(checkBlackOponent(e.getX(), e.getY())){
                            validMove = true;
                        }
                        else{
                            validMove = false;
                        }
                    }                                               
                }
                else{
                    validMove = true;
                }
            }
        }               
        else{ // the move that is being tried is not a diagonal move...
            validMove = false;
        }               
    }   

} 

1 个答案:

答案 0 :(得分:2)

如果国王没有执行铸造动作那么:

  • newX必须等于startXstartX+1startX-1
  • newY必须等于startYstartY+1startY-1

如果是王牌,国王必须处于初始位置,并且只能移动到两个位置中的一个位置。