import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToe implements ActionListener {
//Create a gridlayout (3x3)
//Determines if either the x or the o goes first
//Determines if either the x or the o wins
//If the X win, create a message say either the x or o win/
//Else is a draw game
public TicTacToe(){
JPanel frame=new JPanel();
LayoutManager grid = new GridLayout(3, 3);
frame.setLayout(grid);
setSize(500, 500); //Size of the Frame
setLocationRelativeTo(null); // Center the Frame
setDefaultCloseOperation(EXIT_ON_CLOSE); //Close
frame.setLayout(new GridLayout(3, 3)); // Create Grid(row,column)_
ImageIcon o= new ImageIcon("cross.gif");
ImageIcon x= new ImageIcon("not.gif");
JFrame window = new JFrame();
JButton button1 = new JButton("");
JButton button2 = new JButton("");
JButton button3 = new JButton("");
JButton button4 = new JButton("");
JButton button5 = new JButton("");
JButton button6 = new JButton("");
JButton button7 = new JButton("");
JButton button8 = new JButton("");
JButton button9 = new JButton("");
String letter = "";
private int count = 0;
private boolean win = true;
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
window.setVisible(true);
}
public void actionPerformed(ActionEvent a) {
count++;
ImageIcon o= new ImageIcon("cross.gif");
ImageIcon x=new ImageIcon("not.gif");
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
letter="not.gif";
} else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
letter = "cross.gif";
}
/*Display X's or O's on the buttons*/
if(a.getSource() == button1){
button1.setText(letter);
button1.setEnabled(false);
} else if(a.getSource() == button2){
button2.setText(letter);
button2.setEnabled(false);
} else if(a.getSource() == button3){
button3.setText(letter);
button3.setEnabled(false);
} else if(a.getSource() == button4){
button4.setText(letter);
button4.setEnabled(false);
} else if(a.getSource() == button5){
button5.setText(letter);
button5.setEnabled(false);
} else if(a.getSource() == button6){
button6.setText(letter);
button6.setEnabled(false);
} else if(a.getSource() == button7){
button7.setText(letter);
button7.setEnabled(false);
} else if(a.getSource() == button8){
button8.setText(letter);
button8.setEnabled(false);
} else if(a.getSource() == button9){
button9.setText(letter);
button9.setEnabled(false);
}
if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != ""){
win = true;
}
else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != ""){
win = true;
}
else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != ""){
win = true;
}
else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != ""){
win = true;
}
else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != ""){
win = true;
}
else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != ""){
win = true;
}
else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != ""){
win = true;
}
else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != ""){
win = true;
}
else {
win = false;
}
/*Show a dialog if someone wins or the game is tie*/
if(win == true){
JOptionPane.showMessageDialog(null, letter + " WINS!");
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null,"DRAW GAME!");
}
}
public static void main(String[] args){
new TicTacToe();
}
}
I have the following error:
TicTacToe.java:42: illegal start of expression
private int count = 0;
^
TicTacToe.java:43: illegal start of expression
private boolean win = true;
^
And the other issue I am trying to figure out is instead of using the letter o and x, how will i be able to use images instead?

