Jacqueline Joson 6 년 전
부모
커밋
8d6c596a57

+ 150
- 4
src/main/java/io/zipcoder/casino/cardgames/BlackJack.java 파일 보기

@@ -1,19 +1,33 @@
1 1
 package io.zipcoder.casino.cardgames;
2 2
 
3
+import io.zipcoder.casino.cardgames.cards.Card;
4
+import io.zipcoder.casino.cardgames.cards.CardHand;
5
+import io.zipcoder.casino.cardgames.cards.CardValue;
3 6
 import io.zipcoder.casino.cardgames.cards.Deck;
7
+import io.zipcoder.casino.utilities.Console;
4 8
 import io.zipcoder.casino.utilities.Gamble;
5 9
 import io.zipcoder.casino.player.BlackJackPlayer;
6 10
 import io.zipcoder.casino.player.Player;
7 11
 import io.zipcoder.casino.player.Players;
8 12
 
13
+import java.sql.SQLOutput;
9 14
 import java.util.ArrayList;
15
+import java.util.Vector;
10 16
 
11 17
 public class BlackJack extends CardGame implements Gamble {
18
+
12 19
     private Deck deck = new Deck();
13 20
     private ArrayList<BlackJackPlayer> blackJackPlayers = new ArrayList<>();
21
+    CardHand dealerHand = new CardHand();
22
+    long bet;
23
+
14 24
 
15 25
     public BlackJack(){
16 26
         readyPlayers();
27
+        dealCards(2);
28
+        for (BlackJackPlayer p : blackJackPlayers) {
29
+          run();
30
+        }
17 31
     }
18 32
 
19 33
     public void readyPlayers() {
@@ -21,19 +35,151 @@ public class BlackJack extends CardGame implements Gamble {
21 35
             blackJackPlayers.add(new BlackJackPlayer(player));
22 36
         }
23 37
     }
24
-    public void dealCards(Player player, int numberOfCards) {
25 38
 
39
+    public void run(){
40
+        for(int i = 0; i < blackJackPlayers.size(); i++){
41
+            play(blackJackPlayers.get(i),blackJackPlayers.get(i).getBet());
42
+        }
26 43
     }
27 44
 
28
-    public void revealCard() {
45
+    public boolean play(BlackJackPlayer currentPlayer, long bet) {
46
+        placeBet();
47
+        // this.bet = bet;
48
+
49
+        Console.println("The dealer is dealing cards to the players.");
50
+
51
+        dealerHand.add(deck.removeFirst());
52
+        dealerHand.add(deck.removeFirst());
53
+
54
+        Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
55
+
56
+        if (getSum(dealerHand) == 21){
57
+            Console.println("Dealer has +," +  dealerHand.display());
58
+            Console.println("You have " + currentPlayer.getHand().display());
59
+            Console.println("Dealer has blackjack. Dealer wins.");
60
+        }
61
+
62
+
63
+        Console.println(String.format("%s, you're up!", currentPlayer.getP().getName()));
64
+
65
+        if (getSum(currentPlayer.getHand()) == 21){
66
+            Console.println("Dealer has " + dealerHand.display());
67
+            Console.println("You have " + currentPlayer.getHand().display());
68
+            Console.println("You have blackjack. You win.");
69
+        }
70
+        
71
+        while(true) {
72
+
73
+            Console.println("You have: " + currentPlayer.getHand().display() + "\nYour sum is " + getSum(currentPlayer.getHand()));
74
+
75
+            String hitOrStand = Console.getStringInput("Do you want to Hit or Stand? \n Enter H for Hit or S for Stand");
76
+
77
+            while (getSum(currentPlayer.getHand()) < 21) {
78
+                if (hitOrStand.equalsIgnoreCase("H")) {
79
+                    dealCard(currentPlayer,1);
80
+                } else if (hitOrStand.equalsIgnoreCase("S")) {
81
+                    break;
82
+                } else {
83
+                    hitOrStand = Console.getStringInput("Invalid input. Please enter H for Hit or S for Stand");
84
+                }
85
+            }
86
+
87
+            Console.println("You have: " + currentPlayer.getHand().display());
88
+            Console.println("The sum of your cards is " + getSum(currentPlayer.getHand()));
89
+
90
+            if (getSum(currentPlayer.getHand()) > 21){
91
+                Console.println("You busted. House wins.");
92
+                return false;
93
+            }
94
+
95
+            Console.println("Dealer's card are: " + dealerHand.display());
96
+            Console.println("The sum of dealer's cards is " + getSum(dealerHand));
97
+
98
+            while (getSum(dealerHand) < 17) {
99
+                revealCard();
100
+                Console.println("Dealer's sum is " + getSum(dealerHand));
101
+            }
102
+
103
+            if (getSum(dealerHand) > 21) {
104
+                Console.println("Dealer busted. You win.");
105
+                return false;
106
+            }
107
+
108
+            if (getSum(dealerHand) == getSum(currentPlayer.getHand())) {
109
+                Console.println("It's a tie. You lose.");
110
+                return false;
111
+            } else if (getSum(dealerHand) <=21 && getSum(dealerHand) > getSum(currentPlayer.getHand())) {
112
+                Console.println("Dealer wins.");
113
+                return false;
114
+            } else {
115
+                Console.println("You win!");
116
+                return false;
117
+            }
118
+
119
+        }
120
+
121
+}
122
+
123
+    public void dealCards(int numberOfCards) {
124
+
125
+        for(BlackJackPlayer p: blackJackPlayers){ //deal 2 cards to each player
126
+            for (int i = 0; i < numberOfCards; i++) {
127
+                Card card = deck.removeFirst();
128
+                p.getHand().add(card);
129
+            }
130
+        }
131
+    }
29 132
 
133
+    public void dealCard(BlackJackPlayer p, int numberOfCards) {
134
+        for (int i = 0; i < numberOfCards; i++) {
135
+            Card card = deck.removeFirst();
136
+            p.getHand().add(card);
137
+        }
30 138
     }
31 139
 
32 140
     public void placeBet() {
141
+        this.bet = Console.getLongInput("Please enter your bet.");
142
+    }
143
+
144
+
145
+    public void evaluateBet(Player player, long payout) {
33 146
 
34 147
     }
35 148
 
36
-    public void evaluateBet(Player player, long payout){
37
-        
149
+
150
+    public Card drawCard() {
151
+        // draws card from deck and adds it to "upcard" total
152
+        Card card = deck.removeFirst();
153
+        return card;
154
+    }
155
+
156
+
157
+    public int  getSum(CardHand cardHand) {
158
+        int cardSum = 0;
159
+        boolean ace = false;
160
+
161
+        for (int i = 0; i < cardHand.size(); i++) {
162
+            int cardValue = cardHand.get(i).getCardValue().getCardValue();
163
+
164
+            if (cardValue > 10) {
165
+                cardValue = 10;   // For a Jack, Queen, or King.
166
+            }
167
+            if (cardValue == 1) {
168
+                ace = true;
169
+            }
170
+            cardSum = cardSum + cardValue;
171
+        }
172
+        if (cardHand.size() == 2 && ace == true && cardSum + 10 == 21) {
173
+            cardSum = cardSum + 10;
174
+        }
175
+        return cardSum;
176
+    }
177
+
178
+    public void revealCard(){
179
+        Card newCard = drawCard();
180
+        Console.println("Dealer drew a " + newCard.getCardValue() + " of " + newCard.getSuit());
181
+        dealerHand.add(newCard);
38 182
     }
183
+
184
+
39 185
 }

+ 0
- 1
src/main/java/io/zipcoder/casino/cardgames/CardGame.java 파일 보기

@@ -8,7 +8,6 @@ import io.zipcoder.casino.player.Player;
8 8
 public class CardGame extends Game {
9 9
 
10 10
     Deck deck = new Deck();
11
-    CardHand dealerHand = new CardHand();
12 11
 
13 12
     void dealCards(Player player, int numberOfCards) {
14 13
 

+ 4
- 6
src/main/java/io/zipcoder/casino/cardgames/cards/Card.java 파일 보기

@@ -4,6 +4,7 @@ public final class Card {
4 4
 
5 5
     private CardSuit suit;
6 6
     private CardValue cardValue;
7
+    private String card;
7 8
 
8 9
     public Card (CardValue cardValue, CardSuit suit){
9 10
         this.cardValue = cardValue;
@@ -14,15 +15,12 @@ public final class Card {
14 15
         return suit;
15 16
     }
16 17
 
17
-    public void setSuit(CardSuit suit){
18
-        this.suit = suit;
19
-    }
20
-
21 18
     public CardValue getCardValue(){
22 19
         return cardValue;
23 20
     }
24 21
 
25
-    public void setCardValue(CardValue cardValue){
26
-        this.cardValue = cardValue;
22
+    public String getCard() {
23
+        return cardValue + " of " + suit.getCardGraphic();
27 24
     }
25
+
28 26
 }

+ 16
- 5
src/main/java/io/zipcoder/casino/cardgames/cards/CardHand.java 파일 보기

@@ -11,8 +11,11 @@ public class CardHand extends ArrayList<Card> {
11 11
 //    public CardHand(int numberOfCards) {
12 12
 //        this.numberOfCards = numberOfCards;
13 13
 //    }
14
-//    public CardHand() {
15
-//    }
14
+//
15
+    public CardHand() {
16
+
17
+    }
18
+//
16 19
 //    public void addCard(Card card) {
17 20
 //        add(card);
18 21
 //    }
@@ -22,9 +25,17 @@ public class CardHand extends ArrayList<Card> {
22 25
 
23 26
     public String display() {
24 27
         String result = "";
25
-        for (Card each : this)
26
-            result = each.getCardValue() + " of " + each.getSuit();
27
-        Console.println(result);
28
+        int i = 0;
29
+        for (Card each : this) {
30
+            if (each == this.get(this.size()-1)) {
31
+                result += "and " + each.getCardValue() + " of " + each.getSuit().getCardGraphic();
32
+            } else {
33
+                result += each.getCardValue() + " of " + each.getSuit().getCardGraphic() + ", ";
34
+            }
35
+            i++;
36
+        }
37
+//        Console.println(result);
28 38
         return result;
29 39
     }
40
+
30 41
 }

+ 15
- 4
src/main/java/io/zipcoder/casino/cardgames/cards/CardSuit.java 파일 보기

@@ -1,8 +1,19 @@
1 1
 package io.zipcoder.casino.cardgames.cards;
2 2
 
3 3
 public enum CardSuit {
4
-    HEARTS,
5
-    SPADES,
6
-    CLUBS,
7
-    DIAMONDS
4
+    HEARTS('♥'),
5
+    SPADES('♠'),
6
+    CLUBS('♣'),
7
+    DIAMONDS('♦');
8
+
9
+    private char cardGraphic;
10
+
11
+    CardSuit (char cardGraphic) {
12
+        this.cardGraphic = cardGraphic;
13
+    }
14
+
15
+    public char getCardGraphic() {
16
+        return cardGraphic;
17
+    }
18
+
8 19
 }

+ 15
- 9
src/main/java/io/zipcoder/casino/cardgames/cards/Deck.java 파일 보기

@@ -2,33 +2,37 @@ package io.zipcoder.casino.cardgames.cards;
2 2
 
3 3
 import io.zipcoder.casino.utilities.Console;
4 4
 
5
+
5 6
 import java.util.ArrayDeque;
6 7
 import java.util.ArrayList;
8
+import java.util.Collections;
7 9
 import java.util.Deque;
8 10
 
9 11
 public class Deck extends ArrayDeque<Card> {
10 12
     // private ArrayList<Card> deck;
11 13
 
12 14
     public Deck(){
13
-        // this.deck = new ArrayList();
15
+        ArrayList<Card> freshDeck = new ArrayList<>();
14 16
         for (int i = 0; i < 13; i++){
15 17
             CardValue value = CardValue.values()[i];
16 18
 
17 19
             for (int j =0; j < 4;j++){
18 20
                 Card card = new Card (value, CardSuit.values()[j]);
19
-                addFirst(card);
21
+                freshDeck.add(card);
20 22
             }
21 23
         }
22 24
 
23
-//        Collections.shuffle(deck);
24
-
25
-        for (Card aCard : this) {
26
-            Card oneCard = aCard;
25
+        Collections.shuffle(freshDeck);
27 26
 
28
-            Console.println(oneCard.getCardValue() + " of " + oneCard.getSuit());
29
-//            CardValue.values();
27
+        this.addAll(freshDeck);
30 28
 
31
-        }
29
+//        for (Card aCard : this) {
30
+//            Card oneCard = aCard;
31
+//
32
+////            Console.println(oneCard.getCardValue() + " of " + oneCard.getSuit());
33
+////            CardValue.values();
34
+//
35
+//        }
32 36
     }
33 37
 
34 38
     public static void main(String[] args) {
@@ -42,6 +46,8 @@ public class Deck extends ArrayDeque<Card> {
42 46
             cardCount++;
43 47
 //            System.out.println(card.getCardValue() + " " + card.getSuit());
44 48
         }
49
+
45 50
     }
46 51
 
52
+
47 53
 }

+ 37
- 26
src/main/java/io/zipcoder/casino/dicegames/Craps.java 파일 보기

@@ -37,7 +37,7 @@ public class Craps extends DiceGame implements Gamble {
37 37
     }
38 38
 
39 39
     public void placeBet() {
40
-        this.bet = console.getIntegerInput("Enter your bet");
40
+        this.bet = console.integerInputSameLine("Enter your bet: ");
41 41
     }
42 42
 
43 43
     public void evaluateBet(Player player, long payout) {
@@ -46,39 +46,23 @@ public class Craps extends DiceGame implements Gamble {
46 46
 
47 47
     public void play(CrapsPlayer currentPlayer) {
48 48
         placeBet();
49
+
49 50
         promptEnterKey("roll dice");
50
-        int sum = rollDie(2); // roll two dice, store sum in sum field.
51
+
52
+        int sum = rollDie(2);
53
+
51 54
         console.println("Your roll sum equals: " + sum);
55
+
52 56
         if (sum == 7 || sum == 11) {
53
-            console.println("\n*********");
54
-            console.println("YOU WIN!");
55
-            console.println("*********\n");
56
-            evaluateBet(currentPlayer.getP(), bet*2);
57
+            evalWin(currentPlayer.getP());
57 58
         } else if (sum == 2 || sum == 3 || sum == 12) {
58
-            console.println("\n*********");
59
-            console.println("YOU LOSE!");
60
-            console.println("*********\n");
61
-            evaluateBet(currentPlayer.getP(), -(bet*2));
59
+            evalLoss(currentPlayer.getP());
62 60
         } else {
63 61
             int point = sum;
64 62
             do {
65
-                console.println("\n--------------------");
66
-                console.println("Point to roll for: " + point);
67
-                console.println("--------------------");
68
-                promptEnterKey("roll again");
63
+                printRollAgain(point);
69 64
                 sum = rollDie(2);
70
-                console.println("You rolled a " + sum);
71
-                if (sum == 7) {
72
-                    console.println("\n*********");
73
-                    console.println("YOU LOSE!");
74
-                    console.println("*********\n");
75
-                    evaluateBet(currentPlayer.getP(), -bet);
76
-                } else if (sum == point) {
77
-                    console.println("\n*********");
78
-                    console.println("YOU WIN!");
79
-                    console.println("*********\n");
80
-                    evaluateBet(currentPlayer.getP(), bet);
81
-                }
65
+                evalReRoll(currentPlayer, sum , point);
82 66
             } while (sum != point && sum != 7);
83 67
         }
84 68
     }
@@ -89,6 +73,33 @@ public class Craps extends DiceGame implements Gamble {
89 73
         }
90 74
     }
91 75
 
76
+    public void printRollAgain(int point){
77
+        console.println("\n--------------------");
78
+        console.println("Point to roll for: " + point);
79
+        console.println("--------------------");
80
+        promptEnterKey("roll again");
81
+    }
82
+
83
+    public void evalReRoll(CrapsPlayer currentPlayer, int sum, int point){
84
+        console.println("You rolled a " + sum);
85
+        if (sum == 7) {
86
+            evalLoss(currentPlayer.getP());
87
+        } else if (sum == point) {
88
+            evalWin(currentPlayer.getP());
89
+        }
90
+    }
91
+
92
+    public void evalLoss(Player player){
93
+        console.println("\n*********\nYOU LOSE!\n*********\n");
94
+        evaluateBet(player, -bet);
95
+    }
96
+    public void evalWin(Player player){
97
+        console.println("\n*******************************\n" +
98
+                "WINNER WINNER CHICKEN DINNER!\n" +
99
+                "*******************************\n");
100
+        evaluateBet(player, bet);
101
+    }
102
+
92 103
     public void greetPlayer(Player playa){
93 104
         console.println("Ok " + playa.getName() + ", you're up...\n");
94 105
     }

+ 35
- 2
src/main/java/io/zipcoder/casino/dicegames/dice/Dice.java 파일 보기

@@ -1,11 +1,36 @@
1 1
 package io.zipcoder.casino.dicegames.dice;
2 2
 
3
+    import java.util.ArrayList;
4
+    import java.util.HashMap;
5
+    import java.util.List;
3 6
     import java.util.Random;
4 7
 
5 8
     public class Dice {
6 9
         public int dieCount;
10
+        private ArrayList<Integer> lastRoll = new ArrayList<Integer>();
11
+
12
+        private HashMap<Integer, Character> diceGraphics = new HashMap<Integer, Character>() {{
13
+            put(1,'⚀');
14
+            put(2,'⚁');
15
+            put(3,'⚂');
16
+            put(4,'⚃');
17
+            put(5,'⚄');
18
+            put(6,'⚅');
19
+        }};
20
+
21
+        public char getDieGraphic(int roll) {
22
+            return diceGraphics.get(roll);
23
+        }
24
+
25
+        public String getLastRollGraphic() {
26
+            String lastRollGraphic = "";
27
+            for (Integer roll : lastRoll) {
28
+                lastRollGraphic += getDieGraphic(roll) + " ";
29
+            }
30
+            return lastRollGraphic;
31
+        }
7 32
 
8
-//        Dice(){ this.dieCount = 1; }
33
+        //        Dice(){ this.dieCount = 1; }
9 34
 
10 35
         public Dice(int dieCount) {
11 36
             this.dieCount = dieCount;
@@ -19,11 +44,19 @@ package io.zipcoder.casino.dicegames.dice;
19 44
             Random random = new Random();
20 45
 
21 46
             int rollSum = 0;
47
+            if (lastRoll.size() > 0) {
48
+                lastRoll.clear();
49
+            }
22 50
             for (int i = 0; i < dieCount; i++) {
23 51
                 int roll = random.nextInt(6)+1;
52
+                lastRoll.add(roll);
24 53
                 rollSum += roll;
25 54
             }
26 55
             return rollSum;
27 56
         }
28 57
 
29
-}
58
+        public ArrayList<Integer> getLastRoll() {
59
+            return lastRoll;
60
+        }
61
+
62
+    }

+ 13
- 2
src/main/java/io/zipcoder/casino/player/BlackJackPlayer.java 파일 보기

@@ -2,10 +2,21 @@ package io.zipcoder.casino.player;
2 2
 
3 3
 import io.zipcoder.casino.cardgames.cards.CardHand;
4 4
 
5
-public class BlackJackPlayer {
5
+public class BlackJackPlayer{
6 6
     private CardHand hand;
7
-
8 7
     Player blackJackPlayer;
8
+    private long bet;
9
+
10
+    public long getBet() {
11
+        return bet;
12
+    }
13
+
14
+    public void setBet(long bet) {
15
+        this.bet = bet;
16
+    }
17
+
18
+    public BlackJackPlayer() {
19
+    }
9 20
 
10 21
     public BlackJackPlayer(Player player) {
11 22
         this.blackJackPlayer = player;

+ 5
- 2
src/main/java/io/zipcoder/casino/player/Player.java 파일 보기

@@ -2,8 +2,11 @@ package io.zipcoder.casino.player;
2 2
 
3 3
 public class Player {
4 4
 
5
-    private String name = "";
6
-    private long chipBalance;
5
+    protected String name = "";
6
+    protected long chipBalance;
7
+
8
+    public Player() {
9
+    }
7 10
 
8 11
     public Player(String name, long startingBalance) {
9 12
         this.name = name;

+ 14
- 3
src/main/java/io/zipcoder/casino/utilities/Console.java 파일 보기

@@ -100,7 +100,13 @@ public class Console {
100 100
         print(output + "\n", args);
101 101
     }
102 102
 
103
-    public String getStringInput(String prompt){
103
+    public static void printWcarrot(String output){
104
+        System.out.println(output);
105
+        System.out.print(">");
106
+    }
107
+
108
+
109
+    public static String getStringInput(String prompt){
104 110
         Scanner scanner = new Scanner(System.in);
105 111
         println(prompt);
106 112
         String userInput = scanner.nextLine();
@@ -111,9 +117,14 @@ public class Console {
111 117
          println(prompt);
112 118
          int userInput = scanner.nextInt();
113 119
          return userInput;
114
-
115 120
      }
116
-     public  Long getLongInput(String prompt) {
121
+     public Integer integerInputSameLine(String prompt){
122
+        Scanner scanner = new Scanner(System.in);
123
+        printWcarrot(prompt);
124
+        int userInput = scanner.nextInt();
125
+        return userInput;
126
+     }
127
+     public static Long getLongInput(String prompt) {
117 128
          Scanner scanner = new Scanner(System.in);
118 129
          println(prompt);
119 130
          Long userInput = scanner.nextLong();

+ 0
- 1
src/main/java/io/zipcoder/casino/utilities/Gamble.java 파일 보기

@@ -6,5 +6,4 @@ public interface Gamble {
6 6
     public void placeBet();
7 7
 
8 8
     void evaluateBet(Player player, long payout);
9
-
10 9
 }

+ 8
- 0
src/test/java/io/zipcoder/casino/BlackJackTest.java 파일 보기

@@ -0,0 +1,8 @@
1
+package io.zipcoder.casino;
2
+
3
+import org.junit.Test;
4
+
5
+public class BlackJackTest {
6
+
7
+
8
+}