-
Notifications
You must be signed in to change notification settings - Fork 0
/
Casino.java
321 lines (294 loc) · 10.2 KB
/
Casino.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
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
package blackjack_verkort_tentamen_rob_biemans;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Casino {
// Declarations
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private Scanner sc = new Scanner(System.in);
private String input;
private boolean finished;
private boolean print;
private int hand;
private Blackjack b;
private Player p;
private Dealer d;
// Constructor
public Casino () {
d = new Dealer();
p = new Player();
}
public void play() {
b = new Blackjack(p,d);
setPrint(true);
((Player)p).createHands();
((Dealer)d).createHands();
setFinished(false);
while(!finished) {
if(finished == false) {
checkInput();
if (p.getBet() > p.getMoney()) {
finished = true;
}
}
}
askPlayAgain();
}
// Validate input with each hand
public String checkInput() {
int inactiveHands = 0;
if (print == true) {
b.printGame();
}
for (int i = 0; i < ((Player)p).getHanden().size(); i++) {
if (((Player)p).getHandenInfo().get(i).get(0).equals(" ") || ((Player)p).getHandenInfo().get(i).get(0).equals("Dubbel")) {
System.out.print(((Player)p).getName()+", wat wil je doen met hand "+(i+1)+" ? ");
try {
input = br.readLine().toLowerCase();
input = input.trim();
switch(input) {
case "p":
b.passHand(i);
System.out.println();
break;
case "d":
b.addCard(i);
break;
case "2":
if (((Player)p).getBet() > (((Player)p).getMoney()/getHand()/2)){
System.out.println("Je hebt te weinig geld om te doublen!");
break;
} else {
b.doubleBet(i);
break;
}
case "s":
System.out.println("Tot de volgende keer " + p.getName() + "!");
System.exit(0);
break;
default:
System.out.println();
System.out.println("Je kan alleen {P}, {D}, {2} of {S} invoeren!");
setPrint(false);
checkInput();
break;
}
} catch (IOException e) {
System.out.println("ERROR 5: " + e);
}
} else {
inactiveHands+=1;
}
}
// If all hands are inactive, its the dealers turn
if (inactiveHands == ((Player)p).getHanden().size()) {
setPrint(false);
askDealerToContinue();
}
// if all hands are inactive incl dealer is finished
if (inactiveHands == ((Player)p).getHanden().size() && d.getFinished() == true) {
checkWonBets();
}
return input;
}
public void checkWonBets() {
for (int i = 0; i < ((Player)p).getHanden().size(); i++) {
int dealerpoints = d.getPoints();
int pHandPoints = ((Player)p).calcPointsOfHand(i);
try {
// draw
if (((Player)p).getHandenInfo().get(i).get(0) == "Blackjack" && dealerpoints == 21 && ((Dealer)d).getHanden().get(0).size() == 2) {
((Player) p).setMoney(((Player) p).getMoney()-Integer.parseInt(((Player)p).getHandenInfo().get(i).get(1)));
System.out.println("1 "+((Player)p).getName() +", je wint hand "+ (i+1) +" met inzet van "+((Player)p).getHandenInfo().get(i).get(1));
}
// x2.5 bet
else if (((Player)p).getHandenInfo().get(i).get(0) == "Blackjack" && dealerpoints == 21 && ((Dealer)d).getHanden().get(0).size() != 2) {
((Player) p).setMoney(((Player) p).getMoney()+(Integer.parseInt(((Player)p).getHandenInfo().get(i).get(1))*2.5));
System.out.println("2 "+((Player)p).getName() +", je wint hand "+ (i+1) +" met inzet van "+(Integer.parseInt(((Player)p).getHandenInfo().get(i).get(1))*2.5));
}
// -bet | player hand is death
else if (((Player)p).getHandenInfo().get(i).get(0) == "Dood" || pHandPoints > 21){
((Player) p).setMoney(((Player) p).getMoney()-Integer.parseInt(((Player)p).getHandenInfo().get(i).get(1)));
System.out.println("3 "+((Player)p).getName() +", je verliest hand "+ (i+1) +" met inzet van "+((Player)p).getHandenInfo().get(i).get(1));
}
// x2 bet | dealer hand is death OR player has more points than dealer
else if (dealerpoints > 21 || pHandPoints > dealerpoints) {
((Player) p).setMoney(((Player) p).getMoney()+(Integer.parseInt(((Player)p).getHandenInfo().get(i).get(1))*2));
System.out.println("4 "+((Player)p).getName() +", je wint hand "+ (i+1) +" met inzet van "+(Integer.parseInt(((Player)p).getHandenInfo().get(i).get(1))*2));
}
// -bet
else if (pHandPoints < dealerpoints && dealerpoints <= 21) {
((Player) p).setMoney(((Player) p).getMoney()-Integer.parseInt(((Player)p).getHandenInfo().get(i).get(1)));
System.out.println("5 "+((Player)p).getName() +", je verliest hand "+ (i+1) +" met inzet van "+((Player)p).getHandenInfo().get(i).get(1));
}
// -bet Dealer got blackjack and player don't.
else if (dealerpoints == 21 && ((Dealer)d).getHanden().get(0).size() == 2 && ((Player)p).getHandenInfo().get(i).get(0) != "Blackjack") {
((Player) p).setMoney(((Player) p).getMoney()-Integer.parseInt(((Player)p).getHandenInfo().get(i).get(1)));
System.out.println("6 "+((Player)p).getName() +", je verliest hand "+ (i+1) +" met inzet van "+((Player)p).getHandenInfo().get(i).get(1));
}
// draw
else if(pHandPoints == dealerpoints && dealerpoints <= 21) {
((Player) p).setMoney(((Player) p).getMoney()-Integer.parseInt(((Player)p).getHandenInfo().get(i).get(1)));
System.out.println("7 "+((Player)p).getName() +", je wint hand "+ (i+1) +" met inzet van "+((Player)p).getHandenInfo().get(i).get(1));
}
} catch (Exception e) {
System.out.println("ERROR 6: "+ e);
}
dealerpoints = 0;
}
System.out.println();
System.out.println("Je kapitaal is nu: "+((Player) p).getMoney() + " euro.");
if (((Player) p).getMoney() <= 0) {
askPlayAgain();
} else {
askToContinue();
}
}
public void askToContinue() {
System.out.println("Wil je nog een keer spelen (j/n)?");
String input = sc.nextLine().toLowerCase();
if(input.equals("j")) {
p.clearHands();
d.clearHands();
((Player)p).resetHandInfo();
((Dealer)d).resetHandInfo();
setHand();
} else if (input.equals("n")) {
System.out.println("Tot de volgende keer!");
System.exit(0);
} else {
System.out.println("Dat begrijp ik niet.");
askToContinue();
}
}
public void askDealerToContinue() {
System.out.print("Druk op ENTER om de deler te laten spelen...");
try {
sc.nextLine();
b.dealerTurn();
} catch (Exception e) {
System.out.println("ERROR 0: " + e);
}
}
// Check if player wants to play again
public void askPlayAgain() {
if (((Player) p).getMoney() <= -1){
System.out.println("Je bent geëindigd op " + ((Player) p).getMoney() + " euro. Je kan niet meer verder spelen en je moet de schuld terug betalen door in de keuken te gaan werken.");
} else {
System.out.println("Helaas, je hebt niet genoeg geld meer om door te spelen. Je bent geëindigd op " + ((Player) p).getMoney() + " euro");
}
System.out.println("Wil je opnieuw beginnen spelen (j/n)?");
String input = sc.nextLine().toLowerCase();
if(input.equals("j")) {
((Player)p).setMoney(1000);
System.out.println("..START NIEUW SPEL..");
resetFinished();
askName();
} else if (input.equals("n")) {
System.out.println("Tot de volgende keer!");
System.exit(0);
} else {
System.out.println("Dat begrijp ik niet.");
askPlayAgain();
}
}
// Show possible commands
public void showCommands() {
System.out.println();
System.out.println("Commando's");
System.out.println("-------------");
System.out.println("p = passen");
System.out.println("d = draaien");
System.out.println("2 = inzet verdubbelen");
System.out.println("s = stoppen met spelen");
System.out.println();
}
// Asks for name
public void askName() {
System.out.print("Wat is je naam? ");
try {
input = br.readLine();
input = input.trim();
validateLetters(input);
} catch (IOException e) {
System.out.println("ERROR 1: " + e);
}
if (validateLetters(input) != false && input.length() > 1) {
p.setName(input);
System.out.println("Welkom " + input +". Je startkapitaal is 1000 euro");
setHand();
} else {
System.out.println("ERROR 2: Een naam bestaat alleen uit letters en/of spaties!");
askName();
}
}
// Validate possible name
private boolean validateLetters(String txt) {
String regx = "^[\\p{L} ']+$";
Pattern pattern = Pattern.compile(regx,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(txt);
return matcher.find();
}
// Mutators
public void setHand() {
System.out.print("Met hoeveel handen wil je spelen (1-5)? ");
try {
hand = Integer.parseInt(br.readLine());
} catch(Exception e) {
System.out.println("Dat aantal ligt niet tussen 1 en 5.");
setHand();
}
if (hand >= 1 && hand <= 5) {
((Player)p).setHand(hand);
setBet();
} else {
setHand();
}
}
public void setBet() {
int bet = 0;
System.out.print("Met welke inzet wil je spelen (1-"+((Player)p).getMoney()/getHand()+")? ");
try {
bet = Integer.parseInt(br.readLine());
((Player)p).setBet(1000);
} catch(Exception e) {
System.out.println("Dat aantal ligt niet tussen 1 en "+((Player)p).getMoney()/getHand());
setBet();
}
if (bet > ((Player)p).getMoney()/getHand()) {
System.out.println("Inzet mag niet groter zijn dan "+ ((Player)p).getMoney()/getHand());
setBet();
} else if (bet >= 1 && bet <= ((Player)p).getMoney()/getHand()) {
((Player)p).setBet(bet);
play();
} else {
setBet();
}
}
public void resetFinished() {
p.clearHands();
d.clearHands();
((Player)p).resetHandInfo();
((Dealer)d).resetHandInfo();
setFinished(false);
}
// Accessors
public int getHand() {
return hand;
}
public void setPrint(boolean value) {
print = value;
}
public boolean getPrint() {
return print;
}
public boolean getFinished() {
return finished;
}
public void setFinished(boolean value) {
finished = value;
}
}