yauhenip пре 6 година
родитељ
комит
81a0a86574

+ 6
- 0
src/main/java/io/zipcoder/casino/Console.java Прегледај датотеку

@@ -17,6 +17,12 @@ public class Console {
17 17
         return scan.nextLine();
18 18
     }
19 19
 
20
+    public static int askForInteger(String thingToAsk) {
21
+        System.out.println(thingToAsk);
22
+        Scanner scan = new Scanner(System.in);
23
+        return Integer.valueOf(scan.nextLine());
24
+    }
25
+
20 26
     public static String output(String output) {
21 27
         System.out.println(output);
22 28
         return output;

+ 65
- 101
src/main/java/io/zipcoder/casino/HighLow.java Прегледај датотеку

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

+ 7
- 7
src/test/java/io/zipcoder/casino/HighLowTest.java Прегледај датотеку

@@ -6,14 +6,14 @@ import org.junit.Test;
6 6
 public class HighLowTest {
7 7
 
8 8
     Player player = new Player("Eug");
9
-    HighLow highlow = new HighLow(player);
9
+    HighLow game = new HighLow(player);
10 10
 
11
-//      @Test
12
-//    public void playerContinueTest(){
13
-//        boolean expected = true;
14
-//        boolean actual = highlow.checkIfContinue("y");
15
-//        Assert.assertEquals(expected, actual);
16
-//    }
11
+    @Test
12
+    public void rollDice1(){
13
+        int expected = 6;
14
+        int actual = game.rollDice();
15
+        Assert.assertEquals(expected, actual, 6);
16
+    }
17 17
 
18 18
 
19 19
 }