|
@@ -0,0 +1,109 @@
|
|
1
|
+package io.zipcoder.casino;
|
|
2
|
+
|
|
3
|
+public class HighLow extends DiceGame{
|
|
4
|
+ private Player player;
|
|
5
|
+ private boolean isPlaying = true;
|
|
6
|
+ private int rolledDiceResult;
|
|
7
|
+ private int totalBet;
|
|
8
|
+ private String betCondition;
|
|
9
|
+ private int winnings;
|
|
10
|
+
|
|
11
|
+ public HighLow(Player player) {
|
|
12
|
+ this.player = player;
|
|
13
|
+ }
|
|
14
|
+
|
|
15
|
+ private int askForWager() {
|
|
16
|
+ int wager = 0;
|
|
17
|
+ boolean flag = true;
|
|
18
|
+ Console.output("Current bankroll: " + this.player.getBankroll());
|
|
19
|
+ while (flag) {
|
|
20
|
+ try {
|
|
21
|
+ wager = Console.askForInteger("How much would you like to bet?");
|
|
22
|
+ if (wager <= 0 || wager > this.player.getBankroll()) {
|
|
23
|
+ Console.output("Wrong input! Try again!");
|
|
24
|
+ } else {
|
|
25
|
+ flag = false;
|
|
26
|
+ }
|
|
27
|
+ } catch(NumberFormatException e) {
|
|
28
|
+ Console.output("Wrong input! Try again!");
|
|
29
|
+ }
|
|
30
|
+ }
|
|
31
|
+ this.player.subtractFromBankroll(wager);
|
|
32
|
+ return wager;
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ public String makeBet() {
|
|
36
|
+ betCondition = Console.askForInput("What are you betting on? (H / L / 7)");
|
|
37
|
+ if (betCondition.toLowerCase().equals("h") ||
|
|
38
|
+ betCondition.toLowerCase().equals("l") ||
|
|
39
|
+ betCondition.toLowerCase().equals("7")) {
|
|
40
|
+ return betCondition;
|
|
41
|
+ }
|
|
42
|
+ Console.output("Wrong input. Please try again");
|
|
43
|
+ return makeBet();
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public int rollDice() {
|
|
47
|
+ Dice dice = new Dice(2);
|
|
48
|
+ rolledDiceResult = dice.getSum();
|
|
49
|
+ Console.output("You rolled " + rolledDiceResult);
|
|
50
|
+ return rolledDiceResult;
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ public void declareWinner() {
|
|
54
|
+ if ((betCondition.toUpperCase().equals("H") && rolledDiceResult > 7) ||
|
|
55
|
+ (betCondition.toUpperCase().equals("L") && rolledDiceResult < 7) ||
|
|
56
|
+ (betCondition.toUpperCase().equals("7") && rolledDiceResult == 7)) {
|
|
57
|
+ winnings = rolledDiceResult == 7 ? this.totalBet * 4 : this.totalBet * 2;
|
|
58
|
+ winBet();
|
|
59
|
+ } else {
|
|
60
|
+ loseBet();
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public boolean askToContinue() {
|
|
65
|
+ if (this.player.getBankroll() == 0) {
|
|
66
|
+ Console.output("You're bankrupt! Please come back after pay day!");
|
|
67
|
+ return false;
|
|
68
|
+ }
|
|
69
|
+ String input = Console.askForInput("Would you like to continue? (Y / N)");
|
|
70
|
+ if (input.toLowerCase().equals("y")) {
|
|
71
|
+ return true;
|
|
72
|
+ } else if (input.toLowerCase().equals("n")) {
|
|
73
|
+ return false;
|
|
74
|
+ }
|
|
75
|
+ Console.output("Wrong input. Please try again");
|
|
76
|
+ return askToContinue();
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ public void winBet() {
|
|
80
|
+ player.addToBankroll(winnings);
|
|
81
|
+ Console.output("You won " + winnings);
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ public void loseBet() {
|
|
85
|
+ Console.output("You lost :(");
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ public void startGame() {
|
|
89
|
+ Console.output("You're playing High-Low dice game");
|
|
90
|
+ while (isPlaying) {
|
|
91
|
+ totalBet = askForWager();
|
|
92
|
+ betCondition = makeBet();
|
|
93
|
+ rolledDiceResult = rollDice();
|
|
94
|
+ declareWinner();
|
|
95
|
+ isPlaying = askToContinue();
|
|
96
|
+ }
|
|
97
|
+ // endGame();
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ public void endGame() {
|
|
101
|
+ //???
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ public static void main (String[] args) {
|
|
105
|
+ Player player = new Player("Francis");
|
|
106
|
+ DiceGame app = new HighLow(player);
|
|
107
|
+ app.startGame();
|
|
108
|
+ }
|
|
109
|
+}
|