Browse Source

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

jonathan-hinds 6 years ago
parent
commit
5c19f95d05
2 changed files with 151 additions and 34 deletions
  1. 21
    3
      src/main/java/io/zipcoder/casino/Console.java
  2. 130
    31
      src/main/java/io/zipcoder/casino/Stud.java

+ 21
- 3
src/main/java/io/zipcoder/casino/Console.java View File

@@ -8,12 +8,14 @@ public class Console {
8 8
     private ArrayList<String> gameLib = new ArrayList<>();
9 9
     private Game game = null;
10 10
     private Player player;
11
+    private Player player2;         //need to run createAccount for player two for Stud
11 12
     private boolean running = true;
12 13
 
14
+
13 15
     Console(){
14 16
         gameLib.add("yahtzee");
15 17
         gameLib.add("war");
16
-        gameLib.add("three card stud");
18
+        gameLib.add("stud");
17 19
     }
18 20
 
19 21
     public void createAccount()
@@ -27,6 +29,20 @@ public class Console {
27 29
         player = new Player(name, balance);
28 30
     }
29 31
 
32
+    /**
33
+     * Currently required to make Stud a 2 player game, could later refactor to declare 'player' in createAccount method and pass to PlayerArray
34
+     */
35
+    public void createAccount2()
36
+    {
37
+        System.out.println("Hello, what is your name?");
38
+        String name = scanner.next();
39
+
40
+        System.out.println("How much money are you bringing to the table?");
41
+        int balance = getIntFromUser();
42
+
43
+        player2 = new Player(name, balance);
44
+    }
45
+
30 46
     public void chooseGame()
31 47
     {
32 48
         //get the command from the user
@@ -42,11 +58,13 @@ public class Console {
42 58
                 war.StartGame();
43 59
                 break;
44 60
 
45
-            case "three card stud":
61
+            case "stud":
46 62
                 int[] studMinMax = getMinMax();
47 63
                 Game stud = new Stud(studMinMax[0], studMinMax[1], 10);
64
+                System.out.println("Three card stud is a two player game.\nPlayer 2:\n");
65
+                createAccount2();
48 66
                 ((Stud) stud).addPlayers(player);
49
-                ((Stud) stud).addNpc();
67
+                ((Stud) stud).addPlayers(player2);
50 68
                 stud.StartGame();
51 69
                 break;
52 70
 

+ 130
- 31
src/main/java/io/zipcoder/casino/Stud.java View File

@@ -1,14 +1,13 @@
1 1
 package io.zipcoder.casino;
2
-
3 2
 import java.util.Scanner;
4 3
 
5 4
 public class Stud extends CardGame implements Gamble, Game {
6
-    // ArrayList<Card> playerHand = new ArrayList<>(3);
7 5
     Scanner scanner = new Scanner(System.in);
6
+    Console console;
7
+    // private int roundCount = 0;
8 8
 
9 9
     public Stud(int minBet, int maxBet, int ante) {
10 10
         super(minBet, maxBet, ante);
11
-
12 11
     }
13 12
     
14 13
     public void playCard(Player player, Card card) {
@@ -20,15 +19,78 @@ public class Stud extends CardGame implements Gamble, Game {
20 19
     }
21 20
 
22 21
     /**
23
-     * Need method to determine what hand wins first
22
+     * Determine what player wins by looping through player array and then
23
+     * passing each hand to the 'handValue' method
24 24
      */
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);
25
+    public CardPlayer determineWinner(){
26
+    int max = 0;
27
+    CardPlayer winner = null;
28
+
29
+    for(int i = 0; i < getPlayers().size(); i ++){
30
+        CardPlayer player = getPlayers().get(i);
31
+        int playerHandValue = handValue(player); // 'handValue' method sets 'max' value of this hand
32
+        if(playerHandValue > max){
33
+            max = playerHandValue;
34
+            winner = player;
35
+        }
36
+    }
37
+    System.out.println("The winner is " + winner.getPlayer().getName());
38
+    System.out.println(winner.getPlayer().getName() + "\'s hand was: " + winner.getHand().get(0).getName() + " - " + winner.getHand().get(1).getName() + " - " + winner.getHand().get(2).getName() );
39
+    return winner;
40
+    }
41
+
42
+    /**
43
+     * Method will return a int value, larger value means strong hand to determine the winning player
44
+     * @param player
45
+     * @return
46
+     */
47
+    public int handValue(CardPlayer player){
48
+        int handValue = 0;
49
+        int card1 = player.getHand().get(0).getCardValue();
50
+        int card2 = player.getHand().get(1).getCardValue();
51
+        int card3 = player.getHand().get(2).getCardValue();
52
+
53
+        //Three of a Kind
54
+        if (card1 == card2 && card1 == card3){
55
+            handValue = card1 * 1000000;
56
+        //Two pair
31 57
         }
58
+        else if (card1 == card2){
59
+            handValue = (card1 * 10000) + card3;
60
+        }
61
+        else if (card1 == card3){
62
+            handValue = (card1 * 10000) + card2;
63
+        }
64
+        else if (card2 == card3){
65
+            handValue = (card2 * 10000) + card1;
66
+        //High Card
67
+        } else {
68
+            // Card1 is Highest
69
+            if (card1 > card2 && card1 > card3 && card2 > card3) {
70
+                handValue = (card1 * 100) + (card2 * 10) + card3;
71
+            }
72
+            else if (card1 > card2 && card1 > card3 && card3 > card2) {
73
+                handValue = (card1 * 100) + (card3 * 10) + card2;
74
+            }
75
+            // Card2 is Highest
76
+            else if (card2 > card1 && card2 > card3 && card1 > card3) {
77
+                handValue = (card2 * 100) + (card1 * 10) + card3;
78
+            }
79
+            else if (card2 > card1 && card2 > card3 && card3 > card1) {
80
+                handValue = (card2 * 100) + (card3 * 10) + card1;
81
+            }
82
+            // Card3 is Highest
83
+            else if (card3 > card1 && card3 > card2 && card1 > card3) {
84
+                handValue = (card3 * 100) + (card1 * 10) + card3;
85
+            }
86
+            else if (card3 > card1 && card3 > card2 && card3 > card1) {
87
+                handValue = (card3 * 100) + (card3 * 10) + card1;
88
+            }
89
+            else {
90
+                handValue = 0;
91
+            }
92
+        }
93
+        return handValue;
32 94
     }
33 95
 
34 96
     public void Bet(Player player, int betAmount) {
@@ -40,6 +102,7 @@ public class Stud extends CardGame implements Gamble, Game {
40 102
         if(super.getWinner() != null){
41 103
             super.getWinner().changeBalance(super.getTablePot());
42 104
         }
105
+        System.out.println(getWinner().getName() + " won: " + super.getTablePot());
43 106
     }
44 107
 
45 108
     public void payAnte() {
@@ -51,52 +114,88 @@ public class Stud extends CardGame implements Gamble, Game {
51 114
     }
52 115
 
53 116
     public void Quit() {
54
-        //playAgain?
55
-        // Shuffle
56
-
117
+        System.out.println("Play again? Y : or any key to quit.");
118
+        String answer = scanner.next();
119
+        if (answer.equals("Y")){
120
+            StartGame();
121
+        } else {
122
+            console = new Console();
123
+        }
57 124
     }
58 125
 
59 126
     public void StartGame() {
60
-        Deck deck = new Deck();     //CREATE deck for game       
61
-        setHandSize(3);             //SET Hand Size for game
127
+        // Deck deck = new Deck();     //CREATE deck for game
128
+        setHandSize(3);             //SET Hand Size for game(3)
62 129
         payAnte();                  //PAY ante (all players)
63
-        Deal();                     //DEALS hands to each player
130
+        Deal();                     //DEALS cards/ hands to each player
64 131
         StartRound();               //METHOD called
65
-        Printer.studHandsDealt();   //CONSOLE dealt msg
132
+
66 133
     }
67 134
 
68 135
     /**
69
-     * Game played in this method
136
+     * Game played in this method by calling the 'gameRound' methods
70 137
      */
71 138
     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
-        }
139
+        System.out.println("Welcome to Three Card Stud!");
140
+        //for (int i = 0; i < getHandSize() - 1; i++){    //Each player turns a card in hand to face up
141
+        gameRound1();
142
+        gameRound2();
143
+        //}
75 144
         lastGameRound();
76 145
         determineWinner();
146
+        // Payout();
77 147
     }
78 148
 
79 149
     /**
80 150
      * Plays through rounds that includes flipping cards face up and then betting or folding
81 151
      */
82
-    public void gameRound(){
152
+    public void gameRound1(){
83 153
         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
154
+            CardPlayer player = super.getPlayers().get(j);                       //GET a player
155
+            playCard(player.getPlayer(), player.getHand().get(0));      //SHOW-PRINT players first CARD
156
+            //roundCount++;
157
+        }
158
+        for (int x = 0; x < getPlayers().size(); x++) {                          //Betting round or fold
159
+            CardPlayer player = super.getPlayers().get(x);
160
+            int bet;
161
+            //ask player to bet and pass amount to Bet(betAmount
162
+            System.out.println("Enter a bet, if 0 is entered you fold");
163
+//TRY- CATCH OR WHILE/IF statement
164
+            bet = Integer.parseInt(scanner.next());
165
+            if (bet == 0){
166
+                System.out.println(player.getPlayer().getName() + " folds.");
167
+                //if fold, player is removed from game
168
+                //if only 1 player game ends
169
+            } else {
170
+                Bet(super.getPlayers().get(x).getPlayer(), bet);
171
+                System.out.println(player.getPlayer().getName() + " bets: " + bet);
172
+            }
173
+        }
174
+    }
175
+
176
+
177
+    /**
178
+     * Plays through rounds that includes flipping cards face up and then betting or folding
179
+     */
180
+    public void gameRound2(){
181
+        for (int j = 0; j < getPlayers().size(); j++) {
182
+            CardPlayer player = super.getPlayers().get(j);                       //GET a player
183
+            playCard(player.getPlayer(), player.getHand().get(1));      //SHOW-PRINT players first CARD
184
+            //roundCount++;
86 185
         }
87
-        for (int j = 0; j < getPlayers().size(); j++) {                 //Betting round or fold
88
-            CardPlayer player = super.getPlayers().get(j);
186
+        for (int x = 0; x < getPlayers().size(); x++) {                          //Betting round or fold
187
+            CardPlayer player = super.getPlayers().get(x);
89 188
             int bet;
90 189
             //ask player to bet and pass amount to Bet(betAmount
91 190
             System.out.println("Enter a bet, if 0 is entered you fold");
92
-            bet = scanner.nextInt();
191
+//TRY- CATCH OR WHILE/IF statement
192
+            bet = Integer.parseInt(scanner.next());
93 193
             if (bet == 0){
94 194
                 System.out.println(player.getPlayer().getName() + " folds.");
95 195
                 //if fold, player is removed from game
96
-                //if only 1 player (dealer) game ends
196
+                //if only 1 player game ends
97 197
             } 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);
198
+                Bet(super.getPlayers().get(x).getPlayer(), bet);
100 199
                 System.out.println(player.getPlayer().getName() + " bets: " + bet);
101 200
             }
102 201
         }
@@ -111,7 +210,7 @@ public class Stud extends CardGame implements Gamble, Game {
111 210
     public void lastGameRound(){
112 211
         for (int j = 0; j < getPlayers().size(); j++) {
113 212
             CardPlayer player = super.getPlayers().get(j);              //GET a player
114
-            playCard(player.getPlayer(), player.getHand().get(j));      //SHOW-PRINT players first CARD
213
+            playCard(player.getPlayer(), player.getHand().get(2));      //SHOW-PRINT players first CARD
115 214
         }
116 215
     }
117 216