Procházet zdrojové kódy

Fix merge conflicts

Lauren Green před 6 roky
rodič
revize
fc8e5164c7

+ 2
- 2
pom.xml Zobrazit soubor

@@ -13,8 +13,8 @@
13 13
                 <groupId>org.apache.maven.plugins</groupId>
14 14
                 <artifactId>maven-compiler-plugin</artifactId>
15 15
                 <configuration>
16
-                    <source>7</source>
17
-                    <target>7</target>
16
+                    <source>8</source>
17
+                    <target>8</target>
18 18
                 </configuration>
19 19
             </plugin>
20 20
         </plugins>

+ 2
- 2
src/main/java/io/zipcoder/casino/Card.java Zobrazit soubor

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

+ 58
- 4
src/main/java/io/zipcoder/casino/CardGame.java Zobrazit soubor

@@ -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,57 @@ public abstract class CardGame {
79 80
     public int getHandSize() {
80 81
         return handSize;
81 82
     }
83
+
84
+    public CardPlayer getPlayersTurn() {
85
+        return playersTurn;
86
+    }
87
+
88
+    public void setPlayersTurn(CardPlayer playersTurn) {
89
+        this.playersTurn = playersTurn;
90
+    }
91
+
92
+    public void addNpc(){
93
+        addPlayers(new NPC("Opponant", getAnte()));
94
+    }
95
+
96
+    public void chooseStatingPlayer(){
97
+        //loop through the players
98
+        for(int i = 0; i < getPlayers().size(); i ++){
99
+            //if one is not an NPC
100
+            if(!(getPlayers().get(i).getPlayer() instanceof NPC)){
101
+                //set this player to have the current turn
102
+                setPlayersTurn(getPlayers().get(i));
103
+                break;
104
+            }
105
+        }
106
+    }
107
+
108
+    public void chooseNextTurn(){
109
+        if(playersTurn != null)
110
+        {
111
+            //if it is the end of the turn circle
112
+            if((players.indexOf(playersTurn) + 1) == players.size()){
113
+                //start again at the starting player
114
+                playersTurn = players.get(0);
115
+                //System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
116
+
117
+            //if it is not the end of the turn circle
118
+            } else {
119
+                playersTurn = players.get(players.indexOf(playersTurn) + 1);
120
+                //System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
121
+            }
122
+        }
123
+    }
124
+
125
+    public void printTurn(){
126
+        System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
127
+    }
128
+
129
+    public Player getLoser() {
130
+        return loser;
131
+    }
132
+
133
+    public void setLoser(Player loser) {
134
+        this.loser = loser;
135
+    }
82 136
 }

+ 20
- 0
src/main/java/io/zipcoder/casino/CardPlayer.java Zobrazit soubor

@@ -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;
@@ -30,4 +32,22 @@ public class CardPlayer {
30 32
 
31 33
         return null;
32 34
     }
35
+
36
+    public Card getPlayedCard() {
37
+        return playedCard;
38
+    }
39
+
40
+    public void setPlayedCard(Card playedCard) {
41
+        this.playedCard = playedCard;
42
+    }
43
+
44
+    public void addDiscard(ArrayList<Card> cards) {
45
+        this.discard.addAll(cards);
46
+    }
47
+    public ArrayList<Card> getDiscard() {
48
+        return discard;
49
+    }
50
+    public void setDiscard(ArrayList<Card> discard) {
51
+        this.discard = discard;
52
+    }
33 53
 }

+ 33
- 4
src/main/java/io/zipcoder/casino/Console.java Zobrazit soubor

@@ -1,15 +1,21 @@
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
     public boolean running = true;
11 12
 
12
-    Console(){}
13
+    Console(){
14
+        gameLib.add("yahtzee");
15
+        gameLib.add("war");
16
+        gameLib.add("three card stud");
17
+        gameLib.add("quit");
18
+    }
13 19
 
14 20
     public void createAccount()
