From the Menu to the Game (mousePressed, programming tic tac toe)

please format code with </> button * homework policy * asking questions

Hello, we need to write a programm in processing for the game tic tac toe
But our problem is, when we click on ‘play against human’ the field appears but if we click on one of the little fields to set the first ‘X’ nothing happens. At first we just had the game itself without the menu at the beginning and everything worked. Since we use two mouseClicked / mousePressed its not working anymore. We’ve tried a lot, pls somebody send help :((
Our idea is to make anotehr class for the menu. we tried different things but we are confused …
Maybe someone can help us.
Here is the whole code. I hope its clear enough.

// Tic tac Toe Spiel
// player 1 = X
// player 2 = O
static final int MENUSCREEN = 0;
static final int GameScreenHuman = 1;
static final int GameScreenKiEasy = 2;
static final int GameScreenKiHard = 3;


int screenState = MENUSCREEN;

Menu menu = new Menu();
Square[][] grid;                 // array of squares makes up a grid
int row, col, spaces;            // grid layout
float x, y, w, h;                // values for the grid to be passed to class
int player = 1;                  // start with player 1
boolean gameOver;                // check if the game is finished
int winner;                      // holds the winning player (1 or 2)
boolean play;                    // continue playing - set to false if game won before all squares filled
int value = 0;


float w_button = 150;            // play again button
float h_button = 40;
float x_button = (425/2) - (w_button/2);
float y_button = 475; 


  
void setup(){
  // Fenster wird erstellt
  size (425,600);
  // 3 Zeilen, 3 Spalten, 9 Felder
  
    
  //startGame();    // set up a game
  
}

void draw(){

   drawMenu();
   
  if (screenState == GameScreenHuman) {
    drawGameHuman();
  } else if (screenState == GameScreenKiEasy){
    //drawGameKIEasy(); 
  } else if (screenState == GameScreenKiHard){
    // drawGameKIHard();
  }
   
}

 void startGame(){
  
    
   player = 1;          // start game with player 1
   play = true;         // begin to play
   spaces = 9;          // 9 empty spaces on the grid
   gameOver = false;    // game will continue in draw function
   winner = 0;          // no current winner (must be player 1 or 2 to win)
   
   background(200);    
   // start positions and details of the square
   x = 25;          
   y = 125;
   w = 125; h = 125;
  
   // adjust x, y values when creating squares of the grid
   for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      grid[i][j] = new Square(x, y, w, h);
      x += w;  // adjust x for width
    }
    y += h;    // after each row, ajust for height
    x = 25;    // reset to same x as row above
  } 
 }
 

void drawGameHuman(){
  
  
  row = 3; col = 3; spaces = 9;  
  //Feld erstellt mit den reihen und spalten
  grid = new Square[row][col];
  
  startGame();  
  
  // nach jedem Durchlauf wird geprüft, ob Spiel vorbei ist
  checkGameOver();  // check if the game is over
  
  
  // game is not finished - continue playing
  
  if (gameOver == false){
    
    // draw the grid over the neutral background
    background(200);
    
    // läuft über das Spielfeld
    for(int i = 0; i < row; i++){
      for(int j = 0; j < col; j++){
        // Feld wird gezeigt
        grid[i][j].display();
      }
    }
  
  }
  
  // game is finished - game over (gameOver == true) -> in checkGameOver Methode
  else {
    play = false; // Spiel vorbei
    // set the background depending on the winner (if there is one)
    
    // Spieler 1 bekommt rot (Kreuze auch rot)
    if (winner == 1){background(255,0,0);}
    
    // Spieler 2 bekommt blau ( Kreise auch blau)
    else if (winner == 2){background(0,0,255);}
    else{background(0);}
        
    endOfGameText(winner);    // display the winner 

    // then redraw the grid on top
    for(int i = 0; i < row; i++){
      for(int j = 0; j < col; j++){
        grid[i][j].display();
      }
    }
    
    playAgainButton();      // display the button option to play again
    backtomenuButton();     // display the button to go back to the menu
    
  }
 
}

// if mouse press load the click function
void mouseReleased(){
  // going over the field
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
// click method is from the class Square
      grid[i][j].click(mouseX, mouseY);
    }
  }
  // play again button
  if (mouseX > x_button && mouseX < x_button + w_button &&
      mouseY > y_button && mouseY < y_button + h_button){
    startGame();
  }
 
}

// check if the game is over
// either all spaces are filled, or 3 in a row

