-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.java
116 lines (95 loc) · 4 KB
/
GUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;
public class GUI extends JPanel {
public JButton[][] buttons;
private JPanel[] levels;
private JFrame frame;
private JPanel panel;
public GUI(int dimension) {
this.frame = new JFrame("3D-Tic-Tac-Toe");
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.buttons = new JButton[4][16]; // 4 levels, 16 buttons each.
this.levels = new JPanel[4]; // 4 levels.
this.panel = new JPanel(new GridLayout(1, 4)); // The background panel. Each panel in @levels will be in one of the 4 slots in this panel.
for (int i = 1; i <= this.levels.length; i++) {
TitledBorder border = new TitledBorder("Level " + i);
border.setTitleJustification(TitledBorder.CENTER);
border.setTitlePosition(TitledBorder.TOP);
this.levels[i - 1] = new JPanel(new GridLayout(dimension, dimension));
this.levels[i - 1].setBorder(border);
}
for (int i = 0; i < this.levels.length; i++) {
this.buttons[i] = new JButton[16];
for (int j = 0; j < this.buttons[0].length; j++) {
this.buttons[i][j] = new JButton("");
this.buttons[i][j].addActionListener(new ButtonListener(TicTacToe.DEBUG));
this.buttons[i][j].putClientProperty("column", j % dimension);
this.buttons[i][j].putClientProperty("row", j / dimension);
this.buttons[i][j].putClientProperty("level", i);
this.levels[i].add(this.buttons[i][j]);
}
panel.add(this.levels[i]);
}
frame.add(panel);
frame.setSize(1000, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setButtonsEnabled(boolean enabled) {
for (int i = 0; i < this.levels.length; i++) {
for (int j = 0; j < this.buttons[0].length; j++) {
this.buttons[i][j].setEnabled(enabled);
}
}
}
public void move(Coordinate move) {
this.buttons[move.x][(4 * move.y) + move.z].setText("2");
}
public void showWinMessage(String message) {
JOptionPane.showMessageDialog(null, message);
}
private class ButtonListener implements ActionListener {
private final boolean debug;
public ButtonListener(boolean debug) {
this.debug = debug;
}
@Override
public void actionPerformed(ActionEvent e) {
JButton buttonClicked = (JButton) e.getSource(); // Get the particular button that was clicked
int column = (Integer) buttonClicked.getClientProperty("column");
int row = (Integer) buttonClicked.getClientProperty("row");
int level = (Integer) buttonClicked.getClientProperty("level");
buttonClicked.setText("1");
if (debug) {
System.out.println("\tColumn: " + column);
System.out.println("\tRow: " + row);
System.out.println("\tLevel: " + level);
}
Coordinate move;
boolean success = false;
do {
move = new Coordinate(level, row, column);
success = TicTacToe.board.isSquareBlank(move);
if (!success) {
System.out.println("Invalid move. Try again.");
}
} while (!success);
TicTacToe.board = TicTacToe.board.move(move, 1);
TicTacToe.board.turnCount++;
if (TicTacToe.DEBUG) {
TicTacToe.printDebugMsgs(1);
}
if (TicTacToe.board.isGoalState()) {
TicTacToe.isAITurn = false;
} else {
TicTacToe.isAITurn = true;
}
}
}
}