-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck3.java
93 lines (79 loc) · 1.75 KB
/
Deck3.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
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
/**
* Representation of a Deck of cards.
* Initialized to a standard 52 card deck.
*
* @author Jackie Horton
*/
import java.util.Random;
import java.util.ArrayList;
public class Deck3 extends CardPile
{
/**
NUMBER of cards in standard deck {@value #CARDS_IN_DECK}
**/
final int CARDS_IN_DECK = 52;
/**
* Constructs a regular 52-card deck. Initially, the cards
* are in a sorted order. The shuffle() method can be called to
* randomize the order.
*/
public Deck3()
{
super();
freshDeck();
}
/**
* Create a new collection of 52 cards, in sorted order
*/
public void freshDeck()
{
for (int r = Card.ACE; r<=Card.KING;r++)
{
for (int s=Card.SPADES;s<=Card.DIAMONDS;s++)
{
add(new Card(s,r));
}
}
}
/**
* Remove and return the top Card on the Deck
* @return A reference to a Card that was top on the Deck
*/
public Card dealCard()
{
Card c = remove(0); // remove it (returns removed object)
return c;
}
/**
* Return current number of Cards in Deck
* @return number of Cards in Deck
*/
public int cardsRemaining()
{
return size();
}
/**
* Randomize the order of Cards in Deck
*/
public void shuffle()
{
int randNum;
Card temp;
Random r = new Random();
for (int i = 0; i < size(); i++)
{
randNum = r.nextInt(size());
temp = get(i);
set(i,get(randNum));
set(randNum,temp);
}
}
/**
* Determine if Deck is empty
* @return true if there are no more cards, false otherwise
*/
public boolean isEmpty()
{
return (size() == 0);
}
}