I need to make a reset timer in my Tic Tac Toe game

homework policy * asking questions

int[] Felder = new int[9];
int spielerFlag = 0;
int x = 0;
int y = 0;
int win = 0;
int tp = 0;
boolean reset = false;


void setup() {
  size(displayWidth, displayHeight);
  strokeWeight(10);
  frameRate(60);
  smooth(5);
  for (int i=0; i<9; i++) {// das ist der reset man muss nur noch ein delay nutzen
    Felder[i]=0;
  }
  background(169, 169, 169);
  zeichneSpielfeld();
}


void draw() {

  int count =0;
  for (int i : Felder) {
    if (i==1) {
      zeichneEllipse((count%3)*200+200, int(count/3)*200+100+100, 100);
    }
    if (i==2) {
      zeichneKreuz((count%3)*200+200, int(count/3)*200+50+150, 150);
    }
    count++;
  }
  Text();
  gameOver();
  Reset();
}
void zeichneKreuz(int xM, int yM, int a) {
  stroke(0, 255, 0);
  line(xM-a/2, yM+a/2, xM+a/2, yM-a/2);
  line(xM+a/2, yM+a/2, xM-a/2, yM-a/2);
}
void zeichneEllipse(int xM, int yM, int a) {
  stroke(255, 0, 0);
  fill(255, 255, 255);
  ellipse(xM, yM, a, a);
}

void Reset() {
  if (reset == true) {
    fill(0, 0, 0);
    text("Reset in 5 Sekunden", 780, 300);
  }
  delay(5000);
  {
    
    
    
  }
}

void mousePressed() {
  if (mouseX>100 && mouseX<700 && mouseY>100 && mouseY<700) {

    y= int((mouseY-100)/200);
    x= int((mouseX-100)/200);
    int count = y*3+x;
    if (Felder[count]==0) {
      if (spielerFlag == 0) {
        Felder[y*3+x] = 1;
      }
      if (spielerFlag==1) {
        Felder[y*3+x] = 2;
      }
      tp++;
      spielerFlag = (spielerFlag+1)%2;
    }
  }
}
void zeichneSpielfeld() {
  stroke(0, 0, 0);
  rect(100, 100, 600, 600);
  line(100, 300, 700, 300);
  line(100, 500, 700, 500);
  line(300, 100, 300, 700);
  line(500, 100, 500, 700);
}

void gameOver() {
  if (Felder[0] == 1 && Felder[1] == 1 && Felder[2] == 1 ) {
    win = 1;
  }
  if (Felder[3] == 1 && Felder[4] == 1 && Felder[5] ==1 ) {
    win = 1;
  }
  if (Felder[6] == 1 && Felder[7] == 1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 1 && Felder[3] == 1 && Felder[6] ==1 ) {
    win = 1;
  }
  if (Felder[1] == 1 && Felder[4] == 1 && Felder[7] ==1 ) {
    win = 1;
  }
  if (Felder[2] == 1 && Felder[5] ==1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 1 && Felder[4] ==1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[2] == 1 && Felder[4] ==1 && Felder[6] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 2 && Felder[1] == 2 && Felder[2] == 2 ) {
    win = 2;
  }
  if (Felder[3] == 2 && Felder[4] == 2 && Felder[5] ==2 ) {
    win = 2;
  }
  if (Felder[6] == 2 && Felder[7] == 2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[0] == 2 && Felder[3] == 2 && Felder[6] ==2 ) {
    win = 2;
  }
  if (Felder[1] == 2 && Felder[4] == 2 && Felder[7] ==2 ) {
    win = 2;
  }
  if (Felder[2] == 2 && Felder[5] ==2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[0] == 2 && Felder[4] ==2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[2] == 2 && Felder[4] ==2 && Felder[6] ==2 ) {
    win = 2;
  }

  boolean remis = false;
  if (tp == 9) {
    remis = true;
  }
  if (win == 1) {
    print("Kreis hat gewonnen!");
    noLoop();
    reset  = true;
  }
  if (win == 2) {
    print("Kreuz hat gewonnen!");
    noLoop();
    reset = true;
  }
  if (win == 0 && remis == true) {
    print("Unentschieden!");
    reset = true;
  }
}
void Text() {
  textSize(60);
  fill(255, 0, 0);
  text("Tic", 265, 80);
  fill(0, 255, 0);
  text("Tac", 348, 80);
  fill(0, 0, 255);
  text("Toe", 445, 80);
}

