-
Notifications
You must be signed in to change notification settings - Fork 15
/
CardStack.java
210 lines (188 loc) · 3.7 KB
/
CardStack.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
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ListIterator;
import java.util.Vector;
import javax.swing.JComponent;
/* This is GUI component with a embedded
* data structure. This structure is a mixture
* of a queue and a stack
*/
class CardStack extends JComponent
{
protected final int NUM_CARDS = 52;
protected Vector<Card> v;
protected boolean playStack = false;
protected int SPREAD = 18;
protected int _x = 0;
protected int _y = 0;
public CardStack(boolean isDeck)
{
int f = 1;
this.setLayout(null);
v = new Vector<Card>();
if (isDeck)
{
// set deck position
for (Card.Suit suit : Card.Suit.values())
{
for (Card.Value value : Card.Value.values())
{
v.add(new Card(suit, value));
}
}
} else
{
playStack = true;
}
}
public boolean empty()
{
if (v.isEmpty())
return true;
else
return false;
}
public void putFirst(Card c)
{
v.add(0, c);
}
public Card getFirst()
{
if (!this.empty())
{
return v.get(0);
} else
return null;
}
// analogous to peek()
public Card getLast()
{
if (!this.empty())
{
return v.lastElement();
} else
return null;
}
// queue-like functionality
public Card popFirst()
{
if (!this.empty())
{
Card c = this.getFirst();
v.remove(0);
return c;
} else
return null;
}
public void push(Card c)
{
v.add(c);
}
public Card pop()
{
if (!this.empty())
{
Card c = v.lastElement();
v.remove(v.size() - 1);
return c;
} else
return null;
}
// shuffle the cards
public void shuffle()
{
Vector<Card> v = new Vector<Card>();
while (!this.empty())
{
v.add(this.pop());
}
while (!v.isEmpty())
{
Card c = v.elementAt((int) (Math.random() * v.size()));
this.push(c);
v.removeElement(c);
}
}
public int showSize()
{
System.out.println("Stck Size: " + v.size());
return v.size();
}
// reverse the order of the stack
public CardStack reverse()
{
Vector<Card> v = new Vector<Card>();
while (!this.empty())
{
v.add(this.pop());
}
while (!v.isEmpty())
{
Card c = v.firstElement();
this.push(c);
v.removeElement(c);
}
return this;
}
public void makeEmpty()
{
while (!this.empty())
{
this.popFirst();
}
}
@Override
public boolean contains(Point p)
{
Rectangle rect = new Rectangle(_x, _y, Card.CARD_WIDTH + 10, Card.CARD_HEIGHT * 3);
return (rect.contains(p));
}
public void setXY(int x, int y)
{
_x = x;
_y = y;
// System.out.println("CardStack SET _x: " + _x + " _y: " + _y);
setBounds(_x, _y, Card.CARD_WIDTH + 10, Card.CARD_HEIGHT * 3);
}
public Point getXY()
{
// System.out.println("CardStack GET _x: " + _x + " _y: " + _y);
return new Point(_x, _y);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (playStack)
{
removeAll();
ListIterator<Card> iter = v.listIterator();
Point prev = new Point(); // positioning relative to the container
Point prevWhereAmI = new Point();// abs positioning on the board
if (iter.hasNext())
{
Card c = iter.next();
// this origin is point(0,0) inside the cardstack container
prev = new Point();// c.getXY(); // starting deck pos
add(Solitaire.moveCard(c, prev.x, prev.y));
// setting x & y position
c.setWhereAmI(getXY());
prevWhereAmI = getXY();
} else
{
removeAll();
}
for (; iter.hasNext();)
{
Card c = iter.next();
c.setXY(new Point(prev.x, prev.y + SPREAD));
add(Solitaire.moveCard(c, prev.x, prev.y + SPREAD));
prev = c.getXY();
// setting x & y position
c.setWhereAmI(new Point(prevWhereAmI.x, prevWhereAmI.y + SPREAD));
prevWhereAmI = c.getWhereAmI();
}
}
}
}// END CardStack