void checkGameOver(){
  
 int r = row;
 int c = col;
 
 // three in a row
 // Läuft durch die Zeilen
 for (int i = 0; i < r; i++){
   
   // Spieler 1
   if(grid[i][0].getValue() == 1 && grid[i][1].getValue() ==  1 && grid[i][2].getValue() == 1){
     gameOver = true;
     winner = 1;
   }
   // Spieler 2
   if(grid[i][0].getValue() == 2 && grid[i][1].getValue() ==  2 && grid[i][2].getValue() == 2){
     gameOver = true;
     winner = 2;
   }
 }
 
 // three in a column
 // über Spalten laufen
 for (int i = 0; i < c; i++){
   //Spieler 1
   if(grid[0][i].getValue() == 1 && grid[1][i].getValue() ==  1 && grid[2][i].getValue() == 1){
     gameOver = true;
     winner = 1;
   }
    // Spieler 2
   if(grid[0][i].getValue() == 2 && grid[1][i].getValue() ==  2 && grid[2][i].getValue() == 2){
     gameOver = true;
     winner = 2;
   }
 }
 
 // top left to bottom right
 if(grid[0][0].getValue() == 1 && grid[1][1].getValue() ==  1 && grid[2][2].getValue() == 1){
     gameOver = true;
     winner = 1;
 }
 if(grid[0][0].getValue() == 2 && grid[1][1].getValue() ==  2 && grid[2][2].getValue() == 2){
     gameOver = true;
     winner = 2;
 }
 
 // bottom left to top right
 if(grid[2][0].getValue() == 1 && grid[1][1].getValue() ==  1 && grid[0][2].getValue() == 1){
     gameOver = true;
     winner = 1;
 }
 if(grid[2][0].getValue() == 2 && grid[1][1].getValue() ==  2 && grid[0][2].getValue() == 2){
     gameOver = true;
     winner = 2;
 }
 // wenn keine freien Felder mehr da sind
 if (spaces == 0){
   gameOver = true;
 }
 
}
  
 void endOfGameText(int win){
   float xPosition = 40, yPosition = 450;
   stroke(255);
   textSize(20);
                                        // Position des Textes
   if (win==1){text("Winner: Player 1", xPosition, yPosition);}
   if (win==2){text("Winner: Player 2", xPosition, yPosition);}
   if (win==0){text("Game Over. No Winner", xPosition, yPosition);}
    stroke(0);
 }

// Methode die den playagain Button erstellt 
 void playAgainButton(){
   fill(200);
   stroke(200);
   // Rechteck 
   rect(x_button, y_button, w_button, h_button); 
   stroke(0);
   fill(0);
   text("PLAY AGAIN", x_button+15, y_button+30);
   fill(255);
 }
 
 void backtomenuButton(){
   fill(200);
   stroke(200);
   // Rechteck 
   rect(x_button, 700, 150, 40); 
   stroke(0);
   fill(0);
   text("BACK TO MENU", x_button+30, y_button+60);
   fill(255);
 }

 

 
void drawMenu() {
  background(0);
  PFont f= createFont("Georgia",30);
  fill(255);
  String s=" TIC TAC TOE";
  textFont(f);
  textSize(40);
  textAlign( TOP);
  text(s,100,100);
  
 
  startGamewithhuman();
  // Button Mensch gegen KI
  startGamewithKIEasy();
  // Button Mensch gegen KI 
  startGamewithKIHard();
   
  
}
// Mouse Pressed for the menu
void mousePressed(){
  
// if the Button 'play against human' is pressed
 if (mouseX > 95 && mouseX < 95 + 250 
 && mouseY > 200 && mouseY < 200+ 80){
  // when screenState is = GameScreenHuman -> method drawGameHuman();
   screenState = GameScreenHuman;
   
 }
 
}
// Button
void startGamewithhuman(){

   fill(100);
   stroke(200);
   // rechteck button
   rect(95,200,250,80,7); 
   stroke(0);
   fill(0);
   //Text und ausrichtung
   textSize(20);
   fill(255);
   text("Play against human", 140,250);
   fill(100);

}
// Button
void startGamewithKIEasy(){

   fill(100);
   stroke(200);
   rect(95,300,250,80,7); 
   stroke(0);
   fill(0,255,0);
   //Text und ausrichtung
   textSize(23);

   text("EASY", 190,340);
   
   textSize(14);
   fill(255);

   text ("Play against computer",160,365);
  // fill(100);


}
// Button
void startGamewithKIHard(){

   fill(100);
   stroke(200);
   rect(95,400,250,80,7); 
   stroke(0);
   fill(255);
   //Text und ausrichtung
   textSize(23);
   fill(205,0,0);
   text("HARD", 190,440);
   
   fill(255);
   textSize(14);
   text ("Play against computer",160,465);
  // fill(100);


}
class Square{
  
