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
|
static Position *DFS(Position *via, Position *pos) /*Position *via, *pos;*/
{
int i, j, intab = 0, goodcols = 0; Card moved; Loc onto;
/* check for victory */
if (via->foundations[0] == 13 && via->foundations[1] == 13
&& via->foundations[2] == 13 && via->foundations[3] == 13) {
if (show) ShowPath(via);
else if (verbosity > 0) (void) FollowPath(via, true);
return(NULL);
}
if (maxdepth > 99999) {
if (depth > maxout) maxout = depth;
}
else if (depth >= maxdepth) {
maxout++;
return(pos);
}
if (distinct >= TRACTABLE) return(pos); /* give up */
for (i=3; i>=0 && via->hold[i]==0; i--) {}
for (j=7; j>=0 && via->tableau[via->colSeq[j]].count==0; j--) {}
i = (3 - i + 1) << (7 - j);
if (i > maxfree) {
maxfree = i;
/* if (i == 6) ReadablePosition(via); */
}
/* check for forced moves to foundation */
for (i=0; i<4; i++) {
int r = via->foundations[i]+1; /* next rank to go here */
int opp1 = (i^1), opp2 = (opp1^2); /* other color suits */
if (r > 13 || via->foundations[opp1]+2 < r ||
via->foundations[opp2]+2 < r) continue;
/* move is forced if card is available; note that if card
is swappable with its colormate, then if either card is
available they both are, since next lower cards of other
color are by definition already moved to foundations */
moved = (i<<4) + r;
if (Available(via, moved))
return TryMove(via, pos, moved, (Loc) FOUNDATION);
/* card not available, but note good column to dig in */
goodcols |= (1 << Col(via->location[cardToIndex[moved]]));
}
/* if previous move broke a sequence (and didn't move to foundation),
then mustTry will be >= 0, saying we must move to/from that column
if it is possible to do so */
if (via->mustTry >= 0) {
Column *col = &via->tableau[via->mustTry];
int tried = false;
moved = Bottom(via, via->mustTry);
/* if not a King, try moving to another non-empty column */
if (Rank(moved) < 13) {
Card onto = (moved+1) ^ 0x10;
for (i=0; i<2; i++) {
Loc loc = via->location[cardToIndex[onto]];
if (Available(via, onto) && loc != HOLDING) {
tried = true;
pos = TryMove(via, pos, moved, loc+1);
if (pos == NULL) return(NULL);
}
onto = Colormate(onto);
}
}
/* if can't move within tableau, try moving to holding area
or, if that's full, to empty column */
onto = FOUNDATION;
if (via->hold[3] == 0) onto = HOLDING;
else if (HasSpace(via)) onto = (via->colSeq[7]<<5) + 0;
if (!tried && onto != FOUNDATION &&
(col->count>1 || !HasSpace(via))) {
tried = true;
pos = TryMove(via, pos, moved, onto);
if (pos == NULL) return(NULL);
}
/* if neither of the above, try move to foundation (if can move
elsewhere, will try moving to foundation from there) */
if (!tried && via->foundations[Suit(moved)]+1 == Rank(moved)) {
tried = true;
pos = TryMove(via, pos, moved, (Loc) FOUNDATION);
if (pos == NULL) return(NULL);
}
/* move colormate of just-moved card onto revealed card */
if (Available(via, Colormate(via->moved))) {
tried = true;
pos = TryMove(via, pos, Colormate(via->moved),
(via->mustTry<<5) + col->count);
if (pos == NULL) return(NULL);
}
/* if this column is a singleton and the only place it can
move is into another empty column, abandon the line */
if (col->count == 1 && via->tableau[via->colSeq[7]].count == 0)
tried = true;
if (tried) {
brokeSeq++;
return(pos);
}
}
/* try non-forced moves to foundation, but don't move the second
of colormates up if both lower cards are still in play */
for (i=0; i<4; i++) {
int r = via->foundations[i]+1; /* next rank to go here */
if (r > 13) continue;
if (via->foundations[i^2] >= r && via->foundations[i^1]+2 < r
&& via->foundations[i^3]+2 < r) continue;
moved = (i<<4) + r;
/* if card & colormate both avail, moving original suffices;
other must then be able to move to where original was */
if (Available(via, moved)) {
pos = TryMove(via, pos, moved, (Loc) FOUNDATION);
if (pos == NULL) return(NULL);
}
else if (via->swappable[cardToIndex[Either(moved)]] &&
Available(via, Colormate(moved))) {
pos = TryMove(via, pos, moved, (Loc) FOUNDATION);
if (pos == NULL) return(NULL);
}
}
/* next preference is moves onto the bottoms of non-empty columns;
cards which can do this never need to move to empty cols or
holding cells (though in some odd cases they may end up moving
from the other col to a holding cell) */
for (i=0; i<8; i++) {
Column *col = &via->tableau[i];
if (col->count == 0) continue;
/* bottom card guaranteed to be > Ace else force-move above */
moved = (col->cards[col->count - 1] - 1) ^ 0x10;
onto = (i<<5) + col->count;
for (j=0; j<2; j++) {
if (Available(via, moved)) {
Loc where;
pos = TryMove(via, pos, moved, onto);
if (pos == NULL) return(NULL);
where = via->location[cardToIndex[moved]];
if (where != HOLDING)
intab |= 1 << (Col(where));
}
moved ^= 0x20;
}
}
/* next try moving from columns with 2+ cards to hold; if no hold,
move to empty columns; (if both, can get to empty cols via hold) */
onto = FOUNDATION;
if (via->hold[3] == 0)
onto = HOLDING;
else if (via->tableau[via->colSeq[7]].count == 0)
onto = (via->colSeq[7]<<5) + 0;
if (onto != FOUNDATION) {
for (i=0; i<8; i++)
if ((goodcols & (1<<i)) != 0 && (intab & (1<<i)) == 0) {
if (via->tableau[i].count < 2) continue;
pos = TryMove(via, pos, Bottom(via, i), onto);
if (pos == NULL) return(NULL);
}
for (i=0; i<8; i++)
if ((goodcols & (1<<i)) == 0 && (intab & (1<<i)) == 0) {
if (via->tableau[i].count < 2) continue;
pos = TryMove(via, pos, Bottom(via, i), onto);
if (pos == NULL) return(NULL);
}
}
/* try moving from holding area to empty column; vary which one is
tried first, in case the key move we're looking for is hold[3] */
if (via->tableau[via->colSeq[7]].count == 0) {
for (i=0; i<4; i++) {
Card below;
moved = via->hold[(i+(depth>>2))&3];
if (moved == 0) continue;
if (via->location[cardToIndex[moved]] != HOLDING)
moved ^= 0x20;
/* don't move a card down if both cards that could
use it are already on foundations */
below = (moved-1) ^ 0x10;
if (via->location[cardToIndex[below]] != FOUNDATION
|| via->location[cardToIndex[below^0x20]]
!= FOUNDATION) {
pos = TryMove(via, pos, moved,
(Loc) ((via->colSeq[7]<<5)+0));
if (pos == NULL) return(NULL);
}
}
}
/* try moving from singleton columns to holding area, but only if
there isn't another empty column already */
if (via->hold[3] == 0 && !HasSpace(via)) {
for (i=0; i<8; i++) if ((intab & (1<<i)) == 0) {
if (via->tableau[i].count != 1) continue;
pos = TryMove(via, pos, Bottom(via, i), (Loc) HOLDING);
if (pos == NULL) return(NULL);
}
}
/* last, try non-forced moves to foundation, where we're moving
up the second of colormates and both lower cards are in play */
for (i=0; i<4; i++) {
int r = via->foundations[i]+1; /* next rank to go here */
if (r > 13) continue;
if (!(via->foundations[i^2] >= r && via->foundations[i^1]+2 < r
&& via->foundations[i^3]+2 < r)) continue;
moved = (i<<4) + r;
if (Available(via, moved)) {
pos = TryMove(via, pos, moved, (Loc) FOUNDATION);
if (pos == NULL) return(NULL);
}
}
return(pos);
}
|