# Identifying a tie on tictactoe

**URL:** <https://discourse.processing.org/t/identifying-a-tie-on-tictactoe/40574>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [January 13, 2023, 4:40pm UTC](https://discourse.processing.org/t/identifying-a-tie-on-tictactoe/40574 "2023-01-13T16:40:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![berrae](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@berrae](https://discourse.processing.org/u/berrae)\
**Post date:** [January 13, 2023, 4:40pm UTC](https://discourse.processing.org/t/identifying-a-tie-on-tictactoe/40574/1 "2023-01-13T16:40:15Z")

</div>

im wondering if theres a way to identify a tie in this tic tac toe game using the method of the rest of the game.  
here is my code \<  
Cell board; //Object  
int cols=3; //Number of columns  
int rows=3; //Number of rows  
int player=0; //Player  
int win=0; //Used for wins  
int screen=0; //Game screens  
int full=9; //Cells  
PFont font;

void setup() {  
size(500, 500); //Setting screen size  
font = loadFont(“cutetext.vlw”);  
textFont(font);  
board = new Cell[cols][rows]; //Creating new object  
for (int i=0; i\<cols; i++) //Loop until false columns  
{  
for (int j=0; j\<rows; j++)//Loop until false rows  
{  
board[i][j] = new Cell(width/3 \* i, height/3 \* j, width/3, height/3); //Drawing the board  
}  
}  
}

void draw() {  
background(#FCEDF3); //Setting the background colour to white  
if (screen==0) //determining the screen  
{  
fill(#FAC5D9); //Setting fill colour to black  
textSize(20); //Setting text size  
text(“press enter to start”, width/3.7, height/2); //Text  
textSize(40);  
text(“TIC TAC TOE”,100,140);  
fill(#FAC5D9);  
textSize(10);  
text(“by adelynne and nicole \<3”,10,490);  
} else if (screen==1) //Determining the screen  
{  
checkGame(); //Checking for winners  
for (int i = 0; i \< cols; i++) //Loop until false for columns  
{  
for (int j = 0; j \< rows; j++) //Loop until false for rows  
{  
board[i][j].display(); //Calling display function  
}  
}  
}  
}

void mousePressed() {  
if (screen==1) //Play screen  
{  
if (win==0) //No winner yet  
{  
for (int i=0; i\<cols; i++) //Loop until false for columns  
{  
for (int j=0; j\<rows; j++) //Loop until false for rows  
{  
board[i][j].click(mouseX, mouseY); //Calling click function at mouse point  
}  
}  
}  
}  
}

void keyPressed() {  
if (screen==0) //Starting screen  
{  
if (key==ENTER) //User presses enter key  
{  
screen=1;//Play Screen  
full=9; //Cells are full  
}  
} else if (screen==1 && win==0 && full==0) //Checking for play screen, no winners and empty cells  
{  
if (key==ENTER)//User presses enter key  
{  
screen=0; //Start Again Screen  
for (int i=0; i\<cols; i++) //Loop until false for columns  
{  
for (int j=0; j\<rows; j++) //Loop until false for rows  
{  
board[i][j].clean(); //Calling clean function  
win=0; //Ressetting winner  
full=9; //Cells are full  
}  
}  
}  
} else if (screen==1&&(win==1||win==2)) //Checking for play screen and winner  
{  
if (key==ENTER) //User presses enter key  
{  
screen=0; //Start Again Screen  
for (int i=0; i\<cols; i++) //Adding 1 to i til its greater or equal to cols  
{  
for (int j=0; j\<rows; j++) //Adding 1 to j til its greater or equal to rows  
{  
board[i][j].clean(); //Calling clean function  
win=0; //Resetting winner  
full=9; //Cells are full  
}  
}  
}  
}  
}

void checkGame() { //Checking for winners  
int row=0;  
//Checking for vertical and horizontal condition  
for (int col=0; col\<cols; col++)  
{  
if (board[col][row].checkState()==1&&board[col][row+1].checkState()==1&&board[col][row+2].checkState()==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].checkState()==2&&board[col][row+1].checkState()==2&&board[col][row+2].checkState()==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 diagonal conditions  
if (board[row][row].checkState()==1&&board[row+1][row+1].checkState()==1&&board[row+2][row+2].checkState()==1) {  
win=1; //Diagonal 0 win  
} else if (board[row][row].checkState()==2&&board[row+1][row+1].checkState()==2&&board[row+2][row+2].checkState()==2) {  
win=2; //Diagonal X win  
} else if (board[0][row+2].checkState()==1&&board[1][row+1].checkState()==1&& board[2][row].checkState()==1) {  
win=1; //0 win  
} else if (board[0][row+2].checkState()==2&&board[1][row+1].checkState()==2&& board[2][row].checkState()==2) {  
win=2; //X win  
}

//Printing the winner  
textSize(20); //Setting text size  
if (win==1) { //Player 2 wins  
fill(#F0BFD2); //Fill in pink  
text(“player 2 \n won!”, board[1][1].checkX()+40, board[1][1].checkY()+70);//Text  
} else if (win==2) { //Player 1 wins  
fill(#F0BFD2); //Fill in pink  
text(“player 1 \n won!”, board[1][1].checkX()+40, board[1][1].checkY()+70); //Text  
}  
if (win==1||win==2) { //Either players wins  
fill(#F0BFD2);  
textSize(20); //Setting text size  
text(“press enter to restart”, 130, 90);//Text

}  
if (win==0&& full==0) { //No winners and empty cells  
text(“press enter to restart”, 130, 90);//Text  
}  
}

class Cell { //Class

//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) { //User clicks function  
int mx=tx; //Declaring equality  
int my=ty; //Declaring equality  
if (mx \> x && mx \< x + w && my \> y && my \< y + h) {  
if (player == 0 && state==0) //First players turn  
{  
//New values  
state=1;  
full=-1;  
player= 1;  
} else if (player == 1 && state ==0) { //Secon players turn  
//New values  
state=2;  
full=-1;  
player= 0;  
}  
}  
}

void clean() { //Reset function  
state=0; //Assignning value  
}  
int checkState() { //Retains the state  
return state; //Returning to original state value  
}  
int checkX() { //Retains X  
return x; //Returning to original X value  
}  
int checkY() { //Retains Y  
return y; //Returning to original Y value  
}

void display() { //Board and looks function  
noFill(); //Removing fill  
strokeWeight(3); //Setting stroke thickness  
stroke(#FAC5D9);  
rect(x, y, w, h); //Rectangle for board  
if (state==1) { //Checking state value is equal to 1  
ellipseMode(CORNER); //Enabling corner mode  
ellipse(x, y, w, h); //Ellipse for zero  
} else if (state==2) { //Checking state value is equal to 2  
line(x, y, x+w, y+h); //Diagonal lines for x  
line(x+w, y, x, y+h); //Diagonal lines for x  
}  
}  
}

> pls help

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 13, 2023, 4:56pm UTC](https://discourse.processing.org/t/identifying-a-tie-on-tictactoe/40574/2 "2023-01-13T16:56:03Z")

</div>

Count the fields with state = x or = o (count the full fields)

- When no one wins and the count == 9 we have a tie

**Correction**

Ah, I just saw: you have the counter with variable `full` going from 9 to 0

Alas, you have a spelling mistake:

` full=-1;`

must be

` full -= 1;` (minus in front of = sign)

which is the same as `full = full - 1;` !!!

Or `full--;`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 13, 2023, 7:49pm UTC](https://discourse.processing.org/t/identifying-a-tie-on-tictactoe/40574/3 "2023-01-13T19:49:34Z")

</div>

You use screen variable in draw().

You can add further screens in draw for a win and for a tie

This is a better code than to hide it in other functions

Only in draw() you have the text() command then. Good.
