-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.java
65 lines (57 loc) · 1.81 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
import java.util.ArrayList;
import java.util.Random;
public class Deck {
private ArrayList<Card> deck;
public Deck(boolean init) {
deck = new ArrayList<>();
if (init) {
for (Shape shape : Shape.values()) {
for (int num = 1; num <= 13; num++) {
deck.add(new Card(num, shape));
}
}
}
}
public void addCard(Card card) {
this.deck.add(card);
}
public Card removeTopCard() {
/** removes the top card from the deck, and return it.*/
Card card = this.deck.get(deck.size() - 1);
this.deck.remove(deck.size() - 1);
return card;
}
public boolean isEmpty() {
return this.deck.isEmpty();
}
public void shuffle() {
/**
* shuffles the deck according to a specific algorithm, using
* user-input seed accepted in the main class.
*/
int num1, num2;
Card temp;
Random rand = Main.rnd;
for (int i = 0; i < 50; i++) {
num1 = rand.nextInt(deck.size());
num2 = rand.nextInt(deck.size());
temp = deck.get(num1);
deck.add(num1, deck.get(num2));
deck.remove(num1 + 1);
deck.add(num2, temp);
deck.remove(num2 + 1);
}
}
public Card watchTopCard() {
/** see the top card of the deck without removing it */
return deck.get(deck.size() - 1);
}
public void reverse() {
/** reverses the order of the deck - top to bottom */
ArrayList<Card> reversed = new ArrayList<>();
while (!this.deck.isEmpty()) {
reversed.add(this.deck.remove(deck.size() - 1));
}
this.deck = reversed;
}
}