# TicTacToe Game Support

**URL:** <https://discourse.processing.org/t/tictactoe-game-support/30050>\
**Category:** Coding Questions\
**Created:** [May 11, 2021, 5:44pm UTC](https://discourse.processing.org/t/tictactoe-game-support/30050 "2021-05-11T17:44:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Syntax64](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Syntax64](https://discourse.processing.org/u/Syntax64)\
**Post date:** [May 11, 2021, 5:44pm UTC](https://discourse.processing.org/t/tictactoe-game-support/30050/1 "2021-05-11T17:44:59Z")

</div>

What am I missing? Right Click works, but not Left Click. Most, if not all the code should be correct. Am i messing up a loop somewhere? I tried debugging but to no avail

```auto
char[][] mat;
boolean xturn;
void setup(){
  size(900,900);
  mat = new char [3][3];
  for(int i = 0; i<3; i++)
    for(int j = 0; j<3; j++)
      mat[i][j] = '*';
      stroke(0);  
      strokeWeight(5);
      line(width/3,0,width/3,height);
      line(width*2/3,0,width*2/3,height);
      line(0,height/3,width,height/3);
      line(0,height*2/3,width,height*2/3);
      PFont f = createFont("Calisto MT", 80);
      textFont(f);
      strokeWeight(3);
}
void draw(){}

void mousePressed(){
  if(mouseButton == LEFT)
  {
    if(!full() && win()=='*' && mat[mouseX/(width/3)][mouseY/(height/3)] =='*')
    {
      mat[mouseX/(width/3)][mouseY/(height/3)] = xturn?'X':'O';
      redraw();
      xturn=!xturn;
  }
  if(win()!='*')
  text(win()+" Wins!",380,450);
  if(full() && win()=='*')
  text("Tie!",440,450);
  }
  else{
    for(int i = 0; i<3; i++)
      for(int j = 0; j<3; j++)
        mat[i][j]='*';
        reDraw();
  }
}
void reDraw(){
  background(200);
  stroke(0);
  strokeWeight(5);
      line(width/3,0,width/3,height);
      line(width*2/3,0,width*2/3,height);
      line(0,height/3,width,height/3);
      line(0,height*2/3,width,height*2/3);
      for(int i = 0; i<3; i++)
        for(int j = 0; j<3; j++)  
        {
          if(mat[i][j]=='X')
          {
            stroke(0,0,255);
            line(i*width/3,j*height/3,(i+1)*width/3,(j+1)*height/3);
            line((i+1)*width/3,j*height/3,i*width/3,(j+1)*height/3);
          }   
          else if(mat[i][j]=='O')
          {
            noFill();
            stroke(255,0,0);
            ellipse((i+.5)*width/3,j+.5*height/3,width/3,height/3);
          }
          
}
}
boolean full(){
  for(int i = 0; i<3; i++)
    for(int j = 0; j<3; j++)
      if(mat[i][j] == '*')
        return false;
  return true;
}
char win(){
  for(int i = 0; i < i; i++)
  {
    if(mat[i][i]!='*')
      if((mat[0][i]==mat[1][i]&&mat[1][i]==mat[2][i])||(mat[i][0]==mat[i][1]&&mat[i][1]==mat[i][2]))
        return mat[i][i];
  }
  if((mat[0][0]==mat[1][1]&&mat[1][1]==mat[2][2])||(mat[0][2]==mat[1][1]&&mat[1][1]==mat[2][0]))
    return mat[1][1];
  return '*';
}

```

---

<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:** [May 11, 2021, 5:53pm UTC](https://discourse.processing.org/t/tictactoe-game-support/30050/2 "2021-05-11T17:53:11Z")

</div>

you can use println to check whether a certain section of your code is tun.  
Eg. in the LEFT mouse button of your if.  
That part works.

I think the error is elsewhere, maybe in the next if

Here I just inserted println

```auto

char[][] mat;

boolean xturn;

void setup() {
  size(900, 900);
  mat = new char [3][3];
  for (int i = 0; i<3; i++)
    for (int j = 0; j<3; j++)
      mat[i][j] = '*';
  stroke(0);  
  strokeWeight(5);
  line(width/3, 0, width/3, height);
  line(width*2/3, 0, width*2/3, height);
  line(0, height/3, width, height/3);
  line(0, height*2/3, width, height*2/3);
  PFont f = createFont("Calisto MT", 80);
  textFont(f);
  strokeWeight(3);
}
void draw() {
  stroke(0);  
  strokeWeight(5);
  line(width/3, 0, width/3, height);
  line(width*2/3, 0, width*2/3, height);
  line(0, height/3, width, height/3);
  line(0, height*2/3, width, height*2/3);
}

//---------------------------------------------------------------------------------

void mousePressed() {
  if (mouseButton == LEFT) {
    println("LEFT"); 
    if (!full() && win()=='*' && mat[mouseX/(width/3)][mouseY/(height/3)] =='*')
    {
      mat[mouseX/(width/3)][mouseY/(height/3)] = xturn?'X':'O';
      redraw();
      xturn=!xturn;
    }
    if (win()!='*')
      text(win()+" Wins!", 380, 450);
    if (full() && win()=='*')
      text("Tie!", 440, 450);
  } else {
    println("RT"); 

    for (int i = 0; i<3; i++)
      for (int j = 0; j<3; j++)
        mat[i][j]='*';
    reDraw();
  }
}
void reDraw() {
  background(200);
  stroke(0);
  strokeWeight(5);
  line(width/3, 0, width/3, height);
  line(width*2/3, 0, width*2/3, height);
  line(0, height/3, width, height/3);
  line(0, height*2/3, width, height*2/3);
  for (int i = 0; i<3; i++)
    for (int j = 0; j<3; j++)  
    {
      if (mat[i][j]=='X')
      {
        stroke(0, 0, 255);
        line(i*width/3, j*height/3, (i+1)*width/3, (j+1)*height/3);
        line((i+1)*width/3, j*height/3, i*width/3, (j+1)*height/3);
      } else if (mat[i][j]=='O')
      {
        noFill();
        stroke(255, 0, 0);
        ellipse((i+.5)*width/3, j+.5*height/3, width/3, height/3);
      }
    }
}

boolean full() {
  for (int i = 0; i<3; i++)
    for (int j = 0; j<3; j++)
      if (mat[i][j] == '*')
        return false;
  return true;
}

char win() {
  for (int i = 0; i < i; i++)
  {
    if (mat[i][i]!='*')
      if ((mat[0][i]==mat[1][i]&&mat[1][i]==mat[2][i])||(mat[i][0]==mat[i][1]&&mat[i][1]==mat[i][2]))
        return mat[i][i];
  }
  if ((mat[0][0]==mat[1][1]&& mat[1][1]==mat[2][2])||
    (mat[0][2]==mat[1][1]&&mat[1][1]==mat[2][0]))
    return mat[1][1];
  return '*';
}

```

---

<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:** [May 11, 2021, 6:26pm UTC](https://discourse.processing.org/t/tictactoe-game-support/30050/3 "2021-05-11T18:26:35Z")

</div>

> [@Chrisir](#):
>
> ```auto
> println("LEFT"); 
> if (!full() 
> 
> ```

I don’t know maybe !full() needs to be in () additionally

---

<div class="post-metadata">

**Author:** ![Syntax64](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Syntax64](https://discourse.processing.org/u/Syntax64)\
**Post date:** [May 11, 2021, 6:52pm UTC](https://discourse.processing.org/t/tictactoe-game-support/30050/4 "2021-05-11T18:52:09Z")

</div>

That doesn’t seem to do much

---

<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:** [May 11, 2021, 7:08pm UTC](https://discourse.processing.org/t/tictactoe-game-support/30050/5 "2021-05-11T19:08:02Z")

</div>

please note: ALWAYS write reDraw() with a capital D in your code

(check the left mouse button section: **redraw** , must be reDraw here)

- please note: `redraw` (with a small “d”) is an inbuilt command, so no error message here! But it’s not doing anything either (in your Sketch).

Chrisir

---

<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:** [May 11, 2021, 7:15pm UTC](https://discourse.processing.org/t/tictactoe-game-support/30050/6 "2021-05-11T19:15:36Z")

</div>

1

also check the first for-loop in function win(), it’s not correct

* * *

2

AND same function:

```auto
   if (mat[i][i]!='*')

```

not okay (imho)

* * *

3

> [@Chrisir](#):
>
> ` ellipse((i+.5)*width/3, j+.5*height/3, width/3, height/3);`

remember, you need `(j+.5)` with brackets

* * *

4

Remark: when restarting use `xturn=true;`

* * *

5

Remark: One could add the text **click right mouse button** to the screens win and tie

* * *

_(keywords TicTacToe Tic Tac Toe Tic-Tac-Toe “Noughts and crosses” Xs and Os X’y O’sies)_

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [May 12, 2021, 3:54pm UTC](https://discourse.processing.org/t/tictactoe-game-support/30050/7 "2021-05-12T15:54:22Z")

</div>

Check this out:  
[Step by Step](https://www.youtube.com/watch?v=GTWrWM1UsnA)  
[sourceCode](https://github.com/CodingTrain/website/blob/main/CodingChallenges/CC_149_Tic_Tac_Toe/Processing/CC_149_Tic_Tac_Toe/CC_149_Tic_Tac_Toe.pde)
