-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.java
More file actions
176 lines (131 loc) · 3.03 KB
/
Deck.java
File metadata and controls
176 lines (131 loc) · 3.03 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
/*
By: Dylan Snelgrove
Date: January, 2018
The "Deck" Class
- holds many cards using a vector
- the parent class for "TableauDeck" and "FoundationDeck"
- uses a vector to store cards
- communicators also
- contructor option for standard deck of 52 cards.
- shuffle available
*/
import java.awt.*;
import java.util.*;
import java.awt.image.*;
public class Deck extends ShapeClass
{
private int size;
private boolean isShowing;
private Vector deck = new Vector (0, 1);
//constructor
public Deck ()
{
size = 2;
isShowing = true;
setCenter (200, 200);
setDimensions ((int) (80 * 0.7), 80);
setColor (Color.black);
}
public Deck (String type)
{
this ();
if (type == "standard")
{
for (int i = 1 ; i <= 4 ; i++)
{
for (int j = 1 ; j <= 13 ; j++)
{
Card toAdd = new Card (j, i);
deck.insertElementAt (toAdd, 0);
}
}
}
}
//communicators
public void setSize (int size)
{
for (int i = 0 ; i < deck.size () ; i++)
{
Card newCard = (Card) deck.elementAt (i);
newCard.setSize (size);
deck.setElementAt (newCard, i);
setHeight (40 + 20 * size);
setWidth ((int) (0.7 * (40 + 20 * size)));
}
}
public void setCenter (int ipCenterX, int ipCenterY)
{
super.setCenter (ipCenterX, ipCenterY);
for (int i = 0 ; i < deck.size () ; i++)
{
Card newCard = (Card) deck.elementAt (i);
newCard.setCenter (ipCenterX, ipCenterY);
deck.setElementAt (newCard, i);
}
}
public int getSize ()
{
return size;
}
int getNumberOfCards ()
{
return deck.size ();
}
public void setShowing (boolean bpIsShowing)
{
isShowing = bpIsShowing;
for (int i = 0 ; i < deck.size () ; i++)
{
Card newCard = (Card) deck.elementAt (i);
newCard.setIsShowing (bpIsShowing);
deck.setElementAt (newCard, i);
}
}
public boolean getShowing ()
{
return isShowing;
}
//action
public void add (Card cd, int pos)
{
cd.setCenter (getCenterX (), getCenterY ());
cd.setSize (getSize ());
deck.insertElementAt (cd, pos);
setCenter(getCenterX(), getCenterY());
}
public Card delete (int pos)
{
return (Card) deck.remove (pos);
}
public Card peek (int pos)
{
return (Card) deck.elementAt (pos);
}
public void shuffle ()
{
for (int i = 0 ; i < deck.size () * 4 ; i++)
{
deck.insertElementAt (deck.remove (0), ((int) (Math.random () * (deck.size () + 1))));
}
}
public void draw (Graphics g, Image[] [] images, ImageObserver imgObs)
{
if (getColor () == Color.white)
{
g.fillRect (getCenterX () - getWidth () / 2, getCenterY () - getHeight () / 2, getWidth (), getHeight ());
}
else
{
if (deck.size () == 0)
{
g.setColor (getColor ());
g.drawRect (getCenterX () - getWidth () / 2, getCenterY () - getHeight () / 2, getWidth (), getHeight ());
}
else
{
Card drawCard = (Card) deck.elementAt (0);
drawCard.draw (g, images, imgObs);
}
}
}
}