yauhenip 6 years ago
parent
commit
885a4adc7e

+ 39
- 0
src/main/java/io/zipcoder/casino/CardGame.java View File

@@ -0,0 +1,39 @@
1
+package io.zipcoder.casino;
2
+
3
+public abstract class CardGame implements Game, Gamble {
4
+
5
+
6
+    int playerHand;
7
+    int dealerHand;
8
+    int currentBet;
9
+    Player player;
10
+    CardDeck deck;
11
+
12
+    public CardGame(Player player) {
13
+
14
+        this.player = player;
15
+        this.deck = new CardDeck();
16
+
17
+    }
18
+
19
+
20
+    public abstract int drawCard();
21
+
22
+    public abstract void stand();
23
+
24
+    public abstract void startGame();
25
+
26
+    public abstract void endGame();
27
+
28
+    public abstract void declareWinner();
29
+
30
+    public abstract void winBet();
31
+    
32
+    public abstract void loseBet(Player player);
33
+
34
+
35
+
36
+}
37
+
38
+
39
+

+ 26
- 0
src/main/java/io/zipcoder/casino/DiceGame.java View File

@@ -0,0 +1,26 @@
1
+package io.zipcoder.casino;
2
+
3
+public abstract class DiceGame implements Gamble, Game{
4
+
5
+//    Player player;
6
+//    Dice dice;
7
+//
8
+//    public DiceGame(Player player, int numberOfDice) {
9
+//
10
+//        this.player = player;
11
+//        this.dice = new Dice(numberOfDice);
12
+//    }
13
+
14
+
15
+    public abstract void startGame();
16
+
17
+    public abstract void endGame();
18
+
19
+    public abstract int rollDice();
20
+
21
+    public abstract void winBet();
22
+
23
+    public abstract void loseBet();
24
+
25
+    public abstract void declareWinner();
26
+}

+ 142
- 0
src/main/java/io/zipcoder/casino/HighLow.java View File

@@ -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
+}

+ 4
- 4
src/test/java/io/zipcoder/casino/DiceTest.java View File

@@ -7,7 +7,7 @@ import org.junit.Test;
7 7
 public class DiceTest {
8 8
 
9 9
     @Test
10
-    public void getSum1(){
10
+    public void getSumTest1(){
11 11
         Dice dice = new Dice(2);
12 12
         int expected = 6;
13 13
         int actual = dice.getSum();
@@ -15,7 +15,7 @@ public class DiceTest {
15 15
     }
16 16
 
17 17
     @Test
18
-    public void getSum2(){
18
+    public void getSumTest2(){
19 19
         Dice dice = new Dice(5);
20 20
         int expected = 13;
21 21
         int actual = dice.getSum();
@@ -23,7 +23,7 @@ public class DiceTest {
23 23
     }
24 24
 
25 25
     @Test
26
-    public void getEach1(){
26
+    public void getEachTest1(){
27 27
         Dice dice = new Dice(2);
28 28
         int expected = 3;
29 29
         int actual = dice.getEach()[1];
@@ -31,7 +31,7 @@ public class DiceTest {
31 31
     }
32 32
 
33 33
     @Test
34
-    public void getEach2(){
34
+    public void getEachTest2(){
35 35
         Dice dice = new Dice(6);
36 36
         int expected = 3;
37 37
         int actual = dice.getEach()[3];

+ 19
- 0
src/test/java/io/zipcoder/casino/HighLowTest.java View File

@@ -0,0 +1,19 @@
1
+package io.zipcoder.casino;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class HighLowTest {
7
+
8
+    Player player = new Player("Eug");
9
+    HighLow highlow = new HighLow(player);
10
+
11
+//      @Test
12
+//    public void playerContinueTest(){
13
+//        boolean expected = true;
14
+//        boolean actual = highlow.checkIfContinue("y");
15
+//        Assert.assertEquals(expected, actual);
16
+//    }
17
+
18
+
19
+}