-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.java
54 lines (47 loc) · 1.18 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
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class Deck {
/**Creates a deck with functions shuffle, draw, and print */
public static Card[] cards = new Card[52];
static int index = 0;
public Deck() {
//adds new card of x suit, x value to deck
int i = 0;
for(int suit = 0; suit <= 3; suit++) {
for(int value = 2; value <= 14; value++) {
cards[i] = new Card(value, suit);
i++;
}
}
}
public void shuffle() {
//Shuffles the deck
Random r = ThreadLocalRandom.current();
for (int i = 51; i > 0; i--) {
int j = r.nextInt(i + 1);
Card tmp = cards[j];
cards[j] = cards[i];
cards[i] = tmp;
}
index = 0;
}
public Card[] draw() {
//Draws a card from the deck, defaults to one card
return draw(1);
}
public Card[] draw(int count) {
Card[] hand = new Card[count];
for (int i=0; i<count; i++) {
index++;
//adds a card to hand and subtracts it from the deck
hand[i] = cards[index-1];
}
return hand;
}
public static void printDeck() {
//Prints the deck
for (int i=0; i<cards.length; i++) {
System.out.println(cards[i].toString());
}
}
}