Results 1 to 16 of 16
- 03-09-2012, 01:46 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
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!
- 03-09-2012, 01:57 AM #2
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
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.
- 03-09-2012, 02:00 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
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
- 03-09-2012, 02:03 AM #4
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
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?
- 03-09-2012, 02:09 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
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
- 03-09-2012, 02:13 AM #6
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
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:
and so forthJava Code:class Cell { private boolean ship = false; private char hit = "0"; ........ if(ship == true) { ....... }
- 03-09-2012, 02:24 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
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?
- 03-09-2012, 02:26 AM #8
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
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
- 03-09-2012, 02:33 AM #9
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
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?
- 03-09-2012, 02:36 AM #10
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
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
In the middle will take me a second to figure out, probably a for loop nested inside a while loop.Java Code:int[] ships = {1, 1, 2, 2, 3, 4, 5}; for(int count = 0; count < ships.length; count++) { ..................... }
- 03-09-2012, 02:38 AM #11
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
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.
- 03-09-2012, 02:40 AM #12
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
Re: Having trouble, Battleship program
somethin like that inside the one aboveJava 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; } } }
- 03-09-2012, 02:41 AM #13
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
Re: Having trouble, Battleship program
ok so what is that doing exactly?
- 03-09-2012, 02:47 AM #14
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
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.
- 03-09-2012, 02:54 AM #15
Member
- Join Date
- Mar 2012
- Posts
- 15
- Rep Power
- 0
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.
- 03-09-2012, 03:08 AM #16
Member
- Join Date
- Mar 2012
- Location
- Vestal, NY
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
Trouble with Hello World Program
By Arukas in forum New To JavaReplies: 4Last Post: 02-04-2012, 11:39 PM -
Help with Battleship program! 2D arrays
By tylerkung in forum New To JavaReplies: 3Last Post: 09-23-2011, 01:45 PM -
need help creating a battleship like program
By ss1 in forum New To JavaReplies: 12Last Post: 08-19-2011, 12:00 AM -
Battleship 2D Array Program
By AaronHopkins in forum New To JavaReplies: 6Last Post: 04-05-2011, 04:38 AM -
trouble with program
By jimJohnson in forum New To JavaReplies: 1Last Post: 04-03-2008, 09:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks