Hello! I’ve been trying to make a tic tac toe game, using a 2d array, against a computer. I’m trying to implement this using the number buttons 0-8 (like an array index) instead of through mouse input. The board works fine, and I haven’t gotten to the computer’s turn yet (it will be a simple randomly generated number to fill a blank space, no complicated AI here), but for some the input to put stuff on the board doesn’t work. Every input falls through the case statement and returns the default “incorrect key” error, even when the key pressed is a number in range. I’m having issues progressing / testing further when even this base mechanic doesn’t work. Please help!
int value = 0;
int[][] gameBoard = new int[3][3];
for(int row = 0; row < 3; row++){
for(int col = 0; col < 3; col++) {
gameBoard[row][col] = value++;
}
}
if (keyPressed == true) {
switch(key) {
case 0:
gameBoard[0][0] = 'O';
createShape(ELLIPSE, 70, 70, 200, 200);
System.out.println(gameBoard[0][0]);
break;
case 1:
gameBoard[0][1] = 'O';
break;
case 2:
gameBoard[0][2] = 'O';
break;
case 3:
gameBoard[1][0] = 'O';
break;
case 4:
gameBoard[1][1] = 'O';
break;
case 5:
gameBoard[1][2] = 'O';
break;
case 6:
gameBoard[2][0] = 'O';
break;
case 7:
gameBoard[2][1] = 'O';
break;
case 8:
gameBoard[2][2] = 'O';
break;
default: // catches numbers outside of 0-8
println("Incorrect key pressed! Please select a key between 0-8");
break;
}
}
}```