 float xPos, yPos, s_width, s_height;    // used to draw the square
 int value;                              // 1 = cross, 2 = square

// Konstruktor
 Square(float x, float y, float w, float h){
   xPos = x;
   yPos = y;
   s_width = w;
   s_height = h;
   value = 0;       // default value, use later to check if square is empty
 }
 
 // display the square
 // then display a cross or square as appropriate
 // the values are set in the click function
 
 
 void display(){
   // Rechteck für das Feld
   rect(xPos, yPos, s_width, s_height);
    text(" Tic Tac Toe", 160,80);
   
   if(value == 1){
     drawCross();
   }
   else if
   (value == 2){
     drawCircle();
   }
 }

 // called on mouse click
 // if click is on a square check if it is empty, before drawing a cross or circle
 // only draw if game is not completed
 void click(float m_X, float m_Y){
   
   float mX = m_X;
   float mY = m_Y;
   
   // only so this is the mouse has clicked in a square
   if(mX > xPos && mX < xPos + s_width &&
      mY > yPos && mY < yPos + s_height){
      
      if(value == 0 && play == true){
          
        // player 1, set value for cross, then player 2 turn 
        if(player==1){
          // Spieler 1 hat gelegt dann ist Spieler 2 dran (player = 2)
          value = 1;
          player = 2;
         }
       
         // player 2, set value for circle, then player 1 turn
         else{
           value = 2;
           player = 1;
         }
       
         // a turn has been taken, and a space used on the board
         // ein freies Feld weniger nach jedem Zug
         spaces--;
      }
    }   
 }
 
 // DESIGN ÄNDERN 
 
 // draw a cross with a specific stroke colour and weight
 void drawCross(){
   stroke(255,0,0); strokeWeight(5);
   line(xPos+20, yPos+20, xPos+(s_width-20), yPos+(s_height-20));
   line(xPos+20, yPos+(s_height-20), xPos+(s_width-20), yPos+20);
   stroke(0); strokeWeight(1);
 }
 
 // draw a circle with a specific stroke colour and weight
 void drawCircle(){
   stroke(0,0,255); strokeWeight(5);
   ellipse(xPos+(s_width/2), yPos+(s_height/2), s_width-25, s_height-25);
   stroke(0); strokeWeight(1);
 }
 
 // accessor function to get the value of each square
 // 1 = cross, 2 = circle
 // use this to determine if there is a winer
 int getValue(){
   return value;
 }


}

(we can’t run your code, class Menu is missing. Don’t post in separate sections of code in the forum, please format as ONE code section to make copying for us easier)

You need to implement MENUSCREEN in draw() too:


void draw() {
  if (screenState == MENUSCREEN) {
    drawMenu();
  } else if (screenState == GameScreenHuman) {
    drawGameHuman();
  } else if (screenState == GameScreenKiEasy) {
    //drawGameKIEasy();
  } else if (screenState == GameScreenKiHard) {
    // drawGameKIHard();
  }
}

In mouseReleased() (or mousePressed() which I like more) you also have to use the same if..else if... clause as in draw() to be able to distinguish between the states.

  • You want to do different things when mouse is pressed in the different states

Just out of interest, which grade is this, 11th?

Hey Chrisir,
first of all, thanks for your answer!!
Here is the code for the game without the menu class . its working until i wanna start playing and set the first ‘X’
and its a project from university hahah. and our first time with processing, so we are not that good yet :smiley:
i hope you can run the code now, let me know xx

// Tic tac Toe Spiel
// player 1 = X
// player 2 = O
static final int MENUSCREEN = 0;
static final int GameScreenHuman = 1;
static final int GameScreenKiEasy = 2;
static final int GameScreenKiHard = 3;


int screenState = MENUSCREEN;


Square[][] grid;                 // array of squares makes up a grid
int row, col, spaces;            // grid layout
float x, y, w, h;                // values for the grid to be passed to class
int player = 1;                  // start with player 1
boolean gameOver;                // check if the game is finished
int winner;                      // holds the winning player (1 or 2)
boolean play;                    // continue playing - set to false if game won before all squares filled
int value = 0;


float w_button = 150;            // play again button
float h_button = 40;
float x_button = (425/2) - (w_button/2);
float y_button = 475; 


  
void setup(){
  // Fenster wird erstellt
  size (425,600);
  // 3 Zeilen, 3 Spalten, 9 Felder
  
    
  //startGame();    // set up a game
  
}

