Results 1 to 8 of 8
  1. #1
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Post need help in battleship bluej program

    I am working on a project on bluej called battleship and being new to it I cant figure out how to generate a ship(3 co ordinate long) on a 10X10 multi dimentional array. nothing graphical just plain co ordinates. can you please help me out. ask me questions if you need any more info.
    thanks

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: need help in battleship bluej program

    Hi Haddu, welcome to the forums.

    What have you tried so far and which part exactly are you having difficulties with?

    Regards.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: need help in battleship bluej program

    Without seeing the code, it's hard to be too specific.
    If the ship is horizontal, set 3 locations in a row (row is constant, columns change)
    if the ship is vertical, set 3 locations in a column (column is constant, rows change)
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Post Re: need help in battleship bluej program

    im still a stdent and this is my first time programming so the language is pretty basic. i want to generate a randomized ship in any location at a randomized location and directions around 3 co ordinate long. this program is just a snipet but its to long and doesnt really does the job. the randomized ship should be able to fit in the grid. so i have taken another 2 arrays and did the programing in it and the display will be on the grid which I have fugured out.
    Java Code:
     void program()
        {
          int [ ] a = new int[6];
          int [ ] b = new int[6];
            int rows=9;
        int x=(int)(Math.random()*9);
        int y=(int)(Math.random()*9);
        int direc=(int)(Math.random()*4);
         for(int i=0;i<3;i++)
        {
            if(x+3<=rows && x-3>=0 && y+3<=rows && y-3>=0)
         {
             if(direc==1)
             {a[i]=x;
             b[i]=y+i;
            }
             if(direc==2)
             {a[i]=x;
             b[i]=y-i;
            }
             if(direc==3)
             {a[i]=x+i;
             b[i]=y;
            }
             if(direc==4)
             {a[i]=x-i;
             b[i]=y;
            }
        }
        if(x+3<=rows && x-3>=0 && y+3<=rows)
         {
             if(direc==1)
             {a[i]=x;
             b[i]=y+i;
            }
             if(direc==3)
             {a[i]=x+i;
             b[i]=y;
            }
             if(direc==4)
             {a[i]=x-i;
             b[i]=y;
            }
        }
        if(x+3<=rows && x-3>=0 &&  y-3>=0)
         {
             if(direc==2)
             {a[i]=x;
             b[i]=y-i;
            }
             if(direc==3)
             {a[i]=x+i;
             b[i]=y;
            }
             if(direc==4)
             {a[i]=x-i;
             b[i]=y+1;
            }
        }
        if(x+3<=rows && y+3<=rows && y-3>=0)
         {
             if(direc==1)
             {a[i]=x;
             b[i]=y+i;
            }
             if(direc==2)
             {a[i]=x;
             b[i]=y-i;
            }
             if(direc==3)
             {a[i]=x+i;
             b[i]=y;
            }
             if(direc==4)
             {a[i]=x-i;
             b[i]=y+1;
            }
        }
    }
    }
    }

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: need help in battleship bluej program

    You can think of it this way: the top/left coordinates of your ship has to be in the interval [0,9] x [0,7] if the ship is positioned vertical or in the interval [0,7] x [0,9] if it is positioned horizontally. The rest of the ship lies below (vertical ship) or to the right (horizontal ship) of this first coordinate. Now you only need two directions instead of four ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    haddu is offline Member
    Join Date
    Sep 2014
    Posts
    16
    Rep Power
    0

    Default Re: need help in battleship bluej program

    Quote Originally Posted by JosAH View Post
    You can think of it this way: the top/left coordinates of your ship has to be in the interval [0,9] x [0,7] if the ship is positioned vertical or in the interval [0,7] x [0,9] if it is positioned horizontally. The rest of the ship lies below (vertical ship) or to the right (horizontal ship) of this first coordinate. Now you only need two directions instead of four ...

    kind regards,

    Jos
    can you please explain it to me again so that its a little easier.

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: need help in battleship bluej program

    The end of the ship closest to the top left corner can be at any coordinate within the array between [0][0] and [7][7]. The remainder of the ship either lie to the right of this point or beneath it.

    Regards.
    Last edited by Ronin; 09-12-2014 at 06:44 PM. Reason: Typo

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: need help in battleship bluej program

    To visualize this, take a piece of paper, draw a 10x10 grid, label all the squares with their indexes: 0,0 to 9,9
    The try placing the 3 unit ship at various places on the grid to see where it fits and where is does NOT fit because it runs off the grid.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. GUI for BattleShip program
    By xiz1815 in forum New To Java
    Replies: 2
    Last Post: 12-11-2012, 05:46 PM
  2. Battleship program
    By buckbuck2 in forum New To Java
    Replies: 2
    Last Post: 11-28-2012, 02:57 AM
  3. Battleship Program
    By zizou001 in forum New To Java
    Replies: 6
    Last Post: 11-15-2012, 08:21 AM
  4. Java Battleship Program
    By zizou001 in forum New To Java
    Replies: 6
    Last Post: 11-15-2012, 08:21 AM
  5. Help with Battleship program! 2D arrays
    By tylerkung in forum New To Java
    Replies: 3
    Last Post: 09-23-2011, 01:45 PM

Posting Permissions

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