-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.java
67 lines (55 loc) · 1.7 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
58
59
60
61
62
63
64
65
66
67
/* ****************************************
* Name: Justin Bisignano
* COP 3330 – Spring 2013
* Program 3: Cards - Deck Handler
* Date: March 1, 2013
* **************************************** */
import java.util.Random;
public class Deck {
private static final int NUM_CARDS = 52;
private static final Random randomNumbers = new Random();
private int[] deck;
// the index of the current position in the deck
private int cardIndex = 0;
// Creates a new shuffled deck of cards
public Deck() {
// Initialize a new deck of cards
deck = new int[NUM_CARDS];
// Populate the deck in order
for(int i = 0; i < deck.length; i++)
deck[i] = i;
// Shuffle the deck
int temp, swapIndex;
for(int i = 0; i < deck.length; i++){
// Select a random index from 0 to NUM_CARDS-1
swapIndex = randomNumbers.nextInt(NUM_CARDS);
// Swap the numbers
temp = deck[i];
deck[i] = deck[swapIndex];
deck[swapIndex] = temp;
}
}
/**
*
* @return Returns the int representation of the current card and increments cardIndex.
* If there are no more cards to be dealt, this returns -1
*/
public int dealCard(){
if(cardIndex < deck.length)
return deck[cardIndex++];
else
return -1;
}
/**
* @param card
* The int representation of a card to be named.
* @return
* The the spoken name of the given card, e.g. 0 would return "Two of Clubs"
*/
public String cardName(int card){
// Names are in ranked order from least-value to greatest.
String[] name = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
String[] suit = {"Clubs", "Spades", "Diamonds", "Hearts"};
return name[card%13]+" of "+suit[card/13];
}
}