-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.java
More file actions
117 lines (93 loc) · 2.31 KB
/
Copy pathDeck.java
File metadata and controls
117 lines (93 loc) · 2.31 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
113
114
115
116
117
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collections;
public class Deck {
private ArrayList<Card> deck;
//Inner class Card , Can't you see
public class Card {
String suit;
int value;
String face;
private Card (int value, String suit) {
this.suit = suit;
this.value = value;
}
private Card (String face, String suit) {
this.suit = suit;
this.face = face;
}
}
public Deck() {
deck = new ArrayList<Card>();
createDeck();
}
private void shuffle() {
Collections.shuffle(deck);
}
private Card getNextCard() {
if (deck.isEmpty()) {
throw new ArrayIndexOutOfBoundsException("There are no cards left in this deck");
}
Card next = deck.remove(0);
return next;
}
//Populates the deck with all 52 cards
private void createDeck() {
String[] suits = {"spade", "club", "heart", "diamond"};
HashMap<Integer, String> facecards = new HashMap<Integer, String>(); //Accounting for facecards which dont have a integer value
facecards.put(11, "J");
facecards.put(12, "Q");
facecards.put(13, "K");
for (String suit: suits) {
Card ace = new Card("A", suit);
deck.add(ace);
int value = 2;
while (value <= 10) {
Card c = new Card(value, suit);
deck.add(c);
value++;
}
while (value <= 13) {
Card c = new Card(facecards.get(value), suit);
deck.add(c);
value++;
}
}
}
public static void main(String[] args) {
//Testing whether deck generation is working
Deck d = new Deck();
int count = 0;
while (count < 10) {
Card next = d.getNextCard();
if (next.value == 0) {
System.out.println(next.face + " " + next.suit);
} else {
System.out.println(next.value + " " + next.suit);
}
count++;
}
System.out.println();
System.out.println("-----Shuffled Cards-----");
//Testing whether cards are being shuffled correctly
Deck a = new Deck();
d.shuffle();
count = 0;
while (count < 10) {
Card next = d.getNextCard();
if (next.value == 0) {
System.out.println(next.face + " " + next.suit);
} else {
System.out.println(next.value + " " + next.suit);
}
count++;
}
System.out.println();
System.out.println("-----Error Testing-----");
//Testing whether program throws an error correctly
while (!a.deck.isEmpty()) {
a.getNextCard();
}
a.getNextCard();
}
}