-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.java
57 lines (51 loc) · 1.55 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
//********************************************************
// Deck class for PokerTest
// by Pratishta Yerakala (py2211)
// This class generates a deck of 52 cards. I can also
// shuffle and deal cards.
//
//********************************************************
import java.util.Random;
public class Deck {
private Card[] theDeck;
// the index of the top of the deck
private int top;
private Random randomNumber = new Random();
private int deckSize = 52;
public Deck() {
theDeck = new Card[deckSize];
// adds 52 cards of 4 suits and 13 values
int card = 0;
for (int value = 1; value <= 13; value++) {
for (int suit = 1; suit <= 4; suit++) {
theDeck[card] = new Card(suit, value);
card++;
}
}
}
// shuffles the deck by switching two cards 1000 times
public void shuffle() {
for (int s = 0; s < 1000; s++) {
// random numbers between 1 and 52 assigned to randomIndex1 and
// randomIndex2
int randomIndex1 = randomNumber.nextInt(deckSize);
int randomIndex2 = randomNumber.nextInt(deckSize);
// temporary cards assigned cards that correspond to random number
// in deck
Card temp1 = theDeck[randomIndex1];
Card temp2 = theDeck[randomIndex2];
// cards switch; temp1 is temp2 and vice versa
theDeck[randomIndex1] = temp2;
theDeck[randomIndex2] = temp1;
}
// top assigned to be the "first" card in the shuffled deck
top = 0;
}
// method deals the top cards and increments top so that same indexed card
// isn't being used
public Card deal() {
Card topCard = theDeck[top];
top++;
return topCard;
}
}