void draw(){

  if (screenState == MENUSCREEN){
     drawMenu();
  } else if (screenState == GameScreenHuman) {
    drawGameHuman();
  } else if (screenState == GameScreenKiEasy){
    //drawGameKIEasy(); 
  } else if (screenState == GameScreenKiHard){
    // drawGameKIHard();
  }
   
}

 void startGame(){
  
    
   player = 1;          // start game with player 1
   play = true;         // begin to play
   spaces = 9;          // 9 empty spaces on the grid
   gameOver = false;    // game will continue in draw function
   winner = 0;          // no current winner (must be player 1 or 2 to win)
   
   background(200);    
   // start positions and details of the square
   x = 25;          
   y = 125;
   w = 125; h = 125;
  
   // adjust x, y values when creating squares of the grid
   for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      grid[i][j] = new Square(x, y, w, h);
      x += w;  // adjust x for width
    }
    y += h;    // after each row, ajust for height
    x = 25;    // reset to same x as row above
  } 
 }
 

void drawGameHuman(){
  
  
  row = 3; col = 3; spaces = 9;  
  //Feld erstellt mit den reihen und spalten
  grid = new Square[row][col];
  
  startGame();  
  
  // nach jedem Durchlauf wird geprüft, ob Spiel vorbei ist
  checkGameOver();  // check if the game is over
  
  
  // game is not finished - continue playing
  
  if (gameOver == false){
    
    // draw the grid over the neutral background
    background(200);
    
    // läuft über das Spielfeld
    for(int i = 0; i < row; i++){
      for(int j = 0; j < col; j++){
        // Feld wird gezeigt
        grid[i][j].display();
      }
    }
  
  }
  
  // game is finished - game over (gameOver == true) -> in checkGameOver Methode
  else {
    play = false; // Spiel vorbei
    // set the background depending on the winner (if there is one)
    
    // Spieler 1 bekommt rot (Kreuze auch rot)
    if (winner == 1){background(255,0,0);}
    
    // Spieler 2 bekommt blau ( Kreise auch blau)
    else if (winner == 2){background(0,0,255);}
    else{background(0);}
        
    endOfGameText(winner);    // display the winner 

    // then redraw the grid on top
    for(int i = 0; i < row; i++){
      for(int j = 0; j < col; j++){
        grid[i][j].display();
      }
    }
    
    playAgainButton();      // display the button option to play again
    backtomenuButton();     // display the button to go back to the menu
    
  }
 
}

// if mouse press load the click function
void mousePressed(){
  // über das Feld laufen
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      grid[i][j].click(mouseX, mouseY);
    }
  }
  // play again
  if (mouseX > x_button && mouseX < x_button + w_button &&
      mouseY > y_button && mouseY < y_button + h_button){
    startGame();
  }
 
}

// check if the game is over
// either all spaces are filled, or 3 in a row

void checkGameOver(){
  
 int r = row;
 int c = col;
 
 // three in a row
 // Läuft durch die Zeilen
 for (int i = 0; i < r; i++){
   
   // Spieler 1
   if(grid[i][0].getValue() == 1 && grid[i][1].getValue() ==  1 && grid[i][2].getValue() == 1){
     gameOver = true;
     winner = 1;
   }
   // Spieler 2
   if(grid[i][0].getValue() == 2 && grid[i][1].getValue() ==  2 && grid[i][2].getValue() == 2){
     gameOver = true;
     winner = 2;
   }
 }
 
 // three in a column
 // über Spalten laufen
 for (int i = 0; i < c; i++){
   //Spieler 1
   if(grid[0][i].getValue() == 1 && grid[1][i].getValue() ==  1 && grid[2][i].getValue() == 1){
     gameOver = true;
     winner = 1;
   }
    // Spieler 2
   if(grid[0][i].getValue() == 2 && grid[1][i].getValue() ==  2 && grid[2][i].getValue() == 2){
     gameOver = true;
     winner = 2;
   }
 }
 
 // top left to bottom right
 if(grid[0][0].getValue() == 1 && grid[1][1].getValue() ==  1 && grid[2][2].getValue() == 1){
     gameOver = true;
     winner = 1;
 }
 if(grid[0][0].getValue() == 2 && grid[1][1].getValue() ==  2 && grid[2][2].getValue() == 2){
     gameOver = true;
     winner = 2;
 }
 
 // bottom left to top right
 if(grid[2][0].getValue() == 1 && grid[1][1].getValue() ==  1 && grid[0][2].getValue() == 1){
     gameOver = true;
     winner = 1;
 }
 if(grid[2][0].getValue() == 2 && grid[1][1].getValue() ==  2 && grid[0][2].getValue() == 2){
     gameOver = true;
     winner = 2;
 }
 // wenn keine freien Felder mehr da sind
 if (spaces == 0){
   gameOver = true;
 }
 
}
  
 void endOfGameText(int win){
   float xPosition = 40, yPosition = 450;
   stroke(255);
   textSize(20);
                                        // Position des Textes
   if (win==1){text("Winner: Player 1", xPosition, yPosition);}
   if (win==2){text("Winner: Player 2", xPosition, yPosition);}
   if (win==0){text("Game Over. No Winner", xPosition, yPosition);}
    stroke(0);
 }

