The Artima Developer Community
Sponsored Link

Java Answers Forum
a battleships game in java

2 replies on 1 page. Most recent reply: Mar 1, 2005 2:19 AM by Scott Fisher

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Scott Fisher

Posts: 2
Nickname: tiger86
Registered: Feb, 2005

a battleships game in java Posted: Feb 26, 2005 1:24 PM
Reply to this message Reply
Advertisement
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.


perry anderson

Posts: 10
Nickname: anderson
Registered: Feb, 2005

Re: a battleships game in java Posted: Feb 28, 2005 3:30 PM
Reply to this message Reply
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
  }
}


this is a terrible algorithm

you must be really stupid

sorry, can't be done

tough

- perry

Scott Fisher

Posts: 2
Nickname: tiger86
Registered: Feb, 2005

Re: a battleships game in java Posted: Mar 1, 2005 2:19 AM
Reply to this message Reply
dont worry about it now, i have fininshed it and it does work with some slight alterations.
Anyway thanks for your encouragement and faith.

Scott

Flat View: This topic has 2 replies on 1 page
Topic: AT commands to modem using javax.comm Previous Topic   Next Topic Topic: NullPointerExceptions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use