瀏覽代碼

Yahtzee working. Bugs seem to be all worked out. Need a return to console at end.

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

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

@@ -25,9 +25,9 @@ public abstract class CardGame {
25 25
     }
26 26
 
27 27
     //use hand size to determine dealing
28
-    public abstract void Deal();
28
+    public abstract void deal();
29 29
 
30
-    public void Shuffle(){
30
+    public void shuffle(){
31 31
 
32 32
         //shuffle the card stack
33 33
 

+ 3
- 3
src/main/java/io/zipcoder/casino/Console.java 查看文件

@@ -33,19 +33,19 @@ public class Console {
33 33
                 int[] warMinMax = getMinMax();
34 34
                 Game war = new War(warMinMax[0], warMinMax[1], 10);
35 35
                 ((War) war).addPlayers(player);
36
-                war.StartGame();
36
+                war.startGame();
37 37
                 break;
38 38
 
39 39
             case "three card stud":
40 40
                 int[] studMinMax = getMinMax();
41 41
                 Game stud = new Stud(studMinMax[0], studMinMax[1], 10);
42 42
                 ((Stud) stud).addPlayers(player);
43
-                stud.StartGame();
43
+                stud.startGame();
44 44
                 break;
45 45
 
46 46
             case "yahtzee":
47 47
                 Game yahtzee = new Yahtzee(player);
48
-                yahtzee.StartGame();
48
+                yahtzee.startGame();
49 49
                 break;
50 50
 
51 51
             default:

+ 5
- 0
src/main/java/io/zipcoder/casino/DicePlayer.java 查看文件

@@ -23,5 +23,10 @@ public class DicePlayer {
23 23
         for(Dice d : cup) {
24 24
             System.out.print(d.getValue() + " ");
25 25
         }
26
+        System.out.println();
27
+    }
28
+
29
+    public Player getPlayer() {
30
+        return player;
26 31
     }
27 32
 }

+ 2
- 2
src/main/java/io/zipcoder/casino/Gamble.java 查看文件

@@ -1,6 +1,6 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 public interface Gamble {
4
-     void Bet(int betAmount);
5
-     int Payout(int payoutAmount);
4
+     void bet(int betAmount);
5
+     void payout();
6 6
 }

+ 3
- 3
src/main/java/io/zipcoder/casino/Game.java 查看文件

@@ -1,7 +1,7 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 public interface Game {
4
-     void Quit();
5
-     void StartGame();
6
-     void StartRound();
4
+     void quit();
5
+     void startGame();
6
+     void startRound();
7 7
 }

+ 91
- 37
src/main/java/io/zipcoder/casino/ScoreSheet.java 查看文件

@@ -2,6 +2,8 @@
2 2
 
3 3
 package io.zipcoder.casino;
4 4
 
5
+import com.sun.tools.example.debug.expr.ASCII_UCodeESC_CharStream;
6
+
5 7
 import java.util.*;
6 8
 
7 9
 public class ScoreSheet {
@@ -18,49 +20,101 @@ public class ScoreSheet {
18 20
 
19 21
     public int getTotalScore() {
20 22
 
21
-        int topTotalScore = 0;
22
-        int bottomTotalScore = 0;
23
-        int index = 0;
24
-        for(ROW r : ROW.values()) {
25
-            while (index < 6) {
26
-                topTotalScore += scores.get(r);
27
-                index++;
28
-            }
29
-            if (topTotalScore >= 63) {
30
-                topTotalScore += 35;
31
-            }
32
-            while (index >= 6) {
33
-                bottomTotalScore += scores.get(r);
34
-                index++;
35
-            }
36
-        }
37
-        return topTotalScore + bottomTotalScore;
23
+        int totalScore = 0;
24
+        totalScore = (totalScore + getScore(ScoreSheet.ROW.ACES) + getScore(ScoreSheet.ROW.TWOS) + getScore(ScoreSheet.ROW.THREES) + getScore(ScoreSheet.ROW.FOURS) + getScore(ScoreSheet.ROW.FIVES) + getScore(ScoreSheet.ROW.SIXES));
25
+        if (totalScore >= 63) {
26
+            totalScore += 35;
38 27
         }
28
+        totalScore = (totalScore + getScore(ScoreSheet.ROW.THREEOFAKIND) + getScore(ScoreSheet.ROW.FOUROFAKIND) + getScore(ScoreSheet.ROW.FULLHOUSE) + getScore(ScoreSheet.ROW.SMALLSTRAIGHT) + getScore(ScoreSheet.ROW.LARGESTRAIGHT) + getScore(ScoreSheet.ROW.YAHTZEE) + getScore(ScoreSheet.ROW.CHANCE));
29
+
30
+        return totalScore;
31
+    }
39 32
 
40
-    public void printScoreCard(){
41
-        System.out.println(
42
-                "1. Aces: Totals all Ones ** Score **" + getScore(ScoreSheet.ROW.ACES) + "\n" +
43
-                "2. Twos: Totals all Twos ** Score **" + getScore(ScoreSheet.ROW.TWOS) + "\n" +
44
-                "3. Threes: Totals all Threes ** Score **" + getScore(ScoreSheet.ROW.THREES) + "\n" +
45
-                "4. Fours: Totals all Fours ** Score **" + getScore(ScoreSheet.ROW.FOURS) + "\n" +
46
-                "5. Fives: Totals all Fives ** Score **" + getScore(ScoreSheet.ROW.FIVES) + "\n" +
47
-                "6. Sixes: Totals all Sixes ** Score **" + getScore(ScoreSheet.ROW.SIXES) + "\n" +
48
-                "7. 3 of a Kind ** Score **" + getScore(ScoreSheet.ROW.THREEOFAKIND) + "\n" +
49
-                "8. 4 of a Kind ** Score **" + getScore(ScoreSheet.ROW.FOUROFAKIND) + "\n" +
50
-                "9. Full House ** Score **" + getScore(ScoreSheet.ROW.FULLHOUSE) + "\n" +
51
-                "10. Small Straight: Sequence of 4 ** Score **" + getScore(ScoreSheet.ROW.SMALLSTRAIGHT) + "\n" +
52
-                "11. Large Striaght: Sequence of 5 ** Score **" + getScore(ScoreSheet.ROW.LARGESTRAIGHT) + "\n" +
53
-                "12. Yahtzee: 5 of a Kind ** Score **" + getScore(ScoreSheet.ROW.YAHTZEE) + "\n" +
54
-                "13. Chance: Sum of Dice ** Score **" + getScore(ScoreSheet.ROW.CHANCE) + "\n" +
55
-                "****************** TOTAL SCORE ******************" + getTotalScore());
56 33
 
34
+    public void printScoreCard(){
35
+        System.out.print("1. Aces: Totals all Ones ** Score ** ");
36
+        if(getScore(ScoreSheet.ROW.ACES) != null) {
37
+            System.out.println(getScore(ScoreSheet.ROW.ACES));
38
+        } else {
39
+            System.out.println("open");
40
+        }
41
+        System.out.print("2. Twos: Totals all Twos ** Score ** ");
42
+        if(getScore(ScoreSheet.ROW.TWOS) != null) {
43
+            System.out.println(getScore(ScoreSheet.ROW.TWOS));
44
+        } else {
45
+            System.out.println("open");
46
+        }
47
+        System.out.print( "3. Threes: Totals all Threes ** Score ** ");
48
+        if(getScore(ScoreSheet.ROW.THREES) != null) {
49
+            System.out.println(getScore(ScoreSheet.ROW.THREES));
50
+        } else {
51
+            System.out.println("open");
52
+        }
53
+        System.out.print( "4. Fours: Totals all Fours ** Score ** ");
54
+        if(getScore(ScoreSheet.ROW.FOURS) != null) {
55
+            System.out.println(getScore(ScoreSheet.ROW.FOURS));
56
+        } else {
57
+            System.out.println("open");
58
+        }
59
+        System.out.print( "5. Fives: Totals all Fives ** Score ** ");
60
+        if(getScore(ScoreSheet.ROW.FIVES) != null) {
61
+            System.out.println(getScore(ScoreSheet.ROW.FIVES));
62
+        } else {
63
+            System.out.println("open");
64
+        }
65
+        System.out.print( "6. Sixes: Totals all Sixes ** Score ** ");
66
+        if(getScore(ScoreSheet.ROW.SIXES) != null) {
67
+            System.out.println(getScore(ScoreSheet.ROW.SIXES));
68
+        } else {
69
+            System.out.println("open");
70
+        }
71
+        System.out.print( "7. 3 of a Kind ** Score ** ");
72
+        if(getScore(ScoreSheet.ROW.THREEOFAKIND) != null) {
73
+            System.out.println(getScore(ScoreSheet.ROW.THREEOFAKIND));
74
+        } else {
75
+            System.out.println("open");
76
+        }
77
+        System.out.print( "8. 4 of a Kind ** Score ** ");
78
+        if(getScore(ScoreSheet.ROW.FOUROFAKIND) != null) {
79
+            System.out.println(getScore(ScoreSheet.ROW.FOUROFAKIND));
80
+        } else {
81
+            System.out.println("open");
82
+        }
83
+        System.out.print( "9. Full House ** Score ** ");
84
+        if(getScore(ScoreSheet.ROW.FULLHOUSE) != null) {
85
+            System.out.println(getScore(ScoreSheet.ROW.FULLHOUSE));
86
+        } else {
87
+            System.out.println("open");
88
+        }
89
+        System.out.print("10. Small Straight: Sequence of 4 ** Score ** ");
90
+        if(getScore(ScoreSheet.ROW.SMALLSTRAIGHT) != null) {
91
+            System.out.println(getScore(ScoreSheet.ROW.SMALLSTRAIGHT));
92
+        } else {
93
+            System.out.println("open");
94
+        }
95
+        System.out.print("11. Large Striaght: Sequence of 5 ** Score ** ");
96
+        if(getScore(ScoreSheet.ROW.LARGESTRAIGHT) != null) {
97
+            System.out.println(getScore(ScoreSheet.ROW.LARGESTRAIGHT));
98
+        } else {
99
+            System.out.println("open");
100
+        }
101
+        System.out.print("12. Yahtzee: 5 of a Kind ** Score ** ");
102
+        if(getScore(ScoreSheet.ROW.YAHTZEE) != null) {
103
+            System.out.println(getScore(ScoreSheet.ROW.YAHTZEE));
104
+        } else {
105
+            System.out.println("open");
106
+        }
107
+        System.out.print( "13. Chance: Sum of Dice ** Score ** ");
108
+        if(getScore(ScoreSheet.ROW.CHANCE) != null) {
109
+            System.out.println(getScore(ScoreSheet.ROW.CHANCE));
110
+        } else {
111
+            System.out.println("open");
112
+        }
113
+        System.out.println();
57 114
     }
58 115
 
59 116
     public void setRow(ROW row, Dice[] cup){
60 117
 
61
-        if(scores.get(row) != null) {
62
-            System.out.println("Error, you have already filled that row");
63
-        } else {
64 118
             ArrayList<Integer> numbers = new ArrayList<>();
65 119
             for (Dice d : cup) {
66 120
                 numbers.add(d.getValue());
@@ -134,7 +188,7 @@ public class ScoreSheet {
134 188
             }
135 189
         }
136 190
 
137
-    }
191
+
138 192
 
139 193
     public boolean checkFullHouse(ArrayList<Integer> numbers) {
140 194
 

+ 7
- 10
src/main/java/io/zipcoder/casino/Stud.java 查看文件

@@ -5,7 +5,7 @@ public class Stud extends CardGame implements Gamble, Game {
5 5
         super(minBet, maxBet, ante);
6 6
     }
7 7
 
8
-    public void Deal() {
8
+    public void deal() {
9 9
 
10 10
     }
11 11
 
@@ -22,31 +22,28 @@ public class Stud extends CardGame implements Gamble, Game {
22 22
      * Below 3 Implemented from Gamble
23 23
      * @param betAmount
24 24
      */
25
-    public void Bet(int betAmount) {
25
+    public void bet(int betAmount) {
26 26
 
27 27
     }
28 28
 
29
-    public int Payout(int payoutAmount) {
30
-        return 0;
31
-    }
32
-
33
-    public void Ante(int anteAmount) {
29
+    public void payout() {
34 30
 
35 31
     }
36 32
 
33
+
37 34
     /**
38 35
      * Below 3 Implemented from Game
39 36
      */
40 37
 
41
-    public void Quit() {
38
+    public void quit() {
42 39
 
43 40
     }
44 41
 
45
-    public void StartGame() {
42
+    public void startGame() {
46 43
 
47 44
     }
48 45
 
49
-    public void StartRound() {
46
+    public void startRound() {
50 47
 
51 48
     }
52 49
 }

+ 7
- 8
src/main/java/io/zipcoder/casino/War.java 查看文件

@@ -33,15 +33,14 @@ public class War extends CardGame implements Gamble, Game {
33 33
     /**
34 34
      * Below 3 Implemented from Gamble
35 35
      */
36
-    public void Bet(int betAmount) {
36
+    public void bet(int betAmount) {
37 37
         super.changeTablePot(betAmount);
38 38
     }
39 39
 
40
-    public int Payout(int payoutAmount) {
40
+    public void payout() {
41 41
         if(super.getWinner() != null){
42 42
             super.getWinner().changeBalance(super.getTablePot());
43 43
         }
44
-        return 0;
45 44
     }
46 45
 
47 46
     public void payAnte() {
@@ -56,17 +55,17 @@ public class War extends CardGame implements Gamble, Game {
56 55
      * Below 3 Implemented from Game
57 56
      */
58 57
 
59
-    public void Quit() {
58
+    public void quit() {
60 59
 
61 60
     }
62 61
 
63
-    public void StartGame() {
62
+    public void startGame() {
64 63
         Deck deck = new Deck();
65 64
         payAnte();
66
-        Deal();
65
+        deal();
67 66
     }
68 67
 
69
-    public void StartRound() {
68
+    public void startRound() {
70 69
         //player plays a card faceup
71 70
         //remove cards from player hand
72 71
         //pc plays a card faceup
@@ -79,7 +78,7 @@ public class War extends CardGame implements Gamble, Game {
79 78
         //insert discard into hand facedown
80 79
     }
81 80
 
82
-    public void Deal() {
81
+    public void deal() {
83 82
         //while there are cards in the deck
84 83
         while(super.getDeck().size() != 0){
85 84
             //for each player playing the game

+ 121
- 54
src/main/java/io/zipcoder/casino/Yahtzee.java 查看文件

@@ -3,46 +3,26 @@ package io.zipcoder.casino;
3 3
 import java.util.ArrayList;
4 4
 import java.util.Scanner;
5 5
 
6
-public class Yahtzee extends DiceGame implements Game{
6
+public class Yahtzee extends DiceGame implements Game, Gamble{
7 7
 
8
-    private Dice[] cup;
9 8
     DicePlayer dicePlayer;
10 9
     private Scanner scanner = new Scanner(System.in);
10
+    int betAmount = 0;
11 11
 
12 12
     public Yahtzee(Player player) {
13 13
         this.dicePlayer = new DicePlayer(player);
14 14
     }
15 15
 
16
-    public void compareScore(DicePlayer player1, DicePlayer player2){
17
-
18
-        player1.getScoreSheet().getTotalScore();
16
+    public int getBid() {
17
+        return betAmount;
19 18
     }
20 19
 
21
-    public void reRoll(String diceToRoll){
22
-
23
-        String[] numbersString = diceToRoll.replace(",", "").split(" ");
24
-        ArrayList<Integer> numbers = new ArrayList<>();
25
-        for(String s: numbersString) {
26
-            numbers.add(Integer.parseInt(s));
27
-        }
28
-        for(Integer i : numbers) {
29
-            for(int j = 0; j < 5; j++) {
30
-                if(i == dicePlayer.getCup()[j].getValue()) {
31
-                    dicePlayer.getCup()[j].roll();
32
-                    break;
33
-                }
34
-            }
35
-        }
20
+    public void setBid(int bid) {
21
+        this.betAmount = bid;
36 22
     }
37 23
 
38
-    /**
39
-     * implemented from 'Game'
40
-     */
41
-    public void Quit() {
42 24
 
43
-    }
44
-
45
-    public void StartGame() {
25
+    public void startGame() {
46 26
         Dice dice1 = new Dice();
47 27
         Dice dice2 = new Dice();
48 28
         Dice dice3 = new Dice();
@@ -54,14 +34,25 @@ public class Yahtzee extends DiceGame implements Game{
54 34
         dicePlayer.getCup()[2] = dice3;
55 35
         dicePlayer.getCup()[3] = dice4;
56 36
         dicePlayer.getCup()[4] = dice5;
37
+
38
+        System.out.println("How much would you like to bet on this game?");
39
+        int betAmount = scanner.nextInt();
40
+        setBid(betAmount);
41
+        bet(betAmount);
42
+
43
+        startRound();
44
+        System.out.println("You scored " + dicePlayer.getScoreSheet().getTotalScore() + " points.");
45
+        payout();
46
+        //Return to console.
57 47
     }
58 48
 
59
-    public void StartRound() {
49
+    public void startRound() {
60 50
 
61 51
         for(int i = 0; i < ScoreSheet.getSize(); i++) {
62 52
             for (Dice d : dicePlayer.getCup()) {
63 53
                 d.roll();
64 54
             }
55
+            System.out.println("You rolled:");
65 56
             dicePlayer.printCup();
66 57
 
67 58
             roundRoutine();
@@ -70,13 +61,77 @@ public class Yahtzee extends DiceGame implements Game{
70 61
 
71 62
     }
72 63
 
64
+    public void roundRoutine(){
65
+
66
+        giveOptions();
67
+        giveOptions();
68
+
69
+    }
70
+
71
+    public void giveOptions() {
72
+        int choice = 0;
73
+        System.out.println("Would you like to:\n1. Roll all dice again.\n2. Roll some dice again.\n3. Stop rolling and score.");
74
+
75
+        Scanner in = new Scanner(System.in);
76
+        choice = in.nextInt();
77
+
78
+        switch (choice) {
79
+            case 1:
80
+                for (Dice d : dicePlayer.getCup()) {
81
+                    d.roll();
82
+                }
83
+                System.out.println("You rolled:");
84
+                dicePlayer.printCup();
85
+                System.out.println();
86
+                break;
87
+
88
+            case 2:
89
+                System.out.println("Which numbers would you like to reroll? List the numbers separated by spaces.");
90
+                Scanner in2 = new Scanner(System.in);
91
+                String diceToRoll = in2.next();
92
+                reRoll(diceToRoll);
93
+                System.out.println("You rolled:");
94
+                dicePlayer.printCup();
95
+                break;
96
+
97
+            case 3:
98
+                break;
99
+        }
100
+    }
101
+
102
+    public void reRoll(String diceToRoll){
103
+
104
+        String[] numbersString = diceToRoll.replace(",", "").split(" ");
105
+        ArrayList<Integer> numbers = new ArrayList<>();
106
+        for(String s: numbersString) {
107
+            numbers.add(Integer.parseInt(s));
108
+        }
109
+        for(Integer i : numbers) {
110
+            for(int j = 0; j < 5; j++) {
111
+                if(i == dicePlayer.getCup()[j].getValue()) {
112
+                    dicePlayer.getCup()[j].roll();
113
+                    break;
114
+                }
115
+            }
116
+        }
117
+    }
118
+
73 119
     public void recordingScore() {
120
+
121
+        boolean validEntry = true;
122
+        int choice = 13;
123
+        ScoreSheet.ROW row = ScoreSheet.ROW.CHANCE;
124
+
125
+        while(validEntry) {
74 126
             System.out.println("Which row would you like to apply your turn to on the scoresheet?.\n" +
75 127
                     "Remember you can only use each row once!");
128
+            System.out.println();
76 129
             dicePlayer.getScoreSheet().printScoreCard();
130
+            System.out.println();
131
+
132
+            Scanner scanner2 = new Scanner(System.in);
133
+            choice = scanner2.nextInt();
77 134
 
78
-            int choice = scanner.nextInt();
79
-            ScoreSheet.ROW row = ScoreSheet.ROW.CHANCE;
80 135
             switch(choice) {
81 136
                 case 1: row = ScoreSheet.ROW.ACES;
82 137
                     break;
@@ -105,35 +160,47 @@ public class Yahtzee extends DiceGame implements Game{
105 160
                 case 13: row = ScoreSheet.ROW.CHANCE;
106 161
                     break;
107 162
             }
108
-            dicePlayer.getScoreSheet().setRow(row, dicePlayer.getCup());
163
+            if (dicePlayer.getScoreSheet().getScore(row) == null) {
164
+                validEntry = false;
165
+            } else {
166
+                System.out.println("Error, you have already filled that row");
167
+            }
109 168
         }
110 169
 
170
+        dicePlayer.getScoreSheet().setRow(row, dicePlayer.getCup());
171
+        System.out.println();
172
+        dicePlayer.getScoreSheet().printScoreCard();
173
+        }
111 174
 
112
-    public void roundRoutine(){
113
-
114
-        for(int i = 0; i < 2; i++) {
115
-            System.out.println("Would you like to:\n1. Roll all dice again.\n2. Roll some dice again.\n3.Stop rolling and score.");
116
-            int choice = scanner.nextInt();
117
-
118
-            switch (choice) {
119
-                case 1:
120
-                    for (Dice d : dicePlayer.getCup()) {
121
-                        d.roll();
122
-                    }
123
-                    dicePlayer.printCup();
124
-                    break;
125
-
126
-                case 2:
127
-                    System.out.println("Which numbers would you like to reroll?");
128
-                    String diceToRoll = scanner.next();
129
-                    reRoll(diceToRoll);
130
-                    dicePlayer.printCup();
131
-                    break;
175
+    @Override
176
+    public void bet(int betAmount) {
177
+        dicePlayer.getPlayer().changeBalance(betAmount);
178
+    }
132 179
 
133
-                case 3:
134
-                    break;
135
-            }
180
+    @Override
181
+    public void payout() {
182
+        int score = dicePlayer.getScoreSheet().getTotalScore();
183
+        int payOut = 0;
184
+        if(score == 1575) {
185
+            payOut = getBid() * 100;
186
+        } else if (score > 1000) {
187
+            payOut = getBid() * 20;
188
+        } else if (score > 500) {
189
+            payOut = getBid() * 10;
190
+        } else if (score > 400) {
191
+            payOut = getBid() * 5;
192
+        } else if (score > 300) {
193
+            payOut = getBid() * 2;
194
+        } else if (score > 200) {
195
+            payOut = getBid();
196
+        } else {
197
+            payOut = 0;
136 198
         }
199
+        dicePlayer.getPlayer().changeBalance(payOut);
200
+        System.out.println("You won $" + payOut);
201
+    }
137 202
 
203
+    public void quit() {
204
+        //Return to console
138 205
     }
139 206
 }