戦艦プロジェクトで問題が発生しています。問題はMoveの確認と作成です。
敵のボードをクリックしてコンピューターの番になると、hit = inShips[i].checkMove(x, y); で null ポインター例外が発生します。checkMove メソッドで。
完全なプロジェクトは次のとおりです: http://db.tt/V6YiTJVw (selectionPanel および selectionFrame クラスで画像パスを変更する必要があります)
ご協力いただきありがとうございます
public void checkMove(JButton[][] inBoard, Ship[] inShips, int x, int y) {
boolean hit = false;
// Ship[] inShips = new Ship[5];
int i = -1;
System.out.println("CHECKING MOVE AT CLASS PLAYBOARD (checkMove): ");
System.out.println("xCoord " + x);
System.out.println("yCoord " + y);
System.out.println("");
// do {
i++;
System.out.println("INSHIPS: ");
System.out.println("!!!!!!!!!!!!!!!! " + i);
hit = inShips[i].checkMove(x, y);
System.out.println("IS HIT? : " + String.valueOf(hit));
// } while ((i < 4) && (!hit));
船クラスでのcheckMove:
public boolean checkMove(int x, int y)
{
for (int i = 0; i < this.size; i++)
{
if ((this.shipCoords[i][0] == x) && (this.shipCoords[i][1] == y))
{
this.piecesHit += 1;
this.lastPieceHit[0] = x;
this.lastPieceHit[1] = y;
if (this.piecesHit == this.size)
this.sunk = true;
return true;
}
}
return false;
}
public void getMove(int x, int y) {
if(this.computer == null)
checkMove(this.myBoard, this.myShips, x, y);
else
checkMove(this.myBoard, this.myShips, x, y);
if (this.myTurn == -1)
return;
this.myTurn = 1;
this.status.setText("Status: Waiting for your move.");
}
public void getMove(int x, int y) {
System.out.println("GETTING MOVE FROM COMPUTER CLASS: ");
System.out.println("%%%%%%%%%%%%COMPUTER CLASS X COORD FIRST COMES: " + x);
System.out.println("%%%%%%%%%%%%COMPUTER CLASS Y COORD FIRST COMES: " + y);
this.computerGameBoard.getMove(x, y); //Gets move from the player from PlayBoard class t through computerGameBoard variable.
int XCoord = this.compMoves[this.numMove][0];
int YCoord = this.compMoves[this.numMove][2];
System.out.println("!@#*(!@(#&*!@*&#!@*(#*(!@&*$^!@&$^#!@&*(#@!#!@");
for(int[] zz : compMoves) {
System.out.println("");
System.out.println(Arrays.toString(zz));
}
System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXCOORD: " + XCoord);
System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYCOORD: " + YCoord);
this.playerGameBoard.getMove(XCoord, YCoord);
this.computerGameBoard.makeMove(XCoord, YCoord);
this.numMove = numMove + 1;
}
public void makeMove(int x, int y)
{
System.out.println("MAKING MOVE WITH COORDS: X " + x + " Y " +y );
checkMove(this.opponentBoard, this.opponentShips, x, y);
if (this.myTurn == -1)
return;
this.myTurn = 0;
this.status.setText("Status: Waiting for opponent move");
if (this.computer != null)
this.computer.getMove(x, y);
}