This is just a simple PvP tic tac toe program, I'm learning and just looking for maybe some feedback on how to improve it?
There are mainly two things I can see improvement for, the action listener where I could do something like; "a button has been pressed, get the name of the button, pass the name of the button, and change button to X or O based on that". It would shorten the lines from needing one for each button, to just one for the whole button array.
And also the gameWin
check might be able to be improved? I don't know, maybe like the tie or using another way to compare ordered numbers in an array?
I'm not experienced and simply doing this for fun, any feedback would be awesome XD
package ttt;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ttt {
static JFrame mainFrame = new JFrame("Tic Tac Toe");
static JButton[] gameButtons = new JButton[9];
public static void main (String args[]) {
getGui();
}
public static void getGui() {
mainFrame.setVisible(true);
mainFrame.setResizable(false);
mainFrame.setSize(600, 620);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new GridLayout(3, 3));
mainFrame.add(mainPanel);
for (int i = 0; i < gameButtons.length; i++) {
gameButtons[i] = new JButton(String.valueOf(i));
gameButtons[i].setForeground(Color.WHITE);
gameButtons[i].setBackground(Color.WHITE);
gameButtons[i].setFont(new Font("Times New Roman", Font.BOLD, 60));
gameButtons[i].addActionListener(new Response());
mainPanel.add(gameButtons[i]);
}
}
private static class Response implements ActionListener {
static int rounds = 0;
static JButton winMessage = new JButton(), tieMessage = new JButton();
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == gameButtons[0]) {
roundJudge(0);
} else if (e.getSource() == gameButtons[1]) {
roundJudge(1);
} else if (e.getSource() == gameButtons[2]) {
roundJudge(2);
} else if (e.getSource() == gameButtons[3]) {
roundJudge(3);
} else if (e.getSource() == gameButtons[4]) {
roundJudge(4);
} else if (e.getSource() == gameButtons[5]) {
roundJudge(5);
} else if (e.getSource() == gameButtons[6]) {
roundJudge(6);
} else if (e.getSource() == gameButtons[7]) {
roundJudge(7);
} else if (e.getSource() == gameButtons[8]) {
roundJudge(8);
} else if (e.getSource() == winMessage) {
System.exit(0);
} else if (e.getSource() == tieMessage) {
System.exit(0);
}
gameJudge();
}
public void roundJudge(int buttonNumber) {
if (rounds %2 == 0) {
gameButtons[buttonNumber].setText("X");
} else if (rounds %2 == 1) {
gameButtons[buttonNumber].setText("O");
}
gameButtons[buttonNumber].setEnabled(false);
rounds += 1;
}
public void gameJudge() {
if (gameButtons[0].getText().equals(gameButtons[1].getText()) && gameButtons[1].getText().equals(gameButtons[2].getText())) {
gameWin(gameButtons[0].getText());
} else if (gameButtons[3].getText().equals(gameButtons[4].getText()) && gameButtons[4].getText().equals(gameButtons[5].getText())) {
gameWin(gameButtons[3].getText());
} else if (gameButtons[6].getText().equals(gameButtons[7].getText()) && gameButtons[7].getText().equals(gameButtons[8].getText())) {
gameWin(gameButtons[6].getText());
} else if (gameButtons[0].getText().equals(gameButtons[3].getText()) && gameButtons[3].getText().equals(gameButtons[6].getText())) {
gameWin(gameButtons[0].getText());
} else if (gameButtons[1].getText().equals(gameButtons[4].getText()) && gameButtons[4].getText().equals(gameButtons[7].getText())) {
gameWin(gameButtons[1].getText());
} else if (gameButtons[2].getText().equals(gameButtons[5].getText()) && gameButtons[5].getText().equals(gameButtons[8].getText())) {
gameWin(gameButtons[2].getText());
} else if (gameButtons[0].getText().equals(gameButtons[4].getText()) && gameButtons[4].getText().equals(gameButtons[8].getText())) {
gameWin(gameButtons[0].getText());
} else if (gameButtons[2].getText().equals(gameButtons[4].getText()) && gameButtons[4].getText().equals(gameButtons[6].getText())) {
gameWin(gameButtons[2].getText());
} else if (gameButtons[0].isEnabled() == false && gameButtons[1].isEnabled() == false && gameButtons[2].isEnabled() == false && gameButtons[3].isEnabled() == false && gameButtons[4].isEnabled() == false && gameButtons[5].isEnabled() == false && gameButtons[6].isEnabled() == false && gameButtons[7].isEnabled() == false && gameButtons[8].isEnabled() == false) {
gameTie();
}
}
public static void endFormat(JFrame name1, JButton name2) {
for(JButton gameButton : gameButtons) {
gameButton.setVisible(false);
gameButton.setEnabled(false);
}
mainFrame.setVisible(false);
name1.setLayout(new GridLayout(1, 1));
name1.setVisible(true);
name1.setResizable(false);
name1.setSize(600, 620);
name1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
name2.setToolTipText("Click anywhere to exit the program.");
name2.setFont(new Font("Times New Roman", Font.BOLD, 60));
name2.addActionListener(new Response());
name2.setOpaque(false);
name2.setContentAreaFilled(false);
name2.setBorderPainted(false);
}
public static void gameWin(String team) {
JFrame winFrame = new JFrame("Tic Tac Toe");
endFormat(winFrame, winMessage);
winMessage.setText("Player " + team + " Won!");
winFrame.add(winMessage);
}
public static void gameTie() {
JFrame tieFrame = new JFrame("Tic Tac Toe");
endFormat(tieFrame,tieMessage);
tieMessage.setText("Tie!");
tieFrame.add(tieMessage);
}
}
}
main
you should useSwingUtilities::invokeLater
. That's my 2¢. \$\endgroup\$