my teacher wants the tictactoe game to have a button called play that resets the tictactoe game my button doesn't work....what did i do wrong?
//TicTacToePanel.java Author Carien Anderson
//Represents tic tac toe game board and allows peer to peer competition
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.io.*;
public class TicTacToePanel extends JPanel
{
//instance data
private JButton tile[][] = new JButton[3][3], exit, play;
private String xoro = " ";
private boolean win = false;
private int count =0;
private JPanel panel;
private ButtonListener listener;
public TicTacToePanel()
{
//panel for gameboard
panel = new JPanel();
panel.setPreferredSize(new Dimension(280,275));
panel.setLayout(new GridLayout(3,3));
listener = new ButtonListener();
//create and add button to panel and add the button listener
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tile[i][j] = new JButton();
panel.add(tile[i][j]);
tile[i][j].addActionListener(listener);
}
}
exit = new JButton ("EXIT");
exit.addActionListener(listener);
play = new JButton("Play Again");
play.addActionListener(listener);
this.add(panel, BorderLayout.CENTER);
this.add(exit, BorderLayout.SOUTH);
this.add(play, BorderLayout.SOUTH);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent click)
{
JButton place = (JButton)click.getSource();
if(place == exit)
System.exit(0);
if(place == play)
pAgain(panel);
else
{
//determines whether player is x or o by using modulo division
count++;
if(count % 2 == 0)
{
xoro = "O";
}
else
{
xoro = "X";
}
//determines if place is filled and alerts player or sets x or o
if(place.getText() == " ")
{
count--;
JOptionPane.showMessageDialog(null, "Place is already taken");
}
else
{
place.setText(xoro);
place.setEnabled(false);
}
//determines winner
for(int i=0; i<3; i++)
{
if(tile[i][0].getText()=="X" && tile[i][1].getText()=="X" && tile[i][2].getText()=="X")
win = true;
else if(tile[i][0].getText()=="O" && tile[i][1].getText()=="O" && tile[i][2].getText()=="O")
win = true;
else if(tile[0][i].getText()=="X" && tile[1][i].getText()=="X" && tile[2][i].getText()=="X")
win = true;
else if(tile[0][i].getText()=="O" && tile[1][i].getText()=="O" && tile[2][i].getText()=="O")
win = true;
else if(tile[0][0].getText()=="X" && tile[1][1].getText()=="X" && tile[2][2].getText()=="X")
win = true;
else if(tile[0][2].getText()=="O" && tile[1][1].getText()=="O" && tile[2][0].getText()=="O")
win = true;
}
if(win == true)
{
JOptionPane.showMessageDialog(null, xoro + " wins the game!");
}
else if(count == 9 && win == false)
{
JOptionPane.showMessageDialog(null, "The game was tie!");
}
}
}
}
public void pAgain(JPanel panel)
{
panel= null;
panel.setPreferredSize(new Dimension(280,275));
panel.setLayout(new GridLayout(3,3));
listener = new ButtonListener();
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
tile[i][j] = new JButton();
tile[i][j].setText(" ");
panel.add(tile[i][j]);
tile[i][j].addActionListener(listener);
}
}
this.add(panel, BorderLayout.CENTER);
}
}