-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.java
70 lines (57 loc) · 1.43 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
package solitaire;
import java.util.ArrayList;
import java.util.Random;
public class Deck {
private ArrayList<Card> deck = new ArrayList<Card>();
private ArrayList<Card> pile = new ArrayList<Card>();
private Random r = new Random();
private int rand;
private boolean flipped = false;
Deck() {
for (Card c : Card.values()) {
deck.add(c);
}
rand = r.nextInt(deck.size());
toPile();
}
public Card getCard() {
if (flipped) {
Card temp = deck.get(0);
deck.remove(0);
return temp;
}
else {
Card temp = deck.get(rand);
deck.remove(rand);
if (deck.size() > 1) { rand = r.nextInt(deck.size()); }
else { rand = 0; }
temp.setFaceUp();
return temp;
}
}
public void removeCard(Card c) {
pile.remove(c);
}
public void toPile() {
pile.add(getCard());
}
public int pileSize() {
return pile.size();
}
public Card showTopCard() {
return pile.get(pile.size()-1);
}
public Card show2ndCard() {
return pile.get(pile.size()-2);
}
public int size() {
return deck.size();
}
public void flip() {
for (Card c : pile) {
deck.add(c);
}
pile.clear();
flipped = true;
}
}