问题描述
该方法应该确定游戏是否在最初和一些移动之后结束。
public boolean isGameOver(){
Point [] player1 = new Point [12];
int p1 = 0;
Point [] player2 = new Point [12];
int p2 = 0; (int i = 0; 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()); (int i1 = 0; i1< player1.length; i1 ++){
}
}
}
temp = getPossibleMoves(player1 [i1]);
if(temp.isEmpty())
返回true; (int i1 = 0; i1< player1.length; i1 ++){
}
{
ArrayList< Point& temp = getPossibleMoves(player2 [i1]);
if(temp.isEmpty())
返回true;
}
返回false;
}
问题是当我运行这些测试时,他们都有数组索引的错误超出范围
这些是测试
最初:
@Test(timeout = 1000)
public void testGameOverInitial(){
assertFalse(board.isGameOver());
}
一些动作后:
@Test(timeout = 1000)
public void testGameOverAfterSomeMoves(){
board.move(new Point(1,0),new点(3,2)); // White's turn
board.move(new Point(0,5),new Point(2,5)); // Black's turn
assertFalse(board.isGameOver());
}
由于 p1
和 p2
嵌套在中用于
循环,它们可以增加多达49个你可以检查 p1
的长度,如果它小于 player1.length
喜欢@LuiggiMendoza的建议。或者你可以修复你的循环和你的长度球员
数组。
我不知道你是什么正在努力做您需要为您要解决的问题挑选最佳解决方案。
This method should determine whether the game is over or not initially and after some moves.
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;
}
The problem is when i run those tests they both have an error of array index out of boundsThose are the tests
initially:
@Test(timeout=1000)
public void testGameOverInitial() {
assertFalse(board.isGameOver());
}
after some moves:
@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());
}
Since p1
and p2
are nested in for
loops, they can increase up to 49 instead of 12 that is the size of both of your player arrays.
You can either check the length of p1
if it is smaller than player1.length
like @LuiggiMendoza suggests. Or you can fix your loop and the length of you you player
arrays.
I'm not sure what you are trying to do. You need to pick the best solution for the problem you are trying to solve.
这篇关于数组索引出界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!