-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
358 lines (313 loc) · 11.2 KB
/
Copy pathMain.java
File metadata and controls
358 lines (313 loc) · 11.2 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import java.util.*; // importing all libraries
public class Main {
private static final List<String> ranks = Arrays.asList("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A");
private static final List<String> suits = Arrays.asList("♣", "♦", "♥", "♠");
public static void main(String[] args) {
Game poker = new Game();
poker.play();
}
}
class Game {
private static final List<String> ranks = Arrays.asList("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A");
private static final List<String> suits = Arrays.asList("♣", "♦", "♥", "♠");
private List<String> deck;
private List<String> playerHand;
private Random random;
public Game() {
random = new Random();
deck = generateDeck();
playerHand = dealHand();
}
public void play() {
System.out.println("Welcome to the Poker Game!");
int tokens = 10; // initial amount of tokens given
while (tokens > 0) {
System.out.println("\nTokens: " + tokens); // new line + telling amount of tokens
System.out.println("P - Play a New Hand");
System.out.println("Q - Quit Game");
Scanner scanner = new Scanner(System.in); // getting input
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("q")) { // quit game
System.out.println("Thanks for playing!");
break;
} else if (choice.equalsIgnoreCase("p")) { // play game
// Reset player hand and deck to stop hands from repeating
playerHand.clear();
deck = generateDeck();
playerHand = dealHand(); // random 5 cards
System.out.println("Your hand: ");
showCards(playerHand); // showing the hand
System.out.println("Enter the position of the cards you want to discard (1-5), or enter '0' to skip:");
String[] discardIndices = scanner.nextLine().split(" ");
List<String> discardedCards = new ArrayList<>(); // array list of indices that that user wants to discard
for (String index : discardIndices) {
int discardIndex = Integer.parseInt(index) - 1; // -1 because the indices range from 0-4, not 1-5
if (discardIndex >= 0 && discardIndex < 5) {
discardedCards.add(playerHand.get(discardIndex));
}
}
for (String card : discardedCards) {
playerHand.remove(card); // discarding the cards
deck.add(card); // adding the discarded card to the deck
}
if (discardedCards.size() > 0) {
for (int i = 0; i < discardedCards.size(); i++) {
int randomIndex = random.nextInt(deck.size());
String newCard = deck.remove(randomIndex);
playerHand.add(newCard);
}
}
System.out.println("Your new hand: ");
showCards(playerHand);
String handRanking = getHandRanking(playerHand);
int tokensWon = calculateTokensWon(handRanking);
System.out.println("Poker Hand: " + handRanking);
System.out.println("Tokens Won: " + tokensWon);
tokens += tokensWon;
} else {
System.out.println("Invalid choice! Please try again.");
}
}
}
private List<String> generateDeck() {
List<String> deck = new ArrayList<>();
for (String rank : ranks) {
for (String suit : suits) {
deck.add(rank + " of " + suit);
}
}
return deck;
}
private List<String> dealHand() {
List<String> hand = new ArrayList<>();
for (int i = 0; i < 5; i++) {
int randomIndex = random.nextInt(deck.size());
String card = deck.remove(randomIndex);
hand.add(card);
}
return hand;
}
private void showCards(List<String> cards) {
for (String card : cards) {
int index = cards.indexOf(card);
drawCard(card, index);
}
}
private void drawCard(String card, int index) {
String[] parts = card.split(" ");
String rank = parts[0];
String suit = parts[2];
System.out.println(".------.");
if (rank.equals("10")) { // spacing messed up because 10 is two characters
System.out.println("|" + rank + " |");
} else {
System.out.println("|" + rank + " |");
}
System.out.println("| |");
System.out.println("| " + suit.charAt(0) + " |" + " ------ " + String.valueOf(index+1));
System.out.println("| |");
if (rank.equals("10")) { // spacing messed up because 10 is two characters
System.out.println("| " + rank + "|");
} else {
System.out.println("| " + rank + "|");
}
System.out.println("'------'");
}
private String getHandRanking(List<String> hand) {
if (isRoyalFlush(hand)) {
return "Royal Flush";
} else if (isStraightFlush(hand)) {
return "Straight Flush";
} else if (isFourOfAKind(hand)) {
return "Four of a Kind";
} else if (isFullHouse(hand)) {
return "Full House";
} else if (isFlush(hand)) {
return "Flush";
} else if (isStraight(hand)) {
return "Straight";
} else if (isThreeOfAKind(hand)) {
return "Three of a Kind";
} else if (isTwoPair(hand)) {
return "Two Pair";
} else if (isOnePair(hand)) {
return "One Pair";
} else {
return "High Card (Tokens Lost!)";
}
}
private boolean isRoyalFlush(List<String> hand) {
// Check if the hand has A, K, Q, J, 10 of the same suit
List<String> ranksToCheck = Arrays.asList("A", "K", "Q", "J", "10");
for (String suit : suits) {
boolean hasAllRanks = true;
for (String rank : ranksToCheck) {
if (!hand.contains(rank + " of " + suit)) {
hasAllRanks = false;
break;
}
}
if (hasAllRanks) {
return true;
}
}
return false;
}
private boolean isStraightFlush(List<String> hand) {
// Check if the hand has any five consecutive cards of the same suit
for (String suit : suits) {
List<String> sameSuitCards = new ArrayList<>();
for (String card : hand) {
if (card.endsWith(suit)) {
sameSuitCards.add(card);
}
}
if (sameSuitCards.size() >= 5) {
List<String> sortedRanks = sortRanks(sameSuitCards);
if (isConsecutiveRanks(sortedRanks)) {
return true;
}
}
}
return false;
}
private boolean isFourOfAKind(List<String> hand) {
// Check if the hand has four cards of the same rank
for (String rank : ranks) {
int count = 0;
for (String card : hand) {
if (card.startsWith(rank)) {
count++;
}
}
if (count >= 4) {
return true;
}
}
return false;
}
private boolean isFullHouse(List<String> hand) {
// Check if the hand has three cards of one rank and two cards of another rank
boolean hasThreeOfAKind = false;
boolean hasPair = false;
for (String rank : ranks) {
int count = 0;
for (String card : hand) {
if (card.startsWith(rank)) {
count++;
}
}
if (count == 3) {
hasThreeOfAKind = true;
} else if (count == 2) {
hasPair = true;
}
}
return hasThreeOfAKind && hasPair;
}
private boolean isFlush(List<String> hand) {
// Check if the hand has any five cards of the same suit, not in consecutive order
for (String suit : suits) {
int count = 0;
for (String card : hand) {
if (card.endsWith(suit)) {
count++;
}
}
if (count >= 5) {
return true;
}
}
return false;
}
private boolean isStraight(List<String> hand) {
// Check if the hand has any five consecutive cards of mixed suits
List<String> sortedRanks = sortRanks(hand);
return isConsecutiveRanks(sortedRanks);
}
private boolean isThreeOfAKind(List<String> hand) {
// Check if the hand has three cards of the same rank
for (String rank : ranks) {
int count = 0;
for (String card : hand) {
if (card.startsWith(rank)) {
count++;
}
}
if (count >= 3) {
return true;
}
}
return false;
}
private boolean isTwoPair(List<String> hand) {
// Check if the hand has two sets of two cards of the same rank
int pairCount = 0;
for (String rank : ranks) {
int count = 0;
for (String card : hand) {
if (card.startsWith(rank)) {
count++;
}
}
if (count == 2) {
pairCount++;
}
}
return pairCount >= 2;
}
private boolean isOnePair(List<String> hand) {
// Check if the hand has two cards of the same rank
for (String rank : ranks) {
int count = 0;
for (String card : hand) {
if (card.startsWith(rank)) {
count++;
}
}
if (count >= 2) {
return true;
}
}
return false;
}
private List<String> sortRanks(List<String> hand) {
List<String> sortedRanks = new ArrayList<>(hand);
sortedRanks.sort(Comparator.comparingInt(ranks::indexOf));
return sortedRanks;
}
private boolean isConsecutiveRanks(List<String> ranks) {
for (int i = 1; i < ranks.size(); i++) {
int currentRankIndex = this.ranks.indexOf(ranks.get(i));
int previousRankIndex = this.ranks.indexOf(ranks.get(i - 1));
if (currentRankIndex != previousRankIndex + 1) {
return false;
}
}
return true;
}
private int calculateTokensWon(String handRanking) {
switch (handRanking) {
case "Royal Flush":
return 250;
case "Straight Flush":
return 50;
case "Four of a Kind":
return 25;
case "Full House":
return 9;
case "Flush":
return 6;
case "Straight":
return 4;
case "Three of a Kind":
return 3;
case "Two Pair":
return 2;
case "One Pair":
return 1;
default:
return -2;
}
}
}