-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRun.java
251 lines (208 loc) · 7.08 KB
/
Run.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
/**
* @author AlexBeard, NishaChaube, SimonAnguish
*
* This has all of the deck/hand methods, including:
* -transferring between hands
* -initialization
* -checking if hand has any of a symbol
* transferring all cards of a certain symbol
* etc
* ------------------------------------------
* Work Distribution:
*
* This class was initially written by Simon and Nisha, with Alex doing general bug fixing
* (mainly to clearCardsBySymbol and getCardCountBySymbol)
* and several methods added (getHandSize and printHand)
*/
public class Run {
public static HashMap<String, Integer> deck = new HashMap<String, Integer>();
public static HashMap<String, Integer> player_hand = new HashMap<String, Integer>();
public static HashMap<String, Integer> computer_hand = new HashMap<String, Integer>();
/**
initializeDeck
Creates a full "deck", interpreted as a HashMap of "A"-"K" with value:4
*/
static void initializeDeck() {
for (int i=1;i<14;i++) {
deck.put(determineSymbolFromInt(i), 4);
}
}
/**
shuffleDeckToHands
Shuffles the deck into the player and computers hands.
Removes the card from the deck once it is shuffled.
*/
static void shuffleDeckToHands() {
String card_symbol;
for (int i=0;i<7;i++) {
card_symbol = getRandomCardSymbol(deck);
reduceCardBySymbol(card_symbol, deck);
dealCardBySymbol(card_symbol, player_hand);
card_symbol = getRandomCardSymbol(deck);
reduceCardBySymbol(card_symbol, deck);
dealCardBySymbol(card_symbol, computer_hand);
}
}
/**
transferCards
Moves the cards from the first hand to the second hand
@param card_symbol A string interpretation of a card
@param out_hand the hand to take the cards from
@param in_hand the hand to put the cards into
*/
public static void transferCards(String card_symbol, HashMap<String, Integer> out_hand, HashMap<String, Integer> in_hand) {
reduceCardBySymbol(card_symbol, out_hand);
addCardsToHandBySymbol(card_symbol, in_hand);
}
/**
* gets hand size - used to get computers hand size
* Method was needed because keyList ignores multiple cards of same rank
* @param hand
* @return size of hand
*/
public static int getHandSize(HashMap<String, Integer> hand) {
int size = 0;
for (String value : hand.keySet()) {
size += hand.get(value);
}
return size;
}
/**
Prints cards from a specified hand
Method was needed because keyList ignores multiple cards of same rank
@param hand
*/
public static void printHand(HashMap<String, Integer> hand) {
//horizontal print
for (String value : hand.keySet()) {
for (int i = 0; i < hand.get(value); i++) {
System.out.print(value + " ");
}
}
System.out.println("");
}
/**
getCardCountBySymbol
@param card_symbol A string interpretation of a card
@param hand the specific hand to add the cards to
@return the int count of the specified card symbol
*/
public static int getCardCountBySymbol(String card_symbol, HashMap<String, Integer> hand) {
return hand.get(card_symbol);
}
/**
addCardsToHandBySymbol
@param card_symbol A string interpretation of a card
@param card_count the amount of a certain card to add to the hand
@param hand the specfic hand to add the cards to
*/
public static void addCardsToHandBySymbol(String card_symbol, HashMap<String, Integer> hand) {
int current_card_count;
if (hand.get(card_symbol) == null) {
current_card_count = 0;
}
else {
current_card_count = hand.get(card_symbol);
}
hand.put(card_symbol, current_card_count + 1);
}
/**
hasCardBySymbol
Sees if a hand has a card by the symbol
@param card_symbol A string interpretation of a card
@param hand The specific hand to check
@return Boolean if the card is in the hand
*/
public static boolean hasCardBySymbol(String card_symbol, HashMap<String, Integer> hand) {
return hand.containsKey(card_symbol);
}
/**
cardsRemainCount
calculates the total cards left
@param hand Specific hashmap to check
@return total cards remaining
*/
public static int cardsRemainCount(HashMap<String, Integer> hand) {
int sum = 0;
for (float value : hand.values()) {
sum += value;
}
return sum;
}
/**
clearCardsBySymbol
Removes all cards from a specified hand of a specified symbol
@param card_symbol a string interpretation of a card type
@param hand a hand to remove the cards from
*/
public static void clearCardsBySymbol(String card_symbol, HashMap<String, Integer> hand) {
hand.put(card_symbol, 0);
hand.remove(card_symbol);
}
/**
getRandomCardSymbol
generates a random string interpretation of a card
@return a random card symbol in String form
*/
public static String getRandomCardSymbol(HashMap<String, Integer> hand) {
List<String> keyList = new ArrayList<String>(hand.keySet());
int randomIndex = new Random().nextInt(keyList.size());
String randomKey = keyList.get(randomIndex);
return randomKey;
}
/**
reduceCardBySymbol
Reduces the count of the specified card in the deck by one.
Essentially the first half of dealing a card, before it is places in a hand.
@param card_symbol A string interpretation of a card
*/
private static void reduceCardBySymbol(String card_symbol, HashMap<String, Integer> remove_from) {
int card_count = remove_from.get(card_symbol);
remove_from.put(card_symbol, card_count-1);
if (remove_from.get(card_symbol) == 0) {
remove_from.remove(card_symbol);
}
}
/**
dealCardBySymbol
The second half of dealing a card.
Increases the count of a specified symbol in a specified hand by 1
@param card_symbol a string interpretation of a card value
@param hand the hand to increase the value of the card in
*/
private static void dealCardBySymbol(String card_symbol, HashMap<String, Integer> hand) {
if(hand.containsKey(card_symbol)==true)
{
int card_count = hand.get(card_symbol);
hand.put(card_symbol, card_count+1);
}
else
hand.put(card_symbol, 1);
}
/**
determineSymbolFromInt
Determines the symbol for the given card value.
@param card_value a set value within a certain range (1-13)
@return The string notation of the integer value
*/
public static String determineSymbolFromInt(int card_value) {
String card_symbol;
switch (card_value) {
case 1: card_symbol = "A";
break;
case 11: card_symbol = "J";
break;
case 12: card_symbol = "Q";
break;
case 13: card_symbol = "K";
break;
default: card_symbol = Integer.toString(card_value);
break;
}
return card_symbol;
}
}