15 21
     {
@@ -34,6 +40,7 @@ public class Console {
34 40
                     int[] warMinMax = getMinMax();
35 41
                     Game war = new War(warMinMax[0], warMinMax[1], 10);
36 42
                     ((War) war).addPlayers(player);
43
+                    ((War) war).addNpc();
37 44
                     war.startGame();
38 45
                     break;
39 46
 
@@ -41,6 +48,7 @@ public class Console {
41 48
                     int[] studMinMax = getMinMax();
42 49
                     Game stud = new Stud(studMinMax[0], studMinMax[1], 10);
43 50
                     ((Stud) stud).addPlayers(player);
51
+                    ((Stud) stud).addNpc();
44 52
                     stud.startGame();
45 53
                     break;
46 54
 
@@ -49,13 +57,16 @@ public class Console {
49 57
                     yahtzee.startGame();
50 58
                     break;
51 59
 
60
+                case "quit":
61
+                    System.out.println("Thanks for your money chump!");
62
+                    running = false;
63
+                    break;
64
+
52 65
                 default:
53 66
                     Printer.noMatchingGameName(gameLib);
54 67
                     break;
55 68
             }
56
-
57 69
         }
58
-
59 70
     }
60 71
 
61 72
     public int getIntFromUser(){
@@ -105,4 +116,22 @@ public class Console {
105 116
         command = command.toLowerCase().trim();
106 117
         return command;
107 118
     }
119
+
120
+    public String continueAskGame(){
121
+        String command = "";
122
+
123
+        System.out.println("Please choose a game to play!");
124
+        command = getCommand();
125
+
126
+        if(gameLib.indexOf(command) == -1)
127
+        {
128
+            while(gameLib.indexOf(command) == -1)
129
+            {
130
+                Printer.noMatchingGameName(gameLib);
131
+                command = getCommand();
132
+            }
133
+        }
134
+        return command;
135
+    }
136
+
108 137
 }

+ 5
- 16
src/main/java/io/zipcoder/casino/Deck.java Zobrazit soubor

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

@@ -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
- 0
src/main/java/io/zipcoder/casino/DiceGame.java Zobrazit soubor

@@ -5,4 +5,6 @@ public abstract class DiceGame {
5 5
     private int numberOfRolls;
6 6
     private Player[] players;
7 7
     private Player playersTurn;
8
+
9
+
8 10
 }

+ 8
- 0
src/main/java/io/zipcoder/casino/NPC.java Zobrazit soubor

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

+ 5
- 3
src/main/java/io/zipcoder/casino/Printer.java Zobrazit soubor

@@ -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
- 1
src/main/java/io/zipcoder/casino/Stud.java Zobrazit soubor

@@ -9,7 +9,6 @@ public class Stud extends CardGame implements Gamble, Game {
9 9
 
10 10
     }
11 11
 
12
-
13 12
     public void determineWinner(){
14 13
 
15 14
     }
@@ -31,6 +30,11 @@ public class Stud extends CardGame implements Gamble, Game {
31 30
     }
32 31
 
33 32
 
33
+    public void Ante(int anteAmount) {
34
+
35
+    }
36
+
37
+
34 38
     /**
35 39
      * Below 3 Implemented from Game
36 40
      */

+ 124
- 22
src/main/java/io/zipcoder/casino/War.java Zobrazit soubor

@@ -1,12 +1,18 @@
1 1
 package io.zipcoder.casino;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.HashMap;
3
+import java.util.*;
4
+import java.util.concurrent.Executors;
5
+import java.util.concurrent.ScheduledExecutorService;
6
+import java.util.concurrent.TimeUnit;
5 7
 import java.util.regex.Pattern;
8
+import java.util.concurrent.TimeUnit;
6 9
 
7 10
 public class War extends CardGame implements Gamble, Game {
8 11
 
9 12
     private ArrayList<Card> tableCards = new ArrayList<Card>();
13
+    private ArrayList<CardPlayer> warMembers = new ArrayList<CardPlayer>();
14
+    private Scanner scanner = new Scanner(System.in);
15
+    private boolean war = false;
10 16
 
11 17
     War(int minBet, int maxBet, int ante) {
12 18
         super(minBet, maxBet, ante);
@@ -16,18 +22,93 @@ public class War extends CardGame implements Gamble, Game {
16 22
     /**
17 23
      * Specific to war methods
18 24
      */
19
-    public void playCard(){
20
-        //take a card from the hand
21
-        //add it to the tablecard face up
25
+    public void playCard(boolean cardFace){
26
+        //if the player has cards in their hand
27
+        if(super.getPlayersTurn().getHand().size() > 0) {
28
+            //pull out a card to play
29
+            Card card = super.getPlayersTurn().getHand().get(0);
30
+            //play the card face up, on the table
31
+            card.setVisibility(cardFace);
32
+            tableCards.add(card);
33
+            //store the last played card in the players wrapper class
34
+            super.getPlayersTurn().setPlayedCard(card);
35
+            //remove this card from their hand
36
+            super.getPlayersTurn().getHand().remove(card);
37
+            //print the card that was played
38
+            if(cardFace == true) {
39
+                System.out.println(super.getPlayersTurn().getPlayer().getName() + " has played " + card.getName() + " and has " + super.getPlayersTurn().getHand().size() + " cards left.");
40
+            } else {
41
+                System.out.println(super.getPlayersTurn().getPlayer().getName() + " has played a card face down.");
42
+            }
43
+        //if the player has not cards in their hand but has cards in their discard, pickup their discard and play a card
44
+        } else if(super.getPlayersTurn().getHand().size() == 0 && super.getPlayersTurn().getDiscard().size() > 0) {
45
+            System.out.println(super.getPlayersTurn().getPlayer().getName() + " ran out of cards and picked up their discard pile.");
46
+            super.getPlayersTurn().getHand().addAll(super.getPlayersTurn().getDiscard());
47
+            super.getPlayersTurn().setDiscard(new ArrayList<Card>());
48
+            playCard(true);
49
+        //if the person has no cards in their hand, and no cards in discard they lose.
50
+        } else if(super.getPlayersTurn().getHand().size() == 0 && super.getPlayersTurn().getDiscard().size() == 0){
51
+            super.setLoser(super.getPlayersTurn().getPlayer());
52
+            System.out.println(super.getPlayersTurn().getPlayer().getName() + " has lost the match!");
53
+        }
22 54
     }
23 55
 
24
-    public void warMethod(){
25
-        //take three cards from your hand face down
26
-        //play one card face up
56
+    public CardPlayer warMethod(){
57
+        System.out.println("War!");
58
+
59
+        int max = 0;
60
+        CardPlayer winner = null;
61
+
62
+        //each player plays 3 cards
63
+        for(int i = 0; i < warMembers.size(); i ++){
64
+            for(int m = 0; m < 2; m ++){
65
+                playCard(false);
66
+            }
67
+            playCard(true);
68
+            super.chooseNextTurn();
69
+        }
70
+
71
+        //find the player with the highest value
72
+        winner = determineWinner(warMembers);
73
+        warMembers = new ArrayList<>();
74
+        return winner;
27 75
     }
28 76
 
29
-    public void determineWinner(Card player1card, Card player2card){
77
+    public CardPlayer determineWinner(ArrayList<CardPlayer> playerList){
78
+
79
+        int max = 0;
80
+        CardPlayer winner = null;
81
+        boolean war = false;
82
+
83
+        //loop through and get the max card value
84
+        for(int i = 0; i < playerList.size(); i ++){
85
+            CardPlayer player = playerList.get(i);
86
+            //if the players card is greater than the current max
87
+            if(player.getPlayedCard().getCardValue() > max)
88
+            {
89
+                //set their value as max
90
+                max = player.getPlayedCard().getCardValue();
91
+                //make them the winner
92
+                winner = player;
93
+                //set war to false
94
+                war = false;
95
+            }  else if (player.getPlayedCard().getCardValue() == max){
96
+                warMembers.add(player);
97
+                war = true;
98
+            }
99
+        }
30 100
 
101
+        if(war)
102
+        {
103
+            warMembers.add(winner);
104
+            winner = warMethod();
105
+            return winner;
106
+        } else if(!war)
107
+        {
108
+            System.out.println("The winner is " + winner.getPlayer().getName());
109
+            return winner;
110
+        }
111
+        return null;
31 112
     }
32 113
 
33 114
     /**
@@ -37,8 +118,10 @@ public class War extends CardGame implements Gamble, Game {
37 118
         super.changeTablePot(betAmount);
38 119
     }
39 120
 
121
+
40 122
     public void payout() {
41
-        if(super.getWinner() != null){
123
+        if(super.getWinner() != null)
124
+        {
42 125
             super.getWinner().changeBalance(super.getTablePot());
43 126
         }
44 127
     }
@@ -47,7 +130,7 @@ public class War extends CardGame implements Gamble, Game {
47 130
         for(int i = 0; i < super.getPlayers().size(); i ++)
48 131
         {
49 132
             CardPlayer player = super.getPlayers().get(i);
50
-            player.getPlayer().changeBalance(-super.getAnte());
133
+            player.getPlayer().changeBalance(super.getAnte() * -1);
51 134
         }
52 135
     }
53 136
 
@@ -60,22 +143,40 @@ public class War extends CardGame implements Gamble, Game {
60 143
     }
61 144
 
62 145
     public void startGame() {
63
-        Deck deck = new Deck();
146
+        System.out.println("Welcome to war!");
147
+        super.chooseStatingPlayer();
64 148
         payAnte();
65 149
         deal();
150
+        startRound();
66 151
     }
67 152
 
68 153
     public void startRound() {
69
-        //player plays a card faceup
70
-        //remove cards from player hand
71
-        //pc plays a card faceup
72
-        //remove cards from npc hand
73
-        //determinewinner
74
-        //add all table cards to winners discard facedown
75
-
76
-        //when player is out of cards
77
-        //shuffle players discard
78
-        //insert discard into hand facedown
154
+        while(super.getLoser() == null) {
155
+            System.out.println("Type play to play the top card from your pile.");
156
+            String input = scanner.next();
157
+            input = input.toLowerCase().trim();
158
+            if (input.equals("play")) {
159
+                //each player
160
+                for (CardPlayer player : super.getPlayers()) {
161
+                    //plays a card, then
162
+                    playCard(true);
163
+                    //the turn updates to be the next players.
164
+                    super.chooseNextTurn();
165
+                }
166
+                //determine the winner once all players play a card
167
+                CardPlayer winner = determineWinner(super.getPlayers());
168
+                System.out.println(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
169
+                //add all the table cards to the players discard
170
+                winner.addDiscard(tableCards);
171
+                //clear the table cards pile
172
+                tableCards = new ArrayList<Card>();
173
+                //if the user does not type play
174
+            } else {
175
+                //display a message
176
+                System.out.println("Sorry, I don't understand that command.");
177
+            }
178
+        }
179
+
79 180
     }
80 181
 
81 182
     public void deal() {
@@ -94,5 +195,6 @@ public class War extends CardGame implements Gamble, Game {
94 195
                 super.getDeck().remove(card);
95 196
             }
96 197
         }
198
+        System.out.println(super.getPlayersTurn().getPlayer().getName() + " has: " + super.getPlayersTurn().getHand().size() + " cards.");
97 199
     }
98 200
 }

+ 1
- 1
src/main/java/io/zipcoder/casino/test.java Zobrazit soubor

@@ -8,4 +8,4 @@ public class test {
8 8
         console.chooseGame();
9 9
     }
10 10
 
11
-}
11
+}