here is my code - this is my first time working with object and class
Cell[][] board; //object
int cols=3; //number of columns
int rows=3; //number of rows
int player=0; //player
int win=0; //used for win or draw
int game=0; //for game screens
int full=9; //determines win or draw
void setup() {
size(300,300);
smooth();
int w = width;
int h = height;
board= new Cell[cols][rows];
for (int i=0; i<cols; i++)
{
for (int j=0; j<rows; j++)
{
board[i][j] = new Cell(w * i, h * j, w, h);
}
}
}
void draw() {
background(255); //Setting the background colour to white
if (game==0) //determining the screen
{
fill(0); //Seting fill colour to black
textSize(20); //Setting text size
text("Press Enter to Start", width/2-width/4, height/2); //Text
}
if (game==1) //determining the screen
{
checkGame();
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
board[i][j].display();
}
}
}
}
void mousePressed() {
if (game==1)
{
if (win==1)
{
for (int i=0; i<cols; i++)
{
for (int j=0; j<rows; j++)
{
board[i][j].click(mouseX, mouseY);
}
}
}
}
}
void keyPressed() {
if (game==0)
{
if (key==ENTER)
{
game=1;//Play Screen
full=9;
}
} else if (game==1 && win==0 && full==0)
{
if (key==ENTER)
{
game=0; //Start Again Screen
for (int i=0; i<cols; i++)
{
for (int j=0; j<rows; j++)
{
board[i][j].clean();
win=0;
full=9;
}
}
}
} else if (game==1&&(win==1||win==2))
{
if (key==ENTER)
{
game=0; //Start Again Screen
for (int i=0; i<cols; i++)
{
for (int j=0; j<rows; j++)
{
board[i][j].clean();
win=0;
full=9;
}
}
}
}
}
void checkGame() {
int rows=0;
//Checking for vert and horz condition
for (int col=0; col<cols; col++)
{
if (board[col][row].checkSate()==1&&board[col][row+1].checkSate()==1&&board[col][row+2].checkSate()==1) {
win=1; //vertical 0 win
} else if (board[row][col].checkState()==1&&board[row+1][col].checkState()==1&&board[row+2][col].checkState()==1) {
win = 1; //Horizontal 0 win
}
if (board[col][row].checkSate()==2&&board[col][row+1].checkSate()==2&&board[col][row+2].checkSate()==2) {
win=2; //vertical X win
} else if (board[row][col].checkState()==2&&board[row+1][col].checkState()==2&&board[row+2][col].checkState()==2) {
win = 2; //Horizontal X win
}
}
//Checking the diagonals
if (board[row][row].checkSate()==1&&board[row+1][row+1].checkSate()==1&&board[row+2][row+2].checkSate()==1) {
win=1; //Diagonal 0 win
} else if (board[row][row].checkSate()==2&&board[row+1][row+1].checkSate()==2&&board[row+2][row+2].checkSate()==2) {
win=2; //Diagonal X win
} else if (board[0][row+2].checkSate()==1&&board[1][row+1].checkSate()==1&& board[2][row].checkSate()==1) {
win=1; //0 win
} else if (board[0][row+2].checkSate()==2&&board[1][row+1].checkSate()==2&& board[2][row].checkSate()==2) {
win=2; //X win
}
//Printing the winner
fill(255);
textSize(20);
if (win==1) {
fill(0);
text("Player 1 \n Won", board[1][1].checkX()+40, board[1][1].checkY()+50);
} else if (win==2) {
fill(0);
text("Player2 \n Won", board[1][1].checkX()+40, board[1][1].checkY()+50);
}
if (win==1||win==2) {
fill(0);
textSize(35);
text("Press Enter to Start", width/2-width/2+23, height/2-height/6-20);
}
if (win==0&& full==0) {
fill(0);
textSize(35);
text("Press Enter to Start", width/2-width/2+23, height/2-height/6-20);
}
class Cell {
//declaring variables
int x;
int y;
int w;
int h;
int state;
//constructor
Cell(int tx, int ty, int tw, int th) {
x=tx;
y=ty;
w=tw;
h=th;
}
void click(int tx, int ty) {
int mx=tx;
int my=ty;
if (mx > x && mx < x + w && my >y && my < y + h)
{
if (player ==0 && state==0)
{
state=1;
full=-1;
player= 1;
} else if (player ==1 && state ==0) {
state=2;
full=-1;
player= 0;
}
}
}
void clean() {
state=0;
}
int checkState() {
return state;
}
int checkX() {
return x;
}
int checkY() {
return y;
}
void display() {
noFill();
strokeWeight(3);
rect(x, y, w, h);
if (state==1) {
ellipseMode(CORNER); //ellipse for zero
stroke(0); //setting stroke to black
ellipse(x, y, w, h);
} else if (state==2) {
stroke(0);
line(x, y, x+w, y+h); //Diagonal lines for x
line(x+w, y, x, y+h); //Diagonal lines for x
}
}
}