1

I'm about finished with my tictactoe game, but I cant figure out how to make my reset button start the game over. I would also like the game to reset when someone wins or there's a tie. Any help would be greatly appreciated.

Here is my code(sorry that there's so much of it):

package tictactoe;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
 public class TicTacToe 
 {


    public static void main(String[] args) 
    {
       JFrame ticTacToe = new TicTacToeFrame();
        ticTacToe.setTitle("Phantom TicTacToe Game!");
        ticTacToe.setSize(600, 600);
        ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ticTacToe.setLocationRelativeTo(null);
        ticTacToe.setVisible(true);  
    }

}
 class TicTacToeFrame extends JFrame implements ActionListener 
{

   private char whoseTurn = 'X';
   private boolean gameOver = false;


   private Cell[][] cells = new Cell[3][3];


   JLabel jlblStatus = new JLabel("X's turn to play");


   public TicTacToeFrame()
   {
       JButton btn = new JButton("Reset");
       JPanel panel = new JPanel(new GridLayout(3, 3, 0, 0));
       for (int i = 0; i < 3; i++)
           for (int j = 0; j < 3; j++)
               panel.add(cells[i][j] = new Cell());


       add(btn, BorderLayout.SOUTH);
       add(panel, BorderLayout.CENTER);
       add(jlblStatus, BorderLayout.NORTH);
       jlblStatus.setOpaque(true);
       jlblStatus.setBackground(Color.YELLOW);
   }


    public boolean isFull()
    {
       for (int i = 0; i < 3; i++)
           for (int j = 0; j < 3; j++)
               if (cells[i][j].getToken() == ' ')
                   return false;
       return true;
    }


   public boolean isWon(char token)
   {

       for (int i = 0; i < 3; i++)
           if ((cells[i][0].getToken() == token)
                   && (cells[i][1].getToken() == token)
                   && (cells[i][2].getToken() == token)) 
           {
               return true;
           }


       for (int j = 0; j < 3; j++)
           if ((cells[0][j].getToken() == token)
               && (cells[1][j].getToken() == token)
               && (cells[2][j].getToken() == token))
           {
               return true;
           }

       if ((cells[0][0].getToken() == token)
               && (cells[1][1].getToken() == token)
               && (cells[2][2].getToken() == token))
           {
               return true;
           }

       if ((cells[0][2].getToken() == token)
               && (cells[1][1].getToken() == token)
               && (cells[2][0].getToken() == token))
           {
               return true;
           }

       return false;
   }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        JButton bttn = (JButton) e.getSource();
        if(bttn == btn)
        {

        }
    }


    public class Cell extends JPanel
    {

       private char token = ' ';


       public Cell()
       {
           setBorder(new LineBorder(Color.black, 1));
           addMouseListener(new MyMouseListener());
       }


       public char getToken()
       {
           return token;
       }


       public void setToken(char c)
       {
           token = c;
           repaint();
       }

       @Override
       protected void paintComponent(Graphics g)
       {
           super.paintComponent(g);

           if (token == 'X')
           {
               g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
               g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
           }

           else if (token == 'O')
           {
               g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
           }
       }

       private class MyMouseListener extends MouseAdapter
       {
           @Override
           public void mouseClicked(MouseEvent e)
           {
               if (gameOver)
                   return;


               if (token == ' ' && whoseTurn != ' ')
                   setToken(whoseTurn);


               if (isWon(whoseTurn))
               {
                   jlblStatus.setText(whoseTurn + " won! Game over!");
                   whoseTurn = ' ';
                   gameOver = true;
               }
               else if (isFull())
               {
                   jlblStatus.setText("Tie game! Game over!");
                   whoseTurn = ' ';
                   gameOver = true;
               }
               else
               {
                   whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
                   jlblStatus.setText(whoseTurn + "'s turn.");
               }
           }

       } 
    } 
} 
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Bob
  • 69
  • 10

2 Answers2

0

You need to reset the state of your program, including either a recreation or reset of some of your user interface (the label and the panel with the nine cells need to be changed).

Issues to take a look at:

  • the TicTacToeFrame has an actionPerformed method and implements the ActionListener interface, but the reset button does not have an action listener yet;
  • for the reset: you can for example set the whoseTurn and gameOver fields to their initial value, set the text in the jlblStatus label, and
    • option 1: remove all cells from the panel and add new ones (like you already do in the constructor - this could be an initializeCells method) or
    • option 2 (if you prefer recycling): add a reset method to the Cell class that clears the token field, call this reset method for all cells, and tell the panel to repaint).

Small things to keep in mind:

  • if you want to use the panel and button components outside of the constructor, change them into fields (instead of local variables);
  • the if statement in the actionPerformed method uses the == operator instead of the equals method - are you sure about that? (hint: see What is the difference between == vs equals() in Java?).
Community
  • 1
  • 1
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
0

You already have the code.

    JFrame ticTacToe = new TicTacToeFrame();
    ticTacToe.setTitle("Phantom TicTacToe Game!");
    ticTacToe.setSize(600, 600);
    ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ticTacToe.setLocationRelativeTo(null);
    ticTacToe.setVisible(true);  

Just put it inside a startNewGame()method and call it both at the start and when a game is won ;)

Arnab Datta
  • 5,356
  • 10
  • 41
  • 67