为什么这个棋盘运动不能正常工作?

时间:2013-10-23 05:02:14

标签: java chess

我正在尝试将游戏块从其初始位置移动到新位置。注意:此举被认为是“合法的”。

public void move ( int fromRow, int fromCol, int toRow, int toCol) {
    GamePiece tmp; //Gamepiece is superclass
    tmp=board[fromRow][fromCol];
    board[toRow][toCol]=tmp;
    board[fromRow][fromCol]=new Gamepiece(); //default constructor
    System.out.print(toString()); //this method has the board array printed in correct format
}

当我测试它时,它不会移动正确的部分并且不会给出空白。为什么呢?

1 个答案:

答案 0 :(得分:3)

您在代码中所做的是交换。在常规国际象棋游戏中,您永远不需要交换。只需替换

       tmp=board[fromRow][fromCol];   // don't need this
       board[toRow][toCol]=tmp;  // don't need this
       board[fromRow][fromCol]=new Gamepiece();  // don't need this

只是做:

       board[toRow][toCol] = board[fromRow][fromCol];
       board[fromRow][fromCol] = null

这一切都在考虑您的电路板是2D array of ChessPiece,例如ChessPiece[][] board = new ChessPiece[8][8];

我不知道这是否会解决您的问题,而不会看到更多代码,但我只是指出了这一点。

相关问题