-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeckClass.java
159 lines (128 loc) · 3.48 KB
/
DeckClass.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
// Author: Matthew Leung
// ICS4U1 Final Project
// Penguin Solitaire
import java.awt.*; // Java's Abstract Windowing Toolkit package - includes class Color
import java.util.*; // Vector class is in the 'util' package
public class DeckClass extends ShapeClass
{
private Vector deck = new Vector (0, 1);
public DeckClass ()
{
}
public DeckClass (char deckType)
{
if (deckType == 's') // standard deck
{
for (int n = 2 ; n <= 14 ; n++)
{
for (int s = 1 ; s <= 4 ; s++)
{
CardClass oCard = new CardClass ();
oCard.setUp (true);
oCard.setSuit (s);
oCard.setValueByInt (n);
addCard (oCard, 53);
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////
public void addCard (CardClass newCard, int pos) //add a card to the deck
{
if ((pos == 0 && deck.size () == 0) || pos > deck.size ())
{
deck.addElement (newCard);
}
else
{
deck.insertElementAt (newCard, pos);
}
}
public CardClass dealCard (int Pos) //remove
{
return (CardClass) deck.remove (Pos); // must type cast element
}
public void shuffle () //shuffle the deck
{
final int n = deck.size ();
for (int i = 1 ; i <= n * 10 ; i++)
{
addCard (dealCard ((int) (Math.random () * (n - 1))), (int) (Math.random () * (n - 1)));
}
}
public CardClass getCard (int pos) //returns a specfic card from the deck
{
return (CardClass) deck.get (pos);
}
public void addPile (PileClass p) //adds a pile (if allowed) to the deck
{
if (canAdd (p) == true)
{
addCard (p.dealCard (0), 0);
setCentreAndHeight (getCenterX (), getCenterY (), getHeight ());
}
}
public boolean canAdd (PileClass p) //checks if a pile can be added to the deck
{
if (p.getPileSize () == 1)
{
return true;
}
else
{
return false;
}
}
/////////////////////////////////////////////////////////////////////////////////
public void setWidth (int w)
{
super.setWidth (w);
super.setHeight (w * 10 / 7);
}
public void setHeight (int h)
{
super.setHeight (h);
super.setWidth (h * 7 / 10);
}
public void setCentreAndHeight (int x, int y, int h) //sets the centre and height (and therefore also the width) of every card in the deck
{
setCentre (x, y);
setHeight (h);
for (int i = 0 ; i < deck.size () ; i++)
{
((CardClass) deck.get (i)).setHeight (h);
((CardClass) deck.get (i)).setCentre (x, y);
}
}
public int getDeckSize () //returns the size of the deck
{
return deck.size ();
}
public void draw (Graphics g)
{
if (deck.size () > 0)
{
if (((CardClass) deck.firstElement ()).getUp () == true)
{
((CardClass) deck.firstElement ()).draw (g);
}
else
{
g.setColor (Color.black);
g.fillRect (getCenterX () - getWidth () / 2, getCenterY () - getHeight () / 2, getWidth (), getHeight ());
}
}
else
{
g.setColor (Color.black);
g.drawRect (getCenterX () - getWidth () / 2, getCenterY () - getHeight () / 2, getWidth (), getHeight ());
}
}
public void erase (Graphics g)
{
Color oldColor = getColor ();
setColor (Globals.colorbg);
g.drawRect (getCenterX () - getWidth () / 2, getCenterY () - getHeight () / 2, getWidth (), getHeight ());
setColor (oldColor);
}
}