Browse Source

resolved conflicts afer meging war and working

Jonathan Hinds 6 years ago
parent
commit
731fd0d957

+ 2
- 2
pom.xml View File

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

+ 2
- 2
src/main/java/io/zipcoder/casino/Card.java View File

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)

+ 58
- 4
src/main/java/io/zipcoder/casino/CardGame.java View File

12
     private int maxBet;
12
     private int maxBet;
13
     private int handSize;
13
     private int handSize;
14
     private int ante;
14
     private int ante;
15
-    private Player 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 ArrayList<Card> deck = new ArrayList<>();
19
+    private Deck deck = new Deck();
19
 
20
 
20
 
21
 
21
     CardGame(int minBet, int maxBet, int ante){
22
     CardGame(int minBet, int maxBet, int ante){
41
         card.setVisibility(true);
42
         card.setVisibility(true);
42
     }
43
     }
43
 
44
 
44
-    public ArrayList<Card> getDeck() {
45
+    public Deck getDeck() {
45
         return deck;
46
         return deck;
46
     }
47
     }
47
 
48
 
56
         }
57
         }
57
     }
58
     }
58
 
59
 
59
-    public void setDeck(ArrayList<Card> deck) {
60
+    public void setDeck(Deck deck) {
60
         this.deck = deck;
61
         this.deck = deck;
61
     }
62
     }
62
 
63
 
79
     public int getHandSize() {
80
     public int getHandSize() {
80
         return handSize;
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 View File

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;
30
 
32
 
31
         return null;
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
 }

+ 25
- 2
src/main/java/io/zipcoder/casino/Console.java View File

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
     {
111
         command = command.toLowerCase().trim();
116
         command = command.toLowerCase().trim();
112
         return command;
117
         return command;
113
     }
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
+
114
 }
137
 }

+ 5
- 16
src/main/java/io/zipcoder/casino/Deck.java View File

3
 import java.util.ArrayList;
3
 import java.util.ArrayList;
4
 import java.util.Collections;
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
      * Deck constructor will create an array of all 52 @Card 's
9
      * Deck constructor will create an array of all 52 @Card 's
15
     }
14
     }
16
 
15
 
17
     public void createDeck(){
16
     public void createDeck(){
18
-        this.deck = new ArrayList<>();
19
 
17
 
20
         for (int i = 0; i < 13; i++){
18
         for (int i = 0; i < 13; i++){
21
             Card.CardValue value = Card.CardValue.values()[i];
19
             Card.CardValue value = Card.CardValue.values()[i];
22
 
20
 
23
             for (int j = 0; j < 4; j++){
21
             for (int j = 0; j < 4; j++){
24
                 Card card = new Card(value, Card.Suit.values()[j]);
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
      * @return
33
      * @return
36
      */
34
      */
37
     public Card getCard(int index){
35
     public Card getCard(int index){
38
-        return this.deck.get(index);
36
+        return this.get(index);
39
     }
37
     }
40
 
38
 
41
     /**
39
     /**
43
      * @return
41
      * @return
44
      */
42
      */
45
     public Card pullCard(){
43
     public Card pullCard(){
46
-        Card tempCard = this.deck.remove(0);
44
+        Card tempCard = this.remove(0);
47
         return tempCard;
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 View File

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

+ 8
- 0
src/main/java/io/zipcoder/casino/NPC.java View File

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 View File

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
- 1
src/main/java/io/zipcoder/casino/Stud.java View File

9
 
9
 
10
     }
10
     }
11
 
11
 
12
-
13
     public void determineWinner(){
12
     public void determineWinner(){
14
 
13
 
15
     }
14
     }
31
     }
30
     }
32
 
31
 
33
 
32
 
33
+    public void Ante(int anteAmount) {
34
+
35
+    }
36
+
37
+
34
     /**
38
     /**
35
      * Below 3 Implemented from Game
39
      * Below 3 Implemented from Game
36
      */
40
      */

+ 124
- 22
src/main/java/io/zipcoder/casino/War.java View File

1
 package io.zipcoder.casino;
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
 import java.util.regex.Pattern;
7
 import java.util.regex.Pattern;
8
+import java.util.concurrent.TimeUnit;
6
 
9
 
7
 public class War extends CardGame implements Gamble, Game {
10
 public class War extends CardGame implements Gamble, Game {
8
 
11
 
9
     private ArrayList<Card> tableCards = new ArrayList<Card>();
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
     War(int minBet, int maxBet, int ante) {
17
     War(int minBet, int maxBet, int ante) {
12
         super(minBet, maxBet, ante);
18
         super(minBet, maxBet, ante);
16
     /**
22
     /**
17
      * Specific to war methods
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
         super.changeTablePot(betAmount);
118
         super.changeTablePot(betAmount);
38
     }
119
     }
39
 
120
 
121
+
40
     public void payout() {
122
     public void payout() {
41
-        if(super.getWinner() != null){
123
+        if(super.getWinner() != null)
124
+        {
42
             super.getWinner().changeBalance(super.getTablePot());
125
             super.getWinner().changeBalance(super.getTablePot());
43
         }
126
         }
44
     }
127
     }
47
         for(int i = 0; i < super.getPlayers().size(); i ++)
130
         for(int i = 0; i < super.getPlayers().size(); i ++)
48
         {
131
         {
49
             CardPlayer player = super.getPlayers().get(i);
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
 
59
 
142
 
60
     }
143
     }
61
 
144
 
145
+
62
     public void startGame() {
146
     public void startGame() {
63
-        Deck deck = new Deck();
147
+        System.out.println("Welcome to war!");
148
+        super.chooseStatingPlayer();
64
         payAnte();
149
         payAnte();
65
         deal();
150
         deal();
151
+        startRound();
66
     }
152
     }
67
 
153
 
68
     public void startRound() {
154
     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
155
+        while(super.getLoser() == null) {
156
+            System.out.println("Type play to play the top card from your pile.");
157
+            String input = scanner.next();
158
+            input = input.toLowerCase().trim();
159
+            if (input.equals("play")) {
160
+                //each player
161
+                for (CardPlayer player : super.getPlayers()) {
162
+                    //plays a card, then
163
+                    playCard(true);
164
+                    //the turn updates to be the next players.
165
+                    super.chooseNextTurn();
166
+                }
167
+                //determine the winner once all players play a card
168
+                CardPlayer winner = determineWinner(super.getPlayers());
169
+                System.out.println(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
170
+                //add all the table cards to the players discard
171
+                winner.addDiscard(tableCards);
172
+                //clear the table cards pile
173
+                tableCards = new ArrayList<Card>();
174
+                //if the user does not type play
175
+            } else {
176
+                //display a message
177
+                System.out.println("Sorry, I don't understand that command.");
178
+            }
179
+        }
79
     }
180
     }
80
 
181
 
81
     public void deal() {
182
     public void deal() {
94
                 super.getDeck().remove(card);
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 View File

8
         console.chooseGame();
8
         console.chooseGame();
9
     }
9
     }
10
 
10
 
11
-}
11
+}