-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeckImpl.java
More file actions
93 lines (79 loc) · 2.6 KB
/
DeckImpl.java
File metadata and controls
93 lines (79 loc) · 2.6 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
package a2;
public class DeckImpl implements Deck {
// Instance fields
private Card[] _cards; // Array of cards in the deck.
private int _num_left_to_deal; // Number of cards still left to deal.
// Constructor creates and initializes a shuffled standard 52 card deck of cards.
public DeckImpl() {
_num_left_to_deal = 52;
_cards = new Card[_num_left_to_deal];
int cidx = 0;
// Generate cards
for (Card.Suit s : Card.Suit.values()) { //guessing this loops through the values
for (int rank = 2; rank <= Card.ACE; rank++) {
_cards[cidx] = new CardImpl(rank, s);
cidx += 1;
}
}
// Shuffle
for (int i=0; i<_cards.length; i++) {
int swap_idx = i + ((int) (Math.random() * (_cards.length - i))); //integer i gets bigger so its shuffling
Card tmp = _cards[i];
_cards[i] = _cards[swap_idx];
_cards[swap_idx] = tmp;
}
}
// hasHand() indicates whether the deck has enough cards to deal another
// five card poker hand or not.
public boolean hasHand() {
return (_num_left_to_deal >= 5);
}
// dealNextCard() deals the next card from the deck.
public Card dealNextCard() {
if (_num_left_to_deal== 0) {
throw new RuntimeException("No more cards left to deal in deck");
}
Card dealt_card = _cards[nextUndealtIndex()];
_num_left_to_deal -= 1;
return dealt_card;
}
// dealHand() deals the next five cards and returns the poker hand
// formed by them.
public PokerHand dealHand() {
if (!hasHand()) {
throw new RuntimeException("Deck does not have enough cards to deal another hand");
}
Card[] hand_cards = new Card[5];
for (int i=0; i<hand_cards.length; i++) {
hand_cards[i] = dealNextCard();
}
return new PokerHandImpl(hand_cards);
}
// findAndRemove(Card c) looks for the Card c among the cards still left to deal.
// If found, removes the card from the deck by swapping it with the next card to
// deal and dealing it.
public void findAndRemove(Card c) {
if (_num_left_to_deal == 0) {
return;
}
for (int i=nextUndealtIndex(); i<52; i++) {
if (_cards[i].equals(c)) {
// Found card as undealt. Swap with next card to deal and then
// deal it to remove.
Card tmp = _cards[i];
_cards[i] = _cards[nextUndealtIndex()];
_cards[nextUndealtIndex()] = tmp;
dealNextCard();
return;
}
}
// If we are here, then we did not find the card as one of the cards
// to still be dealt. Must have already been dealt, so simply return.
return;
}
// nextUndealtIndex() is a private helper functions that calcuates the index
// of the next undealt card.
private int nextUndealtIndex() {
return 52-_num_left_to_deal;
}
}