-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.java
More file actions
73 lines (61 loc) · 1.22 KB
/
Copy pathDeck.java
File metadata and controls
73 lines (61 loc) · 1.22 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
/**
* This class defines a deck of playing cards
* @author Titi Gu
*
*/
public class Deck
{
Card[] deck;
private int cardsUsed;
public Deck()
{
deck = new Card[52];
for (int i = 0; i < deck.length; i++)
deck[i] = new Card(new Suit(i / 13 + 1),
new Pips(i % 13 + 1));
cardsUsed = 0;
}
/*
* Shuffle deck of cards
*/
public void shuffle()
{
for (int i = 0; i < deck.length; i++)
{
int k = (int)(Math.random() * 52);
Card t = deck[i];
deck[i] = deck[k];
deck[k] = t;
}
for(int i = 0; i < deck.length; i++)
deck[i].setDiscarded(false);
cardsUsed = 0;
}
/*
* This function returns the number of cards that are still left in the deck
*/
public int cardsLeft()
{
return 52 - cardsUsed;
}
/*
* Deals one card from the deck and returns it
*/
public Card dealCard()
{
if (cardsUsed == 52)
shuffle();
cardsUsed++;
return deck[cardsUsed - 1];
}
public String toString()
{
String t = "";
for (int i = 0; i < 52; i++)
if ( (i + 1) % 5 == 0)
t = t + deck[i] + "\n";
else
t = t + deck[i];
return t;
}
}