// Methode die den playagain Button erstellt 
 void playAgainButton(){
   fill(200);
   stroke(200);
   // Rechteck 
   rect(x_button, y_button, w_button, h_button); 
   stroke(0);
   fill(0);
   text("PLAY AGAIN", x_button+15, y_button+30);
   fill(255);
 }
 
 void backtomenuButton(){
   fill(200);
   stroke(200);
   // Rechteck 
   rect(x_button, 700, 150, 40); 
   stroke(0);
   fill(0);
   text("BACK TO MENU", x_button+30, y_button+60);
   fill(255);
 }

 
void drawMenu() {
  background(0);
  PFont f= createFont("Georgia",30);
  fill(255);
  String s=" TIC TAC TOE";
  textFont(f);
  textSize(40);
  textAlign( TOP);
  text(s,100,100);
  
 
  startGamewithhuman();
  // Button Mensch gegen KI
  startGamewithKIEasy();
  // Button Mensch gegen KI 
  startGamewithKIHard();
  
  //buttonPressed();
  
  
}

void mousePressed(){
  
 if (mouseX > 95 && mouseX < 95 + 250 
 && mouseY > 200 && mouseY < 200+ 80){
  
   screenState = GameScreenHuman;
   
 }
 
}


void keyPressed(){
 
  if ( key == 'n'){
    
       screenState = GameScreenHuman;
  
}
}

void startGamewithhuman(){

   fill(100);
   stroke(200);
   // rechteck button
   rect(95,200,250,80,7); 
   stroke(0);
   fill(0);
   //Text und ausrichtung
   textSize(20);
   fill(255);
   text("Play against human", 140,250);
   fill(100);

}

void startGamewithKIEasy(){

   fill(100);
   stroke(200);
   rect(95,300,250,80,7); 
   stroke(0);
   fill(0,255,0);
   //Text und ausrichtung
   textSize(23);

   text("EASY", 190,340);
   
   textSize(14);
   fill(255);

   text ("Play against computer",160,365);
  // fill(100);


}

void startGamewithKIHard(){

   fill(100);
   stroke(200);
   rect(95,400,250,80,7); 
   stroke(0);
   fill(255);
   //Text und ausrichtung
   textSize(23);
   fill(205,0,0);
   text("HARD", 190,440);
   
   fill(255);
   textSize(14);
   text ("Play against computer",160,465);
  // fill(100);


}

still can’t run it

Square class is missing

I told you what to do in mousePressed. Did it work?

Can’t see it…

Oh im sorry. Here is it again haha. All in one:

// Tic tac Toe Spiel
// player 1 = X
// player 2 = O
static final int MENUSCREEN = 0;
static final int GameScreenHuman = 1;
static final int GameScreenKiEasy = 2;
static final int GameScreenKiHard = 3;


int screenState = MENUSCREEN;


Square[][] grid;                 // array of squares makes up a grid
int row, col, spaces;            // grid layout
float x, y, w, h;                // values for the grid to be passed to class
int player = 1;                  // start with player 1
boolean gameOver;                // check if the game is finished
int winner;                      // holds the winning player (1 or 2)
boolean play;                    // continue playing - set to false if game won before all squares filled
int value = 0;


float w_button = 150;            // play again button
float h_button = 40;
float x_button = (425/2) - (w_button/2);
float y_button = 475; 


  
void setup(){
  // Fenster wird erstellt
  size (425,600);
  // 3 Zeilen, 3 Spalten, 9 Felder
  
    
  //startGame();    // set up a game
  
}

void draw(){

  if (screenState == MENUSCREEN){
     drawMenu();
  } else if (screenState == GameScreenHuman) {
    drawGameHuman();
  } else if (screenState == GameScreenKiEasy){
    //drawGameKIEasy(); 
  } else if (screenState == GameScreenKiHard){
    // drawGameKIHard();
  }
   
}

 void startGame(){
  
    
   player = 1;          // start game with player 1
   play = true;         // begin to play
   spaces = 9;          // 9 empty spaces on the grid
   gameOver = false;    // game will continue in draw function
   winner = 0;          // no current winner (must be player 1 or 2 to win)
   
   background(200);    
   // start positions and details of the square
   x = 25;          
   y = 125;
   w = 125; h = 125;
  
   // adjust x, y values when creating squares of the grid
   for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      grid[i][j] = new Square(x, y, w, h);
      x += w;  // adjust x for width
    }
    y += h;    // after each row, ajust for height
    x = 25;    // reset to same x as row above
  } 
 }
 

