错误讯息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at neegjar.Play.canJump(Play.java:113)
at neegjar.Play.getLegalMoves(Play.java:278)
at neegjar.Board.printLegalMoves(Board.java:198)
at neegjar.main.main(main.java:17)
Java Result: 1
我正在尝试在java中构建一个跳棋游戏(现在在控制台中)。我有6个班级:
Board:显示并跟踪电路板。它是一个名为mySquare的2d Square对象数组。
CheckerMove:包含变量fromX,fromY,toX,toY和boolean isAJump。在Play中,我创建了一个CheckerMove对象数组,以找出哪些移动有效。
Piece:单个棋子。每个玩家都有一组12件对象可供使用。包含变量xCoord,yCoord,isKing,isAlive和team。
游戏:基本上是项目的中坚力量。处理被认为是有效的举动,什么是“跳跃”等。它包含在下面。
方形:方形对象是棋盘上的单个图块。它包含变量squareColor,hasAPiece和hasARedPiece(播放器一)。
main(包含主要方法)
当我尝试运行创建棋盘的主方法,放置棋子并打印合法移动时,一切正常,直到它尝试打印合法移动(它显示上面的错误信息)。
为什么这样做?我不知道。我将不胜感激任何帮助,感谢您花时间阅读本文!
这是Play:
package neegjar;
import java.util.ArrayList;
public class Play extends Board
{
public int currentPlayer;
public boolean gameOver;
public int turnNumber;
public Play()
{
currentPlayer = 1;
gameOver = false;
turnNumber = 0;
}
public void takeTurn()
{
System.out.println("It is player "+ this.currentPlayer + "'s turn");
//for Player 1
have user select a piece
System.out.println("Select a piece by typing and x coordinate, followed by a y coordinate (seperated by commas");
System.out.println("For an example input, type h");
}
//can the piece jump from x1,y1 TO x2,y2?
public boolean canJump(int player, int x1, int y1, int x2, int y2)
{
boolean CURRENT_KING = false; //by default
int CURRENT_PIECE = -1;
if(player == 1)
{
if((x2 != x1+2) && (x2 != x1-2) && (y2 != y1+2) && (y2 != y1-2)) return false;
for(int i=0; i<12; i++)
{
if((player1Pieces[i].getX() == x1)&&(player1Pieces[i].getY() == y1))
{
CURRENT_PIECE = i;
break; //get out of foor loop
}
}
if(player1Pieces[CURRENT_PIECE].isAKing() == true)
{
CURRENT_KING = true;
}
//top left spot
if((x2 == x1-2)&&(y2 == y1-2))
{
//first check to see if there is an enemy black piece on the adjacent square
if((mySquare[y1-1][x1-1].getHasAPiece()==true)&&(mySquare[y1-1][x1-1].getHasARedPiece()==false))
if((mySquare[y1-2][x1-2].getHasAPiece()==true)||(y1-2 < 0)||(x1-2 < 0)) //check for a blocker
{
return false;
}
else
return true;
}
//top right spot
if((x2 == x1+2)&&(y2 == y1-2))
{
//first check to see if there is an enemy black piece on the adjacent square
if((mySquare[y1-1][x1+1].getHasAPiece()==true)&&(mySquare[y1-1][x1+1].getHasARedPiece()==false))
if((mySquare[y1-2][x1+2].getHasAPiece()==true)||(y1-2 < 0)||(x1+2 > 7)) //check for a blocker
{
return false;
}
else
return true;
}
//Next 2 are for Kings only!
if(CURRENT_KING = true)
{
//bottom left
if((x2 == x1-2)&&(y2 == y1+2))
{
if((mySquare[y1+1][x1-1].getHasAPiece()==true)&&(mySquare[y1+1][x1-1].getHasARedPiece()==false))
if((mySquare[y1+2][x1-2].getHasAPiece()==true)||(y1+2 > 7)||(x1-2 < 0)) //check for a blocker or out of bounds
{
return false;
}
else
return true;
}
//bottom right
if((x2 == x1+2)&&(y2 == y1+2))
{
if((mySquare[y1+1][x1+1].getHasAPiece()==true)&&(mySquare[y1+1][x1+1].getHasARedPiece()==false))
if((mySquare[y1+2][x1+2].getHasAPiece()==true)||(y1+2 > 7)||(x1+2 >7)) //check for a blocker
{
return false;
}
else
return true;
}
}//end if King
}//end p1
if(player == 2)
{
if((x2 != x1+2) && (x2 != x1-2) && (y2 != y1+2) && (y2 != y1-2)) return false;
for(int i=0; i<12; i++)
{
if((player2Pieces[i].getX() == x1)&&(player2Pieces[i].getY() == y1))
{
CURRENT_PIECE = i;
break; //get out of foor loop
}
}
if(player2Pieces[CURRENT_PIECE].isAKing() == true)
{
CURRENT_KING = true;
}
//bottom left
if((x2 == x1-2)&&(y2 == y1+2))
{
if((mySquare[y1+1][x1-1].getHasAPiece()==true)&&(mySquare[y1+1][x1-1].getHasARedPiece()==false))
if((mySquare[y1+2][x1-2].getHasAPiece()==true)||(y1+2 > 7)||(x1-2 < 0)) //check for a blocker or out of bounds
{
return false;
}
else
return true;
}
//bottom right
if((x2 == x1+2)&&(y2 == y1+2))
{
if((mySquare[y1+1][x1+1].getHasAPiece()==true)&&(mySquare[y1+1][x1+1].getHasARedPiece()==false))
if((mySquare[y1+2][x1+2].getHasAPiece()==true)||(y1+2 > 7)||(x1+2 >7)) //check for a blocker
{
return false;
}
else
return true;
}
//Next 2 are for Kings only!
if(CURRENT_KING = true)
{
//top left spot
if((x2 == x1-2)&&(y2 == y1-2))
{
//first check to see if there is an enemy black piece on the adjacent square
if((mySquare[y1-1][x1-1].getHasAPiece()==true)&&(mySquare[y1-1][x1-1].getHasARedPiece()==false))
if((mySquare[y1-2][x1-2].getHasAPiece()==true)||(y1-2 < 0)||(x1-2 < 0)) //check for a blocker
{
return false;
}
else
return true;
}
//top right spot
if((x2 == x1+2)&&(y2 == y1-2))
{
//first check to see if there is an enemy black piece on the adjacent square
if((mySquare[y1-1][x1+1].getHasAPiece()==true)&&(mySquare[y1-1][x1+1].getHasARedPiece()==false))
if((mySquare[y1-2][x1+2].getHasAPiece()==true)||(y1-2 < 0)||(x1+2 > 7)) //check for a blocker
{
return false;
}
else
return true;
}
}//end if king
} //end p2
//default, or if nothing is found
return false;
} // end canJump()
/**
* This is called by the getLegalMoves() method to determine whether
* the player can legally move from (r1,c1) to (r2,c2). It is
* assumed that (r1,r2) contains one of the player's pieces and
* that (r2,c2) is a neighboring square.
*/
public boolean canMove(int player, int x1, int y1, int x2, int y2)
{
if (y2 < 0 || y2 >= 8 || x2 < 0 || x2 >= 8) //check to see if move is on the board
return false;
if (mySquare[y2][x2].getHasAPiece() == true)
return false; // (r2,c2) already contains a piece.
if (player == 1)
{
if (mySquare[y1][x1].getHasARedPiece() == true && y2 < y1)
return false; //red can only move up if not a king
return true;
}
else //player == 2
{
if (mySquare[y1][x1].getHasAPiece() == true && mySquare[y1][x1].getHasARedPiece()==false && y2 > y1)
return false; // black can only move down if not a king
return true;
}
} // end canMove()
public CheckerMove[] getLegalMoves(int whichPlayer)
{
int pX =0;
int pY = 0;
int CURRENT_PIECE = -1; //used to signify desired piece
boolean CURRENT_KING = false; //default not a king
ArrayList<CheckerMove> legalMoves = new ArrayList<CheckerMove>(); //ADJUSTABLE arraylist of legalMoves
if((whichPlayer != 1)&&(whichPlayer != 2)) return null; //not a valid player
//player One /********************************************************/
if(whichPlayer == 1)
{
//loop through array to find piece
for(int i=0; i<12; i++)
{
if(player1Pieces[i].getIsAlive() == false) break;
if(player1Pieces[i].isAKing() == true)
CURRENT_KING = true;
pX = player1Pieces[i].getX();
pY = player1Pieces[i].getY();
//check for Jumps 1st
if (canJump(1, pX, pY, pX+2, pY+2))
legalMoves.add(new CheckerMove(pX, pY, pX+2, pY+2, true));
if (canJump(1, pX, pY, pX-2, pY+2))
legalMoves.add(new CheckerMove(pX, pY, pX-2, pY+2, true));
if (canJump(1, pX, pY, pX+2, pY-2))
legalMoves.add(new CheckerMove(pX, pY, pX+2, pY-2, true));
if (canJump(1, pX, pY, pX-2, pY-2))
legalMoves.add(new CheckerMove(pX, pY, pX-2, pY-2, true));
}//end for
if (legalMoves.size() == 0) //if there are no jumps
{
//loop through pieces array to find valid moves for each piece
for(int ctr = 0; ctr < 12; ctr++)
{
if(player1Pieces[ctr].isAlive == true)
{
int currX = player1Pieces[ctr].getX();
int currY = player1Pieces[ctr].getY();
if(canMove(1,currX,currY,currX-1,currY-1))
legalMoves.add(new CheckerMove(currX,currY,currX-1,currY-1,false));
if(canMove(1,currX,currY,currX+1,currY-1))
legalMoves.add(new CheckerMove(currX,currY,currX+1,currY-1,false));
if(canMove(1,currX,currY,currX-1,currY+1))
legalMoves.add(new CheckerMove(currX,currY,currX-1,currY+1,false));
if(canMove(1,currX,currY,currX+1,currY+1))
legalMoves.add(new CheckerMove(currX,currY,currX+1,currY+1,false));
}
}
}//if no legal moves exist
}//end player One /**********************************************/
//player Two
if(whichPlayer == 2)
{
//loop through array to find piece
for(int i=0; i<12; i++)
{
if(player2Pieces[i].getIsAlive() == false) break;
if(player2Pieces[i].isAKing() == true)
CURRENT_KING = true;
pX = player2Pieces[i].getX();
pY = player2Pieces[i].getY();
//check for Jumps 1st
if (canJump(2, pX, pY, pX+2, pY+2))
legalMoves.add(new CheckerMove(pX, pY, pX+2, pY+2, true));
if (canJump(2, pX, pY, pX-2, pY+2))
legalMoves.add(new CheckerMove(pX, pY, pX-2, pY+2, true));
if (canJump(2, pX, pY, pX+2, pY-2))
legalMoves.add(new CheckerMove(pX, pY, pX+2, pY-2, true));
if (canJump(2, pX, pY, pX-2, pY-2))
legalMoves.add(new CheckerMove(pX, pY, pX-2, pY-2, true));
}//end for
if (legalMoves.size() == 0) //if there are no jumps
{
//loop through pieces array to find valid moves for each piece
for(int ctr = 0; ctr < 12; ctr++)
{
if(player2Pieces[ctr].isAlive == true)
{
int currX = player2Pieces[ctr].getX();
int currY = player2Pieces[ctr].getY();
if(canMove(2,currX,currY,currX-1,currY-1))
legalMoves.add(new CheckerMove(currX,currY,currX-1,currY-1,false));
if(canMove(2,currX,currY,currX+1,currY-1))
legalMoves.add(new CheckerMove(currX,currY,currX+1,currY-1,false));
if(canMove(2,currX,currY,currX-1,currY+1))
legalMoves.add(new CheckerMove(currX,currY,currX-1,currY+1,false));
if(canMove(2,currX,currY,currX+1,currY+1))
legalMoves.add(new CheckerMove(currX,currY,currX+1,currY+1,false));
}
}
}
}//end p2
if (legalMoves.size() == 0)
return null; //no legal moves found
else //there are legal moves, so copy array list to an array and return
{
CheckerMove[] validMoves = new CheckerMove[legalMoves.size()];
for (int lastCtr = 0; lastCtr < legalMoves.size(); lastCtr++)
{
validMoves[lastCtr] = legalMoves.get(lastCtr);
}
return validMoves;
}
}//end getLegalMoves
}//end class
这是董事会:
package neegjar;
public class Board
{
public Square mySquare[][] = new Square[8][8];
public boolean gameover;
//initialize an Array of 12 Pieces (for the checkers) for EACH player
public Piece player1Pieces[] = new Piece[12];; //declaration
public Piece player2Pieces[] = new Piece[12]; //declaration
public Board()
{
//CREATE THE BOARd
//mySquare = new Square[8][8];
//initialize mySquare
for(int i = 0; i < 8; i++) //not sure if I need this
{
for(int j = 0; j < 8; j++)
{
mySquare[i][j] = new Square();
}
}
//initialize player1 and 2 Pieces arrays
initPieces(1);
initPieces(2);
//alternating colors
/*
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
if ((row == 0) || (row % 2 == 0) && ((col == 0) || (col % 2 == 0)))
{
mySquare[row][col].squareColor = "white";
}
else
{
// do nothing, the square will be black by default
}
}
}//end for loop
*/
//
}//end constructor
public void placePieces()
{
//set location of the pieces' initial location
//player 1
player1Pieces[0].move(0,5);
mySquare[5][0].setHasAPiece(true);
mySquare[5][0].setHasARedPiece(true);
player1Pieces[1].move(2,5);
mySquare[5][2].setHasAPiece(true);
mySquare[5][2].setHasARedPiece(true);
player1Pieces[2].move(4,5);
mySquare[5][4].setHasAPiece(true);
mySquare[5][4].setHasARedPiece(true);
player1Pieces[3].move(6,5);
mySquare[5][6].setHasAPiece(true);
mySquare[5][6].setHasARedPiece(true);
player1Pieces[4].move(1,6);
mySquare[6][1].setHasAPiece(true);
mySquare[6][1].setHasARedPiece(true);
player1Pieces[5].move(3,6);
mySquare[6][3].setHasAPiece(true);
mySquare[6][3].setHasARedPiece(true);
player1Pieces[6].move(5,6);
mySquare[6][5].setHasAPiece(true);
mySquare[6][5].setHasARedPiece(true);
player1Pieces[7].move(7,6);
mySquare[6][7].setHasAPiece(true);
mySquare[6][7].setHasARedPiece(true);
player1Pieces[8].move(0,7);
mySquare[7][0].setHasAPiece(true);
mySquare[7][0].setHasARedPiece(true);
player1Pieces[9].move(2,7);
mySquare[7][2].setHasAPiece(true);
mySquare[7][2].setHasARedPiece(true);
player1Pieces[10].move(4,7);
mySquare[7][4].setHasAPiece(true);
mySquare[7][4].setHasARedPiece(true);
player1Pieces[11].move(6,7);
mySquare[7][6].setHasAPiece(true);
mySquare[7][6].setHasARedPiece(true);
//player 2
player2Pieces[0].move(1,0);
mySquare[0][1].setHasAPiece(true);
player2Pieces[1].move(3,0);
mySquare[0][3].setHasAPiece(true);
player2Pieces[2].move(5,0);
mySquare[0][5].setHasAPiece(true);
player2Pieces[3].move(7,0);
mySquare[0][7].setHasAPiece(true);
player2Pieces[4].move(0,1);
mySquare[1][0].setHasAPiece(true);
player2Pieces[5].move(2,1);
mySquare[1][2].setHasAPiece(true);
player2Pieces[6].move(4,1);
mySquare[1][4].setHasAPiece(true);
player2Pieces[7].move(6,1);
mySquare[1][6].setHasAPiece(true);
player2Pieces[8].move(1,2);
mySquare[2][1].setHasAPiece(true);
player2Pieces[9].move(3,2);
mySquare[2][3].setHasAPiece(true);
player2Pieces[10].move(5,2);
mySquare[2][5].setHasAPiece(true);
player2Pieces[11].move(7,2);
mySquare[2][7].setHasAPiece(true);
}
public void alternateSquares() //assign every other to white
{
for(int row = 0; row < 8; row+=2)
{
for(int col = 0; col < 8; col+=2)
{
mySquare[row][col].setSquareColor("white");
}
}
for(int row = 1; row < 8; row+=2)
{
for(int col = 1; col < 8; col+=2)
{
mySquare[row][col].setSquareColor("white");
}
}
}
public void initPieces(int whichPlayer)
{
for(int p = 0; p <12; p++)
{
if(whichPlayer == 1)
player1Pieces[p] = new Piece();
else
player2Pieces[p] = new Piece();
}
}
public void printBoard()
{
System.out.print(" 0 1 2 3 4 5 6 7 \n");
System.out.print("---------------------------------------------------\n");
for(int row=0; row<8; row++)
{
System.out.print(row);
for(int col = 0; col < 8; col++)
{
System.out.print(" | ");
if(mySquare[row][col].getHasAPiece() == false)
System.out.print(" ");
else if(mySquare[row][col].getHasARedPiece()==true)
System.out.print(" R ");
else if(mySquare[row][col].getHasARedPiece()==false)
System.out.print(" * ");
}
System.out.print(" |\n");
System.out.print("---------------------------------------------------\n");
}
}
public void printPieces()
{
for(int i=0; i<12; i++)
{
System.out.print(player1Pieces[i].getX() + ", ");
System.out.print(player1Pieces[i].getY());
System.out.println();
}
}
public void printLegalMoves()
{
Play p1 = new Play();
CheckerMove[] temp = p1.getLegalMoves(1);
for(int i=0; i<temp.length; i++)
{
System.out.println("From: "+temp[i].fromX+", "+temp[i].fromY);
System.out.println("To: "+temp[i].toX+", "+temp[i].toY);
}
}
}//end class
主:
包neegjar; import java.util.ArrayList;
公共课主要 {
public static void main(String[] args)
{
Board newBoard = new Board();
//newBoard.alternateSquares();
//newBoard.alternateSquares();
//System.out.println(newBoard.player1Pieces[0].getX());
newBoard.placePieces();
//newBoard.printBoard();
newBoard.printLegalMoves();
//newBoard.printPieces();
}
} //结束课
答案 0 :(得分:0)
这是一个非常基本的调试问题。
你的方法canJump()有几个地方,它用数字索引一个数组,从中减去1或2;在其中一个地方,结果是-1,并且你的数组(或任何java数组)没有-1索引。
错误消息告诉您确切地发生了哪一行;我无法从您的来源告知,因为行号与您发布的行号不符。但是,它会在您的来源上准确无误。
学习查看java错误消息。它们是关于问题所在的非常好的信息来源(这不是语言运行时的普遍性)。它结合了虚拟机和强类型语言,使它能够比其他语言更准确地指出问题的根源。
学会使用调试器。您可以单步执行问题所在的代码,并查看变量在执行过程中的值。它对于发现和解决问题非常有用。