Browse Source

Merge branch 'master' of laurengreen/ZCW-OOP-Casino into working

jonathan-hinds 6 years ago
parent
commit
abcff63819

+ 1
- 1
src/main/java/io/zipcoder/casino/Casino.java View File

@@ -72,7 +72,7 @@ public class Casino {
72 72
                         Printer.printMessage("Invalid bet amount entered, min bet for slot is $1 and max $100 ");
73 73
                         break;
74 74
                     }
75
-                    Game slot= new SlotMachine(slotBet1);
75
+                    Game slot= new SlotMachine(slotBet1, player);
76 76
                     game = slot;
77 77
                     slot.startGame();
78 78
                     ((SlotMachine) slot).slotResult();

+ 36
- 17
src/main/java/io/zipcoder/casino/SlotMachine.java View File

@@ -12,18 +12,23 @@ public class SlotMachine implements Game, Gamble {
12 12
     String word2="";
13 13
     String word3="";
14 14
     double totalBet=0;
15
+    Player player;
16
+
17
+    public SlotMachine(int betAmount, Player player) {
15 18
 
16
-    public SlotMachine(int betAmount) {
17 19
         this.betAmount = betAmount;
20
+        this.player = player;
18 21
     }
19 22
 
20 23
     @Override
21 24
     public void bet(int betAmount) {
22
-        this.betAmount= betAmount;
25
+        player.changeBalance(-betAmount);
23 26
     }
24 27
 
25 28
     public void payout(){
29
+        getPlayer().changeBalance(payoutAmt);
26 30
         Printer.printMessage("Your payout amount for slot machine is: $" + payoutAmt + "\n");
31
+
27 32
     }
28 33
 
29 34
     @Override
@@ -36,15 +41,10 @@ public class SlotMachine implements Game, Gamble {
36 41
         }
37 42
         Printer.printMessage("Your slot is in progress" + "\n");
38 43
 
39
-        try {
40
-            Thread.sleep(3000);
41
-        } catch (InterruptedException e) {
42
-            e.printStackTrace();
43
-        }
44
-
45
-
46
-        outputword = "";
44
+        startRound();
45
+    }
47 46
 
47
+    public void generateWords() {
48 48
         Random rand = new Random();
49 49
 
50 50
         for (int i = 1; i <= 3; i++) {
@@ -79,28 +79,26 @@ public class SlotMachine implements Game, Gamble {
79 79
                 word3 = word;
80 80
             }
81 81
         }
82
-
82
+        outputword= "[ " + word1+ " ]" + "   " + "[ " + word2 + " ]" + "   "+ "[ " + word3 + " ]" + "\n" ;
83 83
     }
84 84
 
85 85
     public void slotResult()
86 86
     {
87
-            outputword= "[ " + word1+ " ]" + "   " + "[ " + word2 + " ]" + "   "+ "[ " + word3 + " ]" + "\n" ;
88
-
89 87
             if(((!word1.equals(word2)) )&& ((!word1.equals(word3))) && ((!word2.equals(word3)))){
90 88
 
91 89
                 outputword= outputword + "\n"+" You have won $0";
92
-                payoutAmt=0;
90
+                setPayoutAmt(0);
93 91
             }
94 92
             else if( (word1.equals(word2) && (!word1.equals(word3))) || ((word1.equals(word3)) && (!word1.equals(word2))) || ((word2.equals(word3)) && (!word2.equals(word1)))){
95 93
 
96 94
                 outputword= outputword + "\n" +" You have won $" + (betAmount*2);
97
-                payoutAmt=betAmount*2;
95
+                setPayoutAmt(betAmount*2);
98 96
             }
99 97
 
100 98
 
101
-            else if(word1.equals(word2) && word1.equals(word3)  /*&& ((word2.equals(word1)) && (word2.equals(word3))) && ( (word3.equals(word1)) && (word3.equals(word2)))*/){
99
+            else if(word1.equals(word2) && word1.equals(word3)) {
102 100
                 outputword= outputword + "\n" + "You have won $" + (betAmount*3);
103
-                payoutAmt=betAmount*3;
101
+                setPayoutAmt(betAmount*3);
104 102
             }
105 103
 
106 104
             Printer.printMessage(( outputword + "\n" ));
@@ -114,11 +112,23 @@ public class SlotMachine implements Game, Gamble {
114 112
 
115 113
     @Override
116 114
     public void startRound() {
115
+        try {
116
+            Thread.sleep(3000);
117
+        } catch (InterruptedException e) {
118
+            e.printStackTrace();
119
+        }
117 120
 
121
+        generateWords();
118 122
     }
123
+
119 124
     public int getPayoutAmt() {
120 125
         return payoutAmt;
121 126
     }
127
+
128
+    public void setPayoutAmt(int payoutAmt) {
129
+        this.payoutAmt = payoutAmt;
130
+    }
131
+
122 132
     public void setWord1(String word1) {
123 133
         this.word1 = word1;
124 134
     }
@@ -130,5 +140,14 @@ public class SlotMachine implements Game, Gamble {
130 140
     public void setWord3(String word3) {
131 141
         this.word3 = word3;
132 142
     }
143
+
144
+    public String getOutputword() {
145
+        return outputword;
146
+    }
147
+
148
+    public Player getPlayer() {
149
+        return player;
150
+    }
151
+
133 152
 }
134 153
 

+ 55
- 3
src/test/java/io/zipcoder/casino/SlotTest.java View File

@@ -11,6 +11,8 @@ import java.util.Scanner;
11 11
 
12 12
 public class SlotTest {
13 13
 
14
+    Player player = new Player("Bob", 400);
15
+
14 16
     @Test
15 17
     public void testSlotResult1(){
16 18
         int betAmount=10;
@@ -18,7 +20,7 @@ public class SlotTest {
18 20
         String word2="MOUSE";
19 21
         String word3="MOUSE";
20 22
         //given
21
-        SlotMachine slotmachine = new SlotMachine(betAmount);
23
+        SlotMachine slotmachine = new SlotMachine(betAmount, player);
22 24
         slotmachine.setWord1(word1);
23 25
         slotmachine.setWord2(word2);
24 26
         slotmachine.setWord3(word3);
@@ -38,7 +40,7 @@ public class SlotTest {
38 40
 
39 41
 
40 42
         //given
41
-        SlotMachine slotmachine = new SlotMachine(betAmount);
43
+        SlotMachine slotmachine = new SlotMachine(betAmount, player);
42 44
 
43 45
         slotmachine.setWord1(word1);
44 46
         slotmachine.setWord2(word2);
@@ -58,7 +60,7 @@ public class SlotTest {
58 60
         String word3="CAT";
59 61
 
60 62
         //given
61
-        SlotMachine slotmachine = new SlotMachine(betAmount);
63
+        SlotMachine slotmachine = new SlotMachine(betAmount, player);
62 64
 
63 65
         slotmachine.setWord1(word1);
64 66
         slotmachine.setWord2(word2);
@@ -68,4 +70,54 @@ public class SlotTest {
68 70
         int payout=slotmachine.getPayoutAmt();
69 71
         Assert.assertEquals(0,payout);
70 72
     }
73
+
74
+    @Test
75
+    public void testGenerateWords() {
76
+        //Given
77
+        SlotMachine game = new SlotMachine(100, player);
78
+        String before = game.getOutputword();
79
+
80
+        //When
81
+        game.generateWords();
82
+        String after = game.getOutputword();
83
+
84
+        //Then
85
+        Assert.assertNotEquals(before, after);
86
+
87
+
88
+    }
89
+
90
+    @Test
91
+    public void testBet() {
92
+        //Given
93
+        SlotMachine game = new SlotMachine(100, player);
94
+        int expected = 300;
95
+
96
+        //When
97
+        game.bet(100);
98
+        int actual = game.getPlayer().getCurrentBalance();
99
+
100
+        //Then
101
+        Assert.assertEquals(expected, actual);
102
+
103
+
104
+    }
105
+
106
+    @Test
107
+    public void testPayout() {
108
+        //Given
109
+        SlotMachine game = new SlotMachine(100, player);
110
+        int expected = 500;
111
+
112
+        //When
113
+        game.bet(100);
114
+        game.setPayoutAmt(200);
115
+        game.payout();
116
+        int actual = game.getPlayer().getCurrentBalance();
117
+
118
+        //Then
119
+        Assert.assertEquals(expected, actual);
120
+
121
+
122
+    }
71 123
 }