Page 3 of 3 FirstFirst 123
Results 41 to 60 of 60
  1. #41
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Help with Battleship Game

    An idea: have an array whose contents would be references to the Ship occupying a square. When there is a hit at a row,col, the row,col in this new array would have a reference to the Ship object.
    If you don't understand my response, don't ignore it, ask a question.

  2. #42
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    Not quite sure i am following this.. would this new array be apart of the Ship object? So the ship class would look like
    Java Code:
    private int size;
        String name;
        private int [][] reference = new int [10][10];
        
        public Ship(int s, String string, int[][]r){
            size = s;
            name = string;
            reference = r;
        }
    Just not sure how to track if all of the points on a ship have been hit or not, determining its orientation etc..

  3. #43
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Help with Battleship Game

    Ship[][] shipLocations = new Ship[#rows][#cols];
    then in the placeShip() method, save a reference to this for all the ship's x,y locations:
    shipLocations[x][y] = this;
    If you don't understand my response, don't ignore it, ask a question.

  4. #44
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    here is what i did, is it correct?
    Java Code:
    private Battleship game;
    	public void placeShip(int [][]a)
        {
           Random r = new Random();
           int orientation = r.nextInt(2);
           if(orientation == 0)//Vertical Ship
           {
              int x = r.nextInt(10);
              int y = r.nextInt(10-size);
              for(int z=0;z<size;z++)
              {
            	
                 a[y+z][x] = 1;
                 game.shipLocations[y+z][x] = this;
              }              
           }
           else//Horizontal Ship
           {
              int x = r.nextInt(10-size);
              int y = r.nextInt(10);
              for(int z=0; z < size; z++)
              {
                 a[y][x+z] = 1;
                 game.shipLocations[y][x+z] = this;
              } 
           }
        }
    Is this right? and what do i do then with the stored shipLocations, i am not quite sure exactly what this did, and the meaning of "this"?

  5. #45
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Help with Battleship Game

    this is a reference to the current object where the code is executing.

    If there is a hit at location: x,y shipLocations[x][y] gives a reference to the Ship object at that location.
    If you don't understand my response, don't ignore it, ask a question.

  6. #46
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    so this is storing hit values in their respective ships in which they hit? So if it is a hit it goes back and detects which ship the hit affected? then how do i reference back to check on which ship has been affected and how many times?

  7. #47
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Help with Battleship Game

    When there is a hit you find the Ship by going into the shipLocations array.
    If you don't understand my response, don't ignore it, ask a question.

  8. #48
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    so each ship has a ship locations array? or is there a single array? How do i put this all together, for some reason i am still not getting it? aircraftcarrier.shipLocations, etc? I dont get the whole concept of doing it this way and what use it is, how i would determine if a ships size has been occupied by 3's or hits, or has been a number of times equal to its size.

  9. #49
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Help with Battleship Game

    Some where you asked a question about how to do .... I've forgotten what you asked.
    The shipLocations array was my answer to that question. If the question has gone away, then there is no need for this array.
    If you don't understand my response, don't ignore it, ask a question.

  10. #50
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    i was asking on how to keep track of hits on each Ship, and the locations of each Ship.

  11. #51
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Help with Battleship Game

    The ship should keep track of its location.

    When a ship is hit, use the shipLocations array to find the ship that was hit.
    If you don't understand my response, don't ignore it, ask a question.

  12. #52
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    So this is basically just creating an array just like the board, for every ship and placing a 1 inside of it where the ship is on the board? Then how would i use the shipLocations array to find which ship got hit?

  13. #53
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Help with Battleship Game

    There would be ONE shipLocations array
    If you don't understand my response, don't ignore it, ask a question.

  14. #54
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    So whats the point of it then? wouldnt there be overlap of the two fields (compfield, myfield) that are already created?

  15. #55
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Help with Battleship Game

    What do those arrays contain?
    What does the shipLocations array contain?
    If you don't understand my response, don't ignore it, ask a question.

  16. #56
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    They contain 0 and 1's where there isnt a ship, and where there is respectively. and i am really still not sure what the shipLocations array contains

  17. #57
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Help with Battleship Game

    what the shipLocations array contains
    Go back and read the previous posts. It was defined and values were assigned to it.
    If you don't understand my response, don't ignore it, ask a question.

  18. #58
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    a reference to the ship object at each location.

  19. #59
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help with Battleship Game

    by the way, this gives me a nullpointer exception. Another way i was thinking which may be easier is to have a "life" attribute to each Ship object, but it wouldn't be as convenient... still determined to figure this out

  20. #60
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Help with Battleship Game

    You should test for null BEFORE using it.
    If you don't understand my response, don't ignore it, ask a question.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Battleship game - arrays
    By makig in forum New To Java
    Replies: 9
    Last Post: 11-17-2011, 07:21 PM
  2. Battleship Game - really need help
    By thrashsynergy in forum Java Applets
    Replies: 5
    Last Post: 04-25-2011, 07:21 PM
  3. Battleship game
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 09:42 PM
  4. Java Battleship Game Help PLEASE
    By mars_red in forum New To Java
    Replies: 0
    Last Post: 02-12-2008, 01:09 AM
  5. Java BattleShip game help
    By mars_red in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 12:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •