-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.java
97 lines (85 loc) · 2.98 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
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
/*
* Deck.java
*
* Computer Science S-111, Harvard University
*
* A blueprint class for objects that represent a deck of Huno cards.
*
* YOU SHOULD NOT MODIFY THIS FILE.
*/
import java.util.*;
public class Deck {
public static final int NUM_CARDS =
Card.COLORS.length * (Card.MAX_VALUE - Card.MIN_VALUE + 1);
/* fields for the deck */
private Card[] cards;
private int numCardsLeft; // number of cards not yet drawn
private Random rand; // random-number generator used when shuffling
/*
* Deck constructor - takes a numeric seed for the random-number
* generator. If the seed is -1, it is not used, which means
* you will get a different ordering each time the program is run.
*/
public Deck(int seed) {
this.cards = new Card[NUM_CARDS];
int count = 0;
for (int colorIndex = 0; colorIndex < Card.COLORS.length; colorIndex++) {
for (int val = Card.MIN_VALUE; val <= Card.MAX_VALUE; val++) {
this.cards[count] = new Card(Card.COLORS[colorIndex], val);
this.cards[count].setEffect();
count++;
}
}
this.numCardsLeft = NUM_CARDS;
if (seed == -1) {
this.rand = new Random();
} else {
this.rand = new Random(seed);
}
}
/*
* Deck constructor that takes no parameters. This is useful when
* you don't want to specify a seed for the random-number generator.
* It calls the other constructor, passing it the special seed of -1.
*/
public Deck() {
this(-1);
}
/*
* reset - restores the deck to a full set of cards.
* This method should typically be followed by a call to shuffle(),
* or else the cards will be re-dealt in the same order as they were
* the first time this deck was used.
*/
public void reset() {
this.numCardsLeft = NUM_CARDS;
}
/*
* shuffle - rearranges the cards in the deck
*/
public void shuffle() {
// Swap each card in the deck with a randomly chosen other card.
for (int i = 0; i < NUM_CARDS; i++) {
int swapWith = Math.abs(this.rand.nextInt()) % NUM_CARDS;
Card temp = this.cards[i];
this.cards[i] = this.cards[swapWith];
this.cards[swapWith] = temp;
}
}
/*
* drawCard - returns a single card from the deck.
* Note that the cards aren't actually removed from the cards array.
* Rather, we use the numCardsLeft field to determine which
* element of the array should be returned. This means that we
* end up going from right to left through the array -- returning
* the last card, then the second-to-last, etc.
*/
public Card drawCard() {
if (this.numCardsLeft == 0) {
this.reset();
this.shuffle();
}
this.numCardsLeft--;
return this.cards[this.numCardsLeft];
}
}