瀏覽代碼

Updated slot machine and tests

Lauren Green 6 年之前
父節點
當前提交
771a66a567

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

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

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

12
     String word2="";
12
     String word2="";
13
     String word3="";
13
     String word3="";
14
     double totalBet=0;
14
     double totalBet=0;
15
+    Player player;
16
+
17
+    public SlotMachine(int betAmount, Player player) {
15
 
18
 
16
-    public SlotMachine(int betAmount) {
17
         this.betAmount = betAmount;
19
         this.betAmount = betAmount;
20
+        this.player = player;
18
     }
21
     }
19
 
22
 
20
     @Override
23
     @Override
21
     public void bet(int betAmount) {
24
     public void bet(int betAmount) {
22
-        this.betAmount= betAmount;
25
+        player.changeBalance(-betAmount);
23
     }
26
     }
24
 
27
 
25
     public void payout(){
28
     public void payout(){
29
+        getPlayer().changeBalance(payoutAmt);
26
         Printer.printMessage("Your payout amount for slot machine is: $" + payoutAmt + "\n");
30
         Printer.printMessage("Your payout amount for slot machine is: $" + payoutAmt + "\n");
31
+
27
     }
32
     }
28
 
33
 
29
     @Override
34
     @Override
42
             e.printStackTrace();
47
             e.printStackTrace();
43
         }
48
         }
44
 
49
 
50
+        generateWords();
45
 
51
 
46
-        outputword = "";
52
+    }
47
 
53
 
54
+    public void generateWords() {
48
         Random rand = new Random();
55
         Random rand = new Random();
49
 
56
 
50
         for (int i = 1; i <= 3; i++) {
57
         for (int i = 1; i <= 3; i++) {
79
                 word3 = word;
86
                 word3 = word;
80
             }
87
             }
81
         }
88
         }
82
-
89
+        outputword= "[ " + word1+ " ]" + "   " + "[ " + word2 + " ]" + "   "+ "[ " + word3 + " ]" + "\n" ;
83
     }
90
     }
84
 
91
 
85
     public void slotResult()
92
     public void slotResult()
86
     {
93
     {
87
-            outputword= "[ " + word1+ " ]" + "   " + "[ " + word2 + " ]" + "   "+ "[ " + word3 + " ]" + "\n" ;
88
-
89
             if(((!word1.equals(word2)) )&& ((!word1.equals(word3))) && ((!word2.equals(word3)))){
94
             if(((!word1.equals(word2)) )&& ((!word1.equals(word3))) && ((!word2.equals(word3)))){
90
 
95
 
91
                 outputword= outputword + "\n"+" You have won $0";
96
                 outputword= outputword + "\n"+" You have won $0";
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
                 outputword= outputword + "\n" + "You have won $" + (betAmount*3);
107
                 outputword= outputword + "\n" + "You have won $" + (betAmount*3);
103
                 payoutAmt=betAmount*3;
108
                 payoutAmt=betAmount*3;
104
             }
109
             }
130
     public void setWord3(String word3) {
135
     public void setWord3(String word3) {
131
         this.word3 = word3;
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
 
7
 
8
 public class SlotTest {
8
 public class SlotTest {
9
 
9
 
10
+    Player player = new Player("Bob", 400);
11
+
10
     @Test
12
     @Test
11
     public void testSlotResult1(){
13
     public void testSlotResult1(){
12
         int betAmount=10;
14
         int betAmount=10;
14
         String word2="MOUSE";
16
         String word2="MOUSE";
15
         String word3="MOUSE";
17
         String word3="MOUSE";
16
         //given
18
         //given
17
-        SlotMachine slotmachine = new SlotMachine(betAmount);
19
+        SlotMachine slotmachine = new SlotMachine(betAmount, player);
18
         slotmachine.setWord1(word1);
20
         slotmachine.setWord1(word1);
19
         slotmachine.setWord2(word2);
21
         slotmachine.setWord2(word2);
20
         slotmachine.setWord3(word3);
22
         slotmachine.setWord3(word3);
34
 
36
 
35
 
37
 
36
         //given
38
         //given
37
-        SlotMachine slotmachine = new SlotMachine(betAmount);
39
+        SlotMachine slotmachine = new SlotMachine(betAmount, player);
38
 
40
 
39
         slotmachine.setWord1(word1);
41
         slotmachine.setWord1(word1);
40
         slotmachine.setWord2(word2);
42
         slotmachine.setWord2(word2);
54
         String word3="CAT";
56
         String word3="CAT";
55
 
57
 
56
         //given
58
         //given
57
-        SlotMachine slotmachine = new SlotMachine(betAmount);
59
+        SlotMachine slotmachine = new SlotMachine(betAmount, player);
58
 
60
 
59
         slotmachine.setWord1(word1);
61
         slotmachine.setWord1(word1);
60
         slotmachine.setWord2(word2);
62
         slotmachine.setWord2(word2);
64
         int payout=slotmachine.getPayoutAmt();
66
         int payout=slotmachine.getPayoutAmt();
65
         Assert.assertEquals(0,payout);
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
 }