Rectangle 27 0

tic tac toe Java Tic Tac Toe: convert string to check for value in 2d array?


input = input.toUpperCase(); // Makes code work for all cases
int x = input.charAt(0) - 'A';
int y = input.charAt(1) - '1';
char currentValue = board[x][y];

After that, currentValue will contain the value currently on the game board at that location.

Do you mind elaborating on what is going on in your second and third line of code? I don't get why you're subtracting 'A' and '1'.

It's because arrays start from 0, if the user inputs 1, he actually refers to the 0 cell in the array. same for 'A'. it's the ASCII code. 'A' - 'A' = 0. 'B'-'A' = 1 (if i'm not mistaken, 'A' s code is 49, since it's an int.)

You can use String.charAt(int) to get a character from any point in the string. Note that, like arrays, the first character in a string is index 0, not 1.

Note