【发布时间】:2012-02-25 20:28:13
【问题描述】:
我的代码应该可以很好地解释我的情况。如果没有,那么;我调用此方法并返回“错误”值。不知何故,它要么改变了一个数组列表的值,要么将它们设置为相等。我做错了什么?
public ArrayList<Piece> bestMove (ArrayList<Piece> b, int index) {
System.out.println ("possible moves = " +this.getPossibleMoves(b, index)); // prints 2
if (this.getPossibleMoves(b, index) > 0) { // the problem isn't here
// is the problem here?
ArrayList<Piece> cloneAlpha = (ArrayList<Piece>) b.clone();
ArrayList<Piece> cloneBeta = (ArrayList<Piece>) b.clone();
int x = b.get(index).x; // x = 4
int y = b.get(index).y; // y = 3
// x + 1 = 5
// y + 1 = 4
if (!this.checkSquare(x+1, x+1, cloneAlpha)) {
cloneAlpha.get(index).setXY((x+1), (x+1));
System.out.println (cloneAlpha.get(index).x + ", " + cloneAlpha.get(index).y); // prints "5, 4"
}
// Something goes on between these two conditions
// it can't be checkSquare method
// nor the setXY
// x - 1 = 3
// y + 1 = 4
if (!this.checkSquare(x-1, y+1, cloneBeta)) {
cloneBeta.get(index).setXY(x-1, y+1);
System.out.println (cloneBeta.get(index).x + ", " + cloneBeta.get(index).y); // prints "3, 4"
}
System.out.println (cloneAlpha.get(index).x + ", " + cloneAlpha.get(index).y); // prints "3, 4"
System.out.println (cloneBeta.get(index).x + ", " + cloneBeta.get(index).y); // prints "3, 4"
return cloneAlpha;
}
return b;
}
【问题讨论】:
-
哪行代码没有达到你的预期?您的期望是什么?行为有何不同?