After seeing Snake coded in 10 minutes, I decided to try a similar challenge, albeit easier. The following is Tic Tac Toe that I coded in 10 minutes. Given the short time frame, I'm sure there are style and convention errors abound, and would appreciate any suggestions :)
I will also specifically ask about one thing - checking if there is a winner(done in the only ActionPerformed method). It feels long and convoluted, but it was the best I could think of at the time. Looking back, I still can't really think of any ways to write it better, actually...
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TicTacToe {
private JFrame main;
private TicButton[][] buttons;
private JPanel[] rows;
private boolean p1turn = true;
private final Color bg1 = new Color(255,0,0);
private final Color bg2 = new Color(0,255,0);
public static void main(String[] args) {
new TicTacToe().go();
}
public void go() {
main = new JFrame("Tic Tac Toe!");
buttons = new TicButton[3][3];
rows = new JPanel[3];
int r = 0;
main.getContentPane().setLayout(new BoxLayout(main.getContentPane(),BoxLayout.Y_AXIS));
for (int i = 0; i < 3; i++) {
rows[r] = new JPanel();
for (int j = 0; j < 3; j++) {
buttons[i][j] = new TicButton();
rows[r].add(buttons[i][j]);
}
main.getContentPane().add(rows[r]);
r++;
}
main.pack();
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setVisible(true);
}
private class TicButton extends JButton{
int value;
private boolean clicked;
public TicButton() {
super("-");
this.value = -1;
clicked = false;
setFocusable(false);
this.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (!clicked) {
clicked = true;
setText(p1turn ? "X" : "O");
value = p1turn ? 1 : 2;
setBackground(p1turn ? bg1 : bg2);
p1turn = !p1turn;
setEnabled(false);
}
boolean winner = false;
String win = "";
for (TicButton[] tt : buttons) {
if (tt[0].value == tt[1].value && tt[0].value == tt[2].value && tt[0].value != -1) {
winner = true;
win = tt[0].value == 1 ? "X" : "O";
break;
}
}
for (int j = 0; j < 3; j++) {
if (buttons[0][j].value == buttons[1][j].value &&
buttons[0][j].value == buttons[2][j].value &&
buttons[0][j].value != -1) {
winner = true;
win = buttons[0][j].value == 1 ? "X" : "O";
break;
}
}
if (buttons[0][0].value == buttons[1][1].value && buttons[0][0].value == buttons[2][2].value &&
buttons[0][0].value != -1) {
winner = true;
win = buttons[0][0].value == 1 ? "X" : "O";
}
if (buttons[0][2].value == buttons[1][1].value && buttons[0][2].value == buttons[2][0].value &&
buttons[0][2].value != -1) {
winner = true;
win = buttons[0][2].value == 1 ? "X" : "O";
}
if (winner) {
JOptionPane.showMessageDialog(main, win + " wins!");
main.setVisible(false);
System.exit(0);
}
}
});
}
}
}