I'm currently working on a tic-tac-toe program and it is working to the point that it runs and X's and O's appear. However when the right combination is made or the board gets full the method "Resultat();" should recognize it and "Clear();" should clear out the board and a JFrame should open asking if i want to exit or restart. This doesn't happen. The board just gets filled without reacting:/
If any one has a clue what I'm doing wrong please help me out.
Thank you in advance!:)
(please dont mind that some of the variables have wierd names, im not english)
//Angelika
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Knapp
{
private JButton [][]Plan=new JButton[3][3]; //3*3 squares
private JButton exit, restart;
private JLabel winO, winX, tie;
private int loop=0;
JFrame frame=new JFrame("Tic-Tac-Toe"); //main JFrame
JFrame PopUp1=new JFrame("Grattis!");
JFrame PopUp2=new JFrame("Grattis!");
JFrame PopUp3=new JFrame("Oavgjort");
public static void main(String[] skit)
{
Knapp k=new Knapp();
}
public Knapp()
{
frame.setLayout(new GridLayout(3,3));
Plan[0][2]=new JButton("");
Plan[0][2].addActionListener(new MyAction());
frame.add(Plan[0][2]);
Plan[1][2]=new JButton("");
Plan[1][2].addActionListener(new MyAction());
frame.add(Plan[1][2]);
Plan[2][2]=new JButton("");
Plan[2][2].addActionListener(new MyAction());
frame.add(Plan[2][2]);
Plan[0][1]=new JButton("");
Plan[0][1].addActionListener(new MyAction());
frame.add(Plan[0][1]);
Plan[1][1]=new JButton("");
Plan[1][1].addActionListener(new MyAction());
frame.add(Plan[1][1]);
Plan[2][1]=new JButton("");
Plan[2][1].addActionListener(new MyAction());
frame.add(Plan[2][1]);
Plan[0][0]=new JButton("");
Plan[0][0].addActionListener(new MyAction());
frame.add(Plan[0][0]);
Plan[1][0]=new JButton("");
Plan[1][0].addActionListener(new MyAction());
frame.add(Plan[1][0]);
Plan[2][0]=new JButton("");
Plan[2][0].addActionListener(new MyAction());
frame.add(Plan[2][0]);
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//***************************************************
PopUp1.setSize(200,200);
PopUp1.setVisible(false);
PopUp1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PopUp1.setLayout(new GridLayout(2,1));
winO=new JLabel("Grattis! Spelare O vann!");
PopUp1.add(winO);
JPanel panel1=new JPanel();
panel1.setLayout(new GridLayout(1,2));
restart=new JButton("Ny omgång");
exit=new JButton("Avsluta");
restart.addActionListener(new AnotherAction());
exit.addActionListener(new AnotherAction());
panel1.add(restart);
panel1.add(exit);
PopUp2.setSize(200,200);
PopUp2.setVisible(false);
PopUp2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PopUp2.setLayout(new GridLayout(2,1));
winX=new JLabel("Grattis! Spelare X vann!");
PopUp2.add(winX);
JPanel panel2=new JPanel();
panel2.setLayout(new GridLayout(1,2));
panel2.add(restart);
panel2.add(exit);
PopUp3.setSize(200,200);
PopUp3.setVisible(false);
PopUp3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PopUp3.setLayout(new GridLayout(2,1));
tie=new JLabel("Oavgjort!");
PopUp3.add(tie);
JPanel panel3=new JPanel();
panel3.setLayout(new GridLayout(1,2));
panel3.add(restart);
panel3.add(exit);
}
public class AnotherAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==restart)
{
Clear();
PopUp1.setVisible(false);
PopUp2.setVisible(false);
PopUp3.setVisible(false);
}
else
{
PopUp1.setVisible(false);
PopUp2.setVisible(false);
PopUp3.setVisible(false);
frame.setVisible(false);
}
}
}
public class MyAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton temp=((JButton)e.getSource());
String text = (String)e.getActionCommand();
if((text.equals(""))&&(0==loop%2))
{
temp.setText("X");
loop++;
Resultat();
}
else if(text.equals(""))
{
temp.setText("O");
loop++;
Resultat();
}
}
}
private void Resultat()
{
if((((Plan[0][2]).equals("O"))&&((Plan[1][2]).equals("O"))&&((Plan[2][2]).equals("O")))||
(((Plan[0][1]).equals("O"))&&((Plan[1][1]).equals("O"))&&((Plan[2][1]).equals("O")))||
(((Plan[0][0]).equals("O"))&&((Plan[1][0]).equals("O"))&&((Plan[2][0]).equals("O")))||
(((Plan[0][2]).equals("O"))&&((Plan[0][1]).equals("O"))&&((Plan[0][0]).equals("O")))||
(((Plan[1][2]).equals("O"))&&((Plan[1][1]).equals("O"))&&((Plan[1][0]).equals("O")))||
(((Plan[2][2]).equals("O"))&&((Plan[2][1]).equals("O"))&&((Plan[2][0]).equals("O")))||
(((Plan[0][2]).equals("O"))&&((Plan[1][1]).equals("O"))&&((Plan[2][0]).equals("O")))||
(((Plan[2][2]).equals("O"))&&((Plan[1][1]).equals("O"))&&((Plan[0][0]).equals("O")))) //solution O
{
PopUp1.setVisible(true);
Clear();
}
else if((((Plan[0][2]).equals("X"))&&((Plan[1][2]).equals("X"))&&((Plan[2][2]).equals("X")))||
(((Plan[0][1]).equals("X"))&&((Plan[1][1]).equals("X"))&&((Plan[2][1]).equals("X")))||
(((Plan[0][0]).equals("X"))&&((Plan[1][0]).equals("X"))&&((Plan[2][0]).equals("X")))||
(((Plan[0][2]).equals("X"))&&((Plan[0][1]).equals("X"))&&((Plan[0][0]).equals("X")))||
(((Plan[1][2]).equals("X"))&&((Plan[1][1]).equals("X"))&&((Plan[1][0]).equals("X")))||
(((Plan[2][2]).equals("X"))&&((Plan[2][1]).equals("X"))&&((Plan[2][0]).equals("X")))||
(((Plan[0][2]).equals("X"))&&((Plan[1][1]).equals("X"))&&((Plan[2][0]).equals("X")))||
(((Plan[2][2]).equals("X"))&&((Plan[1][1]).equals("X"))&&((Plan[0][0]).equals("X")))) //solution X
{
PopUp2.setVisible(true);
Clear();
}
else if((((Plan[0][0]).equals("X"))&&((Plan[0][1]).equals("X"))&&((Plan[0][2]).equals("X"))&&
((Plan[1][0]).equals("X"))&&((Plan[1][1]).equals("X"))&&((Plan[1][2]).equals("X"))&&
((Plan[2][0]).equals("X"))&&((Plan[2][1]).equals("X"))&&((Plan[2][2]).equals("X")))||
(((Plan[0][0]).equals("O"))&&((Plan[0][1]).equals("O"))&&((Plan[0][2]).equals("O"))&&
((Plan[1][0]).equals("O"))&&((Plan[1][1]).equals("O"))&&((Plan[1][2]).equals("O"))&&
((Plan[2][0]).equals("O"))&&((Plan[2][1]).equals("O"))&&((Plan[2][2]).equals("O")))) //oavgjort
{
PopUp3.setVisible(true);
Clear();
}
}
private void Clear()
{
for(int x=0; x<3; x++)
for(int y=0; y<3; y++)
Plan[x][y].setText("");
}
}