浏览代码

Updated slot machine and tests

Lauren Green 6 年前
父节点
当前提交
771a66a567

+ 1
- 1
src/main/java/io/zipcoder/casino/Casino.java 查看文件

@@ -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();

+ 21
- 7
src/main/java/io/zipcoder/casino/SlotMachine.java 查看文件

@@ -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
@@ -42,9 +47,11 @@ public class SlotMachine implements Game, Gamble {
42 47
             e.printStackTrace();
43 48
         }
44 49
 
50
+        generateWords();
45 51
 
46
-        outputword = "";
52
+    }
47 53
 
54
+    public void generateWords() {
48 55
         Random rand = new Random();
49 56
 
50 57
         for (int i = 1; i <= 3; i++) {
@@ -79,13 +86,11 @@ public class SlotMachine implements Game, Gamble {
79 86
                 word3 = word;
80 87
             }
81 88
         }
82
-
89
+        outputword= "[ " + word1+ " ]" + "   " + "[ " + word2 + " ]" + "   "+ "[ " + word3 + " ]" + "\n" ;
83 90
     }
84 91
 
85 92
     public void slotResult()
86 93
     {
87
-            outputword= "[ " + word1+ " ]" + "   " + "[ " + word2 + " ]" + "   "+ "[ " + word3 + " ]" + "\n" ;
88
-
89 94
             if(((!word1.equals(word2)) )&& ((!word1.equals(word3))) && ((!word2.equals(word3)))){
90 95
 
91 96
                 outputword= outputword + "\n"+" You have won $0";
@@ -98,7 +103,7 @@ public class SlotMachine implements Game, Gamble {
98 103
             }
99 104
 
100 105
 
101
-            else if(word1.equals(word2) && word1.equals(word3)  /*&& ((word2.equals(word1)) && (word2.equals(word3))) && ( (word3.equals(word1)) && (word3.equals(word2)))*/){
106
+            else if(word1.equals(word2) && word1.equals(word3)) {
102 107
                 outputword= outputword + "\n" + "You have won $" + (betAmount*3);
103 108
                 payoutAmt=betAmount*3;
104 109
             }
@@ -130,5 +135,14 @@ public class SlotMachine implements Game, Gamble {
130 135
     public void setWord3(String word3) {
131 136
         this.word3 = word3;
132 137
     }
138
+
139
+    public String getOutputword() {
140
+        return outputword;
141
+    }
142
+
143
+    public Player getPlayer() {
144
+        return player;
145
+    }
146
+
133 147
 }
134 148
 

+ 37
- 3
src/test/java/io/zipcoder/casino/SlotTest.java 查看文件

@@ -7,6 +7,8 @@ import org.junit.Assert;
7 7
 
8 8
 public class SlotTest {
9 9
 
10
+    Player player = new Player("Bob", 400);
11
+
10 12
     @Test
11 13
     public void testSlotResult1(){
12 14
         int betAmount=10;
@@ -14,7 +16,7 @@ public class SlotTest {
14 16
         String word2="MOUSE";
15 17
         String word3="MOUSE";
16 18
         //given
17
-        SlotMachine slotmachine = new SlotMachine(betAmount);
19
+        SlotMachine slotmachine = new SlotMachine(betAmount, player);
18 20
         slotmachine.setWord1(word1);
19 21
         slotmachine.setWord2(word2);
20 22
         slotmachine.setWord3(word3);
@@ -34,7 +36,7 @@ public class SlotTest {
34 36
 
35 37
 
36 38
         //given
37
-        SlotMachine slotmachine = new SlotMachine(betAmount);
39
+        SlotMachine slotmachine = new SlotMachine(betAmount, player);
38 40
 
39 41
         slotmachine.setWord1(word1);
40 42
         slotmachine.setWord2(word2);
@@ -54,7 +56,7 @@ public class SlotTest {
54 56
         String word3="CAT";
55 57
 
56 58
         //given
57
-        SlotMachine slotmachine = new SlotMachine(betAmount);
59
+        SlotMachine slotmachine = new SlotMachine(betAmount, player);
58 60
 
59 61
         slotmachine.setWord1(word1);
60 62
         slotmachine.setWord2(word2);
@@ -64,4 +66,36 @@ public class SlotTest {
64 66
         int payout=slotmachine.getPayoutAmt();
65 67
         Assert.assertEquals(0,payout);
66 68
     }
69
+
70
+    @Test
71
+    public void testGenerateWords() {
72
+        //Given
73
+        SlotMachine game = new SlotMachine(100, player);
74
+        String before = game.getOutputword();
75
+
76
+        //When
77
+        game.generateWords();
78
+        String after = game.getOutputword();
79
+
80
+        //Then
81
+        Assert.assertNotEquals(before, after);
82
+
83
+
84
+    }
85
+
86
+    @Test
87
+    public void testBet() {
88
+        //Given
89
+        SlotMachine game = new SlotMachine(100, player);
90
+        int expected = 300;
91
+
92
+        //When
93
+        game.bet(100);
94
+        int actual = game.getPlayer().getCurrentBalance();
95
+
96
+        //Then
97
+        Assert.assertEquals(expected, actual);
98
+
99
+
100
+    }
67 101
 }