-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreeCellGUI.java
More file actions
370 lines (317 loc) · 9.11 KB
/
FreeCellGUI.java
File metadata and controls
370 lines (317 loc) · 9.11 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.Image;
import java.awt.Desktop;
import java.awt.Color;
import java.awt.Dimension;
import java.net.URL;
import java.net.URI;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.BoxLayout;
public class FreeCellGUI extends JFrame implements MouseListener, MouseMotionListener
{
JButton[] options; //Holds the menu of options the player can select
Deck deck;
JPanel menuBar;
JPanel gameArea;
JPanel topRow;
JPanel columns;
ArrayList<CardStack> freeCells;
ArrayList<CardStack> homeCells;
ArrayList<CardStack> playingField;
//The above correspond to the top left free cells, the top right completed sets of cards, and the main 8 columns where the game is played
CardStack dragged; //When a pile is dragged around, it is added to this
JLayeredPane jlp;
Point offset; //Helps properly align where the card will be dragged to
Card tCard;
public FreeCellGUI()
{
super("Free Cell Solitaire");
setMinimumSize(new Dimension(1080, 720));
setMaximumSize(new Dimension(1080, 720));
gameStart();
}
public void gameReset() //Called to reset the game
{
menuBar.removeAll();
gameArea.removeAll();
topRow.removeAll();
columns.removeAll();
freeCells.removeAll(freeCells);
homeCells.removeAll(homeCells);
playingField.removeAll(playingField);
gameStart();
}
public void gameStart() //Called to start the game, whether for the first time or after a reset
{
setLayout(new BorderLayout());
//Adds the bottom menu bar for getting help on playing the game and restarting the game
String[] theOptions = new String[]{"Help", "New Game"};
options = new JButton[2];
for(int i = 0; i < 2; i++)
{
options[i] = new JButton(theOptions[i]);
}
options[0].addActionListener(new ActionListener() //The button for asking how to play
{
@Override
public void actionPerformed(ActionEvent e)
{
int findHelp = JOptionPane.showConfirmDialog(null, "Access the Internet to learn how to play free cell solitaire?", "Help", JOptionPane.YES_NO_OPTION);
if(findHelp == JOptionPane.YES_OPTION)
{
try
{
Desktop.getDesktop().browse(new URL("http://www.solitairecity.com/FreeCell.shtml").toURI());
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
else
JOptionPane.getRootFrame().dispose();
}
});
options[1].addActionListener(new ActionListener() //The button for restarting the game
{
@Override
public void actionPerformed(ActionEvent e)
{
int reset = JOptionPane.showConfirmDialog(null, "Would you like to start a new game from the beginning?", "New Game", JOptionPane.YES_NO_OPTION);
if(reset == JOptionPane.YES_OPTION)
{
gameReset();
}
else
{
JOptionPane.getRootFrame().dispose();
}
}
});
gameArea = new JPanel();
gameArea.setOpaque(false);
gameArea.setLayout(new BoxLayout(gameArea, BoxLayout.PAGE_AXIS));
menuBar = new JPanel(new GridLayout(0,2));
menuBar.add(options[0]);
menuBar.add(options[1]);
add(menuBar, BorderLayout.SOUTH);
//Adds the free cells and foundations to the game board
FlowLayout tFlow = new FlowLayout(FlowLayout.CENTER);
tFlow.setAlignOnBaseline(true);
topRow = new JPanel(tFlow);
add(topRow, FlowLayout.LEFT);
topRow.setOpaque(true);
topRow.setVisible(true);
topRow.setSize(new Dimension(2160, 150));
gameArea.add(topRow);
topRow.setBackground(new Color(30, 80, 25));
//Adds the main game area, where the cards are laid out into 8 columns
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
flow.setAlignOnBaseline(true);
columns = new JPanel(flow);
columns.setOpaque(true);
columns.setMinimumSize(new Dimension(200, 900));
gameArea.add(columns);
columns.setVisible(true);
columns.setBackground(new Color(30, 80, 25));
//Other changes to the JFrame, such as the background color, are done here
jlp = getLayeredPane();
add(gameArea);
playingField = new ArrayList<CardStack>();
freeCells = new ArrayList<CardStack>();
homeCells = new ArrayList<CardStack>();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
deck = new Deck();
int temp;
CardStack[] s = new CardStack[8];
Card card;
topRow.removeAll();
columns.removeAll();
for(int i = 0; i < 8; i++)
{
s[i] = new CardStack(0, 120);
s[i].addCard(new Card("blank", 0));
if(i < 4)
temp = 7;
else
temp = 6;
for(int j = 0; j < temp; j++)
{
card = deck.drawCard();
card.addMouseListener(this);
card.addMouseMotionListener(this);
s[i].addCard(card);
}
playingField.add(s[i]);
columns.add(s[i]);
}
for(int i = 0; i < 8; i++)
{
if(i < 4)
s[i] = new CardStack(1, 120);
else
s[i] = new CardStack(2, 120);
s[i].addCard(new Card("blank", 0));
if(i < 4)
freeCells.add(s[i]);
else
homeCells.add(s[i]);
topRow.add(s[i]);
}
/* for(int i = 0; i < 8; i++)
topRow.add(new Card("blank", 0)); */
validate();
}
public boolean winCondition()
{
for(CardStack s : homeCells)
{
if(s.pile.size() != 14)
return false;
}
return true;
}
@Override
public void mouseDragged(MouseEvent e)
{
if(dragged != null)
{
Point pos = getLocationOnScreen();
pos.x = e.getLocationOnScreen().x - pos.x - offset.x;
pos.y = e.getLocationOnScreen().y - pos.y - offset.y;
dragged.setLocation(pos);
jlp.setVisible(true);
jlp.setOpaque(true);
}
jlp.revalidate();
jlp.repaint();
}
@Override
public void mouseMoved(MouseEvent e)
{
}
@Override
public void mouseClicked(MouseEvent e)
{
}
@Override
public void mousePressed(MouseEvent e)
{
if(e.getComponent() instanceof Card)
{
Card c = (Card)e.getComponent();
offset = e.getPoint();
if(getComponentAt(offset) == columns)
{
if(playingField.get(playingField.indexOf(c.getParent())).getLayer(c) != JLayeredPane.DRAG_LAYER)
return;
CardStack s = (CardStack)c.getParent();
if(s.isEmpty() || s.stackType == 2)
return;
dragged = new CardStack(s.stackType, s.width);
dragged.addCard(c);
s.removeCard(c);
dragged.old = s;
playingField.add(dragged);
jlp.add(dragged, JLayeredPane.DRAG_LAYER);
add(jlp);
Point pos = getLocationOnScreen();
pos.x = e.getLocationOnScreen().x - pos.x - offset.x;
pos.y = e.getLocationOnScreen().y - pos.y - offset.y;
dragged.setLocation(pos);
jlp.setVisible(true);
jlp.setOpaque(true);
jlp.revalidate();
jlp.repaint();
}
else
{
Component comp = topRow.getComponent(0);
if (comp instanceof CardStack)
{
if(((CardStack)comp).stackType == 1)
{
CardStack s = (CardStack)c.getParent();
if(s.isEmpty() || s.stackType == 2)
return;
dragged = new CardStack(s.stackType, s.width);
dragged.addCard(c);
s.removeCard(c);
dragged.old = s;
playingField.add(dragged);
jlp.add(dragged, JLayeredPane.DRAG_LAYER);
Point pos = getLocationOnScreen();
pos.x = e.getLocationOnScreen().x - pos.x - offset.x;
pos.y = e.getLocationOnScreen().y - pos.y - offset.y;
dragged.setLocation(pos);
jlp.setVisible(true);
jlp.setOpaque(true);
jlp.revalidate();
jlp.repaint();
repaint();
}
}
}
}
}
@Override
public void mouseReleased(MouseEvent e)
{
if(dragged != null)
{
boolean cardDrop = false;
Point mPos = e.getLocationOnScreen();
ArrayList<CardStack> temp = new ArrayList<CardStack>();
for(int i = 0; i < 8; i++)
{
temp.add(playingField.get(i));
if(i < 4)
{
temp.add(freeCells.get(i));
temp.add(homeCells.get(i));
}
}
for(CardStack s : temp)
{
Point sPos = s.getLocationOnScreen();
Rectangle r = s.getBounds();
r.x = sPos.x;
r.y = sPos.y;
if(r.contains(mPos) && s.acceptableMove(dragged.pile.get(0)))
{
s.uniteStacks(dragged);
cardDrop = true;
break;
}
}
if(cardDrop == false)
dragged.old.uniteStacks(dragged);
jlp.remove(dragged);
dragged = null;
repaint();
if(winCondition())
JOptionPane.showMessageDialog(this, "Congratulations!");
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}