瀏覽代碼

Changes in scoresheet to simplify code and remove printing

Lauren Green 6 年之前
父節點
當前提交
88f8441dc0
共有 2 個檔案被更改,包括 43 行新增101 行删除
  1. 0
    14
      src/main/java/io/zipcoder/casino/CardGame.java
  2. 43
    87
      src/main/java/io/zipcoder/casino/ScoreSheet.java

+ 0
- 14
src/main/java/io/zipcoder/casino/CardGame.java 查看文件

@@ -32,13 +32,6 @@ public abstract class CardGame {
32 32
 
33 33
     }
34 34
 
35
-    public void faceDown(Card card){
36
-        card.setVisibility(false);
37
-    }
38
-
39
-    public void faceUp(Card card){
40
-        card.setVisibility(true);
41
-    }
42 35
 
43 36
     public Deck getDeck() {
44 37
         return deck;
@@ -55,9 +48,6 @@ public abstract class CardGame {
55 48
         }
56 49
     }
57 50
 
58
-    public void setDeck(Deck deck) {
59
-        this.deck = deck;
60
-    }
61 51
 
62 52
     public int getAnte(){
63 53
         return ante;
@@ -124,10 +114,6 @@ public abstract class CardGame {
124 114
         }
125 115
     }
126 116
 
127
-    public void printTurn(){
128
-        System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
129
-    }
130
-
131 117
     public Player getLoser() {
132 118
         return loser;
133 119
     }

+ 43
- 87
src/main/java/io/zipcoder/casino/ScoreSheet.java 查看文件

@@ -30,67 +30,33 @@ public class ScoreSheet {
30 30
         return totalScore;
31 31
     }
32 32
 
33
-    public void rowToString(ROW row, String description) {
34
-        String rowInfo = String.format("%-35s",description));
33
+    public String rowToString(ROW row, String description) {
34
+        String rowInfo = String.format("%-35s",description);
35 35
         if(getScore(row) != null) {
36 36
             rowInfo += "\n** " + getScore(row) + " **\n";
37 37
         } else {
38 38
             rowInfo += "\n** open **\n";
39 39
         }
40
+        return rowInfo;
40 41
     }
41 42
 
