-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.java
More file actions
112 lines (99 loc) · 3.08 KB
/
Deck.java
File metadata and controls
112 lines (99 loc) · 3.08 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.ArrayList;
import java.util.Random;
public class Deck {
//Instance vars
private ArrayList<Card> cards;
//construct
public Deck(){
this.cards = new ArrayList<Card>();
}
public void createFullDeck(){
//Generate
for(Suit cardSuit : Suit.values()){
for(Value cardValue : Value.values()){
//Add a new card to the mix
this.cards.add(new Card(cardSuit, cardValue));
}
}
}
public void shuffle(){
ArrayList<Card> tmpDeck = new ArrayList<Card>();
//Use Random
Random random = new Random();
int randomCardIndex = 0;
int originalSize = this.cards.size();
for(int i=0; i<originalSize; i++){
//Generate Random Index
randomCardIndex = random.nextInt((this.cards.size()-1 - 0)+1)+0;
tmpDeck.add(this.cards.get(randomCardIndex));
//Remove from original deck
this.cards.remove(randomCardIndex);
}
this.cards = tmpDeck;
}
public String toString(){
String cardListOutput = "";
for(Card aCard: this.cards){
cardListOutput += "-"+ aCard.toString() + "\n";
}
return cardListOutput;
}
public void removeCard(int i){
this.cards.remove(i);
}
public Card getCard(int i){
return this.cards.get(i);
}
public void addCard(Card addCard){
this.cards.add(addCard);
}
//Draws from the deck
public void draw(Deck comingFrom){
this.cards.add(comingFrom.getCard(0));
comingFrom.removeCard(0);
}
public int deckSize(){
return this.cards.size();
}
public void moveAllToDeck(Deck moveTo){
int thisDeckSize = this.cards.size();
// put cards into moveTo deck
for(int i=0; i<thisDeckSize; i++){
moveTo.addCard(this.getCard(i));
}
for(int i=0; i< thisDeckSize; i++){
this.removeCard(0);
}
}
//Return total value of cards in deck
public int cardsValue(){
int totalValue = 0;
int aces = 0;
for(Card aCard: this.cards){
switch(aCard.getValue()){
case TWO: totalValue += 2; break;
case THREE: totalValue += 3; break;
case FOUR: totalValue += 4; break;
case FIVE: totalValue += 5; break;
case SIX: totalValue += 6; break;
case SEVEN: totalValue += 7; break;
case EIGHT: totalValue += 8; break;
case NINE: totalValue += 9; break;
case TEN: totalValue += 10; break;
case JACK: totalValue += 10; break;
case QUEEN: totalValue += 10; break;
case KING: totalValue += 10;break;
case ACE: aces += 1; break;
}
}
for(int i = 0; i < aces; i++){
if(totalValue > 10){
totalValue +=1;
}
else{
totalValue +=11;
}
}
return totalValue;
}
}