-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.java
More file actions
111 lines (93 loc) · 1.78 KB
/
Deck.java
File metadata and controls
111 lines (93 loc) · 1.78 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package Blackjack.src.main.java.model;
/**
* Creates a deck of cards
*
*
* @author Terin Champion
* @version 1.0
*/
public class Deck{
private static Card[] deck = new Card[52];
private int cardsLeft;
public Deck(){
deck = this.loadDeck();
this.shuffle();
}
/**
* Loads all cards into the deck
*
* @returns card[]
*/
private Card[] loadDeck(){
for(int index = 0 ; index < deck.length ; index++){
Card load;
if(index < 26)
if(index > 12)
load = new Card(1,index-13);
else
load = new Card(0,index);
else
if(index < 39)
load = new Card(2,index-26);
else
load = new Card(3,index-39);
deck[index] = load;
}
cardsLeft = 52;
return deck;
}//end of load
/**
* Shuffles the deck of cards.
*/
public void shuffle(){
for(int i = 0 ; i < 500 ; i++){
for(int card = 0 ; card < 51 ; card++){
int swap = (int) (Math.random() * 52);
Card temp = deck[card];
deck[card] = deck[swap];
deck[swap] = temp;
}
}
cardsLeft = 52;
}//end of shuffle
/**
* Get card info from index i
* @param Array index i
* @return Card at index i
*/
public Card getCard(int i){
return deck[i];
}
/**
* Deals the next card in the deck
*
* @return Card
*/
public Card deal(){
Card delt = deck[cardsLeft-1];
cardsLeft--;
if(cardsLeft == 0)
shuffle();
return delt;
}
/**
* Returns the number of cards left in the deck
*
* @return int cardsLeft
*/
public int cardsLeft(){
return cardsLeft;
}
/**
* String representation of the deck of cards
*
* @return String
*/
public String toString(){
String name = "";
for(int card = 0 ; card < 52 ; card++){
name += deck[card].toString();
}
return name;
}
}