Jason Gibbs 6 år sedan
förälder
incheckning
c8d100235e

+ 41
- 29
src/main/java/io/zipcoder/casino/BlackJack.java Visa fil

@@ -3,8 +3,9 @@ import java.util.ArrayList;
3 3
 import java.util.Arrays;
4 4
 import java.util.Scanner;
5 5
 
6
-public class BlackJack {
6
+public class BlackJack extends CardGame {
7 7
     private ArrayList<Card> playerHand = new ArrayList<Card>();
8
+    private ArrayList<Card> playerTwoHand = new ArrayList<Card>();
8 9
     private ArrayList<Card> dealerHand = new ArrayList<Card>();
9 10
     private int currentBet;
10 11
     private boolean playingBJ = false;
@@ -15,8 +16,12 @@ public class BlackJack {
15 16
     static String name;
16 17
     static String choice;
17 18
 
18
-    public BlackJack() {
19
-        this.player = new Player("Eugene");
19
+    public BlackJack(Player player) {
20
+        super(player);
21
+    }
22
+
23
+    public BlackJack(Player player, Player playerTwo) {
24
+        super(player, playerTwo);
20 25
     }
21 26
 
22 27
     public void start() {
@@ -24,13 +29,13 @@ public class BlackJack {
24 29
         this.deck.shuffle();
25 30
     }
26 31
 
27
-    private void dealToPlayerHand(int amountOfCards) {
32
+    public void dealToPlayerHand(int amountOfCards) {
28 33
         for (int i = 0; i < amountOfCards; i++) {
29 34
             this.playerHand.add(deck.draw());
30 35
         }
31 36
     }
32 37
 
33
-    private void dealToDealerHand(int amountOfCards) {
38
+    public void dealToDealerHand(int amountOfCards) {
34 39
         for (int i = 0; i < amountOfCards; i++) {
35 40
             this.dealerHand.add(deck.draw());
36 41
         }
@@ -55,14 +60,18 @@ public class BlackJack {
55 60
     }
56 61
 
57 62
 
58
-    private int getCurrentBet() {
63
+    public int getCurrentBet() {
59 64
         return this.currentBet;
60 65
     }
61 66
 
62
-    private void setCurrentBet(int bet) {
67
+    public void setCurrentBet(int bet) {
63 68
         this.currentBet = bet;
64 69
     }
65 70
 
71
+    public void winBet(int winnings) { player.addToBankroll(winnings); }
72
+
73
+    public void loseBet(int losings) { player.subtractFromBankroll(losings); }
74
+
66 75
     private void setAskToPlayAgain(boolean bool) {
67 76
         this.askToPlayAgain = bool;
68 77
     }
@@ -136,6 +145,8 @@ public class BlackJack {
136 145
         return totalScore;
137 146
     }
138 147
 
148
+    public void endGame() { playingBJ = false; }
149
+
139 150
     public void startGame() {
140 151
         start();
141 152
         Console.output("Starting BlackJack...");
@@ -145,9 +156,10 @@ public class BlackJack {
145 156
                 if (answer.equals("y")) {
146 157
                     setPlayingHands(true);
147 158
                     setAskToPlayAgain(false);
148
-                } else {
159
+                } else if ("n".equalsIgnoreCase(answer)){
149 160
                     setPlayingHands(false);
150 161
                     setAskToPlayAgain(false);
162
+                    endGame();
151 163
                 }
152 164
             }
153 165
             while (playingHands) {
@@ -159,11 +171,11 @@ public class BlackJack {
159 171
 
160 172
                 if (calcHand(dealerHand) == 21) {
161 173
                     Console.output("Dealer has BlackJack! You lose!");
162
-                    player.subtractFromBankroll(currentBet);
174
+                    loseBet(currentBet);
163 175
                     startNextHand();
164 176
                 } else if (calcHand(playerHand) == 21) {
165 177
                     Console.output("You have BlackJack! You win!");
166
-                    player.addToBankroll((currentBet * 3) / 2);
178
+                    winBet(currentBet * 3 / 2);
167 179
                     startNextHand();
168 180
                 }
169 181
 
@@ -215,25 +227,25 @@ public class BlackJack {
215 227
         }
216 228
     }
217 229
 
218
-    public static void main(String[] args) {
219
-        System.out.println("Welcome to Eugene's Casino, where your money is OUR money!");
220
-        System.out.println("What's your name?");
221
-        Scanner scan = new Scanner(System.in);
222
-        Player play = new Player(name);
223
-       // player.setName(name);
224
-        System.out.println("Welcome " + name + "! Which game would you like to play?");
225
-        System.out.println("1: BlackJack");
226
-        System.out.println("2: Baccarat");
227
-        System.out.println("3: Craps");
228
-        choice = scan.nextLine();
229
-        if (choice.equalsIgnoreCase("1")) {
230
-            BlackJack bj = new BlackJack();
231
-            bj.startGame();
232
-        } else {
233
-            System.out.println("This game is not available. Please choose again");
234
-        }
235
-
236
-    }
230
+//    public static void main(String[] args) {
231
+//        System.out.println("Welcome to Eugene's Casino, where your money is OUR money!");
232
+//        System.out.println("What's your name?");
233
+//        Scanner scan = new Scanner(System.in);
234
+//        Player play = new Player(name);
235
+//       // player.setName(name);
236
+//        System.out.println("Welcome " + name + "! Which game would you like to play?");
237
+//        System.out.println("1: BlackJack");
238
+//        System.out.println("2: Baccarat");
239
+//        System.out.println("3: Craps");
240
+//        choice = scan.nextLine();
241
+//        if (choice.equalsIgnoreCase("1")) {
242
+//            BlackJack bj = new BlackJack(player);
243
+//            bj.startGame();
244
+//        } else {
245
+//            System.out.println("This game is not available. Please choose again");
246
+//        }
247
+//
248
+//    }
237 249
 }
238 250
 
239 251
 

+ 4
- 4
src/main/java/io/zipcoder/casino/Card.java Visa fil

@@ -37,13 +37,13 @@ public class Card {
37 37
         if (!isPrivate) {
38 38
             String describe = "";
39 39
             if (suit.equals(Suit.SPADES)) {
40
-                describe = rank + "\u2660 of " + suit;
40
+                describe = rank + " of \u2660 "; //+ suit;
41 41
             } else if (suit.equals(Suit.CLUBS)) {
42
-                describe = rank + "\u2663 of " + suit;
42
+                describe = rank + " of \u2663 "; //+ suit;
43 43
             } else if (suit.equals(Suit.DIAMONDS)) {
44
-                describe = rank + "\u2666 of " + suit;
44
+                describe = rank + " of \u2666 ";// + suit;
45 45
             } else if (suit.equals(Suit.HEARTS)) {
46
-                describe = rank + "\u2665 of " + suit;
46
+                describe = rank + " of \u2665 ";// + suit;
47 47
             }
48 48
             return describe;
49 49
         } else {

+ 21
- 16
src/main/java/io/zipcoder/casino/CardGame.java Visa fil

@@ -1,36 +1,41 @@
1 1
 package io.zipcoder.casino;
2 2
 
3
+import java.util.ArrayList;
4
+
3 5
 public abstract class CardGame implements Game, Gamble {
4 6
 
5
-    int playerHand;
6
-    int dealerHand;
7
-    int currentBet;
8
-    Player player;
9
-    CardDeck deck;
7
+    protected ArrayList<Card> playerHand;
8
+    protected ArrayList<Card> playerTwoHand;
9
+    protected ArrayList<Card> dealerHand;
10
+    protected Player player;
11
+    protected Player playerTwo;
12
+    protected CardDeck deck;
13
+    protected int currentBet;
10 14
 
11 15
     public CardGame(Player player) {
12
-
13 16
         this.player = player;
14 17
         this.deck = new CardDeck();
15
-
18
+        this.playerHand = new ArrayList<Card>();
19
+        this.dealerHand = new ArrayList<Card>();
16 20
     }
17 21
 
22
+    public CardGame(Player player, Player playerTwo) {
23
+        this.player = player;
24
+        this.playerTwo = playerTwo;
25
+        this.deck = new CardDeck();
26
+        this.playerHand = new ArrayList<Card>();
27
+        this.playerTwoHand = new ArrayList<Card>();
28
+        this.dealerHand = new ArrayList<Card>();
29
+    }
18 30
 
19
-    public abstract int drawCard();
20
-
21
-    public abstract void stand();
31
+    public abstract void dealToPlayerHand(int amountOfCards);
22 32
 
23 33
     public abstract void startGame();
24 34
 
25 35
     public abstract void endGame();
26 36
 
27
-    public abstract void declareWinner();
28
-
29
-    public abstract void bet(int currentBet);
30
-
31
-    public abstract void winBet();
37
+//    public abstract void declareWinner();
32 38
 
33
-    public abstract void loseBet(Player player);
34 39
 
35 40
 
36 41
 }

+ 27
- 12
src/main/java/io/zipcoder/casino/Casino.java Visa fil

@@ -5,26 +5,42 @@ public class Casino {
5 5
 
6 6
     private Player player;
7 7
     private String choice;
8
-    private Console console;
9
-    //GameFactory gf;
8
+    private String numberOfPlayers;
9
+    private boolean isPlaying = false;
10 10
 
11 11
     public void startCasino() {
12
+
13
+        System.out.println("Welcome to Eugene's Casino, Where Your Money is OUR Money!");
14
+
15
+//        numberOfPlayers = Console.askForInput("How many players?");
16
+        Player player = new Player();
17
+        player.setName(Console.askForInput("What is your name?"));
18
+        Console.output("Hello " + player.getName() + ". Your bankroll is: $" + player.getBankroll());
19
+
20
+//        if ("2".equalsIgnoreCase(numberOfPlayers)) {
21
+//            Player playerTwo = new Player();
22
+//            playerTwo.setName(Console.askForInput("What is your name, player two?"));
23
+//        }
24
+
12 25
         do {
13
-            System.out.println("Welcome to the Zip Code Casino!");
14 26
             System.out.println("Which game would you like to play? ");
15 27
             System.out.println("1: BlackJack");
16 28
             System.out.println("2: Baccarat");
17 29
             choice = Console.askForInput("3: Craps");
18
-            System.out.println(choice);
19
-
20
-            if (choice.equalsIgnoreCase("1")) {
21
-                BlackJack blackjack = new BlackJack();
22
-                blackjack.start();
23
-
30
+            GameFactory.getGame(choice, player);
31
+            String play = Console.askForInput("Would you like to keep playing in the Casino? Y/N ");
32
+            Console.output("Your bankroll is $" + player.getBankroll());
33
+
34
+            if ("y".equalsIgnoreCase(play)) {
35
+                isPlaying = true;
36
+            } else if ("n".equalsIgnoreCase(play)) {
37
+                isPlaying = false;
24 38
             }
25
-        } while (false);
26 39
 
27
-        }
40
+        } while (isPlaying);
41
+    }
42
+
43
+}
28 44
 
29 45
 
30 46
 //    public static void main(String[] args) {
@@ -38,4 +54,3 @@ public class Casino {
38 54
 //        Casino cas = new Casino();
39 55
 //        cas.startCasino();
40 56
 //    }
41
-}

+ 2
- 0
src/main/java/io/zipcoder/casino/Console.java Visa fil

@@ -28,4 +28,6 @@ public class Console {
28 28
         System.out.println(stringFromNumber(output));
29 29
         return stringFromNumber(output);
30 30
     }
31
+
32
+
31 33
 }

+ 150
- 0
src/main/java/io/zipcoder/casino/Craps.java Visa fil

@@ -0,0 +1,150 @@
1
+package io.zipcoder.casino;
2
+
3
+public class Craps implements Game, Gamble {
4
+
5
+    private Dice dice;
6
+    private Player player;
7
+    private int point;
8
+    private int sixEightBet;
9
+    private int crapsBet;
10
+    private int currentBet;
11
+    private boolean isPlaying;
12
+    private boolean askToPlayAgain;
13
+    private boolean pointIsMet = false;
14
+
15
+    public Craps(Player player) {
16
+        this.player = player;
17
+        dice = new Dice(2);
18
+        currentBet = 0;
19
+        isPlaying = true;
20
+    }
21
+
22
+    public int getSixEightBet() { return this.sixEightBet; }
23
+
24
+    public void setSixEightBet(int amount) { this.sixEightBet = amount; }
25
+
26
+    public int getPoint() { return this.point; }
27
+
28
+    public int getCrapsBet() { return this.crapsBet; }
29
+
30
+    public void setCrapsBet(int amount) { this.crapsBet = amount; }
31
+
32
+    public void setIsPlaying(boolean bool) { this.isPlaying = bool; }
33
+
34
+    public void setAskToPlayAgain(boolean askToPlayAgain) { this.askToPlayAgain = askToPlayAgain; }
35
+
36
+    public void setPointIsMet(boolean bool) { this.pointIsMet = bool; }
37
+
38
+    public int getCurrentBet() {
39
+        return this.currentBet;
40
+    }
41
+
42
+    public void setCurrentBet(int bet) {
43
+        this.currentBet = bet;
44
+    }
45
+
46
+    public void winBet(int winnings) {
47
+        player.addToBankroll(winnings);
48
+    }
49
+
50
+    public void loseBet(int losings) {
51
+        player.subtractFromBankroll(losings);
52
+    }
53
+
54
+    public int comeOutRoll(int currentBet) {
55
+        point = dice.getSum();
56
+        if (point == 2 || point == 3 || point == 12) {
57
+            Console.output("Roll Again");
58
+            comeOutRoll(currentBet);
59
+        } else if (point == 7 || point == 11) {
60
+            Console.output("You win!");
61
+            winBet(currentBet * 2);
62
+            setPointIsMet(true);
63
+            endGame();
64
+        }
65
+
66
+        return point;
67
+    }
68
+
69
+    public int roll(int currentBet) {
70
+        int temp = dice.getSum();
71
+        while (!pointIsMet) {
72
+            if (sixEightBet > 0) {
73
+                if (temp == 6 || temp == 8) {
74
+                    winBet(currentBet);
75
+                }
76
+            } else if (crapsBet > 0) {
77
+                if (temp == 2 || temp == 3 || temp == 12) {
78
+                    winBet(currentBet * 4);
79
+                } else {
80
+                    loseBet(currentBet);
81
+                }
82
+            } else if (temp == 7) {
83
+                Console.output("Crapped Out! Sorry you lose");
84
+                loseBet(currentBet);
85
+                setPointIsMet(true);
86
+            } else if (temp == point) {
87
+                Console.output("Point! You win " + currentBet);
88
+                setPointIsMet(true);
89
+                winBet(currentBet);
90
+                Console.output("Your bankroll is " + player.getBankroll());
91
+            } else {
92
+                roll(currentBet);
93
+            }
94
+        }
95
+        return temp;
96
+    }
97
+
98
+    public void start() {
99
+        while(isPlaying) {
100
+            while(pointIsMet) {
101
+
102
+            }
103
+        }
104
+    }
105
+
106
+    public void startGame() {
107
+        Craps craps = new Craps(player);
108
+        setPointIsMet(false);
109
+        while (isPlaying) {
110
+            setCurrentBet(Console.numberFromString(Console.askForInput("Starting bet amount: ")));
111
+            String ready = Console.askForInput("Ready to Roll? (Y/N)");
112
+            if (ready.equals("y")) {
113
+                comeOutRoll(currentBet);
114
+            }
115
+
116
+            Console.output("Your point is " + point);
117
+            String choose = Console.askForInput("Would you like to bet a 6 - 8? (Y/N)");
118
+            if (choose.equals("y")) {
119
+                setSixEightBet(Console.numberFromString(Console.askForInput("Side bet amount: ")));
120
+            }
121
+            choose = Console.askForInput("Would you like to bet for craps? (Y/N)");
122
+            if (choose.equals("y")) {
123
+                setCrapsBet(Console.numberFromString(Console.askForInput("Side bet amount: ")));
124
+            }
125
+            if (!pointIsMet) {
126
+                ready = Console.askForInput("Ready to Roll? (Y/N)");
127
+                if (ready.equals("y")) {
128
+                    roll(currentBet);
129
+                } else if (ready.equalsIgnoreCase("n")) {
130
+                    setIsPlaying(false);
131
+
132
+                }
133
+            }
134
+            choose = Console.askForInput("Keep playing? (Y/N)");
135
+            if (choose.equals("y")) {
136
+                setIsPlaying(true);
137
+                setPointIsMet(false);
138
+            } else if (choose.equals("n")) {
139
+                endGame();
140
+
141
+            }
142
+
143
+
144
+        }
145
+    }
146
+
147
+    public void endGame() { this.isPlaying = false; }
148
+
149
+
150
+}

+ 23
- 24
src/main/java/io/zipcoder/casino/Dice.java Visa fil

@@ -1,38 +1,37 @@
1 1
 package io.zipcoder.casino;
2 2
 
3
-import java.util.Random;
3
+    public class Dice {
4 4
 
5
-    public class Dice
6
-    {
7
-        private int dice;
8
-        private Random randomGenerator;
5
+        private Die[] dies;
6
+        private int numberOfDice;
9 7
 
10
-        public Dice(int numberOfDice)
11
-        {
12
-            dice = numberOfDice;
8
+        public Dice(int numberOfDice) {
9
+
10
+            this.numberOfDice = numberOfDice;
11
+            dies = new Die[this.numberOfDice];
12
+
13
+            for (int i = 0; i < this.numberOfDice; i++) {
14
+                dies[i] = new Die();
15
+            }
13 16
         }
14 17
 
15
-        public int getSum()
16
-        {
18
+        public int getSum() {
19
+
17 20
             int sumOfTosses = 0;
18
-            for (int i=0; i<dice; i++)
19
-            {
20
-                randomGenerator = new Random();
21
-                int resultOfToss = randomGenerator.nextInt(6)+1;
22
-                sumOfTosses += resultOfToss;
21
+            for (Die die : dies) {
22
+                sumOfTosses += die.toss();
23 23
             }
24
+            Console.output(sumOfTosses);
24 25
             return sumOfTosses;
25 26
         }
26 27
 
27
-        public int[] getEach()
28
-        {
29
-            int[] tosses = new int[dice];
30
-            for (int i=0; i<dice; i++)
31
-            {
32
-                randomGenerator = new Random();
33
-                tosses[i] = randomGenerator.nextInt(6)+1;
28
+        @Override
29
+        public String toString() {
30
+            String describe = "";
31
+            for (int i = 0; i < dies.length; i++) {
32
+                describe = Console.output("Die " + i + dies[i].getValue());
34 33
             }
35
-            return tosses;
34
+            return describe;
36 35
         }
37
-    }
38 36
 
37
+    }

+ 23
- 0
src/main/java/io/zipcoder/casino/Die.java Visa fil

@@ -0,0 +1,23 @@
1
+package io.zipcoder.casino;
2
+import java.util.Random;
3
+
4
+public class Die {
5
+
6
+    private Random randomGenerator;
7
+    private int value;
8
+
9
+    public Die() {
10
+        randomGenerator = new Random();
11
+        value = 1;
12
+    }
13
+
14
+    public int getValue() {
15
+        return value;
16
+    }
17
+
18
+    public int toss() {
19
+        int value = randomGenerator.nextInt(6) + 1;
20
+        return value;
21
+    }
22
+
23
+}

+ 4
- 2
src/main/java/io/zipcoder/casino/Gamble.java Visa fil

@@ -1,9 +1,11 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 public interface Gamble {
4
-     int currentBet = 0;
5
-     void bet(int bet);
4
+
5
+     void setCurrentBet(int bet);
6
+
6 7
      void winBet(int currentBet);
8
+
7 9
      void loseBet(int currentBet);
8 10
      
9 11
 }

+ 1
- 1
src/main/java/io/zipcoder/casino/Game.java Visa fil

@@ -8,7 +8,7 @@ public interface Game {
8 8
 
9 9
     void endGame();
10 10
 
11
-    void declareWinner();
11
+//    void declareWinner();
12 12
 
13 13
 
14 14
 }

+ 17
- 9
src/main/java/io/zipcoder/casino/GameFactory.java Visa fil

@@ -1,11 +1,19 @@
1 1
 package io.zipcoder.casino;
2 2
 
3
-//public class GameFactory {
4
-//    public static Game getGame( gameName, Player player) {
5
-//        gameName = Consoler.numberFromString(gameName);
6
-//        switch(gameName) {
7
-//            case 1:
8
-//
9
-//        }
10
-//    }
11
-//}
3
+public class GameFactory {
4
+
5
+    public static Game getGame(String gameName, Player player) {
6
+        gameName = Console.askForInput(gameName);
7
+
8
+        if ("1".equalsIgnoreCase(gameName)) {
9
+            Game black = new BlackJack(player);
10
+            black.startGame();
11
+            return black;
12
+        } else if ("3".equalsIgnoreCase(gameName)) {
13
+            Game crap = new Craps(player);
14
+            crap.startGame();
15
+            return crap;
16
+        }
17
+        return null;
18
+    }
19
+}

+ 5
- 0
src/main/java/io/zipcoder/casino/Main.java Visa fil

@@ -1,7 +1,12 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 public class Main {
4
+
4 5
     public static void main(String[] args){
6
+//        Dice dice = new Dice(2);
7
+//        Console.output(dice.getSum());
8
+//        Console.output(dice.getSum());
9
+//        Console.output(dice.getSum());
5 10
         Casino casino =  new Casino();
6 11
         casino.startCasino();
7 12
 

+ 10
- 0
src/main/java/io/zipcoder/casino/Player.java Visa fil

@@ -11,6 +11,11 @@ public class Player {
11 11
         bankroll = 1000;
12 12
     }
13 13
 
14
+    public Player() {
15
+        this.name = "Anonymous";
16
+        bankroll = 1000;
17
+    }
18
+
14 19
     public void setName(String name) {
15 20
         this.name = name;
16 21
     }
@@ -39,5 +44,10 @@ public class Player {
39 44
         return score;
40 45
     }
41 46
 
47
+    @Override
48
+    public String toString() {
49
+        return name + " Bankroll: $" + bankroll;
50
+    }
51
+
42 52
 
43 53
 }

+ 40
- 40
src/test/java/io/zipcoder/casino/DiceTest.java Visa fil

@@ -1,40 +1,40 @@
1
-package io.zipcoder.casino;
2
-
3
-import org.junit.Assert;
4
-import org.junit.Before;
5
-import org.junit.Test;
6
-
7
-public class DiceTest {
8
-
9
-    @Test
10
-    public void getSum1(){
11
-        Dice dice = new Dice(2);
12
-        int expected = 6;
13
-        int actual = dice.getSum();
14
-        Assert.assertEquals(expected, actual, 6);
15
-    }
16
-
17
-    @Test
18
-    public void getSum2(){
19
-        Dice dice = new Dice(5);
20
-        int expected = 13;
21
-        int actual = dice.getSum();
22
-        Assert.assertEquals(expected, actual, 12);
23
-    }
24
-
25
-    @Test
26
-    public void getEach1(){
27
-        Dice dice = new Dice(2);
28
-        int expected = 3;
29
-        int actual = dice.getEach()[1];
30
-        Assert.assertEquals(expected, actual, 3);
31
-    }
32
-
33
-    @Test
34
-    public void getEach2(){
35
-        Dice dice = new Dice(6);
36
-        int expected = 3;
37
-        int actual = dice.getEach()[3];
38
-        Assert.assertEquals(expected, actual, 3);
39
-    }
40
-}
1
+//package io.zipcoder.casino;
2
+//
3
+//import org.junit.Assert;
4
+//import org.junit.Before;
5
+//import org.junit.Test;
6
+//
7
+//public class DiceTest {
8
+//
9
+//    @Test
10
+//    public void getSum1(){
11
+//        Dice dice = new Dice(2);
12
+//        int expected = 6;
13
+//        int actual = dice.getSum();
14
+//        Assert.assertEquals(expected, actual, 6);
15
+//    }
16
+//
17
+//    @Test
18
+//    public void getSum2(){
19
+//        Dice dice = new Dice(5);
20
+//        int expected = 13;
21
+//        int actual = dice.getSum();
22
+//        Assert.assertEquals(expected, actual, 12);
23
+//    }
24
+//
25
+//    @Test
26
+//    public void getEach1(){
27
+//        Dice dice = new Dice(2);
28
+//        int expected = 3;
29
+//        int actual = dice.getEach()[1];
30
+//        Assert.assertEquals(expected, actual, 3);
31
+//    }
32
+//
33
+//    @Test
34
+//    public void getEach2(){
35
+//        Dice dice = new Dice(6);
36
+//        int expected = 3;
37
+//        int actual = dice.getEach()[3];
38
+//        Assert.assertEquals(expected, actual, 3);
39
+//    }
40
+//}