ソースを参照

completed war method

Jonathan Hinds 6 年 前
コミット
dfd5d65943
共有3 個のファイルを変更した69 個の追加38 個の削除を含む
  1. 2
    2
      pom.xml
  2. 66
    35
      src/main/java/io/zipcoder/casino/War.java
  3. 1
    1
      src/main/java/io/zipcoder/casino/test.java

+ 2
- 2
pom.xml ファイルの表示

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

+ 66
- 35
src/main/java/io/zipcoder/casino/War.java ファイルの表示

@@ -1,14 +1,18 @@
1 1
 package io.zipcoder.casino;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.HashMap;
5
-import java.util.Scanner;
3
+import java.util.*;
4
+import java.util.concurrent.Executors;
5
+import java.util.concurrent.ScheduledExecutorService;
6
+import java.util.concurrent.TimeUnit;
6 7
 import java.util.regex.Pattern;
8
+import java.util.concurrent.TimeUnit;
7 9
 
8 10
 public class War extends CardGame implements Gamble, Game {
9 11
 
10 12
     private ArrayList<Card> tableCards = new ArrayList<Card>();
13
+    private ArrayList<CardPlayer> warMembers = new ArrayList<CardPlayer>();
11 14
     private Scanner scanner = new Scanner(System.in);
15
+    private boolean war = false;
12 16
 
13 17
     War(int minBet, int maxBet, int ante) {
14 18
         super(minBet, maxBet, ante);
@@ -18,51 +22,93 @@ public class War extends CardGame implements Gamble, Game {
18 22
     /**
19 23
      * Specific to war methods
20 24
      */
21
-    public void playCard(){
25
+    public void playCard(boolean cardFace){
22 26
         //if the player has cards in their hand
23 27
         if(super.getPlayersTurn().getHand().size() > 0) {
24 28
             //pull out a card to play
25 29
             Card card = super.getPlayersTurn().getHand().get(0);
26 30
             //play the card face up, on the table
27
-            card.setVisibility(true);
31
+            card.setVisibility(cardFace);
28 32
             tableCards.add(card);
29 33
             //store the last played card in the players wrapper class
30 34
             super.getPlayersTurn().setPlayedCard(card);
31 35
             //remove this card from their hand
32 36
             super.getPlayersTurn().getHand().remove(card);
33 37
             //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.");
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
+            }
35 43
         //if the player has not cards in their hand but has cards in their discard, pickup their discard and play a card
36 44
         } else if(super.getPlayersTurn().getHand().size() == 0 && super.getPlayersTurn().getDiscard().size() > 0) {
37 45
             System.out.println(super.getPlayersTurn().getPlayer().getName() + " ran out of cards and picked up their discard pile.");
38 46
             super.getPlayersTurn().getHand().addAll(super.getPlayersTurn().getDiscard());
39 47
             super.getPlayersTurn().setDiscard(new ArrayList<Card>());
40
-            playCard();
48
+            playCard(true);
41 49
         //if the person has no cards in their hand, and no cards in discard they lose.
42 50
         } else if(super.getPlayersTurn().getHand().size() == 0 && super.getPlayersTurn().getDiscard().size() == 0){
43 51
             super.setLoser(super.getPlayersTurn().getPlayer());
52
+            System.out.println(super.getPlayersTurn().getPlayer().getName() + " has lost the match!");
44 53
         }
45 54
     }
46 55
 
47
-    public void warMethod(){
48
-        //take three cards from your hand face down
49
-        //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;
50 75
     }
51 76
 
52
-    public CardPlayer determineWinner(){
77
+    public CardPlayer determineWinner(ArrayList<CardPlayer> playerList){
53 78
 
54 79
         int max = 0;
55 80
         CardPlayer winner = null;
81
+        boolean war = false;
56 82
 
57
-        for(int i = 0; i < getPlayers().size(); i ++){
58
-            CardPlayer player = getPlayers().get(i);
59
-            if(player.getPlayedCard().getCardValue() > max){
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
60 90
                 max = player.getPlayedCard().getCardValue();
91
+                //make them the winner
61 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;
62 98
             }
63 99
         }
64
-        System.out.println("The winner is " + winner.getPlayer().getName());
65
-        return winner;
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;
66 112
     }
67 113
 
68 114
     /**
@@ -82,7 +128,7 @@ public class War extends CardGame implements Gamble, Game {
82 128
         for(int i = 0; i < super.getPlayers().size(); i ++)
83 129
         {
84 130
             CardPlayer player = super.getPlayers().get(i);
85
-            player.getPlayer().changeBalance(-super.getAnte());
131
+            player.getPlayer().changeBalance(super.getAnte() * -1);
86 132
         }
87 133
     }
88 134
 
@@ -107,42 +153,27 @@ public class War extends CardGame implements Gamble, Game {
107 153
             System.out.println("Type play to play the top card from your pile.");
108 154
             String input = scanner.next();
109 155
             input = input.toLowerCase().trim();
110
-
111 156
             if (input.equals("play")) {
112 157
                 //each player
113 158
                 for (CardPlayer player : super.getPlayers()) {
114 159
                     //plays a card, then
115
-                    playCard();
160
+                    playCard(true);
116 161
                     //the turn updates to be the next players.
117 162
                     super.chooseNextTurn();
118 163
                 }
119
-                //determine the winner
120
-                CardPlayer winner = determineWinner();
121
-
164
+                //determine the winner once all players play a card
165
+                CardPlayer winner = determineWinner(super.getPlayers());
122 166
                 System.out.println(winner.getPlayer().getName() + " has been rewarded " + tableCards.size() + " cards.");
123
-
124 167
                 //add all the table cards to the players discard
125 168
                 winner.addDiscard(tableCards);
126 169
                 //clear the table cards pile
127 170
                 tableCards = new ArrayList<Card>();
128
-
129 171
                 //if the user does not type play
130 172
             } else {
131 173
                 //display a message
132 174
                 System.out.println("Sorry, I don't understand that command.");
133 175
             }
134 176
         }
135
-
136
-        //player plays a card faceup
137
-        //remove cards from player hand
138
-        //pc plays a card faceup
139
-        //remove cards from npc hand
140
-        //determinewinner
141
-        //add all table cards to winners discard facedown
142
-
143
-        //when player is out of cards
144
-        //shuffle players discard
145
-        //insert discard into hand facedown
146 177
     }
147 178
 
148 179
     public void Deal() {

+ 1
- 1
src/main/java/io/zipcoder/casino/test.java ファイルの表示

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