# Inserting a variable into a 2D array

Im unsure of how i can put either the x or an o into my 2d array for my 7x7 naughts and crosses type game?

``````class cell{
String player;
int turnNum;
char shape;
cell(int row, int col){

}
}

cell[][] grid=new cell;
``````

Here, I’ve hashed out your example a bit more.

``````String player;
int turnNum;

class Cell {
int symbol;
Cell() {
symbol = 0;
}
void assign(int new_symbol) {
symbol = new_symbol;
}
void draw() {
noFill();
stroke(255);
rect(-25,-25,50,50);
//rect(0,0,20,20);
if ( symbol == -1 ) {
fill(0, 0, 200);
text("X", 0, 0);
} else if ( symbol == 1 ) {
fill(200, 0, 0);
text("O", 0, 0);
}
//fill(255);
//text("(" + symbol + ")", 15,0);
}
}

Cell[][] cells = new Cell;

void setup() {
size(350, 350);
player = "AwesomePlayerName";
turnNum = -1;
for ( int i = 0; i < 7; i++) {
for ( int j = 0; j < 7; j++) {
cells[i][j] = new Cell();
cells[i][j].assign(int(random(-2, 2)));
}
}
}

void draw() {
background(0);
for ( int i = 0; i < 7; i++) {
for ( int j = 0; j < 7; j++) {
pushMatrix();
translate(25+50*i, 25+50*j);
cells[i][j].draw();
popMatrix();
}
}
}
``````

Notice that each Cell (now with a Capital C, as this is the naming convention for class names), needs only to store a single `int` to track if it is an X or an O. The symbol drawn in a Cell is based on the value stored in that Cell’s `symbol` variable.

1 Like