소스 검색

war finished - except for war method

Jonathan Hinds 6 년 전
부모
커밋
47a6851247

+ 2
- 2
src/main/java/io/zipcoder/casino/Card.java 파일 보기

@@ -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 파일 보기

@@ -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
 
@@ -102,7 +103,6 @@ public abstract class CardGame {
102 103
                 break;
103 104
             }
104 105
         }
105
-        System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
106 106
     }
107 107
 
108 108
     public void chooseNextTurn(){
@@ -112,13 +112,25 @@ public abstract class CardGame {
112 112
             if((players.indexOf(playersTurn) + 1) == players.size()){
113 113
                 //start again at the starting player
114 114
                 playersTurn = players.get(0);
115
-                System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
115
+                //System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
116 116
 
117 117
             //if it is not the end of the turn circle
118 118
             } else {
119 119
                 playersTurn = players.get(players.indexOf(playersTurn) + 1);
120
-                System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
120
+                //System.out.println("it is now " + playersTurn.getPlayer().getName() + "'s turn");
121 121
             }
122 122
         }
123 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
+    }
124 136
 }

+ 20
- 0
src/main/java/io/zipcoder/casino/CardPlayer.java 파일 보기

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

+ 27
- 5
src/main/java/io/zipcoder/casino/Console.java 파일 보기

@@ -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 파일 보기

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

+ 1
- 1
src/main/java/io/zipcoder/casino/Gamble.java 파일 보기

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

+ 5
- 3
src/main/java/io/zipcoder/casino/Printer.java 파일 보기

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

+ 1
- 4
src/main/java/io/zipcoder/casino/Stud.java 파일 보기

@@ -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
     }
@@ -26,9 +25,7 @@ public class Stud extends CardGame implements Gamble, Game {
26 25
 
27 26
     }
28 27
 
29
-    public int Payout(int payoutAmount) {
30
-        return 0;
31
-    }
28
+    public void Payout() {}
32 29
 
33 30
     public void Ante(int anteAmount) {
34 31
 

+ 72
- 9
src/main/java/io/zipcoder/casino/War.java 파일 보기

@@ -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,8 +49,20 @@ 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
     /**
@@ -37,11 +72,10 @@ public class War extends CardGame implements Gamble, Game {
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,7 +161,6 @@ public class War extends CardGame implements Gamble, Game {
97 161
                 super.getDeck().remove(card);
98 162
             }
99 163
         }
100
-        System.out.println(super.getPlayersTurn().getPlayer().getName() + "has: " + super.getPlayersTurn().getHand().size() + " cards.");
164
+        System.out.println(super.getPlayersTurn().getPlayer().getName() + " has: " + super.getPlayersTurn().getHand().size() + " cards.");
101 165
     }
102
-
103 166
 }