-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.java
More file actions
executable file
·62 lines (52 loc) · 1.14 KB
/
Deck.java
File metadata and controls
executable file
·62 lines (52 loc) · 1.14 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
import java.util.ArrayList;
import java.util.List;
public class Deck
{
private Card[] cards;
private int size;
public Deck()
{
cards = new Card[52];
size = 52;
int pos = 0;
for (String suit : Card.suits)
for (String rank : Card.ranks)
{
cards[pos] = new Card(suit, rank);
pos++;
}
}
public void shuffle()
{
for (int k = size - 1; k > 0; k--) {
int howMany = k + 1;
int randPos = (int) (Math.random() * howMany);
Card temp = cards[k];
cards[k] = cards[randPos];
cards[randPos] = temp;
}
}
public Card deal()
{
Card rtn = cards[size - 1];
cards[size - 1] = null;
size--;
return rtn;
}
public String toString()
{
String rtn = "";
for (int i=0; i<size; i++)
rtn += "\n" + cards[i].toString();
rtn += "\n\n" + size;
return rtn;
}
public boolean isEmpty()
{
return size == 0;
}
public int getSize()
{
return size;
}
}