To reset the game, just reset clicks
to 0 and the buttons to their original state just as they were created:
boxes[x][y] = new JButton(newname);
boxes[x][y].setIcon(back);
Apart from that, you have many things to fix in your code. Here are some:
- Your line
boxes[x][y].addActionListener(tictac);
is in a loop of its own where it should be in the initial loop. No reason to iterate twice over the same arrays. - Your layout decisions could be better (depending on what exactly you want to get).
- Start the GUI with
TicTac frame = new TicTac();
from inside aninvokeLater
method. - Create an
ActionListener
for each button with a unique functionality. In your case it's 1 for play, 1 for restart, and 1 for all the board buttons. - You're using
event.getActionCommand
but you did not callsetActionCommand
on any of the buttons. - Don't use multiple
if
when you check for the button pressed (action command name), useif else
, or better - use aswitch
statement. - Don't use a separate function for each button. Use 1 function which receives the button as its argument.
- Don't create fields when you can create local variables instead.
If you need clarifications leave a comment.
Edit: here is some modified code that resets the buttons (I couldn't stop myself from correcting other parts of the code, though there is still more to do).
public class TicTac extends JFrame {
static JButton[][] boxes = new JButton[4][4];
static int[][] check = new int[4][4];
static JTextField blank1 = new JTextField();
static JTextField blank2 = new JTextField();
static int turns = 1;
static ImageIcon back = new ImageIcon("cardback.jpg");
static ImageIcon x = new ImageIcon("x.jpg");
static ImageIcon o = new ImageIcon("o.jpg");
public TicTac() {
JPanel gamePanel = new JPanel(new GridLayout(5, 4, 10, 10));
for (int x = 0; x<=3; x++) {
for (int y = 0; y<=3; y++) {
boxes[x][y] = new JButton(""+x+y, back);
boxes[x][y].addActionListener(new XOActionListenr(x, y));
gamePanel.add(boxes[x][y]);
}
}
JButton play = new JButton("Play");
JButton restart = new JButton("Restart");
play.addActionListener(new PlayActionListenr());
restart.addActionListener(new RestartActionListenr());
setLayout(new FlowLayout());
gamePanel.add(blank1);
gamePanel.add(play);
gamePanel.add(blank2);
gamePanel.add(restart);
add(gamePanel);
setTitle("Tic Tac Toe");
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void winner() {
// Check for winner
}
private class XOActionListenr implements ActionListener {
private int row, col;
private XOActionListenr(int row, int col) {
this.row = row;
this.col = col;
}
public void actionPerformed(ActionEvent e) {
if (turns % 2 == 0) {
boxes[row][col].setText("O");
boxes[row][col].setIcon(o);
check[row][col] = 2;
}
else {
boxes[row][col].setText("X");
boxes[row][col].setIcon(x);
check[row][col] = 1;
}
winner();
turns++;
}
}
private class PlayActionListenr implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Not sure why you would need the "play" button at all,
// but insert here what it's supposed to do
}
}
private class RestartActionListenr implements ActionListener {
public void actionPerformed(ActionEvent e) {
for (int x = 0; x<=3; x++) {
for (int y = 0; y<=3; y++) {
boxes[x][y].setText(""+x+y);
boxes[x][y].setIcon(back);
}
}
}
}
public static void main(String[] arguments) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TicTac();
}
});
}
}