void drawGameHuman(){
  
  
  row = 3; col = 3; spaces = 9;  
  //Feld erstellt mit den reihen und spalten
  grid = new Square[row][col];
  
  startGame();  
  
  // nach jedem Durchlauf wird geprüft, ob Spiel vorbei ist
  checkGameOver();  // check if the game is over
  
  
  // game is not finished - continue playing
  
  if (gameOver == false){
    
    // draw the grid over the neutral background
    background(200);
    
    // läuft über das Spielfeld
    for(int i = 0; i < row; i++){
      for(int j = 0; j < col; j++){
        // Feld wird gezeigt
        grid[i][j].display();
      }
    }
  
  }
  
  // game is finished - game over (gameOver == true) -> in checkGameOver Methode
  else {
    play = false; // Spiel vorbei
    // set the background depending on the winner (if there is one)
    
    // Spieler 1 bekommt rot (Kreuze auch rot)
    if (winner == 1){background(255,0,0);}
    
    // Spieler 2 bekommt blau ( Kreise auch blau)
    else if (winner == 2){background(0,0,255);}
    else{background(0);}
        
    endOfGameText(winner);    // display the winner 

    // then redraw the grid on top
    for(int i = 0; i < row; i++){
      for(int j = 0; j < col; j++){
        grid[i][j].display();
      }
    }
    
    playAgainButton();      // display the button option to play again
    backtomenuButton();     // display the button to go back to the menu
    
  }
 
}

// if mouse press load the click function
void mouseClicked(){
  // über das Feld laufen
  for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
      grid[i][j].click(mouseX, mouseY);
    }
  }
  // play again
  if (mouseX > x_button && mouseX < x_button + w_button &&
      mouseY > y_button && mouseY < y_button + h_button){
    startGame();
  }
 
}

// check if the game is over
// either all spaces are filled, or 3 in a row

void checkGameOver(){
  
 int r = row;
 int c = col;
 
 // three in a row
 // Läuft durch die Zeilen
 for (int i = 0; i < r; i++){
   
   // Spieler 1
   if(grid[i][0].getValue() == 1 && grid[i][1].getValue() ==  1 && grid[i][2].getValue() == 1){
     gameOver = true;
     winner = 1;
   }
   // Spieler 2
   if(grid[i][0].getValue() == 2 && grid[i][1].getValue() ==  2 && grid[i][2].getValue() == 2){
     gameOver = true;
     winner = 2;
   }
 }
 
 // three in a column
 // über Spalten laufen
 for (int i = 0; i < c; i++){
   //Spieler 1
   if(grid[0][i].getValue() == 1 && grid[1][i].getValue() ==  1 && grid[2][i].getValue() == 1){
     gameOver = true;
     winner = 1;
   }
    // Spieler 2
   if(grid[0][i].getValue() == 2 && grid[1][i].getValue() ==  2 && grid[2][i].getValue() == 2){
     gameOver = true;
     winner = 2;
   }
 }
 
 // top left to bottom right
 if(grid[0][0].getValue() == 1 && grid[1][1].getValue() ==  1 && grid[2][2].getValue() == 1){
     gameOver = true;
     winner = 1;
 }
 if(grid[0][0].getValue() == 2 && grid[1][1].getValue() ==  2 && grid[2][2].getValue() == 2){
     gameOver = true;
     winner = 2;
 }
 
 // bottom left to top right
 if(grid[2][0].getValue() == 1 && grid[1][1].getValue() ==  1 && grid[0][2].getValue() == 1){
     gameOver = true;
     winner = 1;
 }
 if(grid[2][0].getValue() == 2 && grid[1][1].getValue() ==  2 && grid[0][2].getValue() == 2){
     gameOver = true;
     winner = 2;
 }
 // wenn keine freien Felder mehr da sind
 if (spaces == 0){
   gameOver = true;
 }
 
}
  
 void endOfGameText(int win){
   float xPosition = 40, yPosition = 450;
   stroke(255);
   textSize(20);
                                        // Position des Textes
   if (win==1){text("Winner: Player 1", xPosition, yPosition);}
   if (win==2){text("Winner: Player 2", xPosition, yPosition);}
   if (win==0){text("Game Over. No Winner", xPosition, yPosition);}
    stroke(0);
 }

