Browse Source

baccarat and gamefactory added

yauhenip 6 years ago
parent
commit
4ada093698

+ 136
- 0
src/main/java/io/zipcoder/casino/Baccarat.java View File

@@ -0,0 +1,136 @@
1
+package io.zipcoder.casino;
2
+import java.util.ArrayList;
3
+import java.util.Arrays;
4
+public class Baccarat extends CardGame {
5
+    private ArrayList<Card> playerHand = new ArrayList<Card>();
6
+    private ArrayList<Card> dealerHand = new ArrayList<Card>();
7
+    private int currentBet;
8
+    private CardDeck deck;
9
+    private Player player;
10
+    private boolean playingBaccarat = false;
11
+    public Baccarat(Player player) {
12
+        this.player = player;
13
+        this.deck = new CardDeck();
14
+    }
15
+    public void start() {
16
+        playingBaccarat = true;
17
+        this.deck.shuffle();
18
+    }
19
+    public void bet(int amount) { currentBet = amount; }
20
+    public void loseBet(int currentBet){ player.subtractFromBankroll(currentBet);}
21
+    public void winBet(int currentBet) { player.addToBankroll(currentBet); }
22
+    public void endGame() { }
23
+    public void dealToPlayer(int amountOfCards) {
24
+        for(int i =0; i < amountOfCards; i++) {
25
+            this.playerHand.add(deck.draw());
26
+        }
27
+    }
28
+    public void dealToDealer(int amountOfCards) {
29
+        for(int i =0; i < amountOfCards; i++) {
30
+            this.dealerHand.add(deck.draw());
31
+        }
32
+    }
33
+    private void setCurrentBet(int bet) {
34
+        this.currentBet = bet;
35
+    }
36
+    private  int getCardValue(Card card) {
37
+        Rank rank = card.getRank();
38
+        int calculateValue = 0;
39
+        switch (rank) {
40
+            case ACE:
41
+                calculateValue = 1;
42
+                break;
43
+            case TWO:
44
+                calculateValue = 2;
45
+                break;
46
+            case THREE:
47
+                calculateValue = 3;
48
+                break;
49
+            case FOUR:
50
+                calculateValue = 4;
51
+                break;
52
+            case FIVE:
53
+                calculateValue = 5;
54
+                break;
55
+            case SIX:
56
+                calculateValue = 6;
57
+                break;
58
+            case SEVEN:
59
+                calculateValue = 7;
60
+                break;
61
+            case EIGHT:
62
+                calculateValue = 8;
63
+                break;
64
+            case NINE:
65
+                calculateValue = 9;
66
+                break;
67
+            case TEN:
68
+            case JACK:
69
+            case QUEEN:
70
+            case KING:
71
+                calculateValue = 0;
72
+                break;
73
+        }
74
+        return calculateValue;
75
+    }
76
+
77
+    public void declareWinner() {
78
+
79
+    }
80
+
81
+    public int drawCard() {
82
+        return 0;
83
+    }
84
+
85
+    public void stand() {
86
+
87
+    }
88
+
89
+    private int calculatehand(ArrayList<Card> hand) {
90
+        int score = 0;
91
+        for(int i = 0; i < hand.size(); i++) {
92
+            score += getCardValue(hand.get(i));
93
+        }
94
+        if (score >= 10) {
95
+            score = score - 10;
96
+        }
97
+        return score;
98
+    }
99
+    public void startGame() {
100
+        start();
101
+        while(playingBaccarat) {
102
+            playerHand = new ArrayList<Card>();
103
+            dealerHand = new ArrayList<Card>();
104
+            Console.output("Welcome to Baccarat " + player.getName() + ". I will be your dealer.");
105
+            int bet = Console.numberFromString(Console.askForInput("How much would you like to bet?"));
106
+            setCurrentBet(bet);
107
+            String betOn = Console.askForInput("Who do you want to bet on? (P/D)");
108
+            dealToPlayer(2);
109
+            dealToDealer(2);
110
+            System.out.println("PH: " + Arrays.toString(this.playerHand.toArray(new Card[this.playerHand.size()])));
111
+            System.out.println("DH: " + Arrays.toString(this.dealerHand.toArray(new Card[this.dealerHand.size()])));
112
+            if(betOn.equals("p") && calculatehand(playerHand) == 9) {
113
+                Console.output("You win!");
114
+                player.addToBankroll(currentBet);
115
+                currentBet = 0;
116
+            } else if (betOn.equals("d") && calculatehand(dealerHand) == 9) {
117
+                Console.output("You win!");
118
+                player.addToBankroll(currentBet);
119
+                currentBet = 0;
120
+            } else {
121
+                int playerHandAwayFromNine = Math.abs(calculatehand(playerHand) - 9);
122
+                int dealerHandAwayFromNine = Math.abs(calculatehand(dealerHand) - 9);
123
+                if ((betOn.equals("p") && playerHandAwayFromNine < dealerHandAwayFromNine) ||
124
+                        (betOn.equals("d") && playerHandAwayFromNine > dealerHandAwayFromNine)) {
125
+                    Console.output("You win!");
126
+                    player.addToBankroll(currentBet);
127
+                    currentBet = 0;
128
+                } else {
129
+                    Console.output("You lose");
130
+                    player.subtractFromBankroll(currentBet);
131
+                    currentBet = 0;
132
+                }
133
+            }
134
+        }
135
+    }
136
+}

+ 6
- 2
src/main/java/io/zipcoder/casino/CardGame.java View File

@@ -16,6 +16,10 @@ public abstract class CardGame implements Game, Gamble {
16 16
 
17 17
     }
18 18
 
19
+    public CardGame() {
20
+        this.deck = new CardDeck();
21
+    }
22
+
19 23
 
20 24
     public abstract int drawCard();
21 25
 
@@ -27,9 +31,9 @@ public abstract class CardGame implements Game, Gamble {
27 31
 
28 32
     public abstract void declareWinner();
29 33
 
30
-    public abstract void winBet();
34
+    public abstract void winBet(int currentBet);
31 35
     
32
-    public abstract void loseBet(Player player);
36
+    public abstract void loseBet(int currentBet);
33 37
 
34 38
 
35 39
 

+ 23
- 0
src/main/java/io/zipcoder/casino/GameFactory.java View File

@@ -0,0 +1,23 @@
1
+package io.zipcoder.casino;
2
+
3
+
4
+
5
+public class GameFactory{
6
+    public GameFactory(){
7
+
8
+
9
+    }
10
+
11
+    public static void createBlackJack(CardGame game){
12
+
13
+        game = BlackJack.start();
14
+    }
15
+
16
+    public static void createBaccarat(Game game){
17
+        game = Baccarat.startGame();
18
+    }
19
+
20
+    public static void createHighLow(Game game){
21
+        game = HighLow.startGame();
22
+    }
23
+}