-2

The issue: I'm recreating Battleship within a console app and the user input is in the format 'letter-number-letter-number' (A1E1). The program needs to find if the second letter's index when added or subtracted by 4 is the same as the first letter to validate that the user input is of an accepted length to the ship type. However, when my code runs the index is outside the bounds of the array.

For example, a Cruiser is 5 spaces long. Therefore, to place the Cruiser horizontally, the numbers must be the same and the letters must have a distance of 4 elements from one another within the validInputs array.

Some input's work, for example: A3E3 works, however, E3A3 will go outside the bounds.

public static char[] validInputs = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
    'I', 'J', '1', '2', '3', '4', '5', '6', '7', '8', '9', 't' };                 

if (placementInput[1] - placementInput[3] == 0)
{
    char validInputCheck = placementInput[2];

    // Finds index of the 2nd letter in valid inputs
    int validInputIndex = Array.IndexOf(validInputs, validInputCheck);

    // Validates that the 2nd letter is either 4 steps behind or 
    // ahead of the first letter within the validInputs array. 
    if (placementInput[0] == validInputs[validInputIndex - 4] || 
        placementInput[0] == validInputs[validInputIndex + 4])
    {
        validationCounter++;
        break;
    }

Ken White
  • 123,280
  • 14
  • 225
  • 444
Deanjvande
  • 11
  • 1
  • You have to validate that `validInputIndex + 4` and `validInputIndex - 4` are an actually within the bounds of the indexes in the array before you try to access them. – Rufus L Apr 27 '21 at 17:51
  • 1
    Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Ken White Apr 27 '21 at 17:57

1 Answers1

0

The exception means you're attempting to access an index that's outside of the bounds of the array.

The reason that "E3A3" doesn't work is that you're using the second letter for the comparison (validInputCheck = placementInput[2]), and in "E3A3", that letter is A.

The letter A has an index of 0 in the validInputs array, and in your code you attempt to get validInputs[validInputIndex - 4], which evaluates to validInputs[-4], which is obviously outside the array bounds.

The reason that "A3E3" works is that the letter E is far enough away from the ends of the array (at index 4) such that the subtraction and addition don't result in a number outside the bounds.

In order to avoid the exception, you need to validate that your indexes are inside the array bounds before trying to access them.

For example:

if ((validInputIndex - 4 > -1 && 
     placementInput[0] == validInputs[validInputIndex - 4]) || 
    (validInputIndex + 4 < validInputs.Length && 
     placementInput[0] == validInputs[validInputIndex + 4]))
{
    validationCounter++;
    break;
}

As a side note, typically we use the equality operator to determine if two values are equal rather than doing a mathematic operation and then comparing the result, i.e:

if (placementInput[1] == placementInput[3])

Rather than:

if (placementInput[1] - placementInput[3] == 0)
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Thanks so much! I wasn't aware that you had to validate that the index was within bounds in this situation. This was a lot of help. – Deanjvande Apr 27 '21 at 18:22