TicTacToe Game Support

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

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 '*';
}



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




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 '*';
}

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

That doesn’t seem to do much

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

1

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


2

AND same function:

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

not okay (imho)


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)

Check this out:
Step by Step
sourceCode

1 Like