patternjavaMinor
Swinging Tic-Tac-Toe
Viewed 0 times
tactictoeswinging
Problem
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
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() ==
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() ==
Solution
Some immediate thoughts:
- 1.
actionPerformed: I notice that you're going through the array looking for a match and callingroundJudgewith the same index. This can be shortened to aforloop:
for (int i = 0; i < 9; i++) {
if (e.getSource() == gameButtons[i]) {
roundJudge(i);
return;
}
}- 2.
gameJudgehas three segments: checking rows, checking columns, and checking diagonals. The first two can easily be turned intoforloops as well:
if (gameButtons[i].getText().equals(gameButtons[i+1].getText()) && //etc.- 3.
- (Advanced) If you are using Java 8, you can check for ties across the entire array using a
Stream:
boolean allSquaresFull = Arrays.stream(gameButtons).noneMatch(JButton::isEnabled);Code Snippets
for (int i = 0; i < 9; i++) {
if (e.getSource() == gameButtons[i]) {
roundJudge(i);
return;
}
}if (gameButtons[i].getText().equals(gameButtons[i+1].getText()) && //etc.boolean allSquaresFull = Arrays.stream(gameButtons).noneMatch(JButton::isEnabled);Context
StackExchange Code Review Q#143251, answer score: 4
Revisions (0)
No revisions yet.