42
-    public void scoreCardToString(){
43
-        rowToString(ScoreSheet.ROW.ACES, "1. Aces: Totals all Ones");
44
-        rowToString(ScoreSheet.ROW.TWOS, "2. Twos: Totals all Twos");
45
-        rowToString(ScoreSheet.ROW.THREES, "3. Threes: Totals all Threes");
46
-        rowToString(ScoreSheet.ROW.FOURS, "4. Fours: Totals all Fours");
47
-        rowToString(ScoreSheet.ROW.FIVES, "5. Fives: Totals all Fives");
48
-        rowToString(ScoreSheet.ROW.SIXES, "6. Sixes: Totals all Sixes");
49
-        rowToString(ScoreSheet.ROW.THREEOFAKIND, );
50
-
51
-        System.out.print(String.format("%-35s", "7. 3 of a Kind"));
52
-        if(getScore() != null) {
53
-            System.out.println("** " + getScore(ScoreSheet.ROW.THREEOFAKIND) + " **");
54
-        } else {
55
-            System.out.println("** open **");
56
-        }
57
-        System.out.print(String.format("%-35s", "8. 4 of a Kind"));
58
-        if(getScore(ScoreSheet.ROW.FOUROFAKIND) != null) {
59
-            System.out.println("** " + getScore(ScoreSheet.ROW.FOUROFAKIND) + " **");
60
-        } else {
61
-            System.out.println("** open **");
62
-        }
63
-        System.out.print(String.format("%-35s", "9. Full House"));
64
-        if(getScore(ScoreSheet.ROW.FULLHOUSE) != null) {
65
-            System.out.println("** " + getScore(ScoreSheet.ROW.FULLHOUSE) + " **");
66
-        } else {
67
-            System.out.println("** open **");
68
-        }
69
-        System.out.print(String.format("%-35s","10. Small Straight: Sequence of 4"));
70
-        if(getScore(ScoreSheet.ROW.SMALLSTRAIGHT) != null) {
71
-            System.out.println("** " + getScore(ScoreSheet.ROW.SMALLSTRAIGHT) + " **");
72
-        } else {
73
-            System.out.println("** open **");
74
-        }
75
-        System.out.print(String.format("%-35s","11. Large Striaght: Sequence of 5"));
76
-        if(getScore(ScoreSheet.ROW.LARGESTRAIGHT) != null) {
77
-            System.out.println("** " + getScore(ScoreSheet.ROW.LARGESTRAIGHT) + " **");
78
-        } else {
79
-            System.out.println("** open **");
80
-        }
81
-        System.out.print(String.format("%-35s","12. Yahtzee: 5 of a Kind "));
82
-        if(getScore(ScoreSheet.ROW.YAHTZEE) != null) {
83
-            System.out.println("** " + getScore(ScoreSheet.ROW.YAHTZEE) + " **");
84
-        } else {
85
-            System.out.println("** open **");
86
-        }
87
-        System.out.print(String.format("%-35s", "13. Chance: Sum of Dice"));
88
-        if(getScore(ScoreSheet.ROW.CHANCE) != null) {
89
-            System.out.println("** " + getScore(ScoreSheet.ROW.CHANCE) + " **");
90
-        } else {
91
-            System.out.println("** open **");
92
-        }
93
-        System.out.println();
43
+    public String scoreCardToString(){
44
+        String scoreCard = rowToString(ScoreSheet.ROW.ACES, "1. Aces: Totals all Ones") +
45
+        rowToString(ScoreSheet.ROW.TWOS, "2. Twos: Totals all Twos") +
46
+        rowToString(ScoreSheet.ROW.THREES, "3. Threes: Totals all Threes") +
47
+        rowToString(ScoreSheet.ROW.FOURS, "4. Fours: Totals all Fours") +
48
+        rowToString(ScoreSheet.ROW.FIVES, "5. Fives: Totals all Fives") +
49
+        rowToString(ScoreSheet.ROW.SIXES, "6. Sixes: Totals all Sixes") +
50
+        rowToString(ScoreSheet.ROW.THREEOFAKIND, "7. 3 of a Kind") +
51
+        rowToString(ScoreSheet.ROW.FOUROFAKIND, "8. 4 of a Kind") +
52
+        rowToString(ScoreSheet.ROW.FULLHOUSE, "9. Full House") +
53
+        rowToString(ScoreSheet.ROW.SMALLSTRAIGHT, "10. Small Straight: Sequence of 4") +
54
+        rowToString(ScoreSheet.ROW.LARGESTRAIGHT, "11. Large Striaght: Sequence of 5") +
55
+        rowToString(ScoreSheet.ROW.YAHTZEE, "12. Yahtzee: 5 of a Kind") +
56
+        rowToString(ScoreSheet.ROW.CHANCE,  "13. Chance: Sum of Dice");
57
+
58
+        return scoreCard;
59
+
94 60
     }
95 61
 
96 62
     public void setRow(ROW row, Dice[] cup){
@@ -121,46 +87,22 @@ public class ScoreSheet {
121 87
                     scores.put(ROW.SIXES, scoreNumbers(numbers, 6));
122 88
                     break;
123 89
                 case THREEOFAKIND:
124
-                    if (checkOfaKind(numbers, 3)) {
125
-                        scores.put(ROW.THREEOFAKIND, scoreTotalDice(numbers));
126
-                    } else {
127
-                        scores.put(ROW.THREEOFAKIND, 0);
128
-                    }
90
+                    scoreRow(checkOfaKind(numbers,3), ROW.THREEOFAKIND, scoreTotalDice(numbers));
129 91
                     break;
130 92
                 case FOUROFAKIND:
131
-                    if (checkOfaKind(numbers, 4)) {
132
-                        scores.put(ROW.FOUROFAKIND, scoreTotalDice(numbers));
133
-                    } else {
134
-                        scores.put(ROW.FOUROFAKIND, 0);
135
-                    }
93
+                    scoreRow(checkOfaKind(numbers, 4), ROW.FOUROFAKIND, scoreTotalDice(numbers));
136 94
                     break;
137 95
                 case FULLHOUSE:
138
-                    if (checkOfaKind(numbers, 5) || checkFullHouse(numbers)) {
139
-                        scores.put(ROW.FULLHOUSE, 25);
140
-                    } else {
141
-                        scores.put(ROW.FULLHOUSE, 0);
142
-                    }
96
+                    scoreRow(checkOfaKind(numbers, 5), ROW.FULLHOUSE, 25);
143 97
                     break;
144 98
                 case SMALLSTRAIGHT:
145
-                    if (checkSmallStraight(numbers)) {
146
-                        scores.put(ROW.SMALLSTRAIGHT, 30);
147
-                    } else {
148
-                        scores.put(ROW.SMALLSTRAIGHT, 0);
149
-                    }
99
+                    scoreRow(checkSmallStraight(numbers), ROW.SMALLSTRAIGHT, 30);
150 100
                     break;
151 101
                 case LARGESTRAIGHT:
152
-                    if (checkLargeStraight(numbers)) {
153
-                        scores.put(ROW.LARGESTRAIGHT, 40);
154
-                    } else {
155
-                        scores.put(ROW.LARGESTRAIGHT, 0);
156
-                    }
102
+                    scoreRow(checkLargeStraight(numbers), ROW.LARGESTRAIGHT, 40);
157 103
                     break;
158 104
                 case YAHTZEE:
159
-                    if (checkOfaKind(numbers, 5)) {
160
-                        scores.put(ROW.YAHTZEE, 50);
161
-                    } else {
162
-                        scores.put(ROW.YAHTZEE, 0);
163
-                    }
105
+                    scoreRow(checkOfaKind(numbers, 5), ROW.YAHTZEE, 50);
164 106
                     break;
165 107
                 case CHANCE:
166 108
                     scores.put(ROW.CHANCE, scoreTotalDice(numbers));
@@ -168,14 +110,28 @@ public class ScoreSheet {
168 110
             }
169 111
         }
170 112
 
113
+        public void scoreRow(boolean check, ROW row, int score) {
114
+            if (check) {
115
+                scores.put(row, score);
116
+            } else {
117
+                scores.put(row, 0);
118
+            }
119
+        }
171 120
 
172 121
 
173 122
     public boolean checkFullHouse(ArrayList<Integer> numbers) {
174 123
 
175
-        boolean check2 = checkOfaKind(numbers, 2);
176
-        boolean check3 = checkOfaKind(numbers, 3);
124
+        boolean fullHouse = false;
125
+        boolean checkYahtzee = checkOfaKind(numbers, 5);
126
+        if(checkYahtzee) {
127
+            return true;
128
+        } else {
129
+            boolean check2 = checkOfaKind(numbers, 2);
130
+            boolean check3 = checkOfaKind(numbers, 3);
131
+            fullHouse = (check2 && check3);
132
+        }
177 133
 
178
-        return (check2 && check3);
134
+        return fullHouse;
179 135
     }
180 136
 
181 137
     public boolean checkSmallStraight(ArrayList<Integer> numbers) {