Ver código fonte

Merge branch 'stud' of nsatinover/ZCW-OOP-Casino into stud

jonathan-hinds 6 anos atrás
pai
commit
2d43898c18

+ 2
- 2
src/main/java/io/zipcoder/casino/Card.java Ver arquivo

@@ -44,9 +44,9 @@ public class Card {
44 44
         this.suit = suit;
45 45
     }
46 46
 
47
-    public CardValue getCardValue()
47
+    public int getCardValue()
48 48
     {
49
-        return cardValue;
49
+        return cardValue.getCardValue();
50 50
     }
51 51
 
52 52
     public void setCardValue(CardValue cardValue)

+ 62
- 4
src/main/java/io/zipcoder/casino/CardGame.java Ver arquivo

@@ -12,10 +12,11 @@ public abstract class CardGame {
12 12
     private int maxBet;
13 13
     private int handSize;
14 14
     private int ante;
15
-    private Player playersTurn;
15
+    private CardPlayer playersTurn;
16 16
     private Player winner = null;
17
+    private Player loser = null;
17 18
     private ArrayList<CardPlayer> players = new ArrayList<CardPlayer>();
18
-    private ArrayList<Card> deck = new ArrayList<>();
19
+    private Deck deck = new Deck();
19 20
 
20 21
 
21 22
     CardGame(int minBet, int maxBet, int ante){
@@ -41,7 +42,7 @@ public abstract class CardGame {
41 42
         card.setVisibility(true);
42 43
     }
43 44
 
44
-    public ArrayList<Card> getDeck() {
45
+    public Deck getDeck() {
45 46
         return deck;
46 47
     }
47 48
 
@@ -56,7 +57,7 @@ public abstract class CardGame {
56 57
         }
57 58
     }
58 59
 
59
-    public void setDeck(ArrayList<Card> deck) {
60
+    public void setDeck(Deck deck) {
60 61
         this.deck = deck;
61 62
     }
62 63
 
@@ -79,4 +80,61 @@ public abstract class CardGame {
79 80
     public int getHandSize() {
80 81
         return handSize;
81 82
     }
83
+
84
+    public void setHandSize(int handSize) {
85
+        this.handSize = handSize;
86
+    }
87
+
88
+    public CardPlayer getPlayersTurn() {
89
+        return playersTurn;
90
+    }
91
+
92
+    public void setPlayersTurn(CardPlayer playersTurn) {
93
+        this.playersTurn = playersTurn;
94
+    }
95
+
96
+    public void addNpc(){
97
+        addPlayers(new NPC("Opponant", getAnte()));
98
+    }
99
+
100
+    public void chooseStatingPlayer(){
101
+        //loop through the players
102
+        for(int i = 0; i < getPlayers().size(); i ++){
103
+            //if one is not an NPC
104
+            if(!(getPlayers().get(i).getPlayer() instanceof NPC)){
105
+                //set this player to have the current turn
106
+                setPlayersTurn(getPlayers().get(i));
107
+                break;
108
+            }
109
+        }
110
+    }
111
+
112
+    public void chooseNextTurn(){
113
+        if(playersTurn != null)
114
+        {
115
+            //if it is the end of the turn circle
116
+            if((players.indexOf(playersTurn) + 1) == players.size()){
117
+                //start again at the starting player
118
+                playersTurn = players.get(0);
119
+                //System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
120
+
121
+            //if it is not the end of the turn circle
122
+            } else {
123
+                playersTurn = players.get(players.indexOf(playersTurn) + 1);
124
+                //System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
125
+            }
126
+        }
127
+    }
128
+
129
+    public void printTurn(){
130
+        System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
131
+    }
132
+
133
+    public Player getLoser() {
134
+        return loser;
135
+    }
136
+
137
+    public void setLoser(Player loser) {
138
+        this.loser = loser;
139
+    }
82 140
 }

+ 24
- 0
src/main/java/io/zipcoder/casino/CardPlayer.java Ver arquivo

@@ -6,6 +6,8 @@ public class CardPlayer {
6 6
     private Player player;
7 7
     private ArrayList<Card> hand = new ArrayList<>();
8 8
     private ArrayList<Card> discard = new ArrayList<>();
9
+    private Card playedCard = null;
10
+
9 11
 
10 12
     public CardPlayer(Player player){
11 13
         this.player = player;
@@ -15,6 +17,10 @@ public class CardPlayer {
15 17
         return hand;
16 18
     }
17 19
 
20
+    public void setHand(ArrayList<Card> hand) {
21
+        this.hand = hand;
22
+    }
23
+
18 24
     public Player getPlayer() {
19 25
         return player;
20 26
     }
@@ -30,4 +36,22 @@ public class CardPlayer {
30 36
 
31 37
         return null;
32 38
     }
39
+
40
+    public Card getPlayedCard() {
41
+        return playedCard;
42
+    }
43
+
44
+    public void setPlayedCard(Card playedCard) {
45
+        this.playedCard = playedCard;
46
+    }
47
+
48
+    public void addDiscard(ArrayList<Card> cards) {
49
+        this.discard.addAll(cards);
50
+    }
51
+    public ArrayList<Card> getDiscard() {
52
+        return discard;
53
+    }
54
+    public void setDiscard(ArrayList<Card> discard) {
55
+        this.discard = discard;
56
+    }
33 57
 }

+ 30
- 5
src/main/java/io/zipcoder/casino/Console.java Ver arquivo

@@ -1,15 +1,20 @@
1 1
 package io.zipcoder.casino;
2
+import java.util.ArrayList;
2 3
 import java.util.InputMismatchException;
3 4
 import java.util.Scanner;
4 5
 
5 6
 public class Console {
6 7
     private Scanner scanner = new Scanner(System.in);
7
-    private String[] gameLib = {"yahtzee", "war", "three card stud"};
8
+    private ArrayList<String> gameLib = new ArrayList<>();
8 9
     private Game game = null;
9 10
     private Player player;
10 11
     private boolean running = true;
11 12
 
12
-    Console(){}
13
+    Console(){
14
+        gameLib.add("yahtzee");
15
+        gameLib.add("war");
16
+        gameLib.add("three card stud");
17
+    }
13 18
 
14 19
     public void createAccount()
15 20
     {
@@ -24,15 +29,16 @@ public class Console {
24 29
 
25 30
     public void chooseGame()
26 31
     {
27
-        System.out.println("Please choose a game to play!");
28
-        String command = getCommand();
29
-
32
+        //get the command from the user
33
+        String command = continueAskGame();
34
+        //process the command
30 35
         switch(command){
31 36
 
32 37
             case "war":
33 38
                 int[] warMinMax = getMinMax();
34 39
                 Game war = new War(warMinMax[0], warMinMax[1], 10);
35 40
                 ((War) war).addPlayers(player);
41
+                ((War) war).addNpc();
36 42
                 war.StartGame();
37 43
                 break;
38 44
 
@@ -40,6 +46,7 @@ public class Console {
40 46
                 int[] studMinMax = getMinMax();
41 47
                 Game stud = new Stud(studMinMax[0], studMinMax[1], 10);
42 48
                 ((Stud) stud).addPlayers(player);
49
+                ((Stud) stud).addNpc();
43 50
                 stud.StartGame();
44 51
                 break;
45 52
 
@@ -101,4 +108,22 @@ public class Console {
101 108
         command = command.toLowerCase().trim();
102 109
         return command;
103 110
     }
111
+
112
+    public String continueAskGame(){
113
+        String command = "";
114
+
115
+        System.out.println("Please choose a game to play!");
116
+        command = getCommand();
117
+
118
+        if(gameLib.indexOf(command) == -1)
119
+        {
120
+            while(gameLib.indexOf(command) == -1)
121
+            {
122
+                Printer.noMatchingGameName(gameLib);
123
+                command = getCommand();
124
+            }
125
+        }
126
+        return command;
127
+    }
128
+
104 129
 }

+ 5
- 16
src/main/java/io/zipcoder/casino/Deck.java Ver arquivo

@@ -3,8 +3,7 @@ package io.zipcoder.casino;
3 3
 import java.util.ArrayList;
4 4
 import java.util.Collections;
5 5
 
6
-public class Deck {
7
-    ArrayList<Card> deck;
6
+public class Deck extends ArrayList<Card>{
8 7
 
9 8
     /**
10 9
      * Deck constructor will create an array of all 52 @Card 's
@@ -15,17 +14,16 @@ public class Deck {
15 14
     }
16 15
 
17 16
     public void createDeck(){
18
-        this.deck = new ArrayList<>();
19 17
 
20 18
         for (int i = 0; i < 13; i++){
21 19
             Card.CardValue value = Card.CardValue.values()[i];
22 20
 
23 21
             for (int j = 0; j < 4; j++){
24 22
                 Card card = new Card(value, Card.Suit.values()[j]);
25
-                this.deck.add(card);
23
+                this.add(card);
26 24
             }
27 25
         }
28
-        Collections.shuffle(deck);
26
+        Collections.shuffle(this);
29 27
     }
30 28
 
31 29
     /**
@@ -35,7 +33,7 @@ public class Deck {
35 33
      * @return
36 34
      */
37 35
     public Card getCard(int index){
38
-        return this.deck.get(index);
36
+        return this.get(index);
39 37
     }
40 38
 
41 39
     /**
@@ -43,16 +41,7 @@ public class Deck {
43 41
      * @return
44 42
      */
45 43
     public Card pullCard(){
46
-        Card tempCard = this.deck.remove(0);
44
+        Card tempCard = this.remove(0);
47 45
         return tempCard;
48 46
     }
49
-
50
-    /**
51
-     * Used to retrieve the array with instantiated deck shuffled
52
-     * Will RETURN DECK
53
-     * @return
54
-     */
55
-    public ArrayList<Card> getDeck() {
56
-        return this.deck;
57
-    }
58 47
 }

+ 0
- 1
src/main/java/io/zipcoder/casino/Dice.java Ver arquivo

@@ -2,7 +2,6 @@ package io.zipcoder.casino;
2 2
 
3 3
 public class Dice {
4 4
 
5
-
6 5
     private int value;
7 6
 
8 7
     public void roll(){

+ 3
- 2
src/main/java/io/zipcoder/casino/Gamble.java Ver arquivo

@@ -1,6 +1,7 @@
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(Player player, int betAmount);
5
+     // void Bet(int betAmount);
6
+     void Payout();
6 7
 }

+ 8
- 0
src/main/java/io/zipcoder/casino/NPC.java Ver arquivo

@@ -0,0 +1,8 @@
1
+package io.zipcoder.casino;
2
+
3
+public class NPC extends Player {
4
+
5
+    NPC(String name, int initialBalance) {
6
+        super(name, initialBalance);
7
+    }
8
+}

+ 13
- 3
src/main/java/io/zipcoder/casino/Printer.java Ver arquivo

@@ -1,13 +1,15 @@
1 1
 package io.zipcoder.casino;
2 2
 
3
+import java.util.ArrayList;
4
+
3 5
 public class Printer {
4 6
 
5
-    public static void noMatchingGameName(String[] gameNames){
7
+    public static void noMatchingGameName(ArrayList<String> gameNames){
6 8
 
7 9
         String games = "";
8 10
 
9
-        for(int i = 0; i < gameNames.length; i ++){
10
-            games += gameNames[i] + " ";
11
+        for(int i = 0; i < gameNames.size(); i ++){
12
+            games += gameNames.get(i) + " ";
11 13
         }
12 14
         games = games.trim();
13 15
 
@@ -25,4 +27,12 @@ public class Printer {
25 27
     public static void unacceptableMinBet(){
26 28
         System.out.println("Your bet must be above $0");
27 29
     }
30
+
31
+    public static void studHandsDealt(){
32
+        System.out.println("Each player Dealt 3 cards");
33
+    }
34
+
35
+    public static void showCard(Player player, Card card){
36
+        System.out.println(player.getName() + " shows a " + card.getName());
37
+    }
28 38
 }

+ 99
- 20
src/main/java/io/zipcoder/casino/Stud.java Ver arquivo

@@ -1,52 +1,131 @@
1 1
 package io.zipcoder.casino;
2 2
 
3
+import java.util.Scanner;
4
+
3 5
 public class Stud extends CardGame implements Gamble, Game {
6
+    // ArrayList<Card> playerHand = new ArrayList<>(3);
7
+    Scanner scanner = new Scanner(System.in);
8
+
4 9
     public Stud(int minBet, int maxBet, int ante) {
5 10
         super(minBet, maxBet, ante);
6
-    }
7
-
8
-    public void Deal() {
9 11
 
10 12
     }
11
-
12
-
13
-    public void determineWinner(){
14
-
13
+    
14
+    public void playCard(Player player, Card card) {
15
+        card.setVisibility(true);               //CARD isVISIBLE
16
+        Printer.showCard(player, card);         //PRINT card name to CONSOLE
15 17
     }
16 18
 
17 19
     public void fold(){
18
-
19 20
     }
20 21
 
21 22
     /**
22
-     * Below 3 Implemented from Gamble
23
-     * @param betAmount
23
+     * Need method to determine what hand wins first
24 24
      */
25
-    public void Bet(int betAmount) {
26
-
25
+    public void determineWinner(){
26
+        CardPlayer winningPlayer = super.getPlayers().get(0); // 0 index must be dealer in case of a tie
27
+        for(int i = 0; i < super.getPlayers().size(); i ++)
28
+        {
29
+            CardPlayer player = super.getPlayers().get(i);
30
+            player.getHand().get(i);
31
+        }
27 32
     }
28 33
 
29
-    public int Payout(int payoutAmount) {
30
-        return 0;
34
+    public void Bet(Player player, int betAmount) {
35
+        super.changeTablePot(betAmount);
36
+        player.changeBalance(betAmount * -1);
31 37
     }
32 38
 
33
-    public void Ante(int anteAmount) {
34
-
39
+    public void Payout(){
40
+        if(super.getWinner() != null){
41
+            super.getWinner().changeBalance(super.getTablePot());
42
+        }
35 43
     }
36 44
 
37
-    /**
38
-     * Below 3 Implemented from Game
39
-     */
45
+    public void payAnte() {
46
+        for(int i = 0; i < super.getPlayers().size(); i ++)
47
+        {
48
+            CardPlayer player = super.getPlayers().get(i);
49
+            player.getPlayer().changeBalance(-super.getAnte());
50
+        }
51
+    }
40 52
 
41 53
     public void Quit() {
54
+        //playAgain?
55
+        // Shuffle
42 56
 
43 57
     }
44 58
 
45 59
     public void StartGame() {
46
-
60
+        Deck deck = new Deck();     //CREATE deck for game       
61
+        setHandSize(3);             //SET Hand Size for game
62
+        payAnte();                  //PAY ante (all players)
63
+        Deal();                     //DEALS hands to each player
64
+        StartRound();               //METHOD called
65
+        Printer.studHandsDealt();   //CONSOLE dealt msg
47 66
     }
48 67
 
68
+    /**
69
+     * Game played in this method
70
+     */
49 71
     public void StartRound() {
72
+        for (int i = 0; i < getHandSize() - 1; i++){    //Each player turns a card in hand to face up
73
+            gameRound();
74
+        }
75
+        lastGameRound();
76
+        determineWinner();
77
+    }
78
+
79
+    /**
80
+     * Plays through rounds that includes flipping cards face up and then betting or folding
81
+     */
82
+    public void gameRound(){
83
+        for (int j = 0; j < getPlayers().size(); j++) {
84
+            CardPlayer player = super.getPlayers().get(j);              //GET a player
85
+            playCard(player.getPlayer(), player.getHand().get(j));      //SHOW-PRINT players first CARD
86
+        }
87
+        for (int j = 0; j < getPlayers().size(); j++) {                 //Betting round or fold
88
+            CardPlayer player = super.getPlayers().get(j);
89
+            int bet;
90
+            //ask player to bet and pass amount to Bet(betAmount
91
+            System.out.println("Enter a bet, if 0 is entered you fold");
92
+            bet = scanner.nextInt();
93
+            if (bet == 0){
94
+                System.out.println(player.getPlayer().getName() + " folds.");
95
+                //if fold, player is removed from game
96
+                //if only 1 player (dealer) game ends
97
+            } else {
98
+                //wants a cardplayer but bet method is updating balance on a 'player' - casted CardPlayer to Player
99
+                Bet(super.getPlayers().get(j).getPlayer(), bet);
100
+                System.out.println(player.getPlayer().getName() + " bets: " + bet);
101
+            }
102
+        }
103
+    }
50 104
 
105
+    /**
106
+     * PreCondition: Betting rounds already played
107
+     * Plays through round that include flipping last card face up
108
+     * PostCondtion: tablePot is now at max value
109
+     * DetermineWinner() expected to be called after this method
110
+     */
111
+    public void lastGameRound(){
112
+        for (int j = 0; j < getPlayers().size(); j++) {
113
+            CardPlayer player = super.getPlayers().get(j);              //GET a player
114
+            playCard(player.getPlayer(), player.getHand().get(j));      //SHOW-PRINT players first CARD
115
+        }
116
+    }
117
+
118
+
119
+    /**
120
+     * Deal each player(and dealer) 3 face down cards in turn
121
+     */
122
+    public void Deal() {
123
+        for(int i = 0; i < getHandSize(); i ++){                        //OUTER loop - run 3 times as there are 3 cards per hand
124
+            for (int j = 0; j < getPlayers().size(); j++) {             //INNER loop through each player
125
+                Card card = super.getDeck().pullCard();                     //PULL card from deck (removed from deck)
126
+                CardPlayer player = super.getPlayers().get(j);              //GET a player
127
+                player.getHand().add(card);                                 //ADD card to player hand
128
+            }
129
+        }
51 130
     }
52 131
 }

+ 78
- 8
src/main/java/io/zipcoder/casino/War.java Ver arquivo

@@ -2,11 +2,13 @@ package io.zipcoder.casino;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.HashMap;
5
+import java.util.Scanner;
5 6
 import java.util.regex.Pattern;
6 7
 
7 8
 public class War extends CardGame implements Gamble, Game {
8 9
 
9 10
     private ArrayList<Card> tableCards = new ArrayList<Card>();
11
+    private Scanner scanner = new Scanner(System.in);
10 12
 
11 13
     War(int minBet, int maxBet, int ante) {
12 14
         super(minBet, maxBet, ante);
@@ -17,8 +19,29 @@ public class War extends CardGame implements Gamble, Game {
17 19
      * Specific to war methods
18 20
      */
19 21
     public void playCard(){
20
-        //take a card from the hand
21
-        //add it to the tablecard face up
22
+        //if the player has cards in their hand
23
+        if(super.getPlayersTurn().getHand().size() > 0) {
24
+            //pull out a card to play
25
+            Card card = super.getPlayersTurn().getHand().get(0);
26
+            //play the card face up, on the table
27
+            card.setVisibility(true);
28
+            tableCards.add(card);
29
+            //store the last played card in the players wrapper class
30
+            super.getPlayersTurn().setPlayedCard(card);
31
+            //remove this card from their hand
32
+            super.getPlayersTurn().getHand().remove(card);
33
+            //print the card that was played
34
+            System.out.println(super.getPlayersTurn().getPlayer().getName() + " has played " + card.getName() + " and has " + super.getPlayersTurn().getHand().size() + " cards left.");
35
+        //if the player has not cards in their hand but has cards in their discard, pickup their discard and play a card
36
+        } else if(super.getPlayersTurn().getHand().size() == 0 && super.getPlayersTurn().getDiscard().size() > 0) {
37
+            System.out.println(super.getPlayersTurn().getPlayer().getName() + " ran out of cards and picked up their discard pile.");
38
+            super.getPlayersTurn().getHand().addAll(super.getPlayersTurn().getDiscard());
39
+            super.getPlayersTurn().setDiscard(new ArrayList<Card>());
40
+            playCard();
41
+        //if the person has no cards in their hand, and no cards in discard they lose.
42
+        } else if(super.getPlayersTurn().getHand().size() == 0 && super.getPlayersTurn().getDiscard().size() == 0){
43
+            super.setLoser(super.getPlayersTurn().getPlayer());
44
+        }
22 45
     }
23 46
 
24 47
     public void warMethod(){
@@ -26,22 +49,33 @@ public class War extends CardGame implements Gamble, Game {
26 49
         //play one card face up
27 50
     }
28 51
 
29
-    public void determineWinner(Card player1card, Card player2card){
52
+    public CardPlayer determineWinner(){
53
+
54
+        int max = 0;
55
+        CardPlayer winner = null;
30 56
 
57
+        for(int i = 0; i < getPlayers().size(); i ++){
58
+            CardPlayer player = getPlayers().get(i);
59
+            if(player.getPlayedCard().getCardValue() > max){
60
+                max = player.getPlayedCard().getCardValue();
61
+                winner = player;
62
+            }
63
+        }
64
+        System.out.println("The winner is " + winner.getPlayer().getName());
65
+        return winner;
31 66
     }
32 67
 
33 68
     /**
34 69
      * Below 3 Implemented from Gamble
35 70
      */
36
-    public void Bet(int betAmount) {
71
+    public void Bet(Player player, int betAmount) {
37 72
         super.changeTablePot(betAmount);
38 73
     }
39 74
 
40
-    public int Payout(int payoutAmount) {
41
-        if(super.getWinner() != null){
75
+    public void Payout() {
76
+        if(super.getWinner() != null) {
42 77
             super.getWinner().changeBalance(super.getTablePot());
43 78
         }
44
-        return 0;
45 79
     }
46 80
 
47 81
     public void payAnte() {
@@ -61,12 +95,44 @@ public class War extends CardGame implements Gamble, Game {
61 95
     }
62 96
 
63 97
     public void StartGame() {
64
-        Deck deck = new Deck();
98
+        System.out.println("Welcome to war!");
99
+        super.chooseStatingPlayer();
65 100
         payAnte();
66 101
         Deal();
102
+        StartRound();
67 103
     }
68 104
 
69 105
     public void StartRound() {
106
+        while(super.getLoser() == null) {
107
+            System.out.println("Type play to play the top card from your pile.");
108
+            String input = scanner.next();
109
+            input = input.toLowerCase().trim();
110
+
111
+            if (input.equals("play")) {
112
+                //each player
113
+                for (CardPlayer player : super.getPlayers()) {
114
+                    //plays a card, then
115
+                    playCard();
116
+                    //the turn updates to be the next players.
117
+                    super.chooseNextTurn();
118
+                }
119
+                //determine the winner
120
+                CardPlayer winner = determineWinner();
121
+
122
+                System.out.println(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
123
+
124
+                //add all the table cards to the players discard
125
+                winner.addDiscard(tableCards);
126
+                //clear the table cards pile
127
+                tableCards = new ArrayList<Card>();
128
+
129
+                //if the user does not type play
130
+            } else {
131
+                //display a message
132
+                System.out.println("Sorry, I don't understand that command.");
133
+            }
134
+        }
135
+
70 136
         //player plays a card faceup
71 137
         //remove cards from player hand
72 138
         //pc plays a card faceup
@@ -95,5 +161,9 @@ public class War extends CardGame implements Gamble, Game {
95 161
                 super.getDeck().remove(card);
96 162
             }
97 163
         }
164
+
165
+        System.out.println(super.getPlayersTurn().getPlayer().getName() +
166
+                "has: " + super.getPlayersTurn().getHand().size() + " cards.");
167
+
98 168
     }
99 169
 }