-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.java
177 lines (159 loc) · 4.09 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI implements ActionListener {
JButton clear, solve;
JButton[][] matb = new JButton[8][8];
JFrame frame = new JFrame("Reversi");
JPanel panel = new JPanel(new BorderLayout());
JLabel turnLabel = new JLabel();
JLabel scoreLabel = new JLabel();
int b = 0;
/**
* Creates a graphical board
*/
public GUI() {
panel.setLayout(new GridLayout(9, 8));
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
JButton temp = new JButton();
temp.setName(10 * i + j + "");
temp.setBackground(new Color(0, 128, 0));
temp.addActionListener(this);
matb[i][j] = temp;
panel.add(temp);
}
}
scoreLabel.setHorizontalAlignment(JLabel.CENTER);
turnLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(scoreLabel);
panel.add(turnLabel);
frame.add(panel);
frame.setSize(800, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
* Show a dialog value so the user can decide the maximal limit that the
* algorithm has to run to give the utility values
*
* @return the limit time that the algorithm has to run to give the utility
* values
*/
public int getLevel() {
return Integer.parseInt(JOptionPane.showInputDialog("Enter the level:"));
}
public boolean help() {
int n = JOptionPane.showConfirmDialog(frame, "Show valid moves?", "", JOptionPane.YES_NO_OPTION);
return n == 0;
}
public boolean ai() {
int n = JOptionPane.showConfirmDialog(frame, "Play against AI", "", JOptionPane.YES_NO_OPTION);
return n == 0;
}
/**
* Draw the current state of the board
*
* @param matrix
* represent how the board looks like
*/
@SuppressWarnings("deprecation")
public void paint(int[][] matrix, int a, int b) {
int count = 0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
matb[x][y].setLabel("");
if (matrix[x + 1][y + 1] == 1) {
matb[x][y].setBackground(Color.WHITE);
matb[x][y].setOpaque(true);
matb[x][y].setBorderPainted(false);
count++;
} else if (matrix[x + 1][y + 1] == -1) {
matb[x][y].setBackground(Color.BLACK);
matb[x][y].setOpaque(true);
matb[x][y].setBorderPainted(false);
count--;
}
if (a != -1 && b != -1) {
matb[a][b].setBackground(Color.RED);
}
}
}
if (count > 0) {
scoreLabel.setText("White: +" + count);
} else if (count < 0) {
scoreLabel.setText("Black: +" + -count);
} else {
scoreLabel.setText("Equal");
}
}
/**
* Set a label to display who's turn it is
*
* @param player
* represent the player, -1 for black player and 1 for white
* player
*/
public void updateLabel(int player) {
if (player == -1) {
turnLabel.setText("White");
} else {
turnLabel.setText("Black");
}
}
/**
* Draw all the best possible moves and return which button the are being
* pressed
*
* @param rec
* a matrix which represent the best possible moves
* @return b return which button the are being pressed
*/
@SuppressWarnings("deprecation")
public int paintrec(int[][] rec, boolean help) {
if (help) {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
if (rec[x + 1][y + 1] != 100) {
matb[x][y].setLabel("X");
} else {
matb[x][y].setLabel("");
}
}
}
}
b = -1;
while (b == -1 || (b - 100) > 0) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return b;
}
/**
* Show message dialog
*
* @param msg
* message that is being shown
*/
public void msg(String msg) {
JOptionPane.showMessageDialog(frame, msg);
}
/**
* Sets which button that is pressed
*/
public void actionPerformed(ActionEvent e) {
JButton o = (JButton) e.getSource();
String name = o.getName();
b = Integer.parseInt(name);
}
}