So my code works in the fact that I can play Tic Tac Toe but I wanted to make like an automatic reset 5 sec after someone won/remis. I tried doing it with delay(naptime); but I think I did something wrong because after I wrote delay(5000); in my reset code evrything in the tic tac toe game had a delay. I want to make it like the board gets set to 0 and you can play another round.

And I dont know how to set the board to 0. My teacher told me I have to use this: for (int i=0; i<9; i++), for it but I dont know how to do that

In your program the delay command
should be inside the if clause.

It won’t work then anyway either.

Instead say

if ( millis() - timer > 5000) {
   reset=false;
     for (int i=0; i<9; i++) {// das ist der reset man muss nur noch ein delay nutzen
    Felder[i]=0;
  }
}//

First of all thank you for your answer. It still doesnt clear the board. How can I make it so it clears the board?

1 Like

You want to have this additionally at start of draw()

set int timer to millis() when you set reset to true please

you mean it like this?

int[] Felder = new int[9];
int spielerFlag = 0;
int x = 0;
int y = 0;
int win = 0;
int tp = 0;
boolean reset = false;
int timer = millis();


void setup() {
  size(displayWidth, displayHeight);
  strokeWeight(10);
  frameRate(60);
  smooth(5);
  for (int i=0; i<9; i++) {// das ist der reset man muss nur noch ein delay nutzen
    Felder[i]=0;
  }

  zeichneSpielfeld();
}


void draw() {
  background(169, 169, 169);
  int count =0;
  for (int i : Felder) {
    if (i==1) {
      zeichneEllipse((count%3)*200+200, int(count/3)*200+100+100, 100);
    }
    if (i==2) {
      zeichneKreuz((count%3)*200+200, int(count/3)*200+50+150, 150);
    }
    count++;
  }
  Text();
  gameOver();
  Reset();
}
void zeichneKreuz(int xM, int yM, int a) {
  stroke(0, 255, 0);
  line(xM-a/2, yM+a/2, xM+a/2, yM-a/2);
  line(xM+a/2, yM+a/2, xM-a/2, yM-a/2);
}
void zeichneEllipse(int xM, int yM, int a) {
  stroke(255, 0, 0);
  fill(255, 255, 255);
  ellipse(xM, yM, a, a);
}

void Reset(){
if ( millis() - timer > 5000) {
   reset=true;
     for (int i=0; i<9; i++) {// das ist der reset man muss nur noch ein delay nutzen
    Felder[i]=0;
  }
}//
}
void mousePressed() {
  if (mouseX>100 && mouseX<700 && mouseY>100 && mouseY<700) {

    y= int((mouseY-100)/200);
    x= int((mouseX-100)/200);
    int count = y*3+x;
    if (Felder[count]==0) {
      if (spielerFlag == 0) {
        Felder[y*3+x] = 1;
      }
      if (spielerFlag==1) {
        Felder[y*3+x] = 2;
      }
      tp++;
      spielerFlag = (spielerFlag+1)%2;
    }
  }
}
void zeichneSpielfeld() {
  stroke(0, 0, 0);
  rect(100, 100, 600, 600);
  line(100, 300, 700, 300);
  line(100, 500, 700, 500);
  line(300, 100, 300, 700);
  line(500, 100, 500, 700);
}

