Question calculate number of ships in array

Eshman

New member
Joined
Feb 26, 2020
Messages
1
Programming Experience
Beginner
Need to calculate number of ships. Ships are presented as “battleship” game.“1” represent ship, “0” represents water. C#

C#:
namespace Ships
{
    class Program
    {
        static int[,] ships = new int[10, 10] {
            { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, },
            { 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, },
            { 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, },
            { 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, },
            { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, },
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, }
        };

        static void Main(string[] args)
        {
            int count = 0;

            // count ships here
            
            Console.WriteLine(count);

            Console.ReadLine();
        }
    }
}
 
Last edited by a moderator:
Have you put any thought into it? What do you think you might do? You don't just pluck code out of the air. You put some thought into the steps required to achieve your goal, formalise that into an algorithm and then write code to implement that algorithm. I think that it's safe to say that you haven't even tried that yet, so you haven't actually run into a real issue yet.
 
When I played battleship many years ago with original plastic boats, the smallest ship was 2x1 and there were rules on how closely ships could be packed together. I guess the world was moved on...
 
When I played battleship many years ago with original plastic boats, the smallest ship was 2x1 and there were rules on how closely ships could be packed together. I guess the world was moved on...
I was thinking about that myself. For instance, that array contains isolated 1 values, which indicates a 1x1 ship. There is also a 2x2 square there, which means that there is no way to determine whether that is two 2x1 ships, two 1x2 ships or four 1x1 ships. As such, that makes the original task of counting the number of ships present impossible. I would think that a sensible set of rules would require all ships to occupy at least two spaces and no two ships to be directly adjacent. That would mean that all adjacent 1 values in this array could be assumed to be part of the same ship.

In a world like that, it doesn't take much to come up with an algorithm that you can test manually and then implement in code. If you devise an algorithm first then you always have something to compare your code to in order to make sure that it makes sense. Not doing so is how people end up writing nonsensical code that has no connection to what they actually need to do. When people try to write code with nothing but an end goal in mind, the result is unlikely to achieve that goal. The code has to perform the steps but you're unlikely to be able to write that code if you don't know what the steps are.
 
When I played battleship many years ago with original plastic boats, the smallest ship was 2x1 and there were rules on how closely ships could be packed together. I guess the world was moved on...
I don't remember the original plastic "board" game having a rule that you can't place ships right next to each other and the earliest digital game I remember was Radar Mission (Nintendo Game Boy, 1991) which allowed ships to all placed next to each other too.
But the OP's array matrix isn't going to allow for that using just zero's and one's because there's no distinction between ship sizes and orientation.

What I would suggest is he uses multiple values, such as:
0 = empty space
1 = 2x1 ship
2 = one of the 1x3 ships
3 = other 3x1 ship
4 = 5x1 ship
a = 1x1 aircraft that moves around the 5x1 ship each turn after the 10th turn
 
I wonder if that rule was a house rule that my cousins made up to make it easier for a 5 year old to play the game...
 
I've never actually played Battleship so I don't know what the rules are. I guess that having ships adjacent to each other is not really a problem when you're using your eyes to look at a physical board. It definitely is a problem here though. If you use different numbers to represent different types of ships, you can at least solve the original problem here of counting the number of ships on the board. You could still have an issue though. It appears that this game allows either orientation for ships so you could not distinguish between two 2x1 ships next to each other horizontally or vertically.
 
Just a brain storming idea, what if he didn't have an array at all.
What if he had a class that had the the first cell of the grid, the length of the ship, and the orientation (vertical or horizontal).
Then all he would need is the collection of that class for however many (and sizes) of ships he would want his game to have.

The only issue with that approach is he'd want to make sure doesn't happen is cell collision (part of a ship placed on top of a part of another ship), which is only a concern on the "ship placement" screen he'll have for the user.
 
Just a brain storming idea, what if he didn't have an array at all.
What if he had a class that had the the first cell of the grid, the length of the ship, and the orientation (vertical or horizontal).
Then all he would need is the collection of that class for however many (and sizes) of ships he would want his game to have.

The only issue with that approach is he'd want to make sure doesn't happen is cell collision (part of a ship placed on top of a part of another ship), which is only a concern on the "ship placement" screen he'll have for the user.
Sounds reasonable, as long as this is not an assignment that has to be done using a 2D array.
 
What if he had a class that had the the first cell of the grid, the length of the ship, and the orientation (vertical or horizontal).
That's typically what is done when using sparse arrays. Instead of having a massive array, they just have lists of items and what spots they occupy within the array.
 

Latest posts

Back
Top Bottom