This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.java
147 lines (131 loc) · 3.58 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.util.List;
import java.util.ArrayList;
/**
* The Deck class represents a shuffled deck of cards.
* It provides several operations including
* initialize, shuffle, deal, and check if empty.
*/
public class Deck {
/**
* cards contains all the cards in the deck.
*/
private List<Card> cards;
/**
* size is the number of not-yet-dealt cards.
* Cards are dealt from the top (highest index) down.
* The next card to be dealt is at size - 1.
*/
private int size;
/**
* Creates a new <code>Deck</code> instance.<BR>
* It pairs each element of ranks with each element of suits,
* and produces one of the corresponding card.
* @param ranks is an array containing all of the card ranks.
* @param suits is an array containing all of the card suits.
* @param values is an array containing all of the card point values.
*/
public Deck(String[] ranks, String[] suits, int[] values) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
cards = new ArrayList<Card>();
for(int i = 0; i<suits.length; i++){
for(int d = 0; d<ranks.length; d++){
Card d2 = new Card(ranks[d], suits[i], values[d]);
cards.add(d2);
}
}
size = cards.size();
shuffle();
}
/**
* Determines if this deck is empty (no undealt cards).
* @return true if this deck is empty, false otherwise.
*/
public boolean isEmpty() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
if(this.size == 0){
return true;
}
return false;
}
/**
* Accesses the number of undealt cards in this deck.
* @return the number of undealt cards in this deck.
*/
public int size() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
return size;
}
/**
* Randomly permute the given collection of cards
* and reset the size to represent the entire deck.
*/
public void shuffle() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */
for(int k = size - 1; k >= 0; k-- ) {
int r = (int)(Math.random() * k);
Card tmp = cards.get(r);
cards.set(r, cards.get(k));
cards.set(k, tmp);
/*
for (int k = cards.size() - 1; k > 0; k--) {
int howMany = cards.size();
int previousLotto = -1;
int lotto = (int) (Math.random() * howMany);
if(lotto != previousLotto){
Card lll = cards.get(k);
cards.set(k, cards.get(lotto));
cards.set(lotto, lll);
}
else{
k--;
continue;
}*/
}
size = cards.size();
}
/**
* Deals a card from this deck.
* @return the card just dealt, or null if all the cards have been
* previously dealt.
*/
public Card deal() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
if(size == 0)
return null;
else{
size--;
return cards.get(size);
}
}
/**
* Generates and returns a string representation of this deck.
* @return a string representation of this deck.
*/
@Override
public String toString() {
String rtn = "size = " + size + "\nUndealt cards: \n";
for (int k = size - 1; k >= 0; k--) {
rtn = rtn + cards.get(k);
if (k != 0) {
rtn = rtn + ", ";
}
if ((size - k) % 2 == 0) {
// Insert carriage returns so entire deck is visible on console.
rtn = rtn + "\n";
}
}
rtn = rtn + "\nDealt cards: \n";
for (int k = cards.size() - 1; k >= size; k--) {
rtn = rtn + cards.get(k);
if (k != size) {
rtn = rtn + ", ";
}
if ((k - cards.size()) % 2 == 0) {
// Insert carriage returns so entire deck is visible on console.
rtn = rtn + "\n";
}
}
rtn = rtn + "\n";
return rtn;
}
}