|
@@ -6,11 +6,13 @@ import io.zipcoder.casino.CardGame.Deck;
|
6
|
6
|
import io.zipcoder.casino.Console;
|
7
|
7
|
import io.zipcoder.casino.Player;
|
8
|
8
|
|
|
9
|
+import java.util.EmptyStackException;
|
9
|
10
|
import java.util.Scanner;
|
10
|
11
|
import java.util.Stack;
|
11
|
12
|
|
12
|
13
|
import static io.zipcoder.casino.CardGame.Card.toCard;
|
13
|
14
|
import static io.zipcoder.casino.CardGame.Solitaire.Foundation.allFoundsFull;
|
|
15
|
+import static io.zipcoder.casino.CardGame.Solitaire.Foundation.cheatFoundations;
|
14
|
16
|
import static io.zipcoder.casino.CardGame.Solitaire.Foundation.whichSuit;
|
15
|
17
|
|
16
|
18
|
public class Solitaire extends CardGame {
|
|
@@ -19,6 +21,7 @@ public class Solitaire extends CardGame {
|
19
|
21
|
Solitaire s = new Solitaire(new Player("Bill"));
|
20
|
22
|
s.start();
|
21
|
23
|
}
|
|
24
|
+ Console console = new Console(System.in, System.out);
|
22
|
25
|
|
23
|
26
|
Scanner in = new Scanner(System.in);
|
24
|
27
|
//clean up.
|
|
@@ -29,6 +32,8 @@ public class Solitaire extends CardGame {
|
29
|
32
|
public Stack<Card> wastePile;
|
30
|
33
|
public Tableau[] arrayTabs;
|
31
|
34
|
public static Stack<Card> tempStack = new Stack<>();
|
|
35
|
+ public static Stack<Card> lastStack = null;
|
|
36
|
+ public Boolean ifFromWaste = false;
|
32
|
37
|
|
33
|
38
|
public Solitaire(Player player) {
|
34
|
39
|
this.player = player;
|
|
@@ -41,13 +46,27 @@ public class Solitaire extends CardGame {
|
41
|
46
|
tab6 = new Tableau();
|
42
|
47
|
tab7 = new Tableau();
|
43
|
48
|
arrayTabs = new Tableau[]{tab1, tab2, tab3, tab4, tab5, tab6, tab7};
|
44
|
|
- start();
|
45
|
49
|
}
|
46
|
50
|
|
47
|
51
|
public static Deck solitaireDeck = new Deck();
|
48
|
52
|
|
49
|
|
- public void deal() {
|
|
53
|
+
|
|
54
|
+ public void start(){
|
|
55
|
+ System.out.println("Welcome");
|
|
56
|
+ resetDeck();
|
|
57
|
+ wastePile.removeAllElements();
|
|
58
|
+ tempStack.removeAllElements();
|
|
59
|
+ shuffle();
|
|
60
|
+ deal();
|
|
61
|
+ print();
|
|
62
|
+ takeATurn();
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public void shuffle(){
|
50
|
66
|
solitaireDeck.shuffle();
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ public void deal() {
|
51
|
70
|
for (int i = 0; i < arrayTabs.length; i++) {
|
52
|
71
|
for (int j = 0; j < arrayTabs.length; j++) {
|
53
|
72
|
if (j >= i) arrayTabs[j].add(draw());
|
|
@@ -63,6 +82,7 @@ public class Solitaire extends CardGame {
|
63
|
82
|
|
64
|
83
|
public Stack<Card> pickUp(){
|
65
|
84
|
tempStack.push(wastePile.pop());
|
|
85
|
+ lastStack = wastePile;
|
66
|
86
|
return tempStack;
|
67
|
87
|
}
|
68
|
88
|
|
|
@@ -92,80 +112,75 @@ public class Solitaire extends CardGame {
|
92
|
112
|
case '8':
|
93
|
113
|
whichSuit(tempStack);
|
94
|
114
|
break;
|
95
|
|
- case '9':
|
96
|
|
- whichSuit(tempStack);
|
97
|
|
- break;
|
98
|
|
- case '0':
|
99
|
|
- whichSuit(tempStack);
|
100
|
|
- break;
|
101
|
|
- case '-':
|
102
|
|
- whichSuit(tempStack);
|
103
|
|
- break;
|
104
|
|
- case 'E': //develop way to replace original stack on pile. don't change coverage until placed. same with pulled from stack.
|
105
|
|
- break;
|
106
|
115
|
default:
|
107
|
116
|
System.out.println("Not a valid entry. Try again or press \'E\'");
|
108
|
117
|
dropToTab(in.next().charAt(0));
|
109
|
118
|
}
|
110
|
119
|
}
|
111
|
120
|
|
112
|
|
- public Stack<Card> pull(String cardCode){
|
113
|
|
- char f = cardCode.charAt(0);
|
114
|
|
- char s = cardCode.charAt(1);
|
|
121
|
+ public void pull(String cardCode){
|
|
122
|
+ char f = cardCode.toUpperCase().charAt(0);
|
|
123
|
+ char s = cardCode.toUpperCase().charAt(1);
|
115
|
124
|
Card c = toCard(f,s);
|
116
|
|
- return findTab(c).pull(c);
|
|
125
|
+ findTab(c).pull(c);
|
117
|
126
|
}
|
118
|
127
|
|
119
|
128
|
public Tableau findTab(Card c){
|
120
|
129
|
for (Tableau tab : arrayTabs)
|
121
|
|
- if (tab.stack.contains(c))
|
|
130
|
+ if (tab.stack.contains(c)) {
|
|
131
|
+ lastStack = tab.stack;
|
122
|
132
|
return tab;
|
|
133
|
+ }
|
123
|
134
|
return null;
|
124
|
135
|
}
|
125
|
136
|
|
126
|
|
- public void start(){
|
127
|
|
- System.out.println("Welcome");
|
128
|
|
- resetDeck();
|
129
|
|
- wastePile.removeAllElements();
|
130
|
|
- deal();
|
131
|
|
- print();
|
132
|
|
- takeATurn();
|
133
|
|
- }
|
134
|
|
-
|
135
|
|
- public void end() {
|
136
|
|
- System.out.println("Congratulations!");
|
137
|
|
- System.out.println("Enter \'N\' to play again or press \'Q\' to quit");
|
138
|
|
- String command = in.next().toUpperCase();
|
139
|
|
- while (!command.equals("Q") || !command.equals("N")){
|
140
|
|
- if (command.equals("Q")) end();
|
141
|
|
- else if (command.equals("N")) start();
|
142
|
|
- else System.out.println("Invalid. Enter \'N\' to play again or press \'Q\' to quit");
|
143
|
|
- }
|
144
|
|
- }
|
145
|
|
-
|
146
|
|
- public String getInput(){
|
147
|
|
- return in.next();
|
|
137
|
+ public void peekWaste(){
|
|
138
|
+ if(wastePile.size()>0) System.out.println("\n\nDraw pile: " + wastePile.peek().toString2());
|
148
|
139
|
}
|
149
|
140
|
|
|
141
|
+ //you've got a temp stack. so when you pull a card, show it. if it doesn't go, put it back.
|
|
142
|
+ //fix empty stack exceptions
|
|
143
|
+ //draw shouldn't reprint every time. only print top of wastePile
|
|
144
|
+ //build console class for solitaire printouts
|
150
|
145
|
public void takeATurn() {
|
151
|
|
- Console.println("Let's play");
|
152
|
|
- while (!getInput().equals("QUIT") || !allFoundsFull()) {
|
153
|
|
- if (Console.getInputString("Enter draw to draw a card").equals("DRAW")) {
|
154
|
|
- drawCard();
|
155
|
|
- print();
|
156
|
|
- continue;
|
157
|
|
- } else if (Console.getInputString("Enter draw to draw a card").equals("P")) {
|
158
|
|
- pickUp();
|
159
|
|
- dropToTab(getInput().charAt(0));
|
160
|
|
- print();
|
161
|
|
- } else if (Console.getInputString("Enter draw to draw a card").length() == 2) {
|
162
|
|
- pull(String.valueOf(getInput()));
|
163
|
|
- dropToTab(getInput().charAt(0));
|
164
|
|
- print();
|
|
146
|
+ console.println("\n\nReady? Let's Play");
|
|
147
|
+ while (!allFoundsFull() || !gameOver()) {
|
|
148
|
+ String command = console.getInputString("\nWhat now?");
|
|
149
|
+ switch (String.valueOf(command)) {
|
|
150
|
+ case "DRAW":
|
|
151
|
+ drawCard();
|
|
152
|
+ print();
|
|
153
|
+ //console.println("\nDraw pile: " + wastePile.peek().toString2());
|
|
154
|
+ break;
|
|
155
|
+ case "P":
|
|
156
|
+ //try, catch, continue
|
|
157
|
+ try {
|
|
158
|
+ pickUp();
|
|
159
|
+ console.println("\nYou just picked up " + tempStack.peek().toString2());
|
|
160
|
+ dropToTab(console.getDropTab().charAt(0));
|
|
161
|
+ print();
|
|
162
|
+ break;
|
|
163
|
+ } catch (EmptyStackException e) {
|
|
164
|
+ console.println("\nCan't pull from an empty draw pile");
|
|
165
|
+ break;
|
|
166
|
+ }
|
|
167
|
+ case "QUIT":
|
|
168
|
+ gameOver();
|
|
169
|
+// case "FOO":
|
|
170
|
+// cheatFoundations();
|
|
171
|
+ default:
|
|
172
|
+ try {
|
|
173
|
+ pull(String.valueOf(command));
|
|
174
|
+ console.println("\nYou just pulled " + tempStack.peek().toString2());
|
|
175
|
+ dropToTab(console.getDropTab().charAt(0));
|
|
176
|
+ } catch (NullPointerException e) {
|
|
177
|
+ console.println("Card does not exist. Try again :)\n");
|
|
178
|
+ break;
|
|
179
|
+ }
|
|
180
|
+ print();
|
|
181
|
+ break;
|
|
182
|
+ }
|
165
|
183
|
}
|
166
|
|
- }
|
167
|
|
-
|
168
|
|
- if (allFoundsFull()) end();
|
169
|
184
|
}
|
170
|
185
|
|
171
|
186
|
public void addPlayer(Player player) {
|
|
@@ -176,12 +191,6 @@ public class Solitaire extends CardGame {
|
176
|
191
|
|
177
|
192
|
}
|
178
|
193
|
|
179
|
|
- public void moveable() {
|
180
|
|
- }
|
181
|
|
-
|
182
|
|
- public void receivable() {
|
183
|
|
- }
|
184
|
|
-
|
185
|
194
|
public Card draw() {
|
186
|
195
|
return solitaireDeck.draw();
|
187
|
196
|
}
|
|
@@ -189,15 +198,56 @@ public class Solitaire extends CardGame {
|
189
|
198
|
public void resetDeck(){
|
190
|
199
|
solitaireDeck = new Deck();
|
191
|
200
|
}
|
|
201
|
+ Foundation found = new Foundation();
|
192
|
202
|
|
193
|
203
|
public void print(){
|
194
|
|
- int i = 0;
|
|
204
|
+ System.out.println("CLUBS\t\tDIAMONDS\t\tHEARTS\t\tSPADES");
|
|
205
|
+ System.out.println("------\t\t--------\t\t------\t\t------");
|
|
206
|
+
|
|
207
|
+ if (Foundation.clubStack.size() == 0){
|
|
208
|
+ System.out.print(" -- \t\t");
|
|
209
|
+ } else {
|
|
210
|
+ System.out.print(" " + Foundation.clubStack.peek().toString2() + " \t\t");
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ if (Foundation.diamondStack.size() == 0){
|
|
214
|
+ System.out.print(" -- \t\t\t");
|
|
215
|
+ } else {
|
|
216
|
+ System.out.print(" " + Foundation.diamondStack.peek().toString2() + " \t\t\t");
|
|
217
|
+ }
|
|
218
|
+
|
|
219
|
+ if (Foundation.heartStack.size() == 0){
|
|
220
|
+ System.out.print(" -- \t\t");
|
|
221
|
+ } else {
|
|
222
|
+ System.out.print(" " + Foundation.heartStack.peek().toString2() + " \t");
|
|
223
|
+ }
|
|
224
|
+ if (Foundation.spadeStack.size() == 0){
|
|
225
|
+ System.out.println(" -- \t\t");
|
|
226
|
+ } else {
|
|
227
|
+ System.out.print(" " + Foundation.spadeStack.peek().toString2() + " \t\n");
|
|
228
|
+ }
|
|
229
|
+
|
|
230
|
+ int i = 1;
|
195
|
231
|
for (Tableau tab : arrayTabs) {
|
196
|
|
- System.out.println("tab " + i); i++;
|
197
|
|
- tab.stack.forEach(e -> System.out.println(e + " " + e.isCovered()));
|
|
232
|
+ System.out.println("\nCOLUMN " + i); i++;
|
|
233
|
+ tab.stack.forEach(e -> System.out.print(e.toString2() + " " + "\t"));
|
198
|
234
|
}
|
199
|
|
- if (wastePile.size() > 0) System.out.println("Top of Pile " + wastePile.peek());
|
|
235
|
+ peekWaste();
|
200
|
236
|
}
|
201
|
237
|
|
|
238
|
+ public Boolean gameOver(){
|
|
239
|
+ if(console.getInputString("Are you sure you want to quit?\nEnter Y to quit").equals("Y")) return true;
|
|
240
|
+ return false;
|
|
241
|
+ }
|
202
|
242
|
|
203
|
|
-}
|
|
243
|
+ public void end() {
|
|
244
|
+ System.out.println("Congratulations!");
|
|
245
|
+ System.out.println("Enter \'N\' to play again or press \'Q\' to quit");
|
|
246
|
+ String command = in.next().toUpperCase();
|
|
247
|
+ while (!command.equals("Q") || !command.equals("N")){
|
|
248
|
+ if (command.equals("Q"));
|
|
249
|
+ else if (command.equals("N")) start();
|
|
250
|
+ else System.out.println("Invalid. Enter \'N\' to play again or press \'Q\' to quit");
|
|
251
|
+ }
|
|
252
|
+ }
|
|
253
|
+}
|