-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.java
41 lines (38 loc) · 876 Bytes
/
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
/**
* This class is used to represent a deck of cards in general card games.
*
* @author Kenneth Wong
*/
public class Deck extends CardList {
private static final long serialVersionUID = -3886066435694112173L;
/**
* Creates and returns an instance of the Deck class.
*/
public Deck() {
initialize();
}
/**
* Initializes the deck of cards (called implicitly inside the constructor).
*/
public void initialize() {
removeAllCards();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
Card card = new Card(i, j);
addCard(card);
}
}
}
/**
* Shuffles the deck of cards.
*/
public void shuffle() {
for (int i = 0; i < this.size(); i++) {
int j = (int) (Math.random() * this.size());
if (i != j) {
Card card = setCard(i, getCard(j));
setCard(j, card);
}
}
}
}