So me and a friend are trying to make a tic-tac-toe game. Everything was going fine and I ran into this problem: The program is working as long as you don’t press on a cell you have already put an X or an O. Though we have made our program so it wouldn’t have such a problem. Whenever you press a cell you have already put an X or an O the program freezes and doesn’t even let you press Esc. do not care about the width/height stuff it is all correct for sure. just pls check the mousePressed function and the Cell class. Thank you.
- cell[0] is top left
- cell[1] is top middle
- cell[2] is top right
- cell[3] is middle left etc.
Also pl is a variable to determine who has the cell. 2 means it is neutral 1 that it is an X and 0 means an O
This is the class in one tab:
class Cells{
float x;
float y;
int pl = 2;
int count;
Cells(float xpos,float ypos,int num){
x = xpos;
y = ypos;
count = num;
}
void xoro(int num){
strokeWeight(8);
if(num == 1){
stroke(255,0,0);
line(width/42.6+x,height/42.6+y,width/3.87+x,height/3.87+y);
line(width/42.6+x,height/3.87+y,width/3.87+x,height/42.6+y);
}
else if(num == 0){
stroke(0,0,255);
noFill();
ellipse(width/7.1+x,height/7.1+y,width/4.26,height/4.26);
}
}
and the main program in another:
Cells[] cell = new Cells[9];
int moves = 9;
float adjx,adjy;
boolean win = false;
int m;
void setup(){
//fullScreen();
size(640,640);
if (width >= height){
adjx = (width - height)/2;
adjy = 0;
width=height;
}
else{
adjy = (height - width)/2;
adjx = 0;
height=width;
}
background(0);
stroke(150);
strokeWeight(1);
line(adjx+(width/12.8),adjy+(height/3.5+height/12.8),adjx+(width-width/12.8),adjy+(height/3.5+height/12.8));
line(adjx+(width/12.8),adjy+(height-height/2.5+(height/12.8)/2),adjx+(width-width/12.8),adjy+(height-height/2.5+(height/12.8)/2));
line(adjx+(width/3.5+width/12.8),adjy+(height-height/12.8),adjx+(width/3.5+width/12.8),adjy+(height/12.8));
line(adjx+(width-width/2.5+(width/12.8)/2),adjy+(height-height/12.8),adjx+(width-width/2.5+(width/12.8)/2),adjy+(height/12.8));
stroke(100);
text("Press Esc to exit",adjx+(width/2-35),adjy+(height-height/128));
cell[0] = new Cells(adjx+(width/12.8),adjy+(height/12.8), 0);
cell[1] = new Cells(adjx+(width/3.5+width/12.8),adjy+(height/12.8), 1);
cell[2] = new Cells(adjx+(width-width/2.5+(width/12.8)/2),adjy+(height/12.8), 2);
cell[3] = new Cells(adjx+(width/12.8),adjy+(height/3.5+height/12.8), 3);
cell[4] = new Cells(adjx+(width/3.5+width/12.8),adjy+(height/3.5+height/12.8), 4);
cell[5] = new Cells(adjx+(width-width/2.5+(width/12.8)/2),adjy+(height/3.5+height/12.8), 5);
cell[6] = new Cells(adjx+(width/12.8),adjy+(height-height/2.5+(height/12.8)/2), 6);
cell[7] = new Cells(adjx+(width/3.5+width/12.8),adjy+(height-height/2.5+(height/12.8)/2), 7);
cell[8] = new Cells(adjx+(width-width/2.5+(width/12.8)/2),adjy+(height-height/2.5+(height/12.8)/2), 8);
}
void draw(){
}
void mousePressed(){
m = moves % 2;
win = false;
while(win == false){
if (moves > 0){
if (mouseX<adjx+(width/3.5+width/12.8) && mouseY<adjy+(height/3.5+height/12.8) && mouseX>adjx && mouseY>adjy && cell[0].pl == 2){
cell[0].xoro(m);
cell[0].pl = m;
win = true;
}
if (mouseX<adjx+(width-width/2.5+(width/12.8)/2) && mouseY<adjy+(height/3.5+height/12.8) && mouseX>adjx+(width/3.5+width/12.8) && mouseY>adjy && cell[1].pl == 2){
cell[1].xoro(m);
cell[1].pl = m;
win = true;
}
if (mouseX<adjx+width && mouseY<adjy+(height/3.5+height/12.8) && mouseX>adjx+(width-width/2.5+(width/12.8)/2) && mouseY>adjy && cell[2].pl == 2){
cell[2].xoro(m);
cell[2].pl = m;
win = true;
}
if (mouseX<adjx+(width/3.5+width/12.8) && mouseY<adjy+(height-height/2.5+(height/12.8)/2) && mouseX>adjx && mouseY>adjy+(height/3.5+height/12.8) && cell[3].pl == 2){
cell[3].xoro(m);
cell[3].pl = m;
win = true;
}
if (mouseX<adjx+(width-width/2.5+(width/12.8)/2) && mouseY<adjy+(height-height/2.5+(height/12.8)/2) && mouseX>adjx+(width/3.5+width/12.8) && mouseY>adjy+(height/3.5+height/12.8) && cell[4].pl == 2){
cell[4].xoro(m);
cell[4].pl = m;
win = true;
}
if (mouseX<adjx+width && mouseY<adjy+(height-height/2.5+(height/12.8)/2) && mouseX>adjx+(width-width/2.5+(width/12.8)/2) && mouseY>adjy+(height/3.5+height/12.8) && cell[5].pl == 2){
cell[5].xoro(m);
cell[5].pl = m;
win = true;
}
if (mouseX<adjx+(width/3.5+width/12.8) && mouseY<adjy+height && mouseX>adjx && mouseY>adjy+(height-height/2.5+(height/12.8)/2) && cell[6].pl == 2){
cell[6].xoro(m);
cell[6].pl = m;
win = true;
}
if (mouseX<adjx+(width-width/2.5+(width/12.8)/2) && mouseY<adjy+height && mouseX>adjx+(width/3.5+width/12.8) && mouseY>adjy+(height-height/2.5+(height/12.8)/2) && cell[7].pl == 2){
cell[7].xoro(m);
cell[7].pl = m;
win = true;
}
if (mouseX<adjx+width && mouseY<adjy+height && mouseX>adjx+(width-width/2.5+(width/12.8)/2) && mouseY>adjy+(height-height/2.5+(height/12.8)/2) && cell[8].pl == 2){
cell[8].xoro(m);
cell[8].pl = m;
win = true;
}
if (win == true){
moves--;
}
}
}
}