-4

this is my code: `

using System;
using System.Linq;

namespace Schiffeversenken
{
    class Program
    {
        static void Main(string[] args)
        {
            // Größe der Karte
            const int width = 10;
            const int height = 10;

            // Die Karte als 2d Array von ints initialisieren
            int[,] grid = new int[width, height];

            // Schiffe platzieren (fünf Schiffe der Größe 1x1)
            for (int i = 0; i < 5; i++)
            {
                int x = new Random().Next(0, width);
                int y = new Random().Next(0, height);
                grid[x, y] = 1;
            }

            // Spiel loop
            while (true)
            {
// Karte anzeigen
                Console.WriteLine("    0 1 2 3 4 5 6 7 8 9");
                Console.WriteLine("   -------------------");
                for (int yCoord = 0; yCoord < height; yCoord++)
                {
                    Console.Write($" {yCoord} |");
                    for (int xCoord = 0; xCoord < width; xCoord++)
                    {
                        if (grid[xCoord, yCoord] == 0)
                        {
                            Console.Write(" ");
                        }
                        else if (grid[xCoord, yCoord] == 1)
                        {
                            Console.Write("X");
                        }
                        else if (grid[xCoord, yCoord] == 2)
                        {
                            Console.Write("O");
                        }
                        Console.Write("|");

                    }
                    Console.WriteLine();
                }
                Console.WriteLine("   -------------------");
                Console.WriteLine("     0 1 2 3 4 5 6 7 8 9");
                Console.WriteLine("    (X-Koordinate)");
                
                // Schiessen
                Console.WriteLine("Gib die Koordinaten für deinen Schuss ein (x y):");
                int[] input = Console.ReadLine()
                    .Split(' ')
                    .Select(s => int.Parse(s))
                    .ToArray();
                int x = input[0];
                int y = input[1];

// Prüfen, ob die Indizes innerhalb der Grenzen des Arrays liegen
                if (x >= 0 && x < width && y >= 0 && y < height)
                {
                    // Prüfen, ob ein Schiff getroffen wurde
                    if (grid[x, y] == 1)
                    {
                        Console.WriteLine("Treffer!");
                        grid[x, y] = 2;
                    }
                    else
                    {
                        Console.WriteLine("Daneben.");
                    }
                }
                else
                {
                    Console.WriteLine("Die Koordinaten liegen außerhalb des Spielfelds.");
                }


            }
        }
    }
}


Why do I get this IndexOutOfRangeException Error?

System.IndexOutOfRangeException: Index was outside the bounds of the array. at Schiffeversenken.Program.Main(String[] args) in C:\Users\timoa\RiderProjects\Battleships\Battleships\Program.cs:line 64

When I enter the coordinates?

I already tried to let it check if the indices are within the bounds of the array. That didn't worked.

timosaiya
  • 1
  • 2

1 Answers1

1

Input validation is missing. If the user doesn't input something in the format x y then accessing the first or second part will lead to this exception.

            int x = input[0];
            int y = input[1];

should be

            int x,y;
            do
            {
                string[] input = Console.ReadLine()Split(' ');
            } while (!int.TryParse(input.ElementAtOrDefault(0), out x) || !int.TryParse(input.ElementAtOrDefault(1), out y))
Firo
  • 30,626
  • 4
  • 55
  • 94