forked from danielrcollins1/SolitaireSolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameState.java
244 lines (221 loc) · 8.03 KB
/
GameState.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
//********************************************************************
// GameState.java @version 1.06
// State of one game of Klondike solitaire.
// Copyright (c) 2012 Daniel R. Collins. All rights reserved.
// See the bottom of this file for any licensing information.
//********************************************************************
public class GameState {
private boolean gameOver;
private int cardsDrawn, maxPasses, pass;
private Pile[] pile; // Piles of cards in game
final int NUM_PILES = 13;
final int IDX_DECK = 0;
final int IDX_WASTE = 1;
final int IDX_FOUND = 2;
final int IDX_TABLE = 6;
//-----------------------------------------------------------------
// Constructor (blank)
//-----------------------------------------------------------------
public GameState(int cardsDrawn, int maxPasses){
pass = 1;
gameOver = false;
this.maxPasses = maxPasses;
this.cardsDrawn = cardsDrawn;
pile = new Pile[NUM_PILES];
for (int i = 0; i < NUM_PILES; i++) {
pile[i] = new Pile();
}
}
//-----------------------------------------------------------------
// Constructor (copy)
//-----------------------------------------------------------------
public GameState(GameState old) {
pass = old.pass;
gameOver = old.gameOver;
maxPasses = old.maxPasses;
cardsDrawn = old.cardsDrawn;
pile = new Pile[NUM_PILES];
for (int i = 0; i < NUM_PILES; i++) {
pile[i] = new Pile(old.pile[i]);
}
}
//-----------------------------------------------------------------
// Accessors
//-----------------------------------------------------------------
public boolean isOver () { return gameOver; }
public int getcardsDrawn () { return cardsDrawn; }
public int getMaxPasses () { return maxPasses; }
public int getPass () { return pass; }
public Pile deck () { return pile[IDX_DECK]; }
public Pile waste () { return pile[IDX_WASTE]; }
public Pile found (int i) { return pile[IDX_FOUND+i]; }
public Pile table (int i) { return pile[IDX_TABLE+i]; }
public Pile getPile (int i) { return pile[i]; }
//-----------------------------------------------------------------
// Setup a new game
//-----------------------------------------------------------------
public void setupNewGame () {
pile[IDX_DECK] = new Pile(true);
deck().shuffle();
for (int i = 0; i < 7; i++) {
deck().drawToPile(table(i));
table(i).getTopCard().setFaceUp();
for (int j = i+1; j < 7; j++) {
deck().drawToPile(table(j));
}
}
}
//-----------------------------------------------------------------
// Check if game won
//-----------------------------------------------------------------
public boolean isGameWon () {
for (int i = 2; i <= 5; i++) {
if (pile[i].size() < 13)
return false;
}
return true;
}
//-----------------------------------------------------------------
// Scrubs hidden data on player view copy
//-----------------------------------------------------------------
public void scrubHiddenData () {
// On first pass, scrub cards face-down in deck
if (pass == 0) {
for (int i = 0; i < deck().size(); i++) {
deck().get(i).scrubData();
}
}
// On any pass, scrub cards face-down in tables
for (int i = 0; i < NUM_PILES-IDX_TABLE; i++) {
for (int j = 0; j < table(i).size(); j++) {
Card card = table(i).get(j);
if (!card.isFaceUp()) {
card.scrubData();
}
}
}
}
//-----------------------------------------------------------------
// Is integer in range (inclusive)?
//-----------------------------------------------------------------
boolean isInRange (int num, int low, int high) {
return (low <= num && num <= high);
}
//-----------------------------------------------------------------
// Player move call: return if move valid
//-----------------------------------------------------------------
public boolean playerMoveCall (int p1, int p2, int p3) {
if (handleMoveToDeck(p1, p2)) return true;
if (handleMoveToWaste(p1, p2)) return true;
if (handleMoveToFound(p1, p2)) return true;
if (handleMoveToTable(p1, p2, p3)) return true;
if (handleMoveFlipTop(p1, p2)) return true;
if (handleMoveSurrender(p1, p2)) return true;
return false;
}
//-----------------------------------------------------------------
// Handle move to deck (recycle waste, start new pass)
//-----------------------------------------------------------------
boolean handleMoveToDeck (int src, int dst) {
if (dst == 0) {
if (src != 1) return false;
if (waste().isEmpty()) return false;
if (deck().isEmpty() && pass < maxPasses) {
waste().flipWholePileFaceDown(deck());
pass++;
return true;
}
}
return false;
}
//-----------------------------------------------------------------
// Handle move to waste (turn cards from deck)
//-----------------------------------------------------------------
boolean handleMoveToWaste (int src, int dst) {
if (dst == 1) {
if (src != 0) return false;
if (deck().isEmpty()) return false;
for (int i = 0; i < cardsDrawn && !deck().isEmpty(); i++) {
deck().drawToPile(waste());
waste().getTopCard().setFaceUp();
}
return true;
}
return false;
}
//-----------------------------------------------------------------
// Handle move to found (one card from waste or table)
//-----------------------------------------------------------------
boolean handleMoveToFound (int src, int dst) {
if (isInRange(dst, 2, 5)) {
if (!(src == 1 || isInRange(src, 6, 12))) return false;
if (pile[src].isEmpty()) return false;
Card card = pile[src].getTopCard();
if (!card.isFaceUp()) return false;
// Ace to empty foundation
if (card.getRank() == 1 && pile[dst].isEmpty()) {
pile[src].drawToPile(pile[dst]);
return true;
}
// Same suit, one more rank
if (pile[dst].isEmpty()) return false;
Card destCard = pile[dst].getTopCard();
if ((card.getSuit() == destCard.getSuit())
&& (card.getRank() == destCard.getRank()+1)) {
pile[src].drawToPile(pile[dst]);
return true;
}
}
return false;
}
//-----------------------------------------------------------------
// Handle move to table (any non-deck, possibly more than one)
//-----------------------------------------------------------------
boolean handleMoveToTable (int src, int dst, int idx) {
if (isInRange(dst, 6, 12)) {
if (src == 0 || src == dst) return false;
if (pile[src].isEmpty()) return false;
if (isInRange(src, 1, 5)) idx = pile[src].size()-1; // one card
if (!isInRange(idx, 0, pile[src].size()-1)) return false;
Card card = pile[src].get(idx);
if (!card.isFaceUp()) return false;
// King to empty tableau
if (card.getRank() == 13 && pile[dst].isEmpty()) {
pile[src].moveSubpileToPile(idx, pile[dst]);
return true;
}
// Reverse color, one less rank
if (pile[dst].isEmpty()) return false;
Card destCard = pile[dst].getTopCard();
if ((card.isBlack() != destCard.isBlack())
&& (card.getRank() == destCard.getRank()-1)) {
pile[src].moveSubpileToPile(idx, pile[dst]);
return true;
}
}
return false;
}
//-----------------------------------------------------------------
// Handle move flip top card (coded by src == dst)
//-----------------------------------------------------------------
boolean handleMoveFlipTop (int src, int dst) {
if (dst == src) {
if (!isInRange(src, 6, 12)) return false;
if (pile[src].isEmpty()) return false;
if (pile[src].getTopCard().isFaceUp()) return false;
pile[src].getTopCard().setFaceUp();
return true;
}
return false;
}
//-----------------------------------------------------------------
// Handle surrender game
//-----------------------------------------------------------------
boolean handleMoveSurrender (int src, int dst) {
if (dst == 0 && src == 0) {
gameOver = true;
return true;
}
return false;
}
}