Quellcode durchsuchen

merge war w/ stud

Nick Satinover vor 6 Jahren
Ursprung
Commit
6aa4fcd19c

+ 2
- 2
src/main/java/io/zipcoder/casino/Card.java Datei anzeigen

@@ -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)

+ 15
- 3
src/main/java/io/zipcoder/casino/CardGame.java Datei anzeigen

@@ -14,6 +14,7 @@ public abstract class CardGame {
14 14
     private int ante;
15 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 19
     private Deck deck = new Deck();
19 20
 
@@ -106,7 +107,6 @@ public abstract class CardGame {
106 107
                 break;
107 108
             }
108 109
         }
109
-        System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
110 110
     }
111 111
 
112 112
     public void chooseNextTurn(){
@@ -116,13 +116,25 @@ public abstract class CardGame {
116 116
             if((players.indexOf(playersTurn) + 1) == players.size()){
117 117
                 //start again at the starting player
118 118
                 playersTurn = players.get(0);
119
-                System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
119
+                //System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
120 120
 
121 121
             //if it is not the end of the turn circle
122 122
             } else {
123 123
                 playersTurn = players.get(players.indexOf(playersTurn) + 1);
124
-                System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
124
+                //System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
125 125
             }
126 126
         }
127 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
+    }
128 140
 }

+ 20
- 0
src/main/java/io/zipcoder/casino/CardPlayer.java Datei anzeigen

@@ -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;
@@ -34,4 +36,22 @@ public class CardPlayer {
34 36
 
35 37
         return null;
36 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
+    }
37 57
 }

+ 27
- 5
src/main/java/io/zipcoder/casino/Console.java Datei anzeigen

@@ -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,9 +29,9 @@ 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":
@@ -104,4 +109,21 @@ public class Console {
104 109
         return command;
105 110
     }
106 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
+
107 129
 }

+ 0
- 1
src/main/java/io/zipcoder/casino/Dice.java Datei anzeigen

@@ -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(){

+ 2
- 1
src/main/java/io/zipcoder/casino/Gamble.java Datei anzeigen

@@ -2,5 +2,6 @@ package io.zipcoder.casino;
2 2
 
3 3
 public interface Gamble {
4 4
      void Bet(Player player, int betAmount);
5
-     int Payout(int payoutAmount);
5
+     // void Bet(int betAmount);
6
+     void Payout();
6 7
 }

+ 5
- 3
src/main/java/io/zipcoder/casino/Printer.java Datei anzeigen

@@ -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
 

+ 5
- 3
src/main/java/io/zipcoder/casino/Stud.java Datei anzeigen

@@ -11,11 +11,14 @@ public class Stud extends CardGame implements Gamble, Game {
11 11
 
12 12
     }
13 13
     
14
-    public void playCard(Player player, Card card){
14
+    public void playCard(Player player, Card card) {
15 15
         card.setVisibility(true);               //CARD isVISIBLE
16 16
         Printer.showCard(player, card);         //PRINT card name to CONSOLE
17 17
     }
18 18
 
19
+    public void fold(){
20
+    }
21
+
19 22
     /**
20 23
      * Need method to determine what hand wins first
21 24
      */
@@ -33,11 +36,10 @@ public class Stud extends CardGame implements Gamble, Game {
33 36
         player.changeBalance(betAmount * -1);
34 37
     }
35 38
 
36
-    public int Payout(int payoutAmount) {
39
+    public void Payout(){
37 40
         if(super.getWinner() != null){
38 41
             super.getWinner().changeBalance(super.getTablePot());
39 42
         }
40
-        return 0;
41 43
     }
42 44
 
43 45
     public void payAnte() {

+ 74
- 9
src/main/java/io/zipcoder/casino/War.java Datei anzeigen

@@ -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() {
@@ -65,10 +99,40 @@ public class War extends CardGame implements Gamble, Game {
65 99
         super.chooseStatingPlayer();
66 100
         payAnte();
67 101
         Deal();
68
-        //super.chooseNextTurn();
102
+        StartRound();
69 103
     }
70 104
 
71 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
+
72 136
         //player plays a card faceup
73 137
         //remove cards from player hand
74 138
         //pc plays a card faceup
@@ -97,8 +161,9 @@ public class War extends CardGame implements Gamble, Game {
97 161
                 super.getDeck().remove(card);
98 162
             }
99 163
         }
164
+
100 165
         System.out.println(super.getPlayersTurn().getPlayer().getName() +
101 166
                 "has: " + super.getPlayersTurn().getHand().size() + " cards.");
102
-    }
103 167
 
168
+    }
104 169
 }