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

**URL:** <https://discourse.processing.org/t/help-with-tic-tac-toe-game-input-of-keys-0-8/43461>\
**Category:** Processing\
**Created:** [December 12, 2023, 7:55pm UTC](https://discourse.processing.org/t/help-with-tic-tac-toe-game-input-of-keys-0-8/43461 "2023-12-12T19:55:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![XeroX\_ing](https://avatars.discourse-cdn.com/v4/letter/x/7ba0ec/32.png) [@XeroX\_ing](https://discourse.processing.org/u/XeroX_ing)\
**Post date:** [December 12, 2023, 7:55pm UTC](https://discourse.processing.org/t/help-with-tic-tac-toe-game-input-of-keys-0-8/43461/1 "2023-12-12T19:55:24Z")

</div>

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!

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 12, 2023, 8:08pm UTC](https://discourse.processing.org/t/help-with-tic-tac-toe-game-input-of-keys-0-8/43461/2 "2023-12-12T20:08:11Z")

</div>

try with `case '0':` please

```auto
case '1':

case '2':

```

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 12, 2023, 8:09pm UTC](https://discourse.processing.org/t/help-with-tic-tac-toe-game-input-of-keys-0-8/43461/3 "2023-12-12T20:09:22Z")

</div>

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.

```auto

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

}

```

see [keyPressed() / Reference / Processing.org](https://processing.org/reference/keyPressed_.html)

* * *

**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](https://processing.org/tutorials/overview) and here the Hello Mouse section

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 13, 2023, 2:20am UTC](https://discourse.processing.org/t/help-with-tic-tac-toe-game-input-of-keys-0-8/43461/4 "2023-12-13T02:20:12Z")

</div>

Hello @XeroX_ing,

There is always more than one way to do things!

Try this:

```auto
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:

```auto
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](https://processing.org/tutorials)

You may also want to explore Java primitive data types:

> **[Introduction to Java Primitives | Baeldung](https://www.baeldung.com/java-primitives)**
>
> Learn the basics of Java's primitive data types.

`:)`
