gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:29:17 25 4
gpt4 key购买 nike

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

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
}

当我测试它时,它没有移动正确的棋子,也没有给出空白。为什么?

最佳答案

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

       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

这一切都考虑到您的棋盘是一个 ChessPiece 的二维数组,例如 ChessPiece[][] board = new ChessPiece[8][8];/p>

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

关于java - 为什么这个棋盘运动不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19533153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com