|
@@ -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,22 +49,33 @@ 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
|
/**
|
34
|
69
|
* Below 3 Implemented from Gamble
|
35
|
70
|
*/
|
36
|
|
- public void Bet(int betAmount) {
|
|
71
|
+ public void Bet(Player player, int betAmount) {
|
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,8 +161,9 @@ public class War extends CardGame implements Gamble, Game {
|
97
|
161
|
super.getDeck().remove(card);
|
98
|
162
|
}
|
99
|
163
|
}
|
|
164
|
+
|
100
|
165
|
System.out.println(super.getPlayersTurn().getPlayer().getName() +
|
101
|
166
|
"has: " + super.getPlayersTurn().getHand().size() + " cards.");
|
102
|
|
- }
|
103
|
167
|
|
|
168
|
+ }
|
104
|
169
|
}
|