Hi i am designing a battleship game in java, and when i want to place the ships i wish to lookup a location in a 2d array in a class called player from a method in a class called vessel. This code will not compile and i am not sure why.
I can initialise a Player at the start of the vessel class, by typing: Player player1; but i was wandering if there was another way to do this, as when i run the Player class to place a ship a null pointer exception occurs as the Vessel class is not looking at the same player 1. Here is the code for the Vessel class:
public class Vessel { public int shipLength; public String shipLetter;
public Vessel() {
}
public void attack() {
}
public void placeShipPlayer1(int xpos, int ypos, boolean vertical)//method to place the the ships { int xPos = xpos;//x-position integer int yPos = ypos;//y-position integer boolean direction = vertical;//direction boolean int ship = shipLength;//integer for the length of the ship
while(ship > 0){ if(direction == true){ if(player1.defensive[xPos][yPos].equals(" - ")){//checks to see if the location is empty player1.defensive[xPos][yPos] = shipLetter; yPos++; ship--; }else{ System.out.println("There is already a ship in this location");//tells that user that there is already a ship in that location ship--; } } else if(direction == false){ if(player1.defensive[xPos][yPos].equals(" - ")){//checks to see whether the location is empty player1.defensive[xPos][yPos] = " d "; xPos++; ship--; } else{ System.out.println("There is already a ship in this location");//tells the user that there is already a ship in that location ship--; } } else{ System.out.println("Please type true or false in vertical");//informs the user that they have made an error in the vertical field } } player1.showDefensiveGrid();//proves that the ship has been placed correctly } }
Please could you help me with this problem. Any help would be grateful.
publicclass Vessel
{
publicint shipLength;
public String shipLetter;
public Vessel()
{
}
publicvoid attack()
{
}
publicvoid placeShipPlayer1(int xpos, int ypos, boolean vertical)//method to place the the ships
{
int xPos = xpos;//x-position integer
int yPos = ypos;//y-position integer
boolean direction = vertical;//direction boolean
int ship = shipLength;//integer for the length of the ship
while(ship > 0){
if(direction == true){
if(player1.defensive[xPos][yPos].equals(" - ")){//checks to see if the location is empty
player1.defensive[xPos][yPos] = shipLetter;
yPos++;
ship--;
}else{
System.out.println("There is already a ship in this location");//tells that user that there is already a ship in that location
ship--;
}
}
elseif(direction == false){
if(player1.defensive[xPos][yPos].equals(" - ")){//checks to see whether the location is empty
player1.defensive[xPos][yPos] = " d ";
xPos++;
ship--;
}
else{
System.out.println("There is already a ship in this location");//tells the user that there is already a ship in that location
ship--;
}
}
else{
System.out.println("Please type true or false in vertical");//informs the user that they have made an error in the vertical field
}
}
player1.showDefensiveGrid();//proves that the ship has been placed correctly
}
}