|
@@ -0,0 +1,142 @@
|
|
1
|
+package io.zipcoder.casino;
|
|
2
|
+
|
|
3
|
+import com.google.common.annotations.VisibleForTesting;
|
|
4
|
+
|
|
5
|
+public class HighLow extends DiceGame{
|
|
6
|
+ private Player player;
|
|
7
|
+ private boolean isPlaying = true;
|
|
8
|
+ private int roll;
|
|
9
|
+ private int betAmount;
|
|
10
|
+ private String bet;
|
|
11
|
+
|
|
12
|
+ // how do I get a player?
|
|
13
|
+ /*\
|
|
14
|
+ You can get a player through calling the constructor.
|
|
15
|
+ Pass the player through the constructor and set the field.
|
|
16
|
+
|
|
17
|
+ new
|
|
18
|
+ */
|
|
19
|
+
|
|
20
|
+ public HighLow (Player player) {
|
|
21
|
+ this.player = player;
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ public void play() {
|
|
25
|
+ betAmount = getBetAmount();
|
|
26
|
+ bet = makeBet();
|
|
27
|
+ roll = rollDice();
|
|
28
|
+ declareWinner();
|
|
29
|
+ checkIfContinue(askToContinue());
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ public void declareWinner () {
|
|
33
|
+ switch(bet) {
|
|
34
|
+ case "h":
|
|
35
|
+ if (roll > 7) {
|
|
36
|
+ winBet();
|
|
37
|
+ } else {
|
|
38
|
+ loseBet();
|
|
39
|
+ };
|
|
40
|
+ break;
|
|
41
|
+ case "l":
|
|
42
|
+ if (roll < 7) {
|
|
43
|
+ winBet();
|
|
44
|
+ } else {
|
|
45
|
+ loseBet();
|
|
46
|
+ };
|
|
47
|
+ break;
|
|
48
|
+ case "7":
|
|
49
|
+ if (roll == 7) {
|
|
50
|
+ win1to4();
|
|
51
|
+ } else {
|
|
52
|
+ loseBet();
|
|
53
|
+ };
|
|
54
|
+ break;
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ public int getBetAmount () {
|
|
59
|
+ betAmount = Console.numberFromString(Console.askForInput("How much would you like to bet?"));
|
|
60
|
+ player.subtractFromBankroll(betAmount);
|
|
61
|
+ return betAmount;
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public String makeBet () {
|
|
65
|
+ bet = Console.askForInput("What are you betting on? (H / L / 7)");
|
|
66
|
+ if (!bet.equals("h") && !bet.equals("l") && !bet.equals("7")) {
|
|
67
|
+ Console.output("Wrong input. Please try again");
|
|
68
|
+ makeBet();
|
|
69
|
+ }
|
|
70
|
+ return bet;
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ public int rollDice () {
|
|
74
|
+ Dice dice = new Dice(2);
|
|
75
|
+ roll = dice.getSum();
|
|
76
|
+ //Console.askForInput("Please press Enter to roll the dice!");
|
|
77
|
+ Console.output("You rolled " + roll);
|
|
78
|
+ return roll;
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ public String askToContinue() {
|
|
82
|
+ String input = Console.askForInput("Would you like to continue? (Y / N)");
|
|
83
|
+ if (!input.equals("y") && !input.equals("n")) {
|
|
84
|
+ Console.output("Wrong input. Please try again");
|
|
85
|
+ askToContinue();
|
|
86
|
+ }
|
|
87
|
+ return input;
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ public void checkIfContinue (String state) {
|
|
91
|
+ //boolean output = false;
|
|
92
|
+ switch (state) {
|
|
93
|
+ case "n":
|
|
94
|
+ //isPlaying = false;
|
|
95
|
+ endGame();
|
|
96
|
+ break;
|
|
97
|
+ case "y":
|
|
98
|
+ //isPlaying = true;
|
|
99
|
+ play();
|
|
100
|
+ break;
|
|
101
|
+ }
|
|
102
|
+ //return output;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ public void winBet () {
|
|
106
|
+ player.addToBankroll(betAmount);
|
|
107
|
+ Console.output("You won " + betAmount);
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ public void win1to4 () {
|
|
111
|
+ player.addToBankroll(betAmount*4);
|
|
112
|
+ Console.output("You won " + betAmount*4);
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ public void loseBet () {
|
|
116
|
+ Console.output("You lost :(");
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ /*
|
|
120
|
+ You don't need to make a main method inside this method.
|
|
121
|
+ Instead, make a method that INITIATES the start of the game.
|
|
122
|
+ */
|
|
123
|
+
|
|
124
|
+ public void startGame() {
|
|
125
|
+ Console.output("You're playing High-Low dice game");
|
|
126
|
+ // while (isPlaying) {
|
|
127
|
+ play();
|
|
128
|
+ // }
|
|
129
|
+ // endGame();
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ public void endGame() {
|
|
133
|
+ Casino casino = new Casino();
|
|
134
|
+ //casino.doContinue();
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ public static void main (String[] args) {
|
|
138
|
+ Player player = new Player("Francis");
|
|
139
|
+ DiceGame app = new HighLow(player);
|
|
140
|
+ app.startGame();
|
|
141
|
+ }
|
|
142
|
+}
|