void gameOver() {
  if (Felder[0] == 1 && Felder[1] == 1 && Felder[2] == 1 ) {
    win = 1;
  }
  if (Felder[3] == 1 && Felder[4] == 1 && Felder[5] ==1 ) {
    win = 1;
  }
  if (Felder[6] == 1 && Felder[7] == 1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 1 && Felder[3] == 1 && Felder[6] ==1 ) {
    win = 1;
  }
  if (Felder[1] == 1 && Felder[4] == 1 && Felder[7] ==1 ) {
    win = 1;
  }
  if (Felder[2] == 1 && Felder[5] ==1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 1 && Felder[4] ==1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[2] == 1 && Felder[4] ==1 && Felder[6] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 2 && Felder[1] == 2 && Felder[2] == 2 ) {
    win = 2;
  }
  if (Felder[3] == 2 && Felder[4] == 2 && Felder[5] ==2 ) {
    win = 2;
  }
  if (Felder[6] == 2 && Felder[7] == 2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[0] == 2 && Felder[3] == 2 && Felder[6] ==2 ) {
    win = 2;
  }
  if (Felder[1] == 2 && Felder[4] == 2 && Felder[7] ==2 ) {
    win = 2;
  }
  if (Felder[2] == 2 && Felder[5] ==2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[0] == 2 && Felder[4] ==2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[2] == 2 && Felder[4] ==2 && Felder[6] ==2 ) {
    win = 2;
  }

  boolean remis = false;
  if (tp == 9) {
    remis = true;
  }
  if (win == 1) {
    print("Kreis hat gewonnen!");
    noLoop();
    reset  = true;
  }
  if (win == 2) {
    print("Kreuz hat gewonnen!");
    noLoop();
    reset = true;
  }
  if (win == 0 && remis == true) {
    print("Unentschieden!");
    reset = true;
  }
}
void Text() {
  textSize(60);
  fill(255, 0, 0);
  text("Tic", 265, 80);
  fill(0, 255, 0);
  text("Tac", 348, 80);
  fill(0, 0, 255);
  text("Toe", 445, 80);
}

Hi, I don’t know if it can help you, i wrote this little code :

int mill;
int second = 0;
int start;
void timer(int limit){
  mill = (millis());
  if((second + start) != mill/1000){
    second = second+1;
  }
  println (second);
  if(second > limit){
    start = start + second;
    second = 0;
    // do something...
  }
}
void draw(){
  timer(5);
}

Best regards.

Yes it helped a lot the delay is working now. Do you have any know how I can potentually do the board cleaner?

What do you mean by “cleaner”?

Also this at start of draw after background

Look what happens now:

int[] Felder = new int[9];
int spielerFlag = 0;
int x = 0;
int y = 0;
int win = 0;
int tp = 0;
boolean reset = false;
int timer = millis();


void setup() {
  size(displayWidth, displayHeight);
  strokeWeight(10);

  frameRate(60);
  smooth(5);
  for (int i=0; i<9; i++) {// das ist der reset man muss nur noch ein delay nutzen
    Felder[i]=0;
  }
}


void draw() {
  background(169, 169, 169);
  zeichneSpielfeld();
  int count =0;
  for (int i : Felder) {
    if (i==1) {
      zeichneEllipse((count%3)*200+200, int(count/3)*200+100+100, 100);
    }
    if (i==2) {
      zeichneKreuz((count%3)*200+200, int(count/3)*200+50+150, 150);
    }
    count++;
  }
  Text();
  gameOver();
  Reset();
  ResetText();
}
void zeichneKreuz(int xM, int yM, int a) {
  stroke(0, 255, 0);
  line(xM-a/2, yM+a/2, xM+a/2, yM-a/2);
  line(xM+a/2, yM+a/2, xM-a/2, yM-a/2);
}
void zeichneEllipse(int xM, int yM, int a) {
  stroke(255, 0, 0);
  fill(255, 255, 255);
  ellipse(xM, yM, a, a);
}
void ResetText() {
  if (reset == true) {
    fill(0, 0, 0);
    text("Reset in 5 Sekunden", 780, 300);
  }
}
void Reset() {
  if (reset == true) {
    if ( millis() - timer == 5000) {
      print("...");
    }
  }//
}

void mousePressed() {
  if (mouseX>100 && mouseX<700 && mouseY>100 && mouseY<700) {

    y= int((mouseY-100)/200);
    x= int((mouseX-100)/200);
    int count = y*3+x;
    if (Felder[count]==0) {
      if (spielerFlag == 0) {
        Felder[y*3+x] = 1;
      }
      if (spielerFlag==1) {
        Felder[y*3+x] = 2;
      }
      tp++;
      spielerFlag = (spielerFlag+1)%2;
    }
  }
}
void zeichneSpielfeld() {
  stroke(0, 0, 0);
  rect(100, 100, 600, 600);
  line(100, 300, 700, 300);
  line(100, 500, 700, 500);
  line(300, 100, 300, 700);
  line(500, 100, 500, 700);
}

