Search code examples
javatic-tac-toegame-loop

Restarting a game


I am creating a simple game of TicTacToe, but I got stuck in restarting the game whenever the game ends and when I click the restart button. I just did the first horizontal buttons. When you press them a window will pop-up and tell you that there is a winner. I just did it for testing. But how do I restart the game when I press restart?I'm just new to java, and I'm trying to make games on my own to improve. Here are some parts of my code:

public class TicTacToe extends javax.swing.JFrame implements ActionListener{
int i=0;
boolean win = false;
String player[]={"You","Comp"};
/**
 * Creates new form TicTacToe
 */
public TicTacToe() {
    super("TicTacToe (LeeMinHu-bag)");
    initComponents();
    setResizable(false);
    setLocationRelativeTo(null);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b7.addActionListener(this);
    b8.addActionListener(this);
    b9.addActionListener(this);

}
//If a button is pressed, its text will become "X"
public void actionPerformed(ActionEvent e){
   if(e.getSource() == b1){
       b1.setText("X");
       b1.setEnabled(false);
   }else if(e.getSource() == b2){
       b2.setText("X");
       b2.setEnabled(false);
   }else if(e.getSource() == b3){
       b3.setText("X");
       b3.setEnabled(false);
   }else if(e.getSource() == b4){
       b4.setText("X");
       b4.setEnabled(false);
   }else if(e.getSource() == b5){
       b5.setText("X");
       b5.setEnabled(false);
   }else if(e.getSource() == b6){
       b6.setText("X");
       b6.setEnabled(false);
   }else if(e.getSource() == b7){
       b7.setText("X");
       b7.setEnabled(false);
   }else if(e.getSource() == b8){
       b8.setText("X");
       b8.setEnabled(false);
   }else if(e.getSource() == b9){
       b9.setText("X");
       b9.setEnabled(false);
   }     
   i++;
   System.out.println(i);
    if(i%2==0){
       turn.setText(player[0]);
   }else{
       turn.setText(player[1]);
   }

   checkIfWin();

}
//check to see if there is a winner
 public void checkIfWin(){
     if(b1.getText()==b2.getText() && b2.getText()==b3.getText() && b1.getText()!=""){
         win = true;
     }else if(i==9 && win==false){
         JOptionPane.showMessageDialog(null,"Tie!","Game Over",JOptionPane.INFORMATION_MESSAGE);
     }else{
         win=false;
     }      
     ifWin(win); 
 }
 //if there is a winner
 public void ifWin(boolean w){
     if(w==true){
        JOptionPane.showMessageDialog(null,"WIN!","Game Over",JOptionPane.INFORMATION_MESSAGE);
        TicTacToe restart = new TicTacToe();
        restart.validate();
     }else{
         System.out.println("The game is still on!");
     }
 }

Solution

  • It seems that the only state you have is text on buttons. So to restart game you only need to set empty text (what was the initial text?) and enable buttons. Something like this:

    JButton buttons[] = {b1, b2, b3, b4, b5, b6, b7, b8, b9};
    for (JButton button : buttons) {
        button.setText("");
        button.setEnabled(true);
    }
    

    Some additional notes:

    All if in your actionPerformed method can be replaced with this:

    JButton button = (JButtton) e.getSource();
    button.setText("x");
    button.setEnabled(false);
    

    Also it's REALLY bad idea to check string equality using ==. So I advice you to replace all b1.getText() == b2.getText() with b1.getText().equals(b2.getText()). See this link.