-3

Need to calculate the number of ships. Ships are presented as a “battleship” game.

“1” represents a ship, “0” represents water. 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;

     // code write here
    
     Console.WriteLine(count);

     Console.ReadLine();
  }
 }
}
Anastasia
  • 9
  • 4
  • 2
    Technically, you can't. There's not enough info encoded in the board, because if you have a 1x1 up next to a 2x1 or 3x1 there's no way to know if that's two ships or one. – Joel Coehoorn Oct 11 '21 at 14:12
  • 5
    Personally, I'd take the lazy route and use a different number for each type of ship, based on it's length. A 1x1 would be 1, 2x1 is 2, etc. – Broots Waymb Oct 11 '21 at 14:13
  • correct me if I'm wrong but isn't it illegal in a battleships game to place adjacent ships? because then counting the ships would become possible. – J. S. Garcia Oct 11 '21 at 14:15
  • 1
    @JSGarcia - I just checked the Hasbro rules for similar. Believe it's a common house rule, not an official one. – Damien_The_Unbeliever Oct 11 '21 at 14:16
  • 1
    @JSGarcia definitely not true. You can't go diagonal or overlap, but you can definitely be tricky with adjacency. – Code Stranger Oct 11 '21 at 14:17
  • 1
    well. then @BrootsWaymb 's suggestion is the way to go. – J. S. Garcia Oct 11 '21 at 14:19
  • 2
    Is it a given that you must use a twodimensional array? Because I would just _not_ use that, instead opting for tracking individual ship instances and rendering the battlefield dynamically off of that. That would be considerably easier in many ways, including keeping track of the ship count. – Flater Oct 11 '21 at 14:20
  • 1
    @JSGarcia: The given example also breaks the "one ship of each type" rule already. Note the 2x2 square of `1` digits (top-left-ish). This can only be achieved using some combination of 2x1 and 1x1 ships, but always more than one of at least one type. – Flater Oct 11 '21 at 14:22
  • 1
    sorry bad written task you need to count how many 1 in the two dimensional array – Anastasia Oct 11 '21 at 14:35
  • 1
    @Anastasia that changes the question entirely and is **much** simpler.. – Broots Waymb Oct 11 '21 at 14:44

3 Answers3

4

Based on the rewritten question and clarification in your comment..

int count = ships.Cast<int>().Count(x => x == 1);  

This will count the number of 1's in your 2D array (i.e. 20, in your example). Sum() would be slightly simpler, but I think Count is a little more self-documenting in this use case, and is simpler to change if you decide to use a value other than 1.

For a non-LINQ approach, you'd do something like:

int count = 0;
for (int i = 0; i < ships.GetLength(0); i++)
{
    for (int j = 0; j < ships.GetLength(1); j++)
    {
        if (ships[i, j] == 1)
            count++;

        //Could also just do:
        //count += ships[i, j];
        //Since you're only working with 1 and 0
    }
}
Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
0

Not possible: imagine you have this:

1 1 1
1 1 1

Are these two of length 3 or three of length 2?

You need to clarify your problem.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • Even within the normal rules of battleship, if you find just a 1x3 segment and that's all that's left, you can't know from the board alone if it's a 1x3 ship or a 1x2 ship + a 1x1 ship. – Joel Coehoorn Oct 11 '21 at 14:14
  • @JoelCoehoorn You wouldn't be able to distinguish `1 1 1 1 1` as a 3x1 and a 2x1, or a 1x1 and a 4x1. Secondly, note that in OP's example, there is a 2x2 square of `1` characters, which means that OP is not applying the normal rules of "one ship of each type". – Flater Oct 11 '21 at 14:15
  • @Flater If you're just counting ships, though, those are both still two ships regardless. But my comment wasn't meant as a criticism. It was a "this!, and even more!" – Joel Coehoorn Oct 11 '21 at 14:17
0

given the new question this should be enough for you:

int count = ships.Cast<int>().Sum();

with a simple search through the internet you would have easily found this though

J. S. Garcia
  • 366
  • 1
  • 9