// Methode die den playagain Button erstellt 
 void playAgainButton(){
   fill(200);
   stroke(200);
   // Rechteck 
   rect(x_button, y_button, w_button, h_button); 
   stroke(0);
   fill(0);
   text("PLAY AGAIN", x_button+15, y_button+30);
   fill(255);
 }
 
 void backtomenuButton(){
   fill(200);
   stroke(200);
   // Rechteck 
   rect(x_button, 700, 150, 40); 
   stroke(0);
   fill(0);
   text("BACK TO MENU", x_button+30, y_button+60);
   fill(255);
 }

 
void drawMenu() {
  background(0);
  PFont f= createFont("Georgia",30);
  fill(255);
  String s=" TIC TAC TOE";
  textFont(f);
  textSize(40);
  textAlign( TOP);
  text(s,100,100);
  
 
  startGamewithhuman();
  // Button Mensch gegen KI
  startGamewithKIEasy();
  // Button Mensch gegen KI 
  startGamewithKIHard();
  
  //buttonPressed();
  
  
}

void mousePressed(){
  
 if (mouseX > 95 && mouseX < 95 + 250 
 && mouseY > 200 && mouseY < 200+ 80){
  
   screenState = GameScreenHuman;
   
 }
 
}

/** tried it with different opportunities
void keyPressed(){
 
  if ( key == 'n'){
    
       screenState = GameScreenHuman;
  
}
}
*/

void startGamewithhuman(){

   fill(100);
   stroke(200);
   // rechteck button
   rect(95,200,250,80,7); 
   stroke(0);
   fill(0);
   //Text und ausrichtung
   textSize(20);
   fill(255);
   text("Play against human", 140,250);
   fill(100);

}

void startGamewithKIEasy(){

   fill(100);
   stroke(200);
   rect(95,300,250,80,7); 
   stroke(0);
   fill(0,255,0);
   //Text und ausrichtung
   textSize(23);

   text("EASY", 190,340);
   
   textSize(14);
   fill(255);

   text ("Play against computer",160,365);
  // fill(100);


}

void startGamewithKIHard(){

   fill(100);
   stroke(200);
   rect(95,400,250,80,7); 
   stroke(0);
   fill(255);
   //Text und ausrichtung
   textSize(23);
   fill(205,0,0);
   text("HARD", 190,440);
   
   fill(255);
   textSize(14);
   text ("Play against computer",160,465);
  // fill(100);


}

class Square{
  
 float xPos, yPos, s_width, s_height;    // used to draw the square
 int value;                              // 1 = cross, 2 = square

// Konstruktor
 Square(float x, float y, float w, float h){
   xPos = x;
   yPos = y;
   s_width = w;
   s_height = h;
   value = 0;       // default value, use later to check if square is empty
 }
 
 // display the square
 // then display a cross or square as appropriate
 // the values are set in the click function
 
 
 void display(){
   // Rechteck für das Feld
   rect(xPos, yPos, s_width, s_height);
    text(" Tic Tac Toe", 160,80);
   
   if(value == 1){
     drawCross();
   }
   else if
   (value == 2){
     drawCircle();
   }
 }

 // called on mouse click
 // if click is on a square check if it is empty, before drawing a cross or circle
 // only draw if game is not completed
 void click(float m_X, float m_Y){
   
   float mX = m_X;
   float mY = m_Y;
   
   // only so this is the mouse has clicked in a square
   if(mX > xPos && mX < xPos + s_width &&
      mY > yPos && mY < yPos + s_height){
      
      if(value == 0 && play == true){
          
        // player 1, set value for cross, then player 2 turn 
        if(player==1){
          // Spieler 1 hat gelegt dann ist Spieler 2 dran (player = 2)
          value = 1;
          player = 2;
         }
       
         // player 2, set value for circle, then player 1 turn
         else{
           value = 2;
           player = 1;
         }
       
         // a turn has been taken, and a space used on the board
         // ein freies Feld weniger nach jedem Zug
         spaces--;
      }
    }   
 }
 
 // DESIGN ÄNDERN 
 
 // draw a cross with a specific stroke colour and weight
 void drawCross(){
   stroke(255,0,0); strokeWeight(5);
   line(xPos+20, yPos+20, xPos+(s_width-20), yPos+(s_height-20));
   line(xPos+20, yPos+(s_height-20), xPos+(s_width-20), yPos+20);
   stroke(0); strokeWeight(1);
 }
 
