-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solitaire.java
284 lines (265 loc) · 9.83 KB
/
Solitaire.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
/**
* This class is the main solitaire game class. It uses the classes Deck, Card, and Tableau to
* execute a solitaire simulation game.
*
* @author jsupton
*/
public class Solitaire {
/**
* Main method for creating a new Solitaire object and running the game
* @param args
*/
public static void main(String[] args) {
new Solitaire();
}
/**
* Solitaire constructor. Consists of creating a new
* deck of cards, then shuffling those cards, and then
* executing a the solitaire game.
*/
public Solitaire() {
Deck d = new Deck();
ArrayList<Card> deck = d.getDeck();
//Randomly shuffles the deck
System.out.println("SHUFFLED DECK");
d.shuffle();
deck = d.getDeck();
GameBoard t = new GameBoard(d);
//Shuffles the deck for a GUARANTEED WIN
/*System.out.println("\nSHUFFLED DECK - GUARANTEED TO WIN");
d.shuffle_WIN();
deck = d.getDeck();
GameBoard t = new GameBoard(d);*/
//Shuffles the deck for a GUARANTEED LOSS
/*System.out.println("\nSHUFFLED DECK - GUARANTEED TO LOSE");
d.shuffle_LOSE();
deck = d.getDeck();
GameBoard t = new GameBoard(d);*/
//A loop that executes the solitaire game
while(t.getStockCounter()<3){
boolean tab = true;
boolean stock = false;
while(tab)
{
tab = checkTableau(t);
}
while(!stock)
{
stock = checkStock(t);
}
}
//If all of the cards are in the destination piles YOU WIN
ArrayList<ArrayList<Card>> destination = t.getDestination();
if(destination.get(0).size()==13 && destination.get(1).size()==13 &&
destination.get(2).size()==13 && destination.get(3).size()==13) {
System.out.println("YOU WIN");
}
else {
System.out.println("YOU LOSE");
}
}
/**
* This method is used for checking the stock pile. First, it checks to see if the card on the stock
* pile can be moved to a destination pile. It then checks to see if it can be moved to somewhere on
* the tableau.
* @param t the tableau object for the game
* @return returning false will cause the while loop above to loop again. This means, if a stock
* card was placed in a destination pile, it loops again, since nothing was placed on the bottom
* tableau, so that doesn't need to be checked again for moves. Returning true means a stock card
* was placed on the tableau, which will cause the loop to stop since we now need to look over the
* tableau and see if we can do any moves down there
*/
public boolean checkStock(GameBoard t) {
ArrayList<ArrayList<Card>> board = t.getTableau();
ArrayList<Card> stock = t.getStock();
ArrayList<Card> discard = t.getDiscard();
ArrayList<ArrayList<Card>> destination = t.getDestination();
//If the stock is not empty:
if(!stock.isEmpty()) {
Card compC = stock.get(0);
compC.makeVisible();
//Check to see if the stock card can be placed in any of the 4 destination piles
for(int j = 0; j < 4 ; j++) {
ArrayList<Card> dest = destination.get(j);
if(!dest.isEmpty()) {
Card card = dest.get(dest.size()-1);
boolean b = t.validDestinationMove(compC,card);
//If it is a valid move, move the stock card, and flip a new stock card over
if(b) {
stock.remove(0);
dest.add(compC);
if(!stock.isEmpty())
stock.get(0).makeVisible();
t.printGameBoard();
return false;
}
}
//If the stock card is an Ace, it can be automatically placed in one of the empty destination piles
else if(compC.getRank()==1) {
stock.remove(0);
dest.add(compC);
if(!stock.isEmpty())
stock.get(0).makeVisible();
t.printGameBoard();
return false;
}
}
//Check if it can be placed on any of the bottom columns
for(int i = 0; i < 7;i++) {
ArrayList<Card> col = board.get(i);
if(!col.isEmpty()) {
Card c = col.get(col.size()-1);
if(c.getVisibility()==true) {
boolean b = t.validTableauMove(compC,c);
if(b) {
//If it is a valid move, move the stock card, and flip a new stock card over
stock.remove(0);
col.add(compC);
if(!stock.isEmpty())
stock.get(0).makeVisible();
t.printGameBoard();
//
return true;
}
}
}
//If the tableau column is empty, then the stock card can be placed in the empty column
else {
stock.remove(0);
col.add(compC);
if(!stock.isEmpty())
stock.get(0).makeVisible();
t.printGameBoard();
return true;
}
}
//If the card couldn't be placed, remove the card from the stock and put it into the Discard Pile
compC.makeInvisible();
discard.add(compC);
stock.remove(0);
//If the stock isn't empty, flip over a new card
if(!stock.isEmpty()) {
stock.get(0).makeVisible();
return false;
}
//If the stock is empty, the counter is incremented, and the discard pile become the stock again
else {
System.out.println("COUNTER INCREMENTED");
t.incrementStockCounter();
t.swapStockAndDiscard();
if(t.getStockCounter()<3)
return false;
else
return true;
}
}
//If the stock is empty, the counter is incremented, and the discard pile become the stock again
else if(t.getStockCounter()<3) {
System.out.println("COUNTER INCREMENTED");
t.incrementStockCounter();
t.swapStockAndDiscard();
return false;
}
else
return true;
}
/**
* This method is used for checking the tableau. First, it checks to see if any of the cards on the tableau can be moved
* to a destination pile. It then checks to see if any of the cards on the tableau can be moved to another spot on the tableau
* @param t the tableau object for the game
* @return returning true means that a card on the tableau was either moved to a destination pile, or moved to another spot on the
* tableau. Either way, the main loop above continues, and the tableau is rechecked for new possible moves. Returning false means
* that there were no valid moves on the tableau.
*/
public boolean checkTableau(GameBoard t) {
ArrayList<ArrayList<Card>> board = t.getTableau();
ArrayList<ArrayList<Card>> destination = t.getDestination();
//Checks if any of the cards below can be moved to the Top
for(int i = 0; i < 7;i++) {
//gets a column of the tableau
ArrayList<Card> col = board.get(i);
if(!col.isEmpty()) {
//each ending card in each column is checked to see if it can be moved above
Card colCard = col.get(col.size()-1);
for(int j = 0; j < 4; j++) {
ArrayList<Card> dest = destination.get(j);
if(colCard.getVisibility()==true) {
if(!dest.isEmpty()) {
Card destCard = dest.get(dest.size()-1);
boolean b = t.validDestinationMove(colCard,destCard);
if(b) {
dest.add(col.remove(col.size()-1));
if(col.size()!=0)
col.get(col.size()-1).makeVisible();
t.printGameBoard();
return true;
}
}
else if(colCard.getRank()==1) {
col.remove(col.size()-1);
dest.add(colCard);
if(col.size()!=0)
col.get(col.size()-1).makeVisible();
t.printGameBoard();
return true;
}
}
}
}
}
//Check if any of the bottom cards can be moved to eachother
for(int i = 0; i < 6;i++) {
//gets a column of the tableau
ArrayList<Card> col = board.get(i);
if(!col.isEmpty()) {
for(int k = 0;k<col.size();k++) {
//Checks each card in that column of the tableau
Card colCard = col.get(k);
if(colCard.getVisibility()==true) {
for(int j = i+1; j < 7; j++) {
//gets a different column of the tableau
ArrayList<Card> dest = board.get(j);
if(!dest.isEmpty()) {
for(int m = 0;m<dest.size();m++) {
//checks each card in that other column of the tableau
Card destCard = dest.get(m);
if(destCard.getVisibility()==true) {
//Checks both ways to see if either move is valid
boolean a = t.validTableauMove(colCard,destCard);
boolean b = t.validTableauMove(destCard,colCard);
//I chose to only allow cards to swap, if this card is not part of a run already
//for example a 2 of hearts can be moved to a 3 only if the 2 isn't already on another 3
if(m==(dest.size()-1) && k==col.size()-1) {
if(a && (col.size()==1 || col.get(k-1).getVisibility()==false)) {
for(int n = k;n<col.size();n++)
dest.add(col.remove(n));
if(col.size()!=0)
col.get(col.size()-1).makeVisible();
t.printGameBoard();
return true;
}
//I chose to only allow cards to swap, if this card is not part of a run already
//for example a 2 of hearts can be moved to a 3 only if the 2 isn't already on another 3
else if(b && (dest.size()==1 || dest.get(m-1).getVisibility()==false)) {
for(int n = m;n<dest.size();n++)
col.add(dest.remove(n));
if(dest.size()!=0)
dest.get(dest.size()-1).makeVisible();
t.printGameBoard();
return true;
}
}
}
}
}
}
}
}
}
}
return false;
}
}