-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
251 lines (216 loc) · 6.42 KB
/
Copy pathGUI.java
File metadata and controls
251 lines (216 loc) · 6.42 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/****************************************************************
* GUI.java
*
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultCaret;
public class GUI {
public Pot[] pots;
public Pot bigPot1;
public Pot bigPot2;
public JTextArea textArea;
public JScrollPane scrollPanel;
public String logs;
public String player1;
public String player2;
public JFrame frame;
public GUI(String player1, String player2)
{
this.player1 = player1;
this.player2 = player2;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Mancala");
frame.setSize(860, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation((screen.width - 860) / 2, (screen.height - 320) / 2);
frame.setVisible(true);
Container content = frame.getContentPane();
content.setLayout(new BorderLayout());
createPots();
JPanel center = new JPanel(new GridLayout(2, 6));
center.setOpaque(false);
MyPanel panel = new MyPanel();
panel.setLayout(new BorderLayout());
panel.add(center, BorderLayout.CENTER);
addPots(center, panel);
content.add(panel, BorderLayout.CENTER);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
//frame.init();
frame.setSize(860, 320);
frame.setVisible(true);
}
});
}
public void refreshBigPots() {
bigPot1.refresh();
bigPot2.refresh();
}
private void addPots(JPanel center, MyPanel panel) {
panel.add(bigPot1, BorderLayout.EAST);
panel.add(bigPot2, BorderLayout.WEST);
panel.add(scrollPanel, BorderLayout.SOUTH);
for (int i = 11; i > 5; i--) {
center.add(pots[i]);
}
for (int i = 0; i < 6; i++) {
center.add(pots[i]);
}
}
private void createPots()
{
bigPot1 = new BigPot("P1:" + player1);
bigPot2 = new BigPot("P2:" + player2);
pots = new Pot[12];
for (int i = 0; i < pots.length; i++)
{
if (i < 6)
pots[i] = new Pot(true, i);
else
pots[i] = new Pot(false, i);
pots[i].createListener();
}
textArea = new JTextArea(5, 20);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setOpaque(false);
logs ="";
textArea.setText(logs);
scrollPanel = new JScrollPane(textArea);
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
}
public GUI getGUI()
{
return this;
}
public void applyMove(int bin, boolean player1, GameState context)
{
int [] state = context.state;
//clear the original bin
int stones = state[bin];
state[bin] = 0;
//clear the bin in GUI
if (player1)
pots[bin].removeBeans();
else
pots[bin+6].removeBeans();
//sleep to make the move smoother
tSleep(Mancala.SLEEP_TIME);
int stoneTemp = stones;
for (int i = 0; i < stoneTemp; ++i)
{
int nextBin = (bin+i+1)%14;
if (!(nextBin == 6 && bin > 6) && !(nextBin == 13 && bin < 7))
++state[nextBin];
else
++stoneTemp; //ship that stone
}
//scatter the stones in the consequent bins
for (int i = 0; i < stones; i++)
{
int nextBin = (bin + i + 1) % 14;
if (!(nextBin == 6 && bin > 6) && !(nextBin == 13 && bin < 7))
{
if (nextBin == 6 && player1) //this is the only case we can put stone in the mancala
bigPot1.addBeans(1);
else if (nextBin == 6 && !player1)
bigPot2.addBeans(1);
if (player1 && nextBin < 6)
pots[nextBin].addBeans(1);
else if (player1 && nextBin > 6)
pots[nextBin - 1].addBeans(1);
else if (!player1 && nextBin < 6)
pots[nextBin + 6].addBeans(1);
else if (!player1 && nextBin > 6)
pots[nextBin - 7].addBeans(1);
}
else
++ stones;
//sleep for a little bit
tSleep(Mancala.SLEEP_TIME);
}
int lastBin = (bin+stones)%14;
//if free turn
if ((lastBin == 6 || lastBin == 13) && !context.gameOver())
{
return;
}
boolean lastBinEmpty = state[lastBin] == 1;
boolean lastBinOnYourSide = bin/7 == lastBin/7;
if (lastBinEmpty && lastBinOnYourSide && lastBin != 6 && lastBin != 13)
{
int mancalaBin = context.mancalaOf(bin);
int neighborBin = context.neighborOf(lastBin);
//If mancala happens, we also update the GUI
int newNeighborBin, newLastBin;
if (player1)
{
newLastBin = lastBin; //should be in our side
newNeighborBin = neighborBin - 1;
pots[newLastBin].removeBeans();
tSleep(Mancala.SLEEP_TIME);
pots[newNeighborBin].removeBeans();
tSleep(Mancala.SLEEP_TIME);
bigPot1.addBeans(state[neighborBin] + 1);
}
else
{
newLastBin = lastBin + 6;
newNeighborBin = neighborBin - 7;
pots[newLastBin].removeBeans();
tSleep(Mancala.SLEEP_TIME);
pots[newNeighborBin].removeBeans();
tSleep(Mancala.SLEEP_TIME);
bigPot2.addBeans(state[neighborBin] + 1);
}
state[mancalaBin] += state[neighborBin] + 1;
state[neighborBin] = 0;
state[lastBin] = 0;
}
if (context.gameOver())
{
context.stonesToMancalas();
//we also update the gui if this happens
for (int i = 0; i < 6; i++)
{
bigPot1.addBeans(pots[i].beans.size());
pots[i].removeBeans();
}
for (int i = 6; i < 12; i++)
{
bigPot2.addBeans(pots[i].beans.size());
pots[i].removeBeans();
}
}
}
public void tSleep(int sleepTime)
{
try
{
Thread.sleep(sleepTime);
} catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}