Battleship Game

Hi All,

I have to write the Battleship game for a programming assignment, and it's all going fine apart from one thing.

On my 10x10 array grid, the boats are randomly positioned. I have ten boats in all, four subs (length 2), three destroyers (length 3), two battleships (length 4) and one carrier (length 5).

Now, it is easy to check if the carrier has been sunk, because there is only one of them. How can I check to see if one of the other individual boats have been sunk? Keep in mind that two boats of the same type might be right next to eachother on the grid, so checking for that boat type in adjacent grid coordinates may return erroneous results.

Anyone have any ideas?


So the initial design is place stuff on a grid and decide what it belongs to based on a numerical value? The thing is, you are trying to get the type and identity of an entity over the same value, that just doesn't work.
What do you mean? I have generated the grid, and the boats are represented by a number. Blank spaces are represented by a 0. It is a grid of ints. That grid has be generated, or you can't play the game!

Are you saying it is impossible to program this game with boats of the same type?
No, I am saying your design is faulty.
Care to share how it can be properly implemented?
Why don't you have objects with positions? Implement a simple collision detection system with the sinking thing, and you have an easily expandable system.
For example, you could create structs for the ships, which determine position, rotation and size of the ship. You put these in a vector, and for each point you check whether or not a ship is at that position. There are some other things you have to pay attention to, like preventing ships from overlapping, but at least this way it is possible to identify which ship is at which position.
I don't know if this will work but if you use different integers for the individual sections of each boat and use the size to determine what type which boat it is and what type of boat it is.
Topic archived. No new replies allowed.