You are incorrectly determining the edge of the board. Here's one of your lines:
if(myGrid[i-1][j] == 'x'||i != 1){
First, indices start at 0. Second, check if you are on the left edge first before attempting to access the array. Do this by taking advantage of "short circuit" evaluation:
if (i != 0 && myGrid[i - 1][j] == 'x') {
Here, if i
is 0
, then the condition is false
and myGrid[i - 1][j] == 'x'
is never evaluated, and it doesn't have a chance to throw an ArrayIndexOutOfBoundsException
.
You can change your other conditions similarly. You were correct about 11 being the max here.
Incidentally, in each case, you generally break
in both cases of your if
condition, and the else
does nothing but break
. It looks more readable and more concise to just break
outside the if
:
if (condition) {
// perform operation
}
break;