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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
|
/*
* move.c - move and move stacks routines for Freecell Solver
*
* Written by Shlomi Fish (shlomif@vipe.technion.ac.il), 2000
*
* This file is in the public domain (it's uncopyrighted).
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "move.h"
#include "state.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif
#if 0
/* This variable was used for debugging. */
int msc_counter=0;
#endif
#if 0
/* This function allocates an empty move stack */
fcs_move_stack_t * fcs_move_stack_create(void)
{
fcs_move_stack_t * ret;
/* Allocate the data structure itself */
ret = (fcs_move_stack_t *)malloc(sizeof(fcs_move_stack_t));
ret->max_num_moves = FCS_MOVE_STACK_GROW_BY;
ret->num_moves = 0;
/* Allocate some space for the moves */
ret->moves = (fcs_move_t *)malloc(sizeof(fcs_move_t)*ret->max_num_moves);
return ret;
}
#endif
#if 0
int fcs_move_stack_push(fcs_move_stack_t * stack, fcs_move_t move)
{
/* If all the moves inside the stack are taken then
resize the move vector */
if (stack->num_moves == stack->max_num_moves)
{
int a, b;
a = (stack->max_num_moves >> 3);
b = FCS_MOVE_STACK_GROW_BY;
stack->max_num_moves += max(a,b);
stack->moves = realloc(
stack->moves,
stack->max_num_moves * sizeof(fcs_move_t)
);
}
stack->moves[stack->num_moves++] = move;
return 0;
}
#endif
int freecell_solver_move_stack_pop(fcs_move_stack_t * stack, fcs_move_t * move)
{
if (stack->num_moves > 0)
{
*move = stack->moves[--stack->num_moves];
return 0;
}
else
{
return 1;
}
}
#if 0
void fcs_move_stack_destroy(fcs_move_stack_t * stack)
{
free(stack->moves);
free(stack);
}
#endif
void freecell_solver_move_stack_swallow_stack(
fcs_move_stack_t * stack,
fcs_move_stack_t * src_stack
)
{
fcs_move_t move;
while (!fcs_move_stack_pop(src_stack, &move))
{
fcs_move_stack_push(stack, move);
}
fcs_move_stack_destroy(src_stack);
}
#if 0
void fcs_move_stack_reset(
fcs_move_stack_t * stack
)
{
stack->num_moves = 0;
}
#endif
int freecell_solver_move_stack_get_num_moves(
fcs_move_stack_t * stack
)
{
return stack->num_moves;
}
#if 0
/*
This function duplicates a move stack
*/
fcs_move_stack_t * fcs_move_stack_duplicate(
fcs_move_stack_t * stack
)
{
fcs_move_stack_t * ret;
ret = (fcs_move_stack_t *)malloc(sizeof(fcs_move_stack_t));
ret->max_num_moves = stack->max_num_moves;
ret->num_moves = stack->num_moves;
ret->moves = (fcs_move_t *)malloc(sizeof(fcs_move_t) * ret->max_num_moves);
memcpy(ret->moves, stack->moves, sizeof(fcs_move_t) * ret->max_num_moves);
return ret;
}
#endif
#if 0
extern void fcs_derived_states_list_add_state(
fcs_derived_states_list_t * list,
fcs_state_with_locations_t * state,
fcs_move_stack_t * move_stack
)
{
if (list->num_states == list->max_num_states)
{
list->max_num_states += 16;
list->states = realloc(list->states, sizeof(list->states[0]) * list->max_num_states);
list->move_stacks = realloc(list->move_stacks, sizeof(list->move_stacks[0]) * list->max_num_states);
}
list->states[list->num_states] = state;
list->move_stacks[list->num_states] = move_stack;
list->num_states++;
}
#endif
/*
This function performs a given move on a state
*/
void freecell_solver_apply_move(fcs_state_with_locations_t * state_with_locations, fcs_move_t move, int freecells_num, int stacks_num, int decks_num)
{
fcs_state_t * state;
fcs_card_t temp_card;
int a;
int src_stack, dest_stack;
int src_freecell, dest_freecell;
int src_stack_len;
state = (&(state_with_locations->s));
dest_stack = fcs_move_get_dest_stack(move);
src_stack = fcs_move_get_src_stack(move);
dest_freecell = fcs_move_get_dest_freecell(move);
src_freecell = fcs_move_get_src_freecell(move);
switch(fcs_move_get_type(move))
{
case FCS_MOVE_TYPE_STACK_TO_STACK:
{
src_stack_len = fcs_stack_len(*state, src_stack);
for(a=0 ; a<fcs_move_get_num_cards_in_seq(move) ; a++)
{
fcs_push_stack_card_into_stack(
*state,
dest_stack,
src_stack,
src_stack_len - fcs_move_get_num_cards_in_seq(move)+a
);
}
for(a=0 ; a<fcs_move_get_num_cards_in_seq(move) ; a++)
{
fcs_pop_stack_card(
*state,
src_stack,
temp_card
);
}
}
break;
case FCS_MOVE_TYPE_FREECELL_TO_STACK:
{
temp_card = fcs_freecell_card(*state, src_freecell);
fcs_push_card_into_stack(*state, dest_stack, temp_card);
fcs_empty_freecell(*state, src_freecell);
}
break;
case FCS_MOVE_TYPE_FREECELL_TO_FREECELL:
{
temp_card = fcs_freecell_card(*state, src_freecell);
fcs_put_card_in_freecell(*state, dest_freecell, temp_card);
fcs_empty_freecell(*state, src_freecell);
}
break;
case FCS_MOVE_TYPE_STACK_TO_FREECELL:
{
fcs_pop_stack_card(*state, src_stack, temp_card);
fcs_put_card_in_freecell(*state, dest_freecell, temp_card);
}
break;
case FCS_MOVE_TYPE_STACK_TO_FOUNDATION:
{
fcs_pop_stack_card(*state, src_stack, temp_card);
fcs_increment_foundation(*state, fcs_move_get_foundation(move));
}
break;
case FCS_MOVE_TYPE_FREECELL_TO_FOUNDATION:
{
fcs_empty_freecell(*state, src_freecell);
fcs_increment_foundation(*state, fcs_move_get_foundation(move));
}
break;
case FCS_MOVE_TYPE_SEQ_TO_FOUNDATION:
{
for(a=0;a<13;a++)
{
fcs_pop_stack_card(*state, src_stack, temp_card);
fcs_increment_foundation(*state, fcs_move_get_foundation(move));
}
}
break;
case FCS_MOVE_TYPE_FLIP_CARD:
{
fcs_flip_stack_card(*state, src_stack, fcs_stack_len(*state, src_stack)-1);
}
break;
case FCS_MOVE_TYPE_CANONIZE:
{
fcs_canonize_state(state_with_locations, freecells_num, stacks_num);
}
break;
}
}
/*
The purpose of this function is to convert the moves from using
the canonized positions of the stacks and freecells to their
user positions.
*/
void freecell_solver_move_stack_normalize(
fcs_move_stack_t * moves,
fcs_state_with_locations_t * init_state,
int freecells_num,
int stacks_num,
int decks_num
)
{
fcs_move_stack_t * temp_moves;
fcs_move_t in_move, out_move;
fcs_state_with_locations_t dynamic_state;
fcs_move_stack_alloc_into_var(temp_moves);
fcs_duplicate_state(dynamic_state, *init_state);
while (
fcs_move_stack_pop(
moves,
&in_move
) == 0)
{
freecell_solver_apply_move(
&dynamic_state,
in_move,
freecells_num,
stacks_num,
decks_num
);
if (fcs_move_get_type(in_move) == FCS_MOVE_TYPE_CANONIZE)
{
/* Do Nothing */
}
else
{
fcs_move_set_type(out_move, fcs_move_get_type(in_move));
if ((fcs_move_get_type(in_move) == FCS_MOVE_TYPE_STACK_TO_STACK) ||
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_STACK_TO_FREECELL) ||
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_STACK_TO_FOUNDATION) ||
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_SEQ_TO_FOUNDATION)
)
{
fcs_move_set_src_stack(out_move,dynamic_state.stack_locs[(int)fcs_move_get_src_stack(in_move)]);
}
if (
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_FREECELL_TO_STACK) ||
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_FREECELL_TO_FREECELL) ||
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_FREECELL_TO_FOUNDATION))
{
fcs_move_set_src_freecell(out_move,dynamic_state.fc_locs[(int)fcs_move_get_src_freecell(in_move)]);
}
if (
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_STACK_TO_STACK) ||
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_FREECELL_TO_STACK)
)
{
fcs_move_set_dest_stack(out_move,dynamic_state.stack_locs[(int)fcs_move_get_dest_stack(in_move)]);
}
if (
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_STACK_TO_FREECELL) ||
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_FREECELL_TO_FREECELL)
)
{
fcs_move_set_dest_freecell(out_move,dynamic_state.fc_locs[(int)fcs_move_get_dest_freecell(in_move)]);
}
if ((fcs_move_get_type(in_move) == FCS_MOVE_TYPE_STACK_TO_FOUNDATION) ||
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_FREECELL_TO_FOUNDATION) ||
(fcs_move_get_type(in_move) == FCS_MOVE_TYPE_SEQ_TO_FOUNDATION)
)
{
fcs_move_set_foundation(out_move,fcs_move_get_foundation(in_move));
}
if ((fcs_move_get_type(in_move) == FCS_MOVE_TYPE_STACK_TO_STACK))
{
fcs_move_set_num_cards_in_seq(out_move,fcs_move_get_num_cards_in_seq(in_move));
}
fcs_move_stack_push(temp_moves, out_move);
}
}
/*
* temp_moves contain the needed moves in reverse order. So let's use
* swallow_stack to put them in the original in the correct order.
*
* */
fcs_move_stack_reset(moves);
freecell_solver_move_stack_swallow_stack(moves, temp_moves);
fcs_clean_state(&dynamic_state);
}
char * freecell_solver_move_to_string(fcs_move_t move, int standard_notation)
{
char string[256];
switch(fcs_move_get_type(move))
{
case FCS_MOVE_TYPE_STACK_TO_STACK:
if (standard_notation)
{
sprintf(string, "%i%i",
1+fcs_move_get_src_stack(move),
1+fcs_move_get_dest_stack(move)
);
}
else
{
sprintf(string, "Move %i cards from stack %i to stack %i",
fcs_move_get_num_cards_in_seq(move),
fcs_move_get_src_stack(move),
fcs_move_get_dest_stack(move)
);
}
break;
case FCS_MOVE_TYPE_FREECELL_TO_STACK:
if (standard_notation)
{
sprintf(string, "%c%i",
('a'+fcs_move_get_src_freecell(move)),
1+fcs_move_get_dest_stack(move)
);
}
else
{
sprintf(string, "Move a card from freecell %i to stack %i",
fcs_move_get_src_freecell(move),
fcs_move_get_dest_stack(move)
);
}
break;
case FCS_MOVE_TYPE_FREECELL_TO_FREECELL:
if (standard_notation)
{
sprintf(string, "%c%c",
('a'+fcs_move_get_src_freecell(move)),
('a'+fcs_move_get_dest_freecell(move))
);
}
else
{
sprintf(string, "Move a card from freecell %i to freecell %i",
fcs_move_get_src_freecell(move),
fcs_move_get_dest_freecell(move)
);
}
break;
case FCS_MOVE_TYPE_STACK_TO_FREECELL:
if (standard_notation)
{
sprintf(string, "%i%c",
1+fcs_move_get_src_stack(move),
('a'+fcs_move_get_dest_freecell(move))
);
}
else
{
sprintf(string, "Move a card from stack %i to freecell %i",
fcs_move_get_src_stack(move),
fcs_move_get_dest_freecell(move)
);
}
break;
case FCS_MOVE_TYPE_STACK_TO_FOUNDATION:
if (standard_notation)
{
sprintf(string, "%ih", 1+fcs_move_get_src_stack(move));
}
else
{
sprintf(string, "Move a card from stack %i to the foundations",
fcs_move_get_src_stack(move)
);
}
break;
case FCS_MOVE_TYPE_FREECELL_TO_FOUNDATION:
if (standard_notation)
{
sprintf(string, "%ch", ('a'+fcs_move_get_src_freecell(move)));
}
else
{
sprintf(string,
"Move a card from freecell %i to the foundations",
fcs_move_get_src_freecell(move)
);
}
break;
case FCS_MOVE_TYPE_SEQ_TO_FOUNDATION:
if (standard_notation)
{
sprintf(string, "%ih", fcs_move_get_src_stack(move));
}
else
{
sprintf(string,
"Move the sequence on top of Stack %i to the foundations",
fcs_move_get_src_stack(move)
);
}
break;
default:
string[0] = '\0';
break;
}
return strdup(string);
}
|