gpt4 book ai didi

Java - 2D 数组操作正在影响索引的逆

转载 作者:行者123 更新时间:2023-11-30 02:09:22 26 4
gpt4 key购买 nike

我正在尝试通过应用我的知识并制作 Java 国际象棋游戏来学习 Java。目前它是严格的文本。在我的程序中,我有一个功能可以让我移动一个棋子。所有棋子都存储在称为棋盘的二维数组中。然而,当我试图移动一 block 时,问题就出在这个函数上。如果我出于某种原因将棋子(“Rook”)从 board[7][0] 移动到 board[6][0] ,则与此相反(board[0][0]board[1][0])也被移动。

这是使用 displayBoard() 显示的未进行任何修改的主板:

ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK, 
PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN,
ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK,

棋盘已被移动:

null, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK, 
ROOK, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
ROOK, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN,
null, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK,

只有左下方的车应该被移动。

Board.Java类:

public class Board {
public Pieces[][] board = new Pieces[8][8];

private Pieces[] MainLine = new Pieces[]{Pieces.ROOK, Pieces.KNIGHT, Pieces.BISHOP, Pieces.QUEEN, Pieces.KING, Pieces.BISHOP, Pieces.KNIGHT, Pieces.ROOK};
private Pieces[] BishopLine = new Pieces[]{Pieces.PAWN, Pieces.PAWN, Pieces.PAWN, Pieces.PAWN, Pieces.PAWN, Pieces.PAWN, Pieces.PAWN, Pieces.PAWN};

public Board(){
resetBoard();
}

private void resetBoard() {
board = new Pieces[8][8];
board[0] = MainLine;
board[board.length - 1] = MainLine;
board[1] = BishopLine;
board[board.length - 2] = BishopLine;
}

public void displayBoard() {
for(int rank = 0; rank < board.length; rank++) {
System.out.println("");
for(int file = 0; file < board.length; file ++) {
System.out.print(board[rank][file] + ", ");
}
}
for(int x = 0; x < 3; x++) {
System.out.println("");
}
}

public Pieces getPiece(int rank, int file) {
return board[rank][file];
}

public void movePiece(int rank, int file, int newRank, int newFile) {
Pieces temp = getPiece(rank, file);
board[rank][file] = null;
board[newRank][newFile] = temp;
}
}

在我的主类中,我创建了一个新的棋盘实例,显示它,移动一 block movePiece(7, 0, 6, 0),然后再次显示棋盘。我相信主要问题出在 Board.java 类中,我不知道出了什么问题或为什么其他索引受到影响。谁能向我解释为什么会发生这种情况,或者如何更好地操作 2D board 数组以进一步实现我的目标,即将一个棋子移动到给定位置而不影响任何其他棋子。

编辑:添加了请求的额外代码 -

Main.Java 创建一个游戏实例,将来会发生其他条件,但 Game.java 保留对 Board.Java 的直接引用,因为它继承自 Board.Java:

public class Main {
public static void main(String[] args) {
Game game = new Game();

game.displayBoard();
game.movePiece(7, 0, 6, 0);
game.displayBoard();

}
}

Game.java 只是调用 super 来创建棋盘。稍后它将拥有额外的功能:

public class Game extends Board {
public Game() {
super();
}

}

最佳答案

问题出在 Board 的构造函数中,更具体地说是 resetBoard() 方法,您将第一行初始化为与最后一行相同的数组,类似地,第二行引用与倒数第二行相同的数组:

private void resetBoard() {
board = new Pieces[8][8];

// board[0] is a reference to MainLine so if we change move
// into or out of board[0] MainLine will be affected and
// we won't be able to start with a fresh board by calling boardReset()
// board[0] = MainLine;

// Initialize the first row to a fresh array every time we call
// reset.
board[0] = new Pieces[] {
Pieces.ROOK, Pieces.KNIGHT, Pieces.BISHOP, Pieces.QUEEN,
Pieces.KING, Pieces.BISHOP, Pieces.KNIGHT, Pieces.ROOK
};

// This makes the last line and
// the first line point to the same array
// You need to create a new array that is a deep copy
// of the array.
// board[board.length - 1] = MainLine;

// Create new array by copying the original array
board[board.length - 1] = Arrays.copyOf(board[0], board[0].length);

// Similarly
//board[1] = BishopLine;
Arrays.fill(board[1], Pieces.PAWN);
board[board.length - 2] = Arrays.copyOf(board[1], board[1].length);

// You don't need member variables MainLine and BishopLine
// and you can remove them if you want.
}

现在两行都指向不同的数组。当你调用 movePiece(7, 0, 6, 0) 你应该看到

ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK, 
PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN,
ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK,



ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK,
PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
ROOK, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN,
null, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK,

关于Java - 2D 数组操作正在影响索引的逆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50555124/

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