Selaa lähdekoodia

merge war w/ stud

Nick Satinover 6 vuotta sitten
vanhempi
commit
6aa4fcd19c

+ 2
- 2
src/main/java/io/zipcoder/casino/Card.java Näytä tiedosto

44
         this.suit = suit;
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
     public void setCardValue(CardValue cardValue)
52
     public void setCardValue(CardValue cardValue)

+ 15
- 3
src/main/java/io/zipcoder/casino/CardGame.java Näytä tiedosto

14
     private int ante;
14
     private int ante;
15
     private CardPlayer playersTurn;
15
     private CardPlayer playersTurn;
16
     private Player winner = null;
16
     private Player winner = null;
17
+    private Player loser = null;
17
     private ArrayList<CardPlayer> players = new ArrayList<CardPlayer>();
18
     private ArrayList<CardPlayer> players = new ArrayList<CardPlayer>();
18
     private Deck deck = new Deck();
19
     private Deck deck = new Deck();
19
 
20
 
106
                 break;
107
                 break;
107
             }
108
             }
108
         }
109
         }
109
-        System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
110
     }
110
     }
111
 
111
 
112
     public void chooseNextTurn(){
112
     public void chooseNextTurn(){
116
             if((players.indexOf(playersTurn) + 1) == players.size()){
116
             if((players.indexOf(playersTurn) + 1) == players.size()){
117
                 //start again at the starting player
117
                 //start again at the starting player
118
                 playersTurn = players.get(0);
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
             //if it is not the end of the turn circle
121
             //if it is not the end of the turn circle
122
             } else {
122
             } else {
123
                 playersTurn = players.get(players.indexOf(playersTurn) + 1);
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 Näytä tiedosto

6
     private Player player;
6
     private Player player;
7
     private ArrayList<Card> hand = new ArrayList<>();
7
     private ArrayList<Card> hand = new ArrayList<>();
8
     private ArrayList<Card> discard = new ArrayList<>();
8
     private ArrayList<Card> discard = new ArrayList<>();
9
+    private Card playedCard = null;
10
+
9
 
11
 
10
     public CardPlayer(Player player){
12
     public CardPlayer(Player player){
11
         this.player = player;
13
         this.player = player;
34
 
36
 
35
         return null;
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 Näytä tiedosto

1
 package io.zipcoder.casino;
1
 package io.zipcoder.casino;
2
+import java.util.ArrayList;
2
 import java.util.InputMismatchException;
3
 import java.util.InputMismatchException;
3
 import java.util.Scanner;
4
 import java.util.Scanner;
4
 
5
 
5
 public class Console {
6
 public class Console {
6
     private Scanner scanner = new Scanner(System.in);
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
     private Game game = null;
9
     private Game game = null;
9
     private Player player;
10
     private Player player;
10
     private boolean running = true;
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
     public void createAccount()
19
     public void createAccount()
15
     {
20
     {
24
 
29
 
25
     public void chooseGame()
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
         switch(command){
35
         switch(command){
31
 
36
 
32
             case "war":
37
             case "war":
104
         return command;
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 Näytä tiedosto

2
 
2
 
3
 public class Dice {
3
 public class Dice {
4
 
4
 
5
-
6
     private int value;
5
     private int value;
7
 
6
 
8
     public void roll(){
7
     public void roll(){

+ 2
- 1
src/main/java/io/zipcoder/casino/Gamble.java Näytä tiedosto

2
 
2
 
3
 public interface Gamble {
3
 public interface Gamble {
4
      void Bet(Player player, int betAmount);
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 Näytä tiedosto

1
 package io.zipcoder.casino;
1
 package io.zipcoder.casino;
2
 
2
 
3
+import java.util.ArrayList;
4
+
3
 public class Printer {
5
 public class Printer {
4
 
6
 
5
-    public static void noMatchingGameName(String[] gameNames){
7
+    public static void noMatchingGameName(ArrayList<String> gameNames){
6
 
8
 
7
         String games = "";
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
         games = games.trim();
14
         games = games.trim();
13
 
15
 

+ 5
- 3
src/main/java/io/zipcoder/casino/Stud.java Näytä tiedosto

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

+ 74
- 9
src/main/java/io/zipcoder/casino/War.java Näytä tiedosto

2
 
2
 
3
 import java.util.ArrayList;
3
 import java.util.ArrayList;
4
 import java.util.HashMap;
4
 import java.util.HashMap;
5
+import java.util.Scanner;
5
 import java.util.regex.Pattern;
6
 import java.util.regex.Pattern;
6
 
7
 
7
 public class War extends CardGame implements Gamble, Game {
8
 public class War extends CardGame implements Gamble, Game {
8
 
9
 
9
     private ArrayList<Card> tableCards = new ArrayList<Card>();
10
     private ArrayList<Card> tableCards = new ArrayList<Card>();
11
+    private Scanner scanner = new Scanner(System.in);
10
 
12
 
11
     War(int minBet, int maxBet, int ante) {
13
     War(int minBet, int maxBet, int ante) {
12
         super(minBet, maxBet, ante);
14
         super(minBet, maxBet, ante);
17
      * Specific to war methods
19
      * Specific to war methods
18
      */
20
      */
19
     public void playCard(){
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
     public void warMethod(){
47
     public void warMethod(){
26
         //play one card face up
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
      * Below 3 Implemented from Gamble
69
      * Below 3 Implemented from Gamble
35
      */
70
      */
36
-    public void Bet(int betAmount) {
71
+    public void Bet(Player player, int betAmount) {
37
         super.changeTablePot(betAmount);
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
             super.getWinner().changeBalance(super.getTablePot());
77
             super.getWinner().changeBalance(super.getTablePot());
43
         }
78
         }
44
-        return 0;
45
     }
79
     }
46
 
80
 
47
     public void payAnte() {
81
     public void payAnte() {
65
         super.chooseStatingPlayer();
99
         super.chooseStatingPlayer();
66
         payAnte();
100
         payAnte();
67
         Deal();
101
         Deal();
68
-        //super.chooseNextTurn();
102
+        StartRound();
69
     }
103
     }
70
 
104
 
71
     public void StartRound() {
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
         //player plays a card faceup
136
         //player plays a card faceup
73
         //remove cards from player hand
137
         //remove cards from player hand
74
         //pc plays a card faceup
138
         //pc plays a card faceup
97
                 super.getDeck().remove(card);
161
                 super.getDeck().remove(card);
98
             }
162
             }
99
         }
163
         }
164
+
100
         System.out.println(super.getPlayersTurn().getPlayer().getName() +
165
         System.out.println(super.getPlayersTurn().getPlayer().getName() +
101
                 "has: " + super.getPlayersTurn().getHand().size() + " cards.");
166
                 "has: " + super.getPlayersTurn().getHand().size() + " cards.");
102
-    }
103
 
167
 
168
+    }
104
 }
169
 }