-
Notifications
You must be signed in to change notification settings - Fork 1
/
Deck.java
60 lines (47 loc) · 1.01 KB
/
Deck.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
/*
* Alyza Diaz Rodriguez
* Chelsea Dillon
*
*/
public class Deck extends Pile{
private final int LIMIT = 52;
public Deck() {
}
public void deal(Hand h) {
int count = 0; //keep track of cards dealt
while(count<h.getLimit()) {
h.add(draw());
count++;
}
}
public Card draw() {
int rng = (int)(Math.random()*getNumCards());
Card drawn = getCard(rng);
remove(drawn);
return drawn;
}
private Card getCard(int x) {
Card[] cards = toArray();
return cards[x];
}
public void shuffle() {
Card[] cards = toArray();
int times = 3; //shuffle the cards 3 times for increased randomness
while(times>0) {
for(int i=0;i<cards.length;i++) {
int rng = (int)(Math.random()*cards.length);
Card temp = cards[i];
cards[i] = cards[rng];
cards[rng] = temp;
}
times--;
}
clear();
for(int i=0;i<cards.length;i++) {
add(cards[i]);
}
}
public int getLimit() {
return LIMIT;
}
}