-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoker.java
More file actions
187 lines (163 loc) · 3.78 KB
/
Copy pathPoker.java
File metadata and controls
187 lines (163 loc) · 3.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
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
import java.util.Scanner;
/**
* This class defines current poker game
* @author Titi Gu
*
*/
public class Poker
{
private static Scanner kb = new Scanner(System.in);
private static Hand hand;
private static Deck deck;
private static int money;
public static void main(String[] args)
{
String handValue;
deck = new Deck();
hand = new Hand();
money = 20; //Given $20 pocket money
System.out.println("Let's play Poker!\n");
while(true)
{
//Shuffle deck and deal the hand
deck.shuffle();
for (int i = 0; i<5; i++)
hand.setCard(i, deck.dealCard());
//Print Hand
System.out.println("Your Current Hand Is:");
System.out.println(hand);
//Discarding
doDiscard();
//Show final hand
System.out.println("\nYour Final Hand Is:");
System.out.println(hand);
System.out.println();
//Display hand value
handValue = getHandValue();
System.out.println("Hand Value: " + handValue);
//Display money
money += updateMoney(handValue);
System.out.println("Money: $" + money);
//Play Again?
if(money < 1){
System.out.println("You are out of money.");
break;
} else {
System.out.print("Do you want to play again? (y/n) : ");
if(getOption().equals("N"))
break;
}
}
}
/*
* Update pocket money
*/
private static int updateMoney(String handValue)
{
if(handValue.equals("Royal Flush"))
return 250;
if(handValue.equals("Straight Flush"))
return 50;
if(handValue.equals("Four of a Kind"))
return 25;
if(handValue.equals("Full House"))
return 9;
if(handValue.equals("Flush"))
return 6;
if(handValue.equals("Straight"))
return 4;
if(handValue.equals("Three of a Kind"))
return 3;
if(handValue.equals("Two Pair"))
return 2;
if(handValue.equals("Pair"))
return 1;
if(handValue.equals("Nothing"))
return -1;
return -1;
}
/*
* Check combinations of current hand
*/
private static String getHandValue(){
if(hand.isRoyalFlush() == true)
return "Royal Flush";
if(hand.isStraightFlush() == true)
return "Straight Flush";
if(hand.isFourOfAKind() == true)
return "Four of a Kind";
if(hand.isFullHouse() == true)
return "Full House";
if(hand.isFlush() == true)
return "Flush";
if(hand.isStraight() == true)
return "Straight";
if(hand.isThreeOfAKind() == true)
return "Three of a Kind";
if(hand.isTwoPair() == true)
return "Two Pair";
if(hand.isPair() == true)
return "Pair";
return "Nothing";
}
/*
* Discard a card
*/
private static void doDiscard()
{
int discard = -1;
System.out.println("Enter the numbers of the cards you wish to discard.");
System.out.println("Entering the number of a discarded card retrieves it.");
System.out.println("Enter 0 to stop discarding.");
while(discard != 0)
{
discard = getDiscard();
if(discard == 0)
break;
hand.discardCard(discard - 1);
System.out.println(hand);
System.out.println("Select another card or 0 to complete the discard.");
}
hand.replaceDiscarded(deck);
}
/*
* Return discarded card number
*/
private static int getDiscard()
{
String input;
int num = -1;
while(num < 0 || num > 5)
{
try {
input = kb.nextLine();
num = Integer.parseInt(input);
if(num < 0 || num > 5)
throw new NumberFormatException();
} catch(NumberFormatException ex){
System.out.println("- Enter a valid integer from 0 to 5");
}
}
return num;
}
/*
* Pop player playing next round
*/
private static String getOption()
{
String option;
while(true)
{
option = kb.nextLine();
option = option.toUpperCase();
if(option.equals("Y") || option.equals("N"))
{
return(option);
}
else
{
System.out.println("- Enter a valid option: (y/n)");
}
}
}
}