得到角色,我的国际象棋游戏不断提出无效的举动

时间:2018-11-10 17:42:42

标签: java character move chess

很抱歉让我感到烦恼,但对此我感到有些恐慌和压力。请不要讨厌,因为这篇文章可以看作是“请帮助调试”帖子,但我很绝望。我不明白自己在做什么错,并且花了很长时间试图找出答案。它会运行,但会一直显示非法移动消息。非常感谢您对此有任何见解。

游戏...

public class Game {


    private static String WHITEPLAYS_MSG = "White plays. Enter move:";
    private static String BLACKPLAYS_MSG = "Black plays. Enter move:";
    private static String ILLEGALMOVE_MSG = "Illegal move!";
    private static String WHITEWINS_MSG = "White wins!";
    private static String BLACKWINS_MSG = "Black wins!";

    private Board gameBoard;


    // Minimal constructor. Expand as needed (kt54)
    public Game() {
        gameBoard = new Board();
    }

    // Build on this method to implement game logic.
    public void play() {

        Player player1 = new Player("white");           //player one plays white
        Player player2 = new Player("black");           //player two plays black
        Piece piece1 = new Piece();
        Piece piece2 = new Piece();

        EasyIn2 reader = new EasyIn2();

        gameBoard = new Board();     //initializes the board so dont need to do so in main

        boolean done = false;      //2 while loops within large while loop?

        while (!done) {                     //keeps looping when no one has won yet
            gameBoard.printBoard();

            System.out.println(WHITEPLAYS_MSG);


            String Player1Pos1 = reader.getString();         //gets user input ... move from... to....   temporary variables
            int x1From = Player1Pos1.charAt(0) - 'a';                           //to transform the letter      ASCII values
            int y1From = 8 - (Player1Pos1.charAt(1) - '1') - 1;                           // to transform the number

            String Player1Pos2 = reader.getString();
            int x1To = Player1Pos2.charAt(0) - 'a';                           //to transform the letter
            int y1To = 8 - (Player1Pos2.charAt(1) - '1') - 1;                           // to transform the number

            //passing the entire gameBoard instance, not just the array
            if (!gameBoard.canFindPiece(gameBoard.board, x1From, y1From)) {      //to check if the starting position is allowed
                System.out.println(ILLEGALMOVE_MSG);

                if (gameBoard.canMovePiece(gameBoard.board, x1From, y1From, x1To, y1To, piece1, player1)) {
                    gameBoard.movePiece(gameBoard.board, x1From, y1From, x1To, y1To, player1, piece1);
                } else {
                    System.out.println(ILLEGALMOVE_MSG);

                }
            }


            if (player1.getNumberPiecesCaptured() == gameBoard.getStartingPiecesEach()) {
                done = true;
                System.out.println(WHITEWINS_MSG);
                break;
            } else {

木板。...

public class Board {


    private static final int DEFAULT_SIZE = 8;             //images for pieces to be displayed on board
    private static final char FREE = '.';
    private static final char WHITEROOK = '♖';
    private static final char BLACKROOK = '♜';
    private static final char WHITEBISHOP = '♗';
    private static final char BLACKBISHOP = '♝';


    public static int moveNumber=1;
    private int startingPiecesEach= 4 ;
    private int boardsize;
    public char[][] board;


    public Board() {
        this.boardsize = DEFAULT_SIZE;

        board = new char[boardsize][boardsize];

        // Clear all playable fields
        for (int x = 0; x < boardsize; x++)
            for (int y = 0; y < boardsize; y++)
                board[x][y] = FREE;

        // Put a single bishop in the middle
        // Obviously, you will need to replace this with your own initialisation code

        board[0][7] = WHITEROOK;
        board[2][7] = WHITEBISHOP;
        board[5][7] = WHITEBISHOP;
        board[7][7] = WHITEROOK;
        board[0][0] = BLACKROOK;
        board[2][0] = BLACKROOK;
        board[5][0] = BLACKROOK;
        board[7][0] = BLACKROOK;


    }


    public void printBoard() {

        // Print the letters at the top
        System.out.print(" ");
        for (int x = 0; x < boardsize; x++)
            System.out.print(" " + (char) (x + 'a'));
        System.out.println();

        for (int y = 0; y < boardsize; y++) {
            // Print the numbers on the left side
            System.out.print(8 - y);

            // Print the actual board fields
            for (int x = 0; x < boardsize; x++) {
                System.out.print(" ");
                char value = board[x][y];
                if (value == FREE) {
                    System.out.print(".");
                } else if (value >= WHITEKING && value <= BLACKPAWN) {
                    System.out.print(value);
                } else {
                    System.out.print("X");
                }
            }
            // Print the numbers on the right side
            System.out.println(" " + (8 - y));
        }

        // Print the letters at the bottom
        System.out.print(" ");
        for (int x = 0; x < boardsize; x++)
            System.out.print(" " + (char) (x + 'a'));
        System.out.print("\n\n");
    }



    public int getStartingPiecesEach() {
        return (startingPiecesEach);
    }

   public char getChar(int x, int y){
        return(board[x][y]);


   }




    public boolean legalPieceMove(char[][] gameBoard,int fromX, int fromY, int toX, int toY, Piece piece) {

        char pieceType = piece.getPieceType(gameBoard, fromX, fromY);
        if (pieceType == WHITEROOK) {
            Rook rook = new Rook("white");
            return (rook.moveLegal(gameBoard, fromX, fromY, toX, toY));
        } else {
            if (pieceType == BLACKROOK) {
                Rook rook = new Rook("black");
                return (rook.moveLegal(gameBoard, fromX, fromY, toX, toY));
            } else {
                if (pieceType == WHITEBISHOP) {
                    Bishop bishop = new Bishop("white");
                    return (bishop.moveLegal(gameBoard, fromX, fromY, toX, toY));
                } else {
                    if (pieceType == BLACKBISHOP) {
                        Bishop bishop = new Bishop("black");
                        return (bishop.moveLegal(gameBoard, fromX, fromY, toX, toY));
                    } else {
                        return false;

                    }


                }


            }
        }
    }
    public boolean canMovePiece(char[][] gameBoard,int fromX, int fromY, int toX, int toY, Piece piece, Player player) {
        if (legalPieceMove(gameBoard, fromX, fromY, toX, toY, piece) && piece.correctPlayer(gameBoard, fromX, fromY, toX, toY, player)) {

            return true;
        } else
            {
            return false;
        }
    }





    public void movePiece(char[][] gameBoard,int fromX, int fromY, int toX, int toY, Player player, Piece piece) {
        if (gameBoard[toX][toY] != FREE) {
            removePiece(gameBoard, fromX, fromY, toX, toY, player, piece);



        }
        else { gameBoard[fromX][fromY]=FREE;
            gameBoard[toX][toY]=piece.getPieceType(gameBoard,fromX,fromY);
            moveNumber++;
        }
    }

//can possibly merge these to one for efficiency



    public void removePiece(char[][] gameBoard,int fromX,int fromY, int toX, int toY, Player player, Piece piece) {
        gameBoard[fromX][fromY] = FREE;
        gameBoard[toX][toY] = piece.getPieceType(gameBoard, fromX, fromY);
        player.addNumberPiecesCaptured();
        moveNumber++;

    }






    public boolean canFindPiece(char[][] gameBoard, int fromX, int fromY) {     //checks that the player has selected a piece

        for (int i = 0; i < gameBoard.length; i++) {
            for (int j = 0; j < gameBoard.length; j++) {
                if (gameBoard[i][j] == gameBoard[fromX][fromY]) {      //checks the user input co-ordinate  is on the board
                    //break was here?      //checks the piece is real, ie not a free space
                    if (gameBoard[fromX][fromY] != FREE) {

                        return true;
                    }
                    else {
                        return false;

                    }

                }
            }
        }

        return false;
    }
}

件......

public class Piece {


    private static final char WHITEROOK = '♖';
    private static final char BLACKROOK = '♜';
    private static final char WHITEBISHOP = '♗';
    private static final char BLACKBISHOP = '♝';
    public static final char FREE = '.';
    public String colour;


    public char getPieceType(char[][] gameBoard, int x, int y) {     //method only used when space is not free
        if (gameBoard[x][y] == WHITEROOK) {
            return (WHITEROOK);
        } else {
            if (gameBoard[x][y] == BLACKROOK) {
                return (BLACKROOK);
            } else {
                if (gameBoard[x][y] == WHITEBISHOP) {
                    return (WHITEBISHOP);
                } else {
                    if (gameBoard[x][y] == BLACKBISHOP) {
                        return (BLACKBISHOP);
                    } else return (FREE);

                }

            }
        }
    }


    public int getPieceColour(char[][] gameBoard, int x, int y) {//white indicated by 1s, black indicated by 2s, free spaces by 0
        getPieceType(gameBoard, x, y);
        if (gameBoard[x][y] == WHITEROOK) {
            return (1);
        } else {
            if (gameBoard[x][y] == BLACKROOK) {
                return (2);
            } else {
                if (gameBoard[x][y] == WHITEBISHOP) {
                    return (1);
                } else {
                    if (gameBoard[x][y] == BLACKBISHOP) {
                        return (2);
                    } else {

                        return (0);
                    }
                }
            }
        }
    }

    public boolean correctPlayer(char[][] gameBoard, int fromX, int fromY, int toX, int toY, Player player) {

        if ((player.colour.equals("black") && getPieceColour(gameBoard, fromX, fromY) == 2) || (player.colour.equals("white") && getPieceColour(gameBoard, fromX, fromY) == 1)) {
            return true;
        } else {
            return false;
        }
    }
}

菜鸟的例子...

public class Rook extends Piece {


public Rook(String colour) {
    this.colour= colour;
}





    public ArrayList<int[]> possibleMoves = new ArrayList<int[]>();


    public ArrayList<int[]> generatePossibleMoves(char[][] gameBoard, int xFrom, int yFrom) {
        for (int i = 1; xFrom + i < gameBoard.length; i++) {
            if (getPieceColour(gameBoard, xFrom + i, yFrom) != getPieceColour(gameBoard, xFrom, yFrom)) {//cannot go from free space to free
                if (gameBoard[xFrom + i][yFrom] != FREE) {
                    int[] move = {xFrom + i, yFrom};
                    possibleMoves.add(move);
                    break;                              //stops iterating here since a rook is not allowed to jump over other pieces
                } else
                    {
                    int[] move = {xFrom + i, yFrom};
                    possibleMoves.add(move);
                }
            }
        }
        for (int i = 1; xFrom - i >=0; i++) {
            if (getPieceColour(gameBoard, xFrom - i, yFrom) != getPieceColour(gameBoard, xFrom, yFrom)) {
                if (gameBoard[xFrom - i][yFrom] != FREE) {
                    int[] move = {xFrom - i, yFrom};
                    possibleMoves.add(move);
                    break;
                }
                else
                    {
                    int[] move = {xFrom - i, yFrom};
                    possibleMoves.add(move);
                }
            }
        }
        for (int i = 1; yFrom + i < gameBoard.length; i++) {       //makes sure the place to be moved is on the board
            if (getPieceColour(gameBoard, xFrom , yFrom+ i) != getPieceColour(gameBoard, xFrom, yFrom)) {
                if (gameBoard[xFrom][yFrom+i] != FREE) {
                    int[] move = {xFrom, yFrom+i};
                    possibleMoves.add(move);
                    break;
                }
                else
                    {
                    int[] move = {xFrom, yFrom+i};
                    possibleMoves.add(move);
                }
            }
        }
        for (int i = 1; yFrom- i >=0; i++)
            if (getPieceColour(gameBoard, xFrom, yFrom - i) != getPieceColour(gameBoard, xFrom, yFrom)) {
                if (gameBoard[xFrom][yFrom - i] != FREE) {
                    int[] move = {xFrom, yFrom - i};
                    possibleMoves.add(move);
                    break;
                } else {
                    int[] move = {xFrom, yFrom - i};
                    possibleMoves.add(move);
                }
            }
        return possibleMoves;
    }




    public boolean moveLegal(char[][] gameBoard, int xFrom, int yFrom, int xTo, int yTo){
            int wantedMove[] = {xTo, yTo};                                          //created wantedMove variable so that it could be checked if it is in the possibleMoves
            possibleMoves= generatePossibleMoves(gameBoard, xFrom, yFrom);    //problem
            if (possibleMoves.contains(wantedMove)) {
                return true;
            }
            else {
                return false;
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

我在代码中看到了一些问题,但这将有助于解决您的错误。您在帖子中问过如何调试,我将尝试向您展示如何重新排列代码:

此片段完全相同:

 String Player1Pos1 = reader.getString();         //gets user input ... move from... to....   temporary variables
            int x1From = Player1Pos1.charAt(0) - 'a';                           //to transform the letter      ASCII values
            int y1From = 8 - (Player1Pos1.charAt(1) - '1') - 1;                           // to transform the number

为什么不使用单独的功能:

Pair<Integer, Integer> getPlayerPosition(String x, String y)

在legalPieceMove(...)函数中,您有很多重复的代码。我认为您不需要白色和黑色之间的区别。该功能真的很短。使用switch语句或“ if”“ else if”“ else”语句可使代码更具可读性。

在Board类中,几乎每个功能都需要board变量。为什么不使用实例变量。然后您的函数参数列表会短一些。

由于我没有完整的代码,因此我无法修改您的代码。

答案 1 :(得分:0)

我发现了:

for (...) {
    if (getPieceColour(gameBoard, xFrom - i, yFrom) != getPieceColour(gameBoard, xFrom, yFrom)) {
       ...
    }
}

如果getPieceColour(...)== getPieceColour(...),则必须离开for循环:

for (...) {
    if (getPieceColour(gameBoard, xFrom - i, yFrom) != getPieceColour(gameBoard, xFrom, yFrom)) {
       ...
    } else {
        break;
    }
}