-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolitaireDisplay.java
215 lines (181 loc) · 6.04 KB
/
SolitaireDisplay.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
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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class SolitaireDisplay extends JComponent implements MouseListener
{
private static final int CARD_WIDTH = 73;
private static final int CARD_HEIGHT = 97;
private static final int SPACING = 5; //distance between cards
private static final int FACE_UP_OFFSET = 15; //distance for cascading face-up cards
private static final int FACE_DOWN_OFFSET = 5; //distance for cascading face-down cards
public int points;
private long start;
private JFrame frame;
private int selectedRow = -1;
private int selectedCol = -1;
private Solitaire game;
public SolitaireDisplay(Solitaire game)
{
this.game = game;
frame = new JFrame("Solitaire");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
this.setPreferredSize(new Dimension(CARD_WIDTH * 7 + SPACING * 8, CARD_HEIGHT * 2 + SPACING * 3 + FACE_DOWN_OFFSET * 7 + 13 * FACE_UP_OFFSET));
this.addMouseListener(this);
frame.pack();
frame.setVisible(true);
points = 0;
start = System.currentTimeMillis();
}
public void paintComponent(Graphics g)
{
//background
g.setColor(new Color(0, 128, 0));
g.fillRect(0, 0, getWidth(), getHeight());
//clearing screen (SUPER EFFICIENTLY :))
for(int i = 0; i<1000; i++)
{
System.out.println();
}
/*
* First additional credit.
* Prints out the number of total clicks as 'points', where
* a lower number is better.
*/
System.out.println("Points (measured as clicks): " + points++);
//gets current time in milliseconds.
long now = System.currentTimeMillis();
/*
* Second additional credit.
* Computes elapsed time since start and converts it to seconds.
*/
int elapsed = (int)((now-start)/1000.0);
//prints elapsed time.
System.out.println("Time Elapsed (in seconds, updates every time you click): " + elapsed + " seconds");
/*
* Third additional credit - prints out celebration
* message when game is won.
*/
if(game.celebrateTime())
{
for(int i = 0; i<100; i++)
{
System.out.println("YAY!! YOU WON!!");
}
System.out.println("You got a score of " + points + " points! (lower is better)");
System.out.println("You won in " + elapsed + " seconds");
System.out.println("Can you beat that?");
}
//face down
drawCard(g, game.getStockCard(), SPACING, SPACING);
//stock
drawCard(g, game.getWasteCard(), SPACING * 2 + CARD_WIDTH, SPACING);
if (selectedRow == 0 && selectedCol == 1)
drawBorder(g, SPACING * 2 + CARD_WIDTH, SPACING);
//aces
for (int i = 0; i < 4; i++)
drawCard(g, game.getFoundationCard(i), SPACING * (4 + i) + CARD_WIDTH * (3 + i), SPACING);
//piles
for (int i = 0; i < 7; i++)
{
Stack<Card> pile = game.getPile(i);
int offset = 0;
for (int j = 0; j < pile.size(); j++)
{
drawCard(g, pile.get(j), SPACING + (CARD_WIDTH + SPACING) * i, CARD_HEIGHT + 2 * SPACING + offset);
if (selectedRow == 1 && selectedCol == i && j == pile.size() - 1)
drawBorder(g, SPACING + (CARD_WIDTH + SPACING) * i, CARD_HEIGHT + 2 * SPACING + offset);
if (pile.get(j).isFaceUp())
offset += FACE_UP_OFFSET;
else
offset += FACE_DOWN_OFFSET;
}
}
}
private void drawCard(Graphics g, Card card, int x, int y)
{
if (card == null)
{
g.setColor(Color.BLACK);
g.drawRect(x, y, CARD_WIDTH, CARD_HEIGHT);
}
else
{
String fileName = card.getFileName();
if (!new File(fileName).exists())
throw new IllegalArgumentException("bad file name: " + fileName);
Image image = new ImageIcon(fileName).getImage();
g.drawImage(image, x, y, CARD_WIDTH, CARD_HEIGHT, null);
}
}
public void mouseExited(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
//none selected previously
int col = e.getX() / (SPACING + CARD_WIDTH);
int row = e.getY() / (SPACING + CARD_HEIGHT);
if (row > 1)
row = 1;
if (col > 6)
col = 6;
if (row == 0 && col == 0)
game.stockClicked();
else if (row == 0 && col == 1)
game.wasteClicked();
else if (row == 0 && col >= 3)
game.foundationClicked(col - 3);
else if (row == 1)
game.pileClicked(col);
repaint();
}
private void drawBorder(Graphics g, int x, int y)
{
g.setColor(Color.YELLOW);
g.drawRect(x, y, CARD_WIDTH, CARD_HEIGHT);
g.drawRect(x + 1, y + 1, CARD_WIDTH - 2, CARD_HEIGHT - 2);
g.drawRect(x + 2, y + 2, CARD_WIDTH - 4, CARD_HEIGHT - 4);
}
public void unselect()
{
selectedRow = -1;
selectedCol = -1;
}
public boolean isWasteSelected()
{
return selectedRow == 0 && selectedCol == 1;
}
public void selectWaste()
{
selectedRow = 0;
selectedCol = 1;
}
public boolean isPileSelected()
{
return selectedRow == 1;
}
public int selectedPile()
{
if (selectedRow == 1)
return selectedCol;
else
return -1;
}
public void selectPile(int index)
{
selectedRow = 1;
selectedCol = index;
}
}