这种方法应该在最初和一些动作之后确定游戏是否结束。
public boolean isGameOver() {
Point[] player1 = new Point[12];
int p1 = 0;
Point[] player2 = new Point[12];
int p2 = 0;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (board[i][j] == 1) {
Point p = new Point(i, j);
player1[p1] = p;
p1++;
//System.out.println(p.getX()+ " 1 " + p.getY());
} else if (board[i][j] == 2) {
Point p = new Point(i, j);
player2[p2] = p;
p2++;
//System.out.println(p.getX()+ " 2 " + p.getY());
}
}
}
for(int i1=0;i1<player1.length;i1++) {
ArrayList<Point> temp = getPossibleMoves(player1[i1]);
if(temp.isEmpty())
return true;
}
for(int i1=0;i1<player1.length;i1++) {
ArrayList<Point> temp = getPossibleMoves(player2[i1]);
if(temp.isEmpty())
return true;
}
return false;
}
问题是当我运行这些测试时,它们都有数组索引超出范围的错误这些是测试
最初:
@Test(timeout=1000)
public void testGameOverInitial() {
assertFalse(board.isGameOver());
}
经过一些动作:
@Test(timeout=1000)
public void testGameOverAfterSomeMoves() {
board.move(new Point(1, 0), new Point(3, 2)); // White's turn
board.move(new Point(0, 5), new Point(2, 5)); // Black's turn
assertFalse(board.isGameOver());
}