Help with placing a shape on grid for tic tac toe. My code is shown below

please format code with </> button * homework policy * asking questions

char player=‘X’+‘O’; // inputs 2 players

int cols=3;
int rows=3;

Cell[][] board= new Cell[cols][rows];

void setup() {
size(600,600);

for(int i=0; i<cols; i++) {
for(int j=0; j<rows; j++) {
board[i][j]= new Cell(i150,j150,150,150);
}
}
}

void draw(){

for(int i=0; i<cols; i++) {
for(int j=0; j<rows; j++) {
board[i][j].display();
}
}

if(mousePressed){ 

if(mouseButton == RIGHT){
player=‘X’;
stroke(0,255,0); // colour of X
line(mouseX-50,mouseY-50,mouseX+50,mouseY+50);
line(mouseX+50,mouseY-50,mouseX-50,mouseY+50); //Right click the mouse to spawn an X
println(“Player O’s turn”); /Prints when player X took their turn
to indicate player O’s turn
/
}
if(mouseButton == LEFT){
player=‘O’;
stroke(255,0,0); //colour of O
ellipse(mouseX,mouseY,50,50); //Left click the mouse to spawn an O
println(“Player X’s turn”); /*Prints when player O took their turn */

}
}
}

class Cell {
float x;
float y;
float w;
float h;
int state;
Cell (float _x, float _y, float _w, float _h) {
x= _x;
y= _y;
w= _w;
h= _h;
}
void display() {
stroke(0);
fill(255,255,255);
tint (rgb,alpha);
rect(x,y,w,h);
}
}

Hi,

Welcome to the community! :wink:

Few things before we start digging into the real issue :

  • Nice tip : you can auto format your code by pressing Ctrl + T in the Processing IDE. It’s going to correct indentations and spaces.

  • On the forum, you need to always format your code inside code blocks by using the </> button. You can edit your previous post to change that. Multi line code is between those characters : ```

  • When starting a new thread, your issue description shouldn’t be inside the title but rather in the body of the message. Also be descriptive otherwise people are not willing to answer you :wink:
    Anyway check the posting guidelines and the FAQ :

Oh didn’t know most of this have only joined recently, thank you very much

3 Likes