How do I write the code to check for a winner and cat's game?

char matrix[][]=new char[3][3],check; int row,col; boolean xTurn,gameOver; PFont myFont;

void setup() { size(600,800); background(0); check='N'; myFont = createFont("Georgia", 50); textFont(myFont); xTurn=true; gameOver=false; for(int row=0;row<3;row++) for(int col=0;col<3;col++) { matrix[row][col]='*'; } }

void draw() { check=checkForGameOver(); if(check=='X'|| check=='O'|| check=='C') gameOver=true; stroke(255); line(200,0,200,800); line(400,0,400,800); line(0,200,600,200); line(0,400,600,400); rectMode(CORNERS); fill(255); rect(0,600,600,800);

if(!gameOver)

{ if (xTurn) { fill(0); text("Player X's turn", 50, 675); } else { fill(0); text("Player O's turn", 50, 675); } } else { if(gameOver) { matrix[row][col]='X'; fill(0); text("Player qs turn", 50, 675); } }

for (int x=0; x<3; x++){ for(int y=0; y<3; y++){ if(matrix[x][y]=='X'){ stroke(255); line((y+1)200,x200,y200,(x+1)200); line(y200,x200,(y+1)200,(x+1)200); } } } for (int x=0; x<3; x++){ for(int y=0; y<3; y++){ if(matrix[x][y]=='O'){ ellipseMode(CORNER); stroke(255); noFill(); ellipse(y200,x200,200,200); } } } } void mousePressed() { int y=mouseX/200; int x=mouseY/200;

if(matrix[x][y]=='*')

{ if(xTurn==true) matrix[x][y]='X'; else matrix[x][y]='O'; xTurn=!xTurn; } println(x+","+y); }

char checkForGameOver() { if(matrix[0][0]=='X'&& matrix[0][1]=='X'&& matrix[0][2]=='X') return'X';

if(matrix[0][0]=='O'&& matrix[0][1]=='O'&& matrix[0][2]=='O') return'O';

else return'N'; }

Answers

Sign In or Register to comment.