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