-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeckClass.java
More file actions
167 lines (142 loc) · 3.12 KB
/
DeckClass.java
File metadata and controls
167 lines (142 loc) · 3.12 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
//DeckClass
import java.util.*;
import java.awt.*;
import hsa.Console;
public class DeckClass extends ShapeClass
{
protected Vector deck = new Vector (0, 1);
CardClass cc = new CardClass ();
public DeckClass ()
{
}
public DeckClass (String deckType)
{
if (deckType == "faceup") // std deck
{
for (int i = 0 ; i < 13 ; i++)
{
for (int ii = 1 ; ii < 5 ; ii++)
{
cc = new CardClass ();
cc.setsuit (ii);
cc.setface ("" + "A23456789TJQK".charAt (i));
addCard (cc, 0);
}
}
// generate 52 cards (2 nested loops) and add them to the deck
}
if (deckType == "facedown") //facedown deck
{
for (int i = 0 ; i < 13 ; i++)
{
for (int ii = 1 ; ii < 5 ; ii++)
{
cc = new CardClass ();
cc.setsuit (ii);
cc.setface ("" + "A23456789TJQK".charAt (i));
cc.setfaceup (false);
addCard (cc, 0);
}
}
// generate 52 cards (2 nested loops) and add them to the deck
}
shuffle ();
}
public void addCard (CardClass cardToAdd, int Pos)
{
if (cardToAdd != null)
{
if (Pos == 0 && deck.size () == 0)
{
deck.addElement (cardToAdd);
}
else if (Pos > deck.size ())
{
deck.insertElementAt (cardToAdd, deck.size ());
}
else
{
deck.insertElementAt (cardToAdd, Pos);
}
}
}
public CardClass dealCard (int Pos)
{
CardClass dummy = null;
if (Pos >= 0 && Pos <= deck.size ())
{
if (deck.size () != 0)
{
dummy = (CardClass) (deck.elementAt (Pos));
deck.remove (Pos);
}
}
return dummy;
}
public void shuffle ()
{
if (!deck.isEmpty ())
{
for (int i = 0 ; i < deck.size () ; i++)
{
deck.add ((int) (Math.random () * deck.size ()), deck.remove (i));
}
}
}
public boolean isPointInside (int x, int y)
{
if (x > iCentreX - iWidth / 2 && x < iCentreX + iWidth / 2 && y > iCentreY - iHeight / 2 && y < iCentreY + iHeight / 2)
{
return true;
}
else
{
return false;
}
}
public void draw (Console c)
{
if (!deck.isEmpty ())
{
CardClass cc = (CardClass) deck.firstElement ();
cc.setcardsize (iHeight);
cc.setCentre (iCentreX, iCentreY);
cc.draw (c);
}
else
{
Color tempColor = getColor ();
c.setColor (Color.lightGray);
c.fillRect (iCentreX - iWidth / 2, iCentreY - iHeight / 2, iWidth, iHeight);
c.setColor (tempColor);
c.drawRect (iCentreX - iWidth / 2, iCentreY - iHeight / 2, iWidth, iHeight);
}
}
public void draw (Graphics g)
{
if (!deck.isEmpty ())
{
CardClass cc = (CardClass) deck.firstElement ();
cc.setcardsize (iHeight);
cc.setCentre (iCentreX, iCentreY);
cc.draw (g);
}
else
{
Color tempColor = getColor ();
g.setColor (Color.green);
g.fillRect (iCentreX - iWidth / 2, iCentreY - iHeight / 2, iWidth, iHeight);
g.setColor (Color.black);
g.drawRect (iCentreX - iWidth / 2, iCentreY - iHeight / 2, iWidth, iHeight);
g.setColor (tempColor);
}
}
public boolean isDeckEmpty ()
{
return deck.isEmpty ();
}
public int getSize ()
{
return deck.size ();
}
}