Procházet zdrojové kódy

Yahtzee test progress

Lauren Green před 6 roky
rodič
revize
ccde6ad58e

+ 8
- 1
src/main/java/io/zipcoder/casino/Casino.java Zobrazit soubor

@@ -72,7 +72,14 @@ public class Casino {
72 72
 
73 73
                 case 1:
74 74
                     Game yahtzee = new Yahtzee(player);
75
-                    yahtzee.startGame();
75
+                    ((Yahtzee) yahtzee).startGame();
76
+                    int betAmount = console.getIntFromUser("How much would you like to bet on this game?");
77
+                    ((Yahtzee) yahtzee).setBid(betAmount);
78
+                    ((Yahtzee) yahtzee).bet(betAmount);
79
+                    yahtzee.startRound();
80
+                    Printer.printMessage("You scored " + ((Yahtzee) yahtzee).getDicePlayer().getScoreSheet().getTotalScore() + " points.");
81
+                    ((Yahtzee) yahtzee).payout();
82
+                    Printer.printMessage(((Yahtzee) yahtzee).getDicePlayer().balanceAtEnd() + "\n");
76 83
                     break;
77 84
 
78 85
                 case 5:

+ 47
- 65
src/main/java/io/zipcoder/casino/Yahtzee.java Zobrazit soubor

@@ -8,9 +8,11 @@ public class Yahtzee extends DiceGame implements Game, Gamble {
8 8
     DicePlayer dicePlayer;
9 9
     Console console = new Console();
10 10
     int betAmount = 0;
11
+    int totalScore = 0;
11 12
 
12 13
     public Yahtzee(Player player) {
13 14
         this.dicePlayer = new DicePlayer(player);
15
+        this.totalScore = this.dicePlayer.getScoreSheet().getTotalScore();
14 16
     }
15 17
 
16 18
     public int getBid() {
@@ -21,7 +23,7 @@ public class Yahtzee extends DiceGame implements Game, Gamble {
21 23
         this.betAmount = bid;
22 24
     }
23 25
 
24
-    public void createGame() {
26
+    public void startGame() {
25 27
         Dice dice1 = new Dice();
26 28
         Dice dice2 = new Dice();
27 29
         Dice dice3 = new Dice();
@@ -33,70 +35,52 @@ public class Yahtzee extends DiceGame implements Game, Gamble {
33 35
         dicePlayer.getCup()[2] = dice3;
34 36
         dicePlayer.getCup()[3] = dice4;
35 37
         dicePlayer.getCup()[4] = dice5;
36
-    }
37
-
38
-    public void startGame() {
39
-        createGame();
40
-        int betAmount = console.getIntFromUser("How much would you like to bet on this game?");
41
-        setBid(betAmount);
42
-        bet(betAmount);
43
-
44
-        startRound();
45
-        System.out.println("You scored " + dicePlayer.getScoreSheet().getTotalScore() + " points.");
46
-        payout();
47
-        dicePlayer.printBalanceAtEnd();
48
-        System.out.println();
49 38
 
50 39
     }
51 40
 
41
+    @Override
52 42
     public void startRound() {
53
-
54 43
         for (int i = 0; i < ScoreSheet.getSize(); i++) {
55
-            for (Dice d : dicePlayer.getCup()) {
56
-                d.roll();
44
+            rollAll();
45
+            for(int j = 0; j < 2; j++) {
46
+                int choice = console.getIntFromUser("Would you like to:\n1. Roll all dice again.\n2. Roll some dice again.\n3. Stop rolling and score.\nNumber of Selection: ");
47
+                rollOptions(choice); }
48
+
49
+            boolean validEntry = false;
50
+            ScoreSheet.ROW row = ScoreSheet.ROW.CHANCE;
51
+            while (!validEntry) {
52
+                Printer.printMessage(dicePlayer.getScoreSheet().scoreCardToString());
53
+
54
+                int choice = console.getIntFromUser("Which row would you like to apply your turn to on the scoresheet?.\n" +
55
+                        "Remember you can only use each row once!");
56
+                row = selectingRow(choice);
57
+                validEntry = checkEmptyRow(row);
57 58
             }
58
-            System.out.println("\nYou rolled:");
59
-            dicePlayer.printCup();
60
-            System.out.println();
59
+            dicePlayer.getScoreSheet().setRow(row, dicePlayer.getCup());
60
+            Printer.printMessage("\n" + dicePlayer.getScoreSheet().scoreCardToString());
61 61
 
62
-            roundRoutine();
63
-            recordingScore();
64 62
         }
65 63
 
66 64
     }
67 65
 
68
-    public void roundRoutine() {
69
-
70
-        giveOptions();
71
-        giveOptions();
72
-
66
+    public void rollAll() {
67
+        for (Dice d : dicePlayer.getCup()) {
68
+            d.roll();
69
+        }
70
+        Printer.printMessage("\nYou rolled:\n" + dicePlayer.cupToString() + "\n");
73 71
     }
74 72
 
75
-    public void giveOptions() {
76
-        int choice = 0;
77
-        System.out.print("Would you like to:\n1. Roll all dice again.\n2. Roll some dice again.\n3. Stop rolling and score.\nNumber of Selection: ");
78 73
 
79
-        Scanner in = new Scanner(System.in);
80
-        choice = in.nextInt();
74
+    public void rollOptions(int choice) {
81 75
 
82 76
         switch (choice) {
83 77
             case 1:
84
-                for (Dice d : dicePlayer.getCup()) {
85
-                    d.roll();
86
-                }
87
-                System.out.println("\nYou rolled:");
88
-                dicePlayer.printCup();
89
-                System.out.println();
78
+                rollAll();
90 79
                 break;
91 80
 
92 81
             case 2:
93
-                System.out.println("Which numbers would you like to reroll? List the numbers separated by spaces.");
94
-                Scanner in2 = new Scanner(System.in);
95
-                String diceToRoll = in2.nextLine();
82
+                String diceToRoll = console.getLineFromUser("Which numbers would you like to reroll? List the numbers separated by spaces.");
96 83
                 reRoll(diceToRoll);
97
-                System.out.println("\nYou rolled:");
98
-                dicePlayer.printCup();
99
-                System.out.println();
100 84
                 break;
101 85
 
102 86
             case 3:
@@ -120,24 +104,14 @@ public class Yahtzee extends DiceGame implements Game, Gamble {
120 104
                 }
121 105
             }
122 106
         }
107
+        Printer.printMessage("\nYou rolled:\n" + dicePlayer.cupToString() + "\n");
108
+
123 109
     }
124 110
 
125
-    public void recordingScore() {
111
+    public ScoreSheet.ROW selectingRow(int choice) {
126 112
 
127
-        boolean validEntry = true;
128
-        int choice = 13;
129 113
         ScoreSheet.ROW row = ScoreSheet.ROW.CHANCE;
130 114
 
131
-        while (validEntry) {
132
-            Printer.printMessage(dicePlayer.getScoreSheet().scoreCardToString());
133
-            System.out.println();
134
-            System.out.println("Which row would you like to apply your turn to on the scoresheet?.\n" +
135
-                    "Remember you can only use each row once!");
136
-            System.out.print("Row:");
137
-
138
-            Scanner scanner2 = new Scanner(System.in);
139
-            choice = scanner2.nextInt();
140
-
141 115
             switch (choice) {
142 116
                 case 1:
143 117
                     row = ScoreSheet.ROW.ACES;
@@ -179,17 +153,21 @@ public class Yahtzee extends DiceGame implements Game, Gamble {
179 153
                     row = ScoreSheet.ROW.CHANCE;
180 154
                     break;
181 155
             }
156
+
157
+            return row;
158
+
159
+        }
160
+
161
+        public boolean checkEmptyRow(ScoreSheet.ROW row) {
182 162
             if (dicePlayer.getScoreSheet().getScore(row) == null) {
183
-                validEntry = false;
163
+                return true;
184 164
             } else {
185
-                System.out.println("Error, you have already filled that row");
165
+                Printer.printMessage("Error, you have already filled that row");
166
+                return false;
186 167
             }
187 168
         }
188 169
 
189
-        dicePlayer.getScoreSheet().setRow(row, dicePlayer.getCup());
190
-        System.out.println();
191
-        Printer.printMessage(dicePlayer.getScoreSheet().scoreCardToString());
192
-    }
170
+
193 171
 
194 172
     @Override
195 173
     public void bet(int betAmount) {
@@ -198,7 +176,7 @@ public class Yahtzee extends DiceGame implements Game, Gamble {
198 176
 
199 177
     @Override
200 178
     public void payout() {
201
-        int score = dicePlayer.getScoreSheet().getTotalScore();
179
+        int score = getTotalScore();
202 180
         int payOut = 0;
203 181
         if (score == 1575) {
204 182
             payOut = getBid() * 100;
@@ -216,7 +194,7 @@ public class Yahtzee extends DiceGame implements Game, Gamble {
216 194
             payOut = 0;
217 195
         }
218 196
         dicePlayer.getPlayer().changeBalance(payOut);
219
-        System.out.println("You won $" + payOut);
197
+        Printer.printMessage("You won $" + payOut);
220 198
     }
221 199
 
222 200
     @Override
@@ -227,4 +205,8 @@ public class Yahtzee extends DiceGame implements Game, Gamble {
227 205
     public DicePlayer getDicePlayer() {
228 206
         return dicePlayer;
229 207
     }
208
+
209
+    public int getTotalScore() {
210
+        return totalScore;
211
+    }
230 212
 }

+ 68
- 1
src/test/java/io/zipcoder/casino/YahtzeeTest.java Zobrazit soubor

@@ -35,7 +35,7 @@ public class YahtzeeTest {
35 35
     @Test
36 36
     public void testCreateGame() {
37 37
         //When
38
-        yahtzee.createGame();
38
+        yahtzee.startGame();
39 39
         int expected = 5;
40 40
         int actual = yahtzee.getDicePlayer().getCup().length;
41 41
 
@@ -45,5 +45,72 @@ public class YahtzeeTest {
45 45
 
46 46
     }
47 47
 
48
+    @Test
49
+    public void testRollOptionsNoRoll() {
50
+        //When
51
+        yahtzee.startGame();
52
+        String expected = yahtzee.getDicePlayer().cupToString();
53
+        yahtzee.rollOptions(3);
54
+        String actual = yahtzee.getDicePlayer().cupToString();
55
+
56
+        //Then
57
+        Assert.assertEquals(expected, actual);
58
+
59
+
60
+    }
61
+
62
+    @Test
63
+    public void testRollOptionsRollAll() {
64
+        //When
65
+        yahtzee.startGame();
66
+        String expected = yahtzee.getDicePlayer().cupToString();
67
+        yahtzee.rollOptions(1);
68
+        String actual = yahtzee.getDicePlayer().cupToString();
69
+
70
+        //Then
71
+        Assert.assertNotEquals(expected, actual);
72
+
73
+
74
+    }
75
+
76
+    @Test
77
+    public void testReRoll() {
78
+        //When
79
+        yahtzee.startGame();
80
+        int diceOneValue = yahtzee.getDicePlayer().getCup()[0].getValue();
81
+        int diceTwoValue = yahtzee.getDicePlayer().getCup()[1].getValue();
82
+        yahtzee.reRoll(Integer.toString(diceTwoValue));
83
+        int actual = yahtzee.getDicePlayer().getCup()[0].getValue();
84
+
85
+        //Then
86
+        Assert.assertNotEquals(diceOneValue, actual);
87
+
88
+
89
+    }
90
+
91
+    @Test
92
+    public void testEmptyRowTrue() {
93
+        //When
94
+        yahtzee.startGame();
95
+
96
+        //Then
97
+        Assert.assertTrue(yahtzee.checkEmptyRow(ScoreSheet.ROW.CHANCE));
98
+
99
+
100
+    }
101
+
102
+    @Test
103
+    public void testEmptyRowFalse() {
104
+        //When
105
+        yahtzee.startGame();
106
+        yahtzee.rollAll();
107
+        yahtzee.getDicePlayer().getScoreSheet().setRow(ScoreSheet.ROW.CHANCE, yahtzee.getDicePlayer().getCup());
108
+
109
+        //Then
110
+        Assert.assertFalse(yahtzee.checkEmptyRow(ScoreSheet.ROW.CHANCE));
111
+
112
+
113
+    }
114
+
48 115
 
49 116
 }