Results 1 to 16 of 16
  1. #1
    Corrrrr is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Having trouble, Battleship program

    Ok so my problem isnt exactly with the code aspect but how to make the parts of my program work together.
    I have to make a battleship program with four classes(Board, Cell, Ship, and Main)
    The problem I'm having is not knowing what needs to be done to get these classes to work together.

    So right now I have a 10x10 2d array that displays on the console(I'm using Eclipse).
    You can put in your coordinates and then I just have it change the display from a 0 to 1.

    I don't know how I am supposed to get the Cell class to represent a Ship or no Ship.
    Right now I don't have too much code but I'll be happy to provide it if its needed to get help.
    Right now I think I could use a separate array for Cell and then an array that just displays the "board"

    Please help!!! And I'm not looking for someone to just send me a block of code, I want to learn how to do this myself!

  2. #2
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    I use blueJ so i don't know if i can help that much, but it sounds like you could just change the color of the cell where you input the coordinates. Are you using JPanel or applet? i could help you more with JPanel than applet.

  3. #3
    Corrrrr is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    Im not using any kind of GUI. It just puts a 10x10 square of 0s on the console. you know, like 00000
    00000
    But I need Cell to represent either a Ship or not a Ship so when the user guesses the coordinate, the 0 will either become an X for a hit or a space for a miss
    like 000X0
    00 X0
    000 0

  4. #4
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    Oh, ok. So when the user inputs the ships coordinates, what are you looking for, the location of the first one of course, but are you also having the user input the end point or are the ships a set size?

  5. #5
    Corrrrr is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    Yeah we have to have Ships with sizes 1,1,2,2,3,4,5 so 7 ships in all that need to be placed on the board and cant intersect. So the board needs to made up of Cells that are either a ship or not.
    But I have no idea how to do that part. Make the Cell class be a Ship object or whatever. And then after that, how would I make the Cell a 0, X, or space based on being a Ship or not... Im pretty lost

  6. #6
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    Just make a boolean ship in the cell class. That sounds like it would solve part of your problem. I don't think it sounds like you need a ship class. Ex:

    Java Code:
    class Cell
    {
    private boolean ship = false;
    private char hit = "0";
    ........
    if(ship == true)
    {
    .......
    }
    and so forth

  7. #7
    Corrrrr is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    Yeah that would probably be easier, but the professor gave us a list of "preferred" classes to use and that itll be graded on program design. I think I'll just try what you mentioned because that looks way easier. So if I do that, how would I go about placing the "ships" on the board?

  8. #8
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    How about making them be displayed as a 1? When ship is true, the char at that cell would be 1, and if hit is true, make it an X. That sounds like it'll work

  9. #9
    Corrrrr is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    Ok, so now I just need a way to place the ships onto the board randomly. could you make like a for loop that says something like:

    for (int row = 0; row < (size of ship: 3,4,5); row++){
    board[row][col] = 1;
    }

    so that would make the first ship of whatever size take up a vertical piece of the board??
    The for loop probably isnt perfect, but something kind of like that?

  10. #10
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    ok, you will need nested loops to place them randomly and a Random object. I'm thinking you will need an array with the values of the ships so

    Java Code:
    int[] ships = {1, 1, 2, 2, 3, 4, 5};
    for(int count = 0; count < ships.length; count++)
    {
    .....................
    }
    In the middle will take me a second to figure out, probably a for loop nested inside a while loop.

  11. #11
    Corrrrr is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    Ok yeah I have the array for the ship lengths. That looks a little better than my idea lol thanks.

  12. #12
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    Java Code:
    for(int counter = 0; counter < board.length; counter++)
    {
    for(int xcount = 0; xcount < board[counter].length; xcount++)
    {
    if(board[counter][xcount].ship != true)
    {
    ship = true;
    board[counter][xcount] = 1;
    }
    }
    }
    somethin like that inside the one above

  13. #13
    Corrrrr is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    ok so what is that doing exactly?

  14. #14
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    Well, i didn't put in the code to select where to put it, because thats easy enough its just the looping thats hard. That goes through the entire board and places a ship where ever theres an open space. You would want to restrict the area that is being run through the loops so that it doesn't fill in the entire board.

  15. #15
    Corrrrr is offline Member
    Join Date
    Mar 2012
    Posts
    15
    Rep Power
    0

    Default Re: Having trouble, Battleship program

    ok ok cool. that should help me a ton. Thanks so much. I'll probably be back on here asking for help later, maybe tomorrow. But thanks so much for the ideas.

  16. #16
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

Similar Threads

  1. Trouble with Hello World Program
    By Arukas in forum New To Java
    Replies: 4
    Last Post: 02-05-2012, 12:39 AM
  2. Help with Battleship program! 2D arrays
    By tylerkung in forum New To Java
    Replies: 3
    Last Post: 09-23-2011, 02:45 PM
  3. need help creating a battleship like program
    By ss1 in forum New To Java
    Replies: 12
    Last Post: 08-19-2011, 01:00 AM
  4. Battleship 2D Array Program
    By AaronHopkins in forum New To Java
    Replies: 6
    Last Post: 04-05-2011, 05:38 AM
  5. trouble with program
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-03-2008, 10:29 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
  •