 // draw a circle with a specific stroke colour and weight
 void drawCircle(){
   stroke(0,0,255); strokeWeight(5);
   ellipse(xPos+(s_width/2), yPos+(s_height/2), s_width-25, s_height-25);
   stroke(0); strokeWeight(1);
 }
 
 // accessor function to get the value of each square
 // 1 = cross, 2 = circle
 // use this to determine if there is a winer
 int getValue(){
   return value;
 }


}

i honestly dont really know how to change the mousepressed… you mean i should copy the if else clause from the draw inside mouseclicked? there we are going above the field with the for-loop and calling a method from the square class. im not sure what you mean

Yes!

and then in the section for the game play, move the code section you have in mouseclicked now

This code section doesn’t apply in the menu, right?

In the section for the menu

if (screenState == MENUSCREEN){
     // check the menu buttons HERE 
  } 

I think you want to merge the functions mousePressed and mouseClicked

I gotta go now…

Good luck.

void mousePressed() {


  if (screenState == MENUSCREEN) {
    // MENU 
    if (mouseX > 95 && mouseX < 95 + 250 
      && mouseY > 200 && mouseY < 200+ 80) {

      screenState = GameScreenHuman;
    }

    // HIER FEHLEN DIE ANDEREN BEIDEN BUTTONS IM MENU 

    if (mouseX > 95 && mouseX < 95 + 250 // ???????????????????
      && mouseY > 200 && mouseY < 200+ 80) {

      screenState = GameScreenKiEasy;
    }

    if (mouseX > 95 && mouseX < 95 + 250 // ???????????????????
      && mouseY > 200 && mouseY < 200+ 80) {

      screenState = GameScreenKiHard;
    }
  } 

  // ---------------------------------------------------------------------

  else if (screenState == GameScreenHuman) {
    // über das Feld laufen
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        grid[i][j].click(mouseX, mouseY);
      }
    }
    // play again
    if (mouseX > x_button && mouseX < x_button + w_button &&
      mouseY > y_button && mouseY < y_button + h_button) {
      startGame();
    }
    //
  }

  //------------------------------

  else if (screenState == GameScreenKiEasy) {
    // ..................
  }

  //------------------------------

  else if (screenState == GameScreenKiHard) {
       // ..................
  }

  //------------------------------
}

/** tried it with different opportunities
 void keyPressed(){
 
 if ( key == 'n'){
 
 screenState = GameScreenHuman;
 
 }
 }
 */

by the way: boolean gameOver; could be a further state I think (and maybe variable play too)

When i use the code you’ve sent me i cant press the first button in the menu anymore :confused: Or rather nothing happens when i press it.
But thanks! I will try my best.

1 Like

My bad.

You have to change the if-clause mouse data where I wrote it (see ???) and use else if…

void mousePressed() {


  if (screenState == MENUSCREEN) {
    // MENU 
    if (mouseX > 95 && mouseX < 95 + 250 
      && mouseY > 200 && mouseY < 200+ 80) {
      println("here");
      screenState = GameScreenHuman;
    }

    // HIER FEHLEN DIE ANDEREN BEIDEN BUTTONS IM MENU 

    else if (mouseX > 95 && mouseX < 95 + 250 // ???????????????????
      && mouseY > 200 && mouseY < 200+ 80) {

      screenState = GameScreenKiEasy;
    } else if (mouseX > 95 && mouseX < 95 + 250 // ???????????????????
      && mouseY > 200 && mouseY < 200+ 80) {

      screenState = GameScreenKiHard;
    }
  } 

  // ---------------------------------------------------------------------

  else if (screenState == GameScreenHuman) {
    // über das Feld laufen
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        grid[i][j].click(mouseX, mouseY);
      }
    }
    // play again
    if (mouseX > x_button && mouseX < x_button + w_button &&
      mouseY > y_button && mouseY < y_button + h_button) {
      startGame();
    }
    //
  }

  //------------------------------

  else if (screenState == GameScreenKiEasy) {
    // ..................
  }

  //------------------------------

  else if (screenState == GameScreenKiHard) {
    // ..................
  }

  //------------------------------
}



/** tried it with different opportunities
 void keyPressed(){
 
 if ( key == 'n'){
 
 screenState = GameScreenHuman;
 
 }
 }
 */
1 Like

several errors

you didn’t have values for row and col

use this: int row=3, col=3, spaces; // grid layout

Then you had these lines

  //  grid = new Square[row][col];
  //startGame();

in drawGameHuman() resetting the board every time!!!

So use

  • startGame(); in mousePressed() and
  • grid = new Square[row][col]; at start of startGame()

Chrisir

1 Like

Thanks for your help :slight_smile:

1 Like