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;
}