|
@@ -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
|
|
|
@@ -59,23 +142,41 @@ public class War extends CardGame implements Gamble, Game {
|
59
|
142
|
|
60
|
143
|
}
|
61
|
144
|
|
|
145
|
+
|
62
|
146
|
public void startGame() {
|
63
|
|
- Deck deck = new Deck();
|
|
147
|
+ System.out.println("Welcome to war!");
|
|
148
|
+ super.chooseStatingPlayer();
|
64
|
149
|
payAnte();
|
65
|
150
|
deal();
|
|
151
|
+ startRound();
|
66
|
152
|
}
|
67
|
153
|
|
68
|
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
|
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
|
}
|