Hello there,
here is my code for a tic tac toe game. How can I reset the game by pressing the reset button? I only want to reset draw(). Is there any way to do this?
int rows = 3;
int cols = 3;
int [] [] matrix = new int [rows] [cols];
boolean pauseDraw = false;
void setup() {
size(600, 600);
background(255, 0, 0);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
matrix[i][j] = 0;
}
}
strokeWeight(6);
line(0, 200, 600, 200);
line(0, 400, 600, 400);
line(200, 0, 200, 600);
line(400, 0, 400, 600);
}
int checkRow (int row) {
if (matrix [0] [row] == 1 && matrix [1] [row] == 1 && matrix [2] [row] == 1) {
return 1; }
else if (matrix [0] [row] == 2 && matrix [1] [row] == 2 && matrix [2] [row] == 2) {
return 2; }
return 0;
}
int checkCol (int col) {
if (matrix [col] [0] == 1 && matrix [col] [1] == 1 && matrix [col] [2] == 1) {
return 1; }
else if (matrix [col] [0] == 2 && matrix [col] [1] == 2 && matrix [col] [2] == 2) {
return 2; }
return 0;
}
void drawRes (int res) {
String out = "looser";
if (res == 1) {
out = "Kreuz gewinnt"; }
else if (res == 2) {
out = "Kreis gewinnt"; }
fill(0, 255, 0);
textSize(75);
text(out, width/14, height/2);
pauseDraw = true;
textSize(26);
rect(220, 380, 160, 40);
fill(0);
text("reset", 265, 408);
}
void cross(int cross1X,int cross1Y,int cross2X,int cross2Y,int cross3X,int cross3Y,int cross4X,int cross4y) {
line(cross1X, cross1Y, cross2X, cross2Y);
line(cross3X, cross3Y, cross4X, cross4y);
}
void circle(int middle,int radius) {
circle(middle, radius);
}
void draw() {
if (pauseDraw) {
return; }
//Feld
if (keyPressed) {
//Kreuze
if (mouseX <= 200 && mouseY <= 200) {
if (key == 'x') {
cross(20, 180, 180, 20, 20, 20, 180, 180);
matrix [0][0] = 1; }
else if (key == 'o') {
fill(255, 0, 0);
circle(100, 100, 180);
matrix [0][0] = 2; }
}
if (mouseX >= 200 && mouseY <= 200 && mouseX <= 400) {
if (key == 'x') {
cross(220, 180, 380, 20, 220, 20, 380, 180);
matrix [1][0] = 1; }
else if (key == 'o') {
fill(255, 0, 0);
circle(300, 100, 180);
matrix [1][0] = 2; }
}
if (mouseX >= 400 && mouseY <= 200) {
if (key == 'x') {
cross(420, 180, 580, 20, 420, 20, 580, 180);
matrix [2][0] = 1; }
else if (key == 'o') {
fill(255, 0, 0);
circle(500, 100, 180);
matrix [2][0] = 2; }
}
if (mouseX <= 200 && mouseY <= 400 && mouseY >= 200) {
if (key == 'x') {
cross(20, 380, 180, 220, 20, 220, 180, 380);
matrix [0][1] = 1; }
else if (key == 'o') {
fill(255, 0, 0);
circle(100, 300, 180);
matrix [0][1] = 2; }
}
if (mouseX >= 200 && mouseX <= 400 && mouseY >= 200 && mouseX <= 400 && mouseY <= 400) {
if (key == 'x') {
cross(220, 380, 380, 220, 220, 220, 380, 380);
matrix [1][1] = 1; }
if (key == 'o') {
fill(255, 0, 0);
circle(300, 300, 180);
matrix [1][1] = 2; }
}
if (mouseX >= 400 && mouseY >= 200 && mouseY <= 400) {
if (key == 'x') {
cross(420, 380, 580, 220, 420, 220, 580, 380);
matrix [2][1] = 1; }
else if (key == 'o') {
fill(255, 0, 0);
circle(500, 300, 180);
matrix [2][1] = 2; }
}
if (mouseY >= 400 && mouseX <= 200) {
if (key == 'x') {
cross(20, 580, 180, 420, 20, 420, 180, 580);
matrix [0][2] = 1; }
else if (key == 'o') {
fill(255, 0, 0);
circle(100, 500, 180);
matrix [0][2] = 2; }
}
if (mouseX >= 200 && mouseY >= 400 && mouseX <= 400) {
if (key == 'x') {
cross(220, 580, 380, 420, 220, 420, 380, 580);
matrix [1][2] = 1; }
else if (key == 'o') {
fill(255, 0, 0);
circle(300, 500, 180);
matrix [1][2] = 2; }
}
if (mouseX >= 400 && mouseY >= 400) {
if (key == 'x') {
cross(420, 580, 580, 420, 420, 420, 580, 580);
matrix [2][2] = 1; }
else if (key == 'o') {
fill(255, 0, 0);
circle(500, 500, 180);
matrix [2][2] = 2; }
}
}
for (int row = 0; row < rows; row++) {
int res = checkRow (row);
if (res == 1 || res == 2) {
drawRes(res); }
}
for (int col = 0; col < cols; col++) {
int res = checkCol (col);
if (res == 1 || res == 2) {
drawRes(res); }
}
}