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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
|
/*
* state.c - state functions module 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 <string.h>
#include <stdio.h>
#include "config.h"
#include "state.h"
#include "card.h"
#include "fcs_enums.h"
#include "app_str.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif
#ifdef DEBUG_STATES
fcs_card_t freecell_solver_empty_card = {0,0};
#elif defined(COMPACT_STATES) || defined (INDIRECT_STACK_STATES)
fcs_card_t freecell_solver_empty_card = (fcs_card_t)0;
#endif
#ifdef INDIRECT_STACK_STATES
void freecell_solver_clean_state(
fcs_state_with_locations_t * state
)
{
int s;
for(s=0;s<MAX_NUM_STACKS;s++)
{
if (state->s.stacks[s] != NULL)
{
free(state->s.stacks[s]);
}
}
}
#endif
#ifdef INDIRECT_STACK_STATES
void freecell_solver_duplicate_state_proto(
fcs_state_with_locations_t * dest,
fcs_state_with_locations_t * src
)
{
int a;
int stack_len;
*dest = *src;
for(a=0;a<MAX_NUM_STACKS;a++)
{
if (src->s.stacks[a] != NULL)
{
stack_len = src->s.stacks[a][0];
#if 0
dest->s.stacks[a] = malloc(stack_len+14);
memset(dest->s.stacks[a], '\0', stack_len+14);
#else
dest->s.stacks[a] = malloc(stack_len+53);
memset(dest->s.stacks[a], '\0', stack_len+53);
#endif
memcpy(dest->s.stacks[a], src->s.stacks[a], stack_len+1);
}
}
}
#endif
static int fcs_card_compare(const void * card1, const void * card2)
{
const fcs_card_t * c1 = (const fcs_card_t *)card1;
const fcs_card_t * c2 = (const fcs_card_t *)card2;
if (fcs_card_card_num(*c1) > fcs_card_card_num(*c2))
{
return 1;
}
else if (fcs_card_card_num(*c1) < fcs_card_card_num(*c2))
{
return -1;
}
else
{
if (fcs_card_suit(*c1) > fcs_card_suit(*c2))
{
return 1;
}
else if (fcs_card_suit(*c1) < fcs_card_suit(*c2))
{
return -1;
}
else
{
return 0;
}
}
}
#ifdef DEBUG_STATES
static int fcs_stack_compare(const void * s1, const void * s2)
{
fcs_card_t card1 = ((const fc_stack_t *)s1)->cards[0];
fcs_card_t card2 = ((const fc_stack_t *)s2)->cards[0];
return fcs_card_compare(&card1, &card2);
}
#elif defined(COMPACT_STATES)
static int fcs_stack_compare(const void * s1, const void * s2)
{
fcs_card_t card1 = ((fcs_card_t*)s1)[1];
fcs_card_t card2 = ((fcs_card_t*)s2)[1];
return fcs_card_compare(&card1, &card2);
}
#elif defined(INDIRECT_STACK_STATES)
#if MAX_NUM_DECKS == 1
static int fcs_stack_compare_for_stack_sort(const void * s1, const void * s2)
{
fcs_card_t card1 = ((fcs_card_t*)s1)[1];
fcs_card_t card2 = ((fcs_card_t*)s2)[1];
return fcs_card_compare(&card1, &card2);
}
#endif
int freecell_solver_stack_compare_for_comparison(const void * v_s1, const void * v_s2)
{
const fcs_card_t * s1 = (const fcs_card_t *)v_s1;
const fcs_card_t * s2 = (const fcs_card_t *)v_s2;
int min_len;
int a, ret;
min_len = min(s1[0], s2[0]);
for(a=0;a<min_len;a++)
{
ret = fcs_card_compare(s1+a+1,s2+a+1);
if (ret != 0)
{
return ret;
}
}
/*
* The reason I do the stack length comparisons after the card-by-card
* comparison is to maintain correspondence with
* fcs_stack_compare_for_stack_sort, and with the one card comparison
* of the other state representation mechanisms.
* */
if (s1[0] < s2[0])
{
return -1;
}
else if (s1[0] > s2[0])
{
return 1;
}
else
{
return 0;
}
}
#endif
#ifdef FCS_WITH_TALONS
static int fcs_talon_compare_with_context(const void * p1, const void * p2, fcs_compare_context_t context)
{
fcs_card_t * t1 = (fcs_card_t *)p1;
fcs_card_t * t2 = (fcs_card_t *)p2;
if (t1[0] < t2[0])
{
return -1;
}
else if (t1[0] > t2[0])
{
return 1;
}
else
{
return memcmp(t1,t2,t1[0]+1);
}
}
#endif
#ifdef DEBUG_STATES
void freecell_solver_canonize_state(fcs_state_with_locations_t * state, int freecells_num, int stacks_num)
{
int b,c;
fc_stack_t temp_stack;
fcs_card_t temp_freecell;
int temp_loc;
/* Insertion-sort the stacks */
for(b=1;b<stacks_num;b++)
{
c = b;
while(
(c>0) &&
(fcs_stack_compare(
&(state->s.stacks[c]),
&(state->s.stacks[c-1])
) < 0)
)
{
temp_stack = state->s.stacks[c];
state->s.stacks[c] = state->s.stacks[c-1];
state->s.stacks[c-1] = temp_stack;
temp_loc = state->stack_locs[c];
state->stack_locs[c] = state->stack_locs[c-1];
state->stack_locs[c-1] = temp_loc;
c--;
}
}
/* Insertion sort the freecells */
for(b=1;b<freecells_num;b++)
{
c = b;
while(
(c>0) &&
(fcs_card_compare(
&(state->s.freecells[c]),
&(state->s.freecells[c-1])
) < 0)
)
{
temp_freecell = state->s.freecells[c];
state->s.freecells[c] = state->s.freecells[c-1];
state->s.freecells[c-1] = temp_freecell;
temp_loc = state->fc_locs[c];
state->fc_locs[c] = state->fc_locs[c-1];
state->fc_locs[c-1] = temp_loc;
c--;
}
}
}
#elif defined(COMPACT_STATES)
void freecell_solver_canonize_state(
fcs_state_with_locations_t * state,
int freecells_num,
int stacks_num)
{
int b,c;
char temp_stack[(MAX_NUM_CARDS_IN_A_STACK+1)];
fcs_card_t temp_freecell;
char temp_loc;
/* Insertion-sort the stacks */
for(b=1;b<stacks_num;b++)
{
c = b;
while(
(c>0) &&
(fcs_stack_compare(
state->s.data+c*(MAX_NUM_CARDS_IN_A_STACK+1),
state->s.data+(c-1)*(MAX_NUM_CARDS_IN_A_STACK+1)
) < 0)
)
{
memcpy(temp_stack, state->s.data+c*(MAX_NUM_CARDS_IN_A_STACK+1), (MAX_NUM_CARDS_IN_A_STACK+1));
memcpy(state->s.data+c*(MAX_NUM_CARDS_IN_A_STACK+1), state->s.data+(c-1)*(MAX_NUM_CARDS_IN_A_STACK+1), (MAX_NUM_CARDS_IN_A_STACK+1));
memcpy(state->s.data+(c-1)*(MAX_NUM_CARDS_IN_A_STACK+1), temp_stack, (MAX_NUM_CARDS_IN_A_STACK+1));
temp_loc = state->stack_locs[c];
state->stack_locs[c] = state->stack_locs[c-1];
state->stack_locs[c-1] = temp_loc;
c--;
}
}
/* Insertion-sort the freecells */
for(b=1;b<freecells_num;b++)
{
c = b;
while(
(c>0) &&
(fcs_card_compare(
state->s.data+FCS_FREECELLS_OFFSET+c,
state->s.data+FCS_FREECELLS_OFFSET+c-1
) < 0)
)
{
temp_freecell = (state->s.data[FCS_FREECELLS_OFFSET+c]);
state->s.data[FCS_FREECELLS_OFFSET+c] = state->s.data[FCS_FREECELLS_OFFSET+c-1];
state->s.data[FCS_FREECELLS_OFFSET+c-1] = temp_freecell;
temp_loc = state->fc_locs[c];
state->fc_locs[c] = state->fc_locs[c-1];
state->fc_locs[c-1] = temp_loc;
c--;
}
}
}
#elif defined(INDIRECT_STACK_STATES)
void freecell_solver_canonize_state(
fcs_state_with_locations_t * state,
int freecells_num,
int stacks_num)
{
int b,c;
fcs_card_t * temp_stack;
fcs_card_t temp_freecell;
char temp_loc;
/* Insertion-sort the stacks */
for(b=1;b<stacks_num;b++)
{
c = b;
while(
(c>0) &&
(
#if MAX_NUM_DECKS > 1
freecell_solver_stack_compare_for_comparison
#else
fcs_stack_compare_for_stack_sort
#endif
(
(state->s.stacks[c]),
(state->s.stacks[c-1])
)
< 0
)
)
{
temp_stack = state->s.stacks[c];
state->s.stacks[c] = state->s.stacks[c-1];
state->s.stacks[c-1] = temp_stack;
temp_loc = state->stack_locs[c];
state->stack_locs[c] = state->stack_locs[c-1];
state->stack_locs[c-1] = temp_loc;
c--;
}
}
/* Insertion sort the freecells */
for(b=1;b<freecells_num;b++)
{
c = b;
while(
(c>0) &&
(fcs_card_compare(
&(state->s.freecells[c]),
&(state->s.freecells[c-1])
) < 0)
)
{
temp_freecell = state->s.freecells[c];
state->s.freecells[c] = state->s.freecells[c-1];
state->s.freecells[c-1] = temp_freecell;
temp_loc = state->fc_locs[c];
state->fc_locs[c] = state->fc_locs[c-1];
state->fc_locs[c-1] = temp_loc;
c--;
}
}
}
#endif
static void fcs_state_init(fcs_state_with_locations_t * state, int stacks_num)
{
int a;
memset((void*)&(state->s), 0, sizeof(fcs_state_t));
for(a=0;a<MAX_NUM_STACKS;a++)
{
state->stack_locs[a] = a;
}
#ifdef INDIRECT_STACK_STATES
for(a=0;a<stacks_num;a++)
{
state->s.stacks[a] = malloc(MAX_NUM_DECKS*52+1);
memset(state->s.stacks[a], '\0', MAX_NUM_DECKS*52+1);
}
for(;a<MAX_NUM_STACKS;a++)
{
state->s.stacks[a] = NULL;
}
#endif
for(a=0;a<MAX_NUM_FREECELLS;a++)
{
state->fc_locs[a] = a;
}
}
#if (FCS_STATE_STORAGE != FCS_STATE_STORAGE_INDIRECT)
int freecell_solver_state_compare(const void * s1, const void * s2)
{
return memcmp(s1,s2,sizeof(fcs_state_t));
}
int freecell_solver_state_compare_equal(const void * s1, const void * s2)
{
return (!memcmp(s1,s2,sizeof(fcs_state_t)));
}
int freecell_solver_state_compare_with_context(
const void * s1,
const void * s2,
fcs_compare_context_t context
)
{
return memcmp(s1,s2,sizeof(fcs_state_t));
}
#else
int freecell_solver_state_compare_indirect(const void * s1, const void * s2)
{
return memcmp(*(fcs_state_with_locations_t * *)s1, *(fcs_state_with_locations_t * *)s2, sizeof(fcs_state_t));
}
int freecell_solver_state_compare_indirect_with_context(const void * s1, const void * s2, void * context)
{
return memcmp(*(fcs_state_with_locations_t * *)s1, *(fcs_state_with_locations_t * *)s2, sizeof(fcs_state_t));
}
#endif
static const char * const freecells_prefixes[] = { "FC:", "Freecells:", "Freecell:", ""};
static const char * const foundations_prefixes[] = { "Decks:", "Deck:", "Founds:", "Foundations:", "Foundation:", "Found:", ""};
static const char * const talon_prefixes[] = { "Talon:", "Queue:" , ""};
static const char * const num_redeals_prefixes[] = { "Num-Redeals:", "Readels-Num:", "Readeals-Number:", ""};
#ifdef WIN32
#define strncasecmp(a,b,c) (strnicmp((a),(b),(c)))
#endif
fcs_state_with_locations_t freecell_solver_initial_user_state_to_c(
const char * string,
int freecells_num,
int stacks_num,
int decks_num
#ifdef FCS_WITH_TALONS
,int talon_type
#endif
)
{
fcs_state_with_locations_t ret_with_locations;
int s,c;
const char * str;
fcs_card_t card;
int first_line;
int prefix_found;
const char * const * prefixes;
int i;
int decks_index[4];
fcs_state_init(&ret_with_locations, stacks_num);
str = string;
first_line = 1;
#define ret (ret_with_locations.s)
#ifdef FCS_WITH_TALONS
if (talon_type == FCS_TALON_KLONDIKE)
{
fcs_klondike_talon_num_redeals_left(ret) = -1;
}
#endif
for(s=0;s<stacks_num;s++)
{
/* Move to the next stack */
if (!first_line)
{
while((*str) != '\n')
{
str++;
}
str++;
}
first_line = 0;
prefixes = freecells_prefixes;
prefix_found = 0;
for(i=0;prefixes[i][0] != '\0'; i++)
{
if (!strncasecmp(str, prefixes[i], strlen(prefixes[i])))
{
prefix_found = 1;
str += strlen(prefixes[i]);
break;
}
}
if (prefix_found)
{
for(c=0;c<freecells_num;c++)
{
fcs_empty_freecell(ret, c);
}
for(c=0;c<freecells_num;c++)
{
if (c!=0)
{
while(
((*str) != ' ') &&
((*str) != '\t') &&
((*str) != '\n') &&
((*str) != '\r')
)
{
str++;
}
if ((*str == '\n') || (*str == '\r'))
{
break;
}
str++;
}
while ((*str == ' ') || (*str == '\t'))
{
str++;
}
if ((*str == '\r') || (*str == '\n'))
break;
if ((*str == '*') || (*str == '-'))
{
card = fcs_empty_card;
}
else
{
card = fcs_card_user2perl(str);
}
fcs_put_card_in_freecell(ret, c, card);
}
while ((*str != '\n') && (*str != '\0'))
{
str++;
}
s--;
continue;
}
prefixes = foundations_prefixes;
prefix_found = 0;
for(i=0;prefixes[i][0] != '\0'; i++)
{
if (!strncasecmp(str, prefixes[i], strlen(prefixes[i])))
{
prefix_found = 1;
str += strlen(prefixes[i]);
break;
}
}
if (prefix_found)
{
int d;
for(d=0;d<decks_num*4;d++)
{
fcs_set_foundation(ret, d, 0);
}
for(d=0;d<4;d++)
{
decks_index[d] = 0;
}
while (1)
{
while((*str == ' ') || (*str == '\t'))
str++;
if ((*str == '\n') || (*str == '\r'))
break;
d = fcs_u2p_suit(str);
str++;
while (*str == '-')
str++;
c = fcs_u2p_card_number(str);
while (
(*str != ' ') &&
(*str != '\t') &&
(*str != '\n') &&
(*str != '\r')
)
{
str++;
}
fcs_set_foundation(ret, (decks_index[d]*4+d), c);
decks_index[d]++;
if (decks_index[d] >= decks_num)
{
decks_index[d] = 0;
}
}
s--;
continue;
}
#ifdef FCS_WITH_TALONS
prefixes = talon_prefixes;
prefix_found = 0;
for(i=0;prefixes[i][0] != '\0'; i++)
{
if (!strncasecmp(str, prefixes[i], strlen(prefixes[i])))
{
prefix_found = 1;
str += strlen(prefixes[i]);
break;
}
}
if (prefix_found)
{
/* Input the Talon */
int talon_size;
talon_size = MAX_NUM_DECKS*52+16;
ret.talon = malloc(sizeof(fcs_card_t)*talon_size);
fcs_talon_pos(ret) = 0;
for(c=0 ; c < talon_size ; c++)
{
/* Move to the next card */
if (c!=0)
{
while(
((*str) != ' ') &&
((*str) != '\t') &&
((*str) != '\n') &&
((*str) != '\r')
)
{
str++;
}
if ((*str == '\n') || (*str == '\r'))
{
break;
}
}
while ((*str == ' ') || (*str == '\t'))
{
str++;
}
if ((*str == '\n') || (*str == '\r'))
{
break;
}
card = fcs_card_user2perl(str);
fcs_put_card_in_talon(ret, c+(talon_type==FCS_TALON_KLONDIKE), card);
}
fcs_talon_len(ret) = c;
if (talon_type == FCS_TALON_KLONDIKE)
{
int talon_len;
talon_len = fcs_talon_len(ret);
fcs_klondike_talon_len(ret) = talon_len;
fcs_klondike_talon_stack_pos(ret) = -1;
fcs_klondike_talon_queue_pos(ret) = 0;
}
s--;
continue;
}
prefixes = num_redeals_prefixes;
prefix_found = 0;
for(i=0;prefixes[i][0] != '\0'; i++)
{
if (!strncasecmp(str, prefixes[i], strlen(prefixes[i])))
{
prefix_found = 1;
str += strlen(prefixes[i]);
break;
}
}
if (prefix_found)
{
while ((*str < '0') && (*str > '9') && (*str != '\n'))
{
str++;
}
if (*str != '\n')
{
int num_redeals;
num_redeals = atoi(str);
if (talon_type == FCS_TALON_KLONDIKE)
{
fcs_klondike_talon_num_redeals_left(ret) =
(num_redeals < 0) ?
(-1) :
((num_redeals > 127) ? 127 : num_redeals)
;
}
}
s--;
continue;
}
#endif
for(c=0 ; c < MAX_NUM_CARDS_IN_A_STACK ; c++)
{
/* Move to the next card */
if (c!=0)
{
while(
((*str) != ' ') &&
((*str) != '\t') &&
((*str) != '\n') &&
((*str) != '\r')
)
{
str++;
}
if ((*str == '\n') || (*str == '\r'))
{
break;
}
}
while ((*str == ' ') || (*str == '\t'))
{
str++;
}
if ((*str == '\n') || (*str == '\r'))
{
break;
}
card = fcs_card_user2perl(str);
fcs_push_card_into_stack(ret, s, card);
}
}
return ret_with_locations;
}
#undef ret
int freecell_solver_check_state_validity(
fcs_state_with_locations_t * state_with_locations,
int freecells_num,
int stacks_num,
int decks_num,
#ifdef FCS_WITH_TALONS
int talon_type,
#endif
fcs_card_t * misplaced_card)
{
int cards[4][14];
int c, s, d, f;
fcs_state_t * state;
state = (&(state_with_locations->s));
/* Initialize all cards to 0 */
for(d=0;d<4;d++)
{
for(c=1;c<=13;c++)
{
cards[d][c] = 0;
}
}
/* Mark the cards in the decks */
for(d=0;d<decks_num*4;d++)
{
for(c=1;c<=fcs_foundation_value(*state, d);c++)
{
cards[d%4][c]++;
}
}
/* Mark the cards in the freecells */
for(f=0;f<freecells_num;f++)
{
if (fcs_freecell_card_num(*state, f) != 0)
{
cards
[fcs_freecell_card_suit(*state, f)]
[fcs_freecell_card_num(*state, f)] ++;
}
}
/* Mark the cards in the stacks */
for(s=0;s<stacks_num;s++)
{
for(c=0;c<fcs_stack_len(*state,s);c++)
{
if (fcs_stack_card_num(*state, s, c) == 0)
{
*misplaced_card = fcs_empty_card;
return 3;
}
cards
[fcs_stack_card_suit(*state, s, c)]
[fcs_stack_card_num(*state, s, c)] ++;
}
}
#ifdef FCS_WITH_TALONS
/* Mark the cards in the (gypsy) talon */
if ((talon_type == FCS_TALON_GYPSY) || (talon_type == FCS_TALON_KLONDIKE))
{
for(c = ((talon_type == FCS_TALON_GYPSY)?fcs_talon_pos(*state):1) ;
c < ((talon_type==FCS_TALON_GYPSY) ? fcs_talon_len(*state) : (fcs_klondike_talon_len(*state)+1)) ;
c++)
{
if (fcs_get_talon_card(*state,c) != fcs_empty_card)
{
cards
[fcs_card_suit(fcs_get_talon_card(*state, c))]
[fcs_card_card_num(fcs_get_talon_card(*state, c))] ++;
}
}
}
#endif
/* Now check if there are extra or missing cards */
for(d=0;d<4;d++)
{
for(c=1;c<=13;c++)
{
if (cards[d][c] != decks_num)
{
*misplaced_card = fcs_empty_card;
fcs_card_set_suit(*misplaced_card, d);
fcs_card_set_num(*misplaced_card, c);
return (cards[d][c] < decks_num) ? 1 : 2;
}
}
}
return 0;
}
#undef state
char * freecell_solver_state_as_string(
fcs_state_with_locations_t * state_with_locations,
int freecells_num,
int stacks_num,
int decks_num,
int parseable_output,
int canonized_order_output,
int display_10_as_t
)
{
fcs_state_t * state;
char freecell[10], decks[MAX_NUM_DECKS*4][10], stack_card_[10];
int a, card_num_is_null, b;
int max_num_cards, s, card_num, len;
//char string[8000];
char str2[128], str3[128], * str2_ptr, * str3_ptr;
freecell_solver_append_string_t * app_str;
//char * str;
int stack_locs[MAX_NUM_STACKS];
int freecell_locs[MAX_NUM_FREECELLS];
state = (&(state_with_locations->s));
if (canonized_order_output)
{
for(a=0;a<stacks_num;a++)
{
stack_locs[a] = a;
}
for(a=0;a<freecells_num;a++)
{
freecell_locs[a] = a;
}
}
else
{
for(a=0;a<stacks_num;a++)
{
stack_locs[(int)(state_with_locations->stack_locs[a])] = a;
}
for(a=0;a<freecells_num;a++)
{
freecell_locs[(int)(state_with_locations->fc_locs[a])] = a;
}
}
for(a=0;a<decks_num*4;a++)
{
fcs_p2u_card_number(
fcs_foundation_value(*state, a),
decks[a],
&card_num_is_null,
display_10_as_t,
0
);
if (decks[a][0] == ' ')
decks[a][0] = '0';
}
//str = string;
app_str = freecell_solver_append_string_alloc(512);
if(!parseable_output)
{
for(a=0;a<((freecells_num/4)+((freecells_num%4==0)?0:1));a++)
{
str2_ptr = str2;
str3_ptr = str3;
for(b=0;b<min(freecells_num-a*4, 4);b++)
{
str2_ptr += sprintf(str2_ptr, "%3s ",
fcs_card_perl2user(
fcs_freecell_card(
*state,
freecell_locs[a*4+b]
),
freecell,
display_10_as_t
)
);
str3_ptr += sprintf(str3_ptr, "--- ");
}
if (a < decks_num)
{
freecell_solver_append_string_sprintf(
app_str,
"%-16s H-%1s C-%1s D-%1s S-%1s\n",
str2,
decks[a*4],
decks[a*4+1],
decks[a*4+2],
decks[a*4+3]
);
}
else
{
freecell_solver_append_string_sprintf(
app_str,
"%s\n", str2
);
}
freecell_solver_append_string_sprintf(
app_str,
"%s\n", str3
);
}
for(;a<decks_num;a++)
{
freecell_solver_append_string_sprintf(
app_str,
"%-16s H-%1s C-%1s D-%1s S-%1s\n",
"",
decks[a*4],
decks[a*4+1],
decks[a*4+2],
decks[a*4+3]
);
}
freecell_solver_append_string_sprintf(
app_str,
"%s",
"\n\n"
);
for(s=0;s<stacks_num;s++)
{
freecell_solver_append_string_sprintf(app_str, "%s", " -- ");
}
freecell_solver_append_string_sprintf(
app_str,
"%s",
"\n"
);
max_num_cards = 0;
for(s=0;s<stacks_num;s++)
{
if (fcs_stack_len(*state, stack_locs[s]) > max_num_cards)
{
max_num_cards = fcs_stack_len(*state, stack_locs[s]);
}
}
for(card_num=0;card_num<max_num_cards;card_num++)
{
for(s = 0; s<stacks_num; s++)
{
if (card_num >= fcs_stack_len(*state, stack_locs[s]))
{
freecell_solver_append_string_sprintf(
app_str,
" "
);
}
else
{
freecell_solver_append_string_sprintf(
app_str,
"%3s ",
fcs_card_perl2user(
fcs_stack_card(
*state,
stack_locs[s],
card_num),
stack_card_,
display_10_as_t
)
);
}
}
freecell_solver_append_string_sprintf(app_str, "%s", "\n");
}
}
else
{
freecell_solver_append_string_sprintf(app_str, "%s", "Foundations: ");
for(a=0;a<decks_num;a++)
{
freecell_solver_append_string_sprintf(
app_str,
"H-%s C-%s D-%s S-%s ",
decks[a*4],
decks[a*4+1],
decks[a*4+2],
decks[a*4+3]
);
}
freecell_solver_append_string_sprintf(app_str, "%s", "\nFreecells: ");
for(a=0;a<freecells_num;a++)
{
freecell_solver_append_string_sprintf(
app_str,
"%3s",
fcs_card_perl2user(
fcs_freecell_card(
*state,
freecell_locs[a]
),
freecell,
display_10_as_t
)
);
if (a < freecells_num-1)
{
freecell_solver_append_string_sprintf(app_str, "%s", " ");
}
}
freecell_solver_append_string_sprintf(app_str, "%s", "\n");
for(s=0;s<stacks_num;s++)
{
freecell_solver_append_string_sprintf(app_str, "%s", ": ");
len = fcs_stack_len(*state, stack_locs[s]);
for(card_num=0;card_num<len;card_num++)
{
fcs_card_perl2user(
fcs_stack_card(
*state,
stack_locs[s],
card_num
),
stack_card_,
display_10_as_t
);
freecell_solver_append_string_sprintf(app_str, "%s", stack_card_);
if (card_num < len-1)
{
freecell_solver_append_string_sprintf(app_str, "%s", " ");
}
}
freecell_solver_append_string_sprintf(app_str, "%s", "\n");
}
}
return freecell_solver_append_string_finalize(app_str);
}
|