void gameOver() {
  if (Felder[0] == 1 && Felder[1] == 1 && Felder[2] == 1 ) {
    win = 1;
  }
  if (Felder[3] == 1 && Felder[4] == 1 && Felder[5] ==1 ) {
    win = 1;
  }
  if (Felder[6] == 1 && Felder[7] == 1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 1 && Felder[3] == 1 && Felder[6] ==1 ) {
    win = 1;
  }
  if (Felder[1] == 1 && Felder[4] == 1 && Felder[7] ==1 ) {
    win = 1;
  }
  if (Felder[2] == 1 && Felder[5] ==1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 1 && Felder[4] ==1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[2] == 1 && Felder[4] ==1 && Felder[6] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 2 && Felder[1] == 2 && Felder[2] == 2 ) {
    win = 2;
  }
  if (Felder[3] == 2 && Felder[4] == 2 && Felder[5] ==2 ) {
    win = 2;
  }
  if (Felder[6] == 2 && Felder[7] == 2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[0] == 2 && Felder[3] == 2 && Felder[6] ==2 ) {
    win = 2;
  }
  if (Felder[1] == 2 && Felder[4] == 2 && Felder[7] ==2 ) {
    win = 2;
  }
  if (Felder[2] == 2 && Felder[5] ==2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[0] == 2 && Felder[4] ==2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[2] == 2 && Felder[4] ==2 && Felder[6] ==2 ) {
    win = 2;
  }

  boolean remis = false;
  if (tp == 9) {
    remis = true;
  }
  if (win == 1) {
    print("Kreis hat gewonnen!");
    noLoop();
    reset  = true;
  }
  if (win == 2) {
    print("Kreuz hat gewonnen!");
    noLoop();
    reset = true;
  }
  if (win == 0 && remis == true) {
    print("Unentschieden!");
    reset = true;
  }
}
void Text() {
  textSize(60);
  fill(255, 0, 0);
  text("Tic", 265, 80);
  fill(0, 255, 0);
  text("Tac", 348, 80);
  fill(0, 0, 255);
  text("Toe", 445, 80);
}

instead

