在ClassA中,我得到一个nullPointerException。我做错了什么?
在这些课程中,我试图模拟战舰板的建造。
真正的C类如下:
package boardBuilder;
import java.util.Random;
public class Ship {
int[] x;
int[] y;
int shipSize;
public void setSize(int setSizeSize){
shipSize = setSizeSize;
x = new int[shipSize];
y = new int[shipSize];
}
public int getSize(){
return shipSize;
}
public int[] getX(){
return x;
}
public int[] getY(){
return y;
}
public void createShipForm(){
Random rnd = new Random();
x[0]=0;
y[0]=0;
int direction;
for(int extraPlace = 1; extraPlace+1<=shipSize; extraPlace++){
direction = Math.abs(rnd.nextInt()%4);
if(direction==0){
x[extraPlace]=x[extraPlace-1];
y[extraPlace]=y[extraPlace-1]+1;
}
if(direction==1){
x[extraPlace]=x[extraPlace-1]+1;
y[extraPlace]=y[extraPlace-1];
}
if(direction==2){
x[extraPlace]=x[extraPlace-1];
y[extraPlace]=y[extraPlace-1]-1;
}
if(direction==3){
x[extraPlace]=x[extraPlace-1]-1;
y[extraPlace]=y[extraPlace-1];
}
for(int checkLocation=0;checkLocation<extraPlace;checkLocation++){
if(x[extraPlace]==x[checkLocation]&&y[extraPlace]==y[checkLocation]){
extraPlace--;
break;
}
}
}
}
}
真正的A类如下:
package boardBuilder;
public class BoardBuilder {
int[][] board;
Ship[][] ships;
public void initializeBoard(int boardWidth, int boardLength, int num1, int num2, int num3, int num4, int num5, int num6){
board = new int[boardWidth][boardLength];
Ship[] ships1 = new Ship[num1];
Ship[] ships2 = new Ship[num2];
Ship[] ships3 = new Ship[num3];
Ship[] ships4 = new Ship[num4];
Ship[] ships5 = new Ship[num5];
Ship[] ships6 = new Ship[num6];
Ship[][] shipsCopy = {ships1, ships2, ships3, ships4, ships5, ships6};
ships = shipsCopy;
}
public void setShips(){
int count6 = ships[5].length;
int count5 = ships[4].length;
int count4 = ships[3].length;
int count3 = ships[2].length;
int count2 = ships[1].length;
int count1 = ships[0].length;
for(int set6 = 1; set6 <= count6; set6++){
board = AddShip.addShip(board, 6, ships[5][set6-1]);
}
for(int set5 = 1; set5 <= count5; set5++){
board = AddShip.addShip(board, 5, ships[4][set5-1]);
}
for(int set4 = 1; set4 <= count4; set4++){
board = AddShip.addShip(board, 4, ships[3][set4-1]);
}
for(int set3 = 1; set3 <= count3; set3++){
board = AddShip.addShip(board, 3, ships[2][set3-1]);
}
for(int set2 = 1; set2 <= count2; set2++){
board = AddShip.addShip(board, 2, ships[1][set2-1]);
}
for(int set1 = 1; set1 <= count1; set1++){
board = AddShip.addShip(board, 1, ships[0][set1-1]);
}
}
public int[][] getBoard(){
return board;
}
public Ship[][] getShips(){
return ships;
}
}
在setShips()方法的第8行,我输入了船只对象。
真正的B类:我在本课程第19行得到错误。
package boardBuilder;
import java.util.Random;
public class AddShip {
public static int[][] addShip(int[][] board, int size, Ship ship1){
Ship ship = new Ship();
ship = ship1;
boolean shipPlaced = false;
int[][] boardCopy = board;
int[] shipX = new int[size];
int[] shipY = new int[size];
int yPosition = 0;
int xPosition = 0;
int width = board.length;
int height = board[0].length;
while(!shipPlaced){
Random rnd = new Random();
ship.setSize(size);
ship.createShipForm();
shipX = ship.getX();
shipY = ship.getY();
for(int placeAttempt = 1; placeAttempt <=10&!shipPlaced; placeAttempt++){
shipPlaced=true;
xPosition = Math.abs(rnd.nextInt()%width);
yPosition = Math.abs(rnd.nextInt()%height);
for(int setCoordinate = 0; setCoordinate<size;setCoordinate++){
if(xPosition + shipX[setCoordinate]>=0&xPosition + shipX[setCoordinate]<width&yPosition + shipY[setCoordinate]>=0&yPosition + shipY[setCoordinate]<height){
if(boardCopy[xPosition + shipX[setCoordinate]][yPosition + shipY[setCoordinate]] == 0){
boardCopy[xPosition + shipX[setCoordinate]][yPosition + shipY[setCoordinate]] = 2;
}else{
shipPlaced=false;
break;
}
}else{
shipPlaced=false;
break;
}
}
if(shipPlaced){
boardCopy=surroundShip(boardCopy, shipX, shipY, xPosition, yPosition);
break;
}
if(shipPlaced==false){
boardCopy=board;
}
}
}
board = boardCopy;
return board;
}
public static int[][] surroundShip(int[][] board, int[] shipX, int[] shipY, int startX, int startY){
int shipSize = shipX.length;
int width = board.length;
int length = board[0].length;
for(int coordinate = 0; coordinate<shipSize;coordinate++){
for(int relX = -1; relX<=1; relX++){
for(int relY = -1; relY<=1; relY++){
if(relX+shipX[coordinate]+startX<width&relX+shipX[coordinate]+startX>=0&relY+shipY[coordinate]+startY<length&relY+shipY[coordinate]+startY>=0){
if(board[relX+shipX[coordinate]+startX][relY+shipY[coordinate]+startY]!=2){
board[relX+shipX[coordinate]+startX][relY+shipY[coordinate]+startY]=5;
}
}
}
}
}
return board;
}
}