Help with Tic-Tac-Toe Game (Input of keys 0..8)

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

try with case '0': please

case '1':

case '2':

they key is char and therefore the numbers must be char too

AND

int[][] gameBoard = new int[3][3];

should be

char[][] gameBoard = new char[3][3];

because you say later: gameBoard[2][1] = 'O';

‘O’ and ‘X’ are char (characters) not int (integer)


Remark I

You use keyPressed as a variable. This gets registered multiple times (potentially).

More reliable is keyPressed() as a function. This gets registered only once.


void keyPressed() {
switch(key) {
  case '0':
   ...
   ...


}

see keyPressed() / Reference / Processing.org


Remark II

for your project you need setup() and draw() later

because only with functions the Sketch loops and can receive inputs and make screen updates

check Processing Overview / Processing.org and here the Hello Mouse section

1 Like

Hello @XeroX_ing,

There is always more than one way to do things!

Try this:

void setup()
  { 
  println(0, '0', int('0'), hex('0'));  
  println("Press a number from 0 to 9");  
  }

void draw()
  {
  }

void keyPressed()
  {
  println(key, int(key), '0', int('0'), key-'0'); // The subtractiong casts to an int: int(key) - int('0') 
  }

This will work with your code:

switch(key-'0') // becomes an integer number and will work with your switch\case statement.

You will have to look up ASCII to help understand this.

There is a text tutorial on Interactivity here that may help:
https://processing.org/tutorials

You may also want to explore Java primitive data types:

:)

1 Like