void Reset(){
if (reset){  // I forgot this 
if ( millis() - timer > 5000) {  // use > here !!!!!
   reset=false;  // !!!!!!!!!!!!!!!!!!!!
     for (int i=0; i<9; i++) {// das ist der reset man muss nur noch ein delay nutzen
    Felder[i]=0;
  }
}//
}//

Ok I did it do you have any idea what my teacher meant when he said that I have to use this here to clear the board?

 for (int i=0; i<9; i++) {// das ist der reset man muss nur noch ein delay nutzen
    Felder[i]=0;

So, did it work?

Your teacher was right. Those are the lines I used.
For a delay we use the if clause with millis().

Remarks

You had noLoop() in a few places. No chance to reset anything then!
We need to say timer = millis(); when you detect a win or a remis.

Here is my version

I also added if (reset==false) { where you decide who has won, so it is avoided that timer = millis() is executed again and again. Then the timer wouldn’t work anymore.

We also need to reset spielerFlag = 0; and win = 0; for a new game.

Warm regards,

Chrisir


int[] Felder = new int[9];
int spielerFlag = 0;
int x = 0;
int y = 0;
int win = 0;
int tp = 0;
boolean reset = false;
int timer = millis();
String message; 

void setup() {
  size(displayWidth, displayHeight);
  strokeWeight(10);

  frameRate(60);
  smooth(5);
  for (int i=0; i<9; i++) {// das ist der reset 
    Felder[i]=0;
  }

  background(169, 169, 169);
  zeichneSpielfeld();
}

void draw() {
  background(169, 169, 169);
  zeichneSpielfeld();

  int count = 0;
  for (int i : Felder) {
    if (i==1) {
      zeichneEllipse((count%3)*200+200, int(count/3)*200+100+100, 100);
    }
    if (i==2) {
      zeichneKreuz((count%3)*200+200, int(count/3)*200+50+150, 150);
    }
    count++;
  }

  Text();
  gameOver();
  Reset();
  ResetText();
}

void zeichneKreuz(int xM, int yM, int a) {
  stroke(0, 255, 0);
  line(xM-a/2, yM+a/2, xM+a/2, yM-a/2);
  line(xM+a/2, yM+a/2, xM-a/2, yM-a/2);
}
void zeichneEllipse(int xM, int yM, int a) {
  stroke(255, 0, 0);
  // fill(255, 255, 255);
  noFill(); 
  ellipse(xM, yM, a, a);
}

void ResetText() {
  if (reset) {
    fill(0, 0, 0);
    text("Reset in 5 Sekunden", 780, 300);
    text(message, 780, 300+111);
  }
}

void Reset() {
  // Wenn das Spiel vorĂźber ist
  if (reset) {  
    // Und 5 Sekunden verstrichen sind 
    if ( millis() - timer > 5000) {  
      // reset 
      reset=false;
      message=""; 
      spielerFlag = 0;
      win = 0;
      tp = 0;
      for (int i=0; i<9; i++) {// das ist der reset 
        Felder[i]=0;
      }
    }//
  }
}

void mousePressed() {
  if (mouseX>100 && mouseX<700 && mouseY>100 && mouseY<700) {
    x= int((mouseX-100)/200);
    y= int((mouseY-100)/200);
    int count = y*3+x;
    if (Felder[count]==0) {
      if (spielerFlag == 0) {
        Felder[y*3+x] = 1;
      }
      if (spielerFlag==1) {
        Felder[y*3+x] = 2;
      }
      tp++;
      spielerFlag = (spielerFlag+1)%2;
    }
  }
}

void zeichneSpielfeld() {
  stroke(0, 0, 0);
  fill(0, 0, 255);
  rect(100, 100, 600, 600);
  line(100, 300, 700, 300);
  line(100, 500, 700, 500);
  line(300, 100, 300, 700);
  line(500, 100, 500, 700);
}

void gameOver() {
  if (Felder[0] == 1 && Felder[1] == 1 && Felder[2] == 1 ) {
    win = 1;
  }
  if (Felder[3] == 1 && Felder[4] == 1 && Felder[5] ==1 ) {
    win = 1;
  }
  if (Felder[6] == 1 && Felder[7] == 1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 1 && Felder[3] == 1 && Felder[6] ==1 ) {
    win = 1;
  }
  if (Felder[1] == 1 && Felder[4] == 1 && Felder[7] ==1 ) {
    win = 1;
  }
  if (Felder[2] == 1 && Felder[5] ==1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 1 && Felder[4] ==1 && Felder[8] ==1 ) {
    win = 1;
  }
  if (Felder[2] == 1 && Felder[4] ==1 && Felder[6] ==1 ) {
    win = 1;
  }
  if (Felder[0] == 2 && Felder[1] == 2 && Felder[2] == 2 ) {
    win = 2;
  }
  if (Felder[3] == 2 && Felder[4] == 2 && Felder[5] ==2 ) {
    win = 2;
  }
  if (Felder[6] == 2 && Felder[7] == 2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[0] == 2 && Felder[3] == 2 && Felder[6] ==2 ) {
    win = 2;
  }
  if (Felder[1] == 2 && Felder[4] == 2 && Felder[7] ==2 ) {
    win = 2;
  }
  if (Felder[2] == 2 && Felder[5] ==2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[0] == 2 && Felder[4] ==2 && Felder[8] ==2 ) {
    win = 2;
  }
  if (Felder[2] == 2 && Felder[4] ==2 && Felder[6] ==2 ) {
    win = 2;
  }

  if (reset==false) {
    boolean remis = false;
    if (tp == 9) {
      remis = true;
    }
    if (win == 1) {
      println("Kreis hat gewonnen!");
      message="Kreis hat gewonnen!";
      reset  = true;
      timer = millis();
    }
    if (win == 2) {
      println("Kreuz hat gewonnen!");
      message="Kreuz hat gewonnen!";
      reset = true;
      timer = millis();
    }
    if (win == 0 && remis == true) {
      print("Unentschieden!");
      message="Unentschieden"; 
      reset = true;
      timer = millis();
    }
  }
}
void Text() {
  textSize(60);
  fill(255, 0, 0);
  text("Tic", 265, 80);
  fill(0, 255, 0);
  text("Tac", 348, 80);
  fill(0, 0, 255);
  text("Toe", 445, 80);
}

1 Like

Thank you so much it works perfectly!

1 Like

If you use an if in every draw-call to test if the time is over, then you can alternatively just count back a number and reset when that number is 0. That‘s how I do it.

1 Like