简体   繁体   中英

First text based Java program

I am learning Java and this is my first big project. I am designing a text based Chess game using Java. I have managed to get the castling work but for some reason I couldn't get the Pawns to move. I would love to share the rest of the code but i can't because of the character limit however I would love to send the whole program to u via DM.

package il.co.Pawn;
import il.co.ChessInterface.ChessPiece;
public class Pawn extends ChessPiece{
    int columnEnd;
    int rowEnd;
    int columnStart;
    int rowStart;
    String name;
    boolean simpleMove;
    boolean twoSquareMove;
    boolean devourMove;
    boolean valid;
    String chessPiece;
    boolean pieceDevour;
    char firstLetter;
    char secondLetter;
    boolean secondLetterNotNull;
    public Pawn(String name){
        this.name=name;
    }
    @Override
    public String toString() {
        return "Pawn [name=" + name + "]";
    }
    //pieces, columnStart,rowStart,columnEnd,rowEnd
    public boolean isMoveValid(ChessPiece[][] pieces, int columnStart,  int rowStart, int columnEnd, int rowEnd) {
        int deltaX = rowEnd - rowStart;
        if (deltaX == 1 || deltaX == -1)
            simpleMove = true;
        boolean pieceMove1 = false;
        boolean pieceMove2 = false;
        boolean pieceMove3 = false;
        boolean pieceMove4 = false;
        pieceMove1 = true;
        pieceMove2 = true;
        pieceMove3 = true;
        pieceMove4 = true;
        return pieceMove1  || pieceMove2 || pieceMove3 || pieceMove4;

    }
    //This part displays the Pawns at their respective locations and it is used to differentiate between the White pieces and black pieces.
    String pawn="";
    public String print() {
        if(name.charAt(0)=='B'){
            pawn= "|"+ name + "|";
        }
        else{
            pawn="|"+ name + "|";
        }
        return pawn;
    }
    @Override
    //This part finds the piece in the board.
    public String FindPiece() {
        return this.name;
    }

    //pieces,rowStart,columnStart ,columnEnd,rowEnd
    //this method is the part where the program checks if the piece is moving in accordance to the rules or not.
    public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd) {
        boolean isPawnMovingUP=false;
        boolean isPawnMovingUP2=false;
        boolean isPawnMovingRIGHT=false;
        boolean isPawnMovingDOWN=false;
        boolean isPawnMovingDOWN2=false;
        boolean isPawnMovingLEFT=false;
        if(pieces[rowStart][columnStart].FindPiece().charAt(0)=='W'){
            if(rowStart==7 && (rowEnd==6 ||  rowEnd==5) && columnStart==columnEnd){
                isPawnMovingUP = true;
            }
            else if((rowStart-1)==rowEnd && pieces[rowEnd][columnEnd]==null && columnStart==columnEnd){
                isPawnMovingUP2 = true;
                //isPawnMovingUP = true;
            }
            else{
                //isPawnMovingUP = false;
                isPawnMovingUP2 = false;
            }
        }
        if(pieces[rowStart][columnStart].FindPiece().charAt(0)=='B'){
            if(rowStart==2 && (rowEnd==3 ||  rowEnd==4)&& columnStart==columnEnd){
                isPawnMovingDOWN = true;
            }
            else if((rowStart+1)==rowEnd && pieces[rowEnd][columnEnd]==null && columnStart==columnEnd){
                //isPawnMovingDOWN2 = true;
                //============================
                isPawnMovingDOWN = true;
                //============================
            }
            else{
                isPawnMovingDOWN = false;
                //isPawnMovingDOWN2 = false;
            }
        }
        if(pieces[rowStart][columnStart].FindPiece().charAt(0)=='W'){
            if(isPawnMovingDOWN == false && (rowStart-1)==rowEnd && (columnStart-1)==columnEnd  && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true && columnStart!=columnEnd){
                //if(isPawnMovingDOWN2 == false && isPawnMovingDOWN == false && (rowStart-1)==rowEnd && (columnStart-1)==columnEnd  && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true && columnStart!=columnEnd){
                isPawnMovingLEFT=true;

            }
            else if(isPawnMovingDOWN == false &&(rowStart-1)==rowEnd && (columnStart+1)==columnEnd  && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true&& columnStart!=columnEnd){
                //else if(isPawnMovingDOWN2 == false && isPawnMovingDOWN == false &&(rowStart-1)==rowEnd && (columnStart+1)==columnEnd  && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true&& columnStart!=columnEnd){
                isPawnMovingRIGHT=true;

            }
            else{
                isPawnMovingLEFT=false;
                isPawnMovingRIGHT=false;

            }
        }
        if(pieces[rowStart][columnStart].FindPiece().charAt(0)=='B'){
            if(isPawnMovingUP==false && isPawnMovingUP2==false && (rowStart+1)==rowEnd && (columnStart-1)==columnEnd && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true && columnStart!=columnEnd){
                isPawnMovingLEFT=true;

            }
            else if(isPawnMovingUP==false && isPawnMovingUP2==false &&  (rowStart+1)==rowEnd && (columnStart+1)==columnEnd && pieces[rowEnd][columnEnd]!=null && piecesDevour(pieces, rowStart, columnStart , columnEnd, rowEnd)==true && columnStart!=columnEnd){
                isPawnMovingRIGHT=true;

            }
            else{
                isPawnMovingLEFT=false;
                isPawnMovingRIGHT=false;
            }
        }
        return isPawnMovingUP || isPawnMovingRIGHT|| isPawnMovingDOWN || isPawnMovingLEFT || isPawnMovingUP2 || isPawnMovingDOWN2;
    }
    public boolean piecesDevour(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd){
        firstLetter = pieces[rowStart][columnStart].FindPiece().charAt(0);
        if (pieces[rowEnd][columnEnd] != null) {
            secondLetterNotNull = true;
            secondLetter = pieces[rowEnd][columnEnd].FindPiece().charAt(0);
        } else {
            secondLetterNotNull = false;
        }
        if (secondLetter != firstLetter && secondLetterNotNull) {
            pieceDevour = true;
        } else {
            pieceDevour = false;
        }
        return pieceDevour;
    }
}    

isMoveValid will always return true.

    boolean pieceMove1 = false;
    boolean pieceMove2 = false;
    boolean pieceMove3 = false;
    boolean pieceMove4 = false;
    pieceMove1 = true;
    pieceMove2 = true;
    pieceMove3 = true;
    pieceMove4 = true;
    return pieceMove1  || pieceMove2 || pieceMove3 || pieceMove4;

pieceMove1 through 4 will all be true when the last statement is evaluated, and true OR true OR true OR true equals true. I'm not even sure what you are trying to accomplish here, so I cannot tell you how to fix it, but generally you should rethink how you structure your data types and which class actually needs which information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM