Fun Stuff > CLIKC
A programming thread!
Masterpiece:
Why can't you iterate over the array? It only has 10x10 fields anyway.
If you do it the other way around, you'd have to iterate over the list of boats, which would have to iterate over the fields they occupy, which just seems more, well, cumbersome.
Do you have to make a visual representation of the field? In that case I guess you could do the boat thing. In any other case, I guess it would be irrelevant to the code if the field you hit was a cruiser or an airship.
Beware that I'm only musing and am not a reliable reference.
If you do need to know which ship you're using, how about using a Map? In this case, I would create a Point -> Ship kind of map, aka, you input a point (for example, 4,6) and it either returns the reference to the ship that occupies that point, or a Null reference (as a stand-in for water). Then you can do fancy things like check if the ship is still alive after that shot, and removing that ship from the list of alive ships (which may be valuable if you want to check for a game-ended condition).
If you have any questions to my line of thinking, please ask. I'm not sure I know how I'd implement it in detail, but I'd love to work it out together with you. What are the methods you have to implement? What are their parameters, and their return values (and throws declarations, while we're at it).
If you want to, I can go online and we can have a chat on mumble. I love practical programming exercises like these.
Masterpiece:
Hmm. I didn't read that part about only being allowed access to java.lang libraries. Which ones do they entail?
Masterpiece:
By the way, if you really want to chat, send me a pm as i'll react to that quicker than to a forum post.
ankhtahr:
At first: Thanks!
java.lang
What I'm worried about is, that the tutors correcting these tasks value modelling above everything.
I need to implement two toString methods, creating the opponents and the players view. In the players view I need to differentiate between the different types of ships.
Here's an example output (toStringWithShips() on the top, toStringWithBombs() on the bottom. Their names, not mine.):
--- Code: ---.1.......
.....2...
..3..2...
..3......
4.3.4444.
4....1...
4......22
4........
.........
..1.333..
......XO..
..X.......
......O...
.O.O......
..........
.......O..
...O......
.......O..
..X.......
--- End code ---
And as this is supposed to work for any field sizes and any number of ships, and there are always less ships than fields, I thought it a good idea to iterate over the ships only.
The way I'm currently working on it is that every ship is an object, I have an array of ships, and every ship has a getter which tells me, whether it's sunk or not (by checking every field it's placed on, an information I can get by saving the end point, direction and ship type with every ship).
What I'm currently struggling with is changing the type of the fields the ship is placed on in the constructor, as the field is private in the "Sea" class.
And I actually just now remembered how arrays work in Java, and now things got a little bit more complicated.
Masterpiece:
Not being allowed to use util is MEAN. I'm afraid I can't help much here.
Wait, what's the sea class?
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version