Source

fc-solve / fc-solve / source / freecell.c

   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
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
/* Copyright (c) 2000 Shlomi Fish
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
/*
 * freecell.c - The various movement tests performed by Freecell Solver
 *
 */

#define BUILDING_DLL 1

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <assert.h>


#include "config.h"

#include "state.h"
#include "card.h"
#include "instance.h"
#include "scans.h"

#include "meta_move_funcs_helpers.h"
#include "unused.h"
#include "freecell.h"

#include "inline.h"
#include "likely.h"

#ifndef min
#define min(a,b) (((a)<(b))?(a):(b))
#endif

/*
 * Throughout this code the following local variables are used to quickly
 * access the instance's members:
 *
 * LOCAL_STACKS_NUM - the number of stacks in the state
 * LOCAL_FREECELLS_NUM - the number of freecells in the state
 * sequences_are_built_by - the type of sequences of this board.
 * */

/*
 * This function tries to move stack cards that are present at the
 * top of stacks to the foundations.
 * */
DECLARE_MOVE_FUNCTION(fc_solve_sfs_move_top_stack_cards_to_founds)
{
    tests_define_accessors();

#ifndef HARD_CODED_NUM_STACKS
    SET_GAME_PARAMS();
#endif

    for (int stack_idx=0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
    {
        fcs_cards_column_t col = fcs_state_get_col(state, stack_idx);
        const int cards_num = fcs_col_len(col);
        if (cards_num)
        {
            /* Get the top card in the stack */
            const fcs_card_t card = fcs_col_get_card(col, cards_num-1);
            for (int deck=0;deck < INSTANCE_DECKS_NUM;deck++)
            {
                if (fcs_foundation_value(state, deck*4+fcs_card_suit(card)) == fcs_card_rank(card) - 1)
                {
                    /* We can put it there */

                    sfs_check_state_begin();

                    my_copy_stack(stack_idx);
                    {
                        fcs_cards_column_t new_temp_col;
                        new_temp_col = fcs_state_get_col(new_state, stack_idx);
                        fcs_col_pop_top(new_temp_col);
                    }

                    fcs_increment_foundation(new_state, deck*4+fcs_card_suit(card));

                    fcs_internal_move_t temp_move;

                    fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_FOUNDATION);
                    fcs_int_move_set_src_stack(temp_move,stack_idx);
                    fcs_int_move_set_foundation(temp_move,deck*4+fcs_card_suit(card));

                    fcs_move_stack_push(moves, temp_move);

                    fcs_flip_top_card(stack_idx);

                    sfs_check_state_end()
                    break;
                }
            }
        }
    }

    return;
}

static GCC_INLINE void sort_derived_states(
        fcs_derived_states_list_t * derived_states_list,
        int initial_derived_states_num_states
        )
{
    fcs_derived_states_list_item_t * start =
        derived_states_list->states + initial_derived_states_num_states;
    fcs_derived_states_list_item_t * limit =
        derived_states_list->states + derived_states_list->num_states;

    for (fcs_derived_states_list_item_t * b = start+1 ; b < limit ; b++)
    {
        for (fcs_derived_states_list_item_t * c = b ; (c > start) && (c[0].context.i < c[-1].context.i); c--)
        {
            fcs_derived_states_list_item_t temp = c[-1];
            c[-1] = c[0];
            c[0] = temp;
        }
    }
}

/*
 * This test moves single cards that are present in the freecells to
 * the foundations.
 * */
DECLARE_MOVE_FUNCTION(fc_solve_sfs_move_freecell_cards_to_founds)
{
    tests_define_accessors_no_stacks();

#ifndef HARD_CODED_NUM_FREECELLS
    SET_GAME_PARAMS();
#endif

    /* Now check the same for the free cells */
    for (int fc=0 ; fc < LOCAL_FREECELLS_NUM ; fc++)
    {
        const fcs_card_t card = fcs_freecell_card(state, fc);

        if (fcs_card_is_valid(card))
        {
            for (int deck=0 ; deck < INSTANCE_DECKS_NUM ; deck++)
            {
                if (fcs_foundation_value(state, deck*4+fcs_card_suit(card)) == fcs_card_rank(card) - 1)
                {
                    /* We can put it there */
                    sfs_check_state_begin()

                    fcs_empty_freecell(new_state, fc);

                    fcs_increment_foundation(new_state, deck*4+fcs_card_suit(card));

                    fcs_internal_move_t temp_move;

                    fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_FREECELL_TO_FOUNDATION);
                    fcs_int_move_set_src_freecell(temp_move,fc);
                    fcs_int_move_set_foundation(temp_move,deck*4+fcs_card_suit(card));

                    fcs_move_stack_push(moves, temp_move);

                    sfs_check_state_end();
                }
            }
        }
    }

    return;
}

/*
 * This function empties two stacks from the new state
 * into freeeclls and empty columns
 */
static GCC_INLINE int empty_two_cols_from_new_state(
    fc_solve_soft_thread_t * soft_thread,
    fcs_kv_state_t * kv_ptr_new_state,
    fcs_move_stack_t * moves,
    const int cols_indexes[3],
    int nc1, int nc2
)
{
#define key_ptr_new_state_key (kv_ptr_new_state->key)
#define my_new_out_state_key (*key_ptr_new_state_key)
#define temp_new_state_key (*key_ptr_new_state_key)
    int ret = -1;

    int num_cards_to_move_from_columns[3];
    num_cards_to_move_from_columns[0] = nc1;
    num_cards_to_move_from_columns[1] = nc2;
    num_cards_to_move_from_columns[2] = -1;

    const int * col_idx = cols_indexes;
    int * col_num_cards = num_cards_to_move_from_columns;

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)))
    SET_INSTANCE_GAME_PARAMS(soft_thread->hard_thread->instance);
#endif

#ifdef INDIRECT_STACK_STATES
    char * indirect_stacks_buffer = soft_thread->hard_thread->indirect_stacks_buffer;
#endif


    {
        int dest_fc_idx;

        dest_fc_idx = 0;

        while (1)
        {
            while ((*col_num_cards) == 0)
            {
                col_num_cards++;
                if (*(++col_idx) == -1)
                {
                    return ret;
                }
            }

            /* Find a vacant freecell */
            for( ; dest_fc_idx < LOCAL_FREECELLS_NUM ; dest_fc_idx++)
            {
                if (fcs_freecell_rank(
                        temp_new_state_key, dest_fc_idx
                    ) == 0)
                {
                    break;
                }
            }
            if (dest_fc_idx == LOCAL_FREECELLS_NUM)
            {
                /*  Move on to the stacks. */
                break;
            }

            fcs_cards_column_t new_from_which_col = fcs_state_get_col(temp_new_state_key, *col_idx);

            fcs_card_t top_card;
            fcs_col_pop_card(new_from_which_col, top_card);

            fcs_put_card_in_freecell(
                temp_new_state_key,
                dest_fc_idx,
                top_card
            );

            fcs_internal_move_t temp_move;
            fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_FREECELL);
            fcs_int_move_set_src_stack(temp_move,*col_idx);
            fcs_int_move_set_dest_freecell(temp_move,dest_fc_idx);
            fcs_move_stack_push(moves, temp_move);

            ret = dest_fc_idx;

            fcs_flip_top_card(*col_idx);

            (*col_num_cards)--;
            dest_fc_idx++;
        }
    }

    while ((*col_num_cards) == 0)
    {
        col_num_cards++;
        if (*(++col_idx) == -1)
        {
            return ret;
        }
    }

    {
        int put_cards_in_col_idx;

        put_cards_in_col_idx = 0;
        /* Fill the free stacks with the cards below them */
        while (1)
        {
            fcs_cards_column_t new_b_col;

            while ((*col_num_cards) == 0)
            {
                col_num_cards++;
                if (*(++col_idx) == -1)
                {
                    return ret;
                }
            }

            /*  Find a vacant stack */
            for( ; put_cards_in_col_idx < LOCAL_STACKS_NUM ; put_cards_in_col_idx++)
            {
                if (fcs_col_len(
                    fcs_state_get_col(temp_new_state_key, put_cards_in_col_idx)
                    ) == 0)
                {
                    break;
                }
            }

            assert(put_cards_in_col_idx < LOCAL_STACKS_NUM );

            fcs_copy_stack(temp_new_state_key, *(kv_ptr_new_state->val), put_cards_in_col_idx, indirect_stacks_buffer);

            new_b_col = fcs_state_get_col(temp_new_state_key, put_cards_in_col_idx);

            fcs_cards_column_t new_from_which_col = fcs_state_get_col(temp_new_state_key, *col_idx);

            fcs_card_t top_card;
            fcs_col_pop_card(new_from_which_col, top_card);
            fcs_col_push_card(new_b_col, top_card);

            fcs_internal_move_t temp_move;
            fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_STACK);
            fcs_int_move_set_src_stack(temp_move,*col_idx);
            fcs_int_move_set_dest_stack(temp_move,put_cards_in_col_idx);
            fcs_int_move_set_num_cards_in_seq(temp_move,1);
            fcs_move_stack_push(moves, temp_move);

            ret = (put_cards_in_col_idx | (1 << 8));

            fcs_flip_top_card(*col_idx);

            (*col_num_cards)--;
            put_cards_in_col_idx++;
        }
    }
#undef key_ptr_new_state_key
#undef my_new_out_state_key
#define temp_new_state_key (*key_ptr_new_state_key)
}

#define CALC_POSITIONS_BY_RANK() \
    char * positions_by_rank = \
        fc_solve_get_the_positions_by_rank_data( \
            soft_thread, \
            raw_ptr_state_raw \
        )

DECLARE_MOVE_FUNCTION(fc_solve_sfs_move_freecell_cards_on_top_of_stacks)
{
    tests_define_accessors();
    tests_define_seqs_built_by();
    tests_define_empty_stacks_fill();

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)) || (!defined(HARD_CODED_NUM_DECKS)))
    SET_GAME_PARAMS();
#endif

    SET_VACANT_PTR();
    SET_VACANT_FREECELLS_AND_STACKS();

    int initial_derived_states_num_states = derived_states_list->num_states;

    CALC_POSITIONS_BY_RANK();

    /* Let's try to put cards in the freecells on top of stacks */

    /* Scan the freecells */
    for (int fc=0 ; fc < LOCAL_FREECELLS_NUM ; fc++)
    {
        fcs_card_t src_card = fcs_freecell_card(state, fc);

        /* If the freecell is not empty and dest_card is its parent
         * */
        if (
                /* The Cell should not be empty. */
                fcs_card_is_valid(src_card)
                &&
                /* We cannot put a king anywhere. */
                (fcs_card_rank(src_card) != 13)
            )
        {

#define FCS_POS_BY_RANK_MAP(x) ((x) << 1)

#ifdef FCS_FREECELL_ONLY

#define FCS_PROTO_CARD_SUIT_POSITIONS_BY_RANK_INITIAL_OFFSET(card) ((fcs_card_suit(card)^0x1)&(0x2-1))

#define FCS_PROTO_CARD_SUIT_POSITIONS_BY_RANK_STEP() (2)

#else

#define FCS_PROTO_CARD_SUIT_POSITIONS_BY_RANK_INITIAL_OFFSET(card) \
            ((sequences_are_built_by == FCS_SEQ_BUILT_BY_RANK) ? 0 \
                : (sequences_are_built_by == FCS_SEQ_BUILT_BY_SUIT) ? \
                fcs_card_suit(card) : ((fcs_card_suit(card)^0x1)&(0x2-1)) \
            )

#define FCS_PROTO_CARD_SUIT_POSITIONS_BY_RANK_STEP() \
            ((sequences_are_built_by == FCS_SEQ_BUILT_BY_RANK) ? 1 \
                : (sequences_are_built_by == FCS_SEQ_BUILT_BY_SUIT) ? \
                4 : 2 \
            )

#endif

#define FCS_CARD_SUIT_POSITIONS_BY_RANK_INITIAL_OFFSET(card) FCS_POS_BY_RANK_MAP(FCS_PROTO_CARD_SUIT_POSITIONS_BY_RANK_INITIAL_OFFSET(card))
#define FCS_CARD_SUIT_POSITIONS_BY_RANK_STEP() FCS_POS_BY_RANK_MAP(FCS_PROTO_CARD_SUIT_POSITIONS_BY_RANK_STEP())

#define FCS_POS_IDX_TO_CHECK_START_LOOP(src_card) \
            char * pos_idx_to_check = &positions_by_rank[ \
                (FCS_POS_BY_RANK_WIDTH * (fcs_card_rank(src_card))) \
            ]; \
            char * last_pos_idx; \
                 \
            for (last_pos_idx = pos_idx_to_check + FCS_POS_BY_RANK_WIDTH, \
                 pos_idx_to_check += FCS_CARD_SUIT_POSITIONS_BY_RANK_INITIAL_OFFSET(src_card) \
                ; \
                pos_idx_to_check < last_pos_idx \
                ; \
                pos_idx_to_check += FCS_CARD_SUIT_POSITIONS_BY_RANK_STEP() \
               )

            FCS_POS_IDX_TO_CHECK_START_LOOP(src_card)
            {
                int ds;
                if ((ds = pos_idx_to_check[0]) == -1)
                {
                    continue;
                }
                int dc = pos_idx_to_check[1];

                fcs_cards_column_t dest_col = fcs_state_get_col(state, ds);
                fcs_card_t dest_card = fcs_col_get_card(dest_col, dc);

                int dest_cards_num = fcs_col_len(dest_col);
                /* Let's check if we can put it there */

                /* Check if the destination card is already below a
                 * suitable card */
                fcs_bool_t is_seq_in_dest = FALSE;
                if (dest_cards_num - 1 > dc)
                {
                    fcs_card_t dest_below_card =
                        fcs_col_get_card(dest_col, dc+1);
                    if (fcs_is_parent_card(dest_below_card, dest_card))
                    {
                        is_seq_in_dest = TRUE;
                    }
                }

                if (! is_seq_in_dest)
                {
                    int num_cards_to_relocate = dest_cards_num - dc - 1;

                    int freecells_to_fill = min(num_cards_to_relocate, num_vacant_freecells);

                    num_cards_to_relocate -= freecells_to_fill;

                    int freestacks_to_fill;
                    if (tests__is_filled_by_any_card())
                    {
                        freestacks_to_fill = min(num_cards_to_relocate, num_vacant_stacks);

                        num_cards_to_relocate -= freestacks_to_fill;
                    }
                    else
                    {
                        freestacks_to_fill = 0;
                    }

                    if (num_cards_to_relocate == 0)
                    {
                        int cols_indexes[3];
                        /* We can move it */
                        fcs_cards_column_t new_dest_col;

                        sfs_check_state_begin()

                        /* Fill the freecells with the top cards */

                        my_copy_stack(ds);

                        cols_indexes[0] = ds;
                        cols_indexes[1] = -1;
                        cols_indexes[2] = -1;

                        empty_two_cols_from_new_state(
                                soft_thread,
                                NEW_STATE_BY_REF(),
                                moves,
                                cols_indexes,
                                dest_cards_num - dc - 1,
                                0
                        );

                        new_dest_col = fcs_state_get_col(new_state, ds);

                        /* Now put the freecell card on top of the stack */
                        fcs_col_push_card(new_dest_col, src_card);
                        fcs_empty_freecell(new_state, fc);

                        fcs_internal_move_t temp_move;
                        fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_FREECELL_TO_STACK);
                        fcs_int_move_set_src_freecell(temp_move,fc);
                        fcs_int_move_set_dest_stack(temp_move,ds);
                        fcs_move_stack_push(moves, temp_move);

                        /*
                         * This is to preserve the order that the
                         * initial (non-optimized) version of the
                         * function used - for backwards-compatibility
                         * and consistency.
                         * */
                        state_context_value =
                            ((ds << 16) | ((255-dc) << 8) | fc)
                            ;

                        sfs_check_state_end()
                    }
                }
            }
        }
    }

    sort_derived_states(derived_states_list, initial_derived_states_num_states);

    return;
}

DECLARE_MOVE_FUNCTION(fc_solve_sfs_move_non_top_stack_cards_to_founds)
{
    tests_define_accessors();
    tests_define_empty_stacks_fill();

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)))
    SET_GAME_PARAMS();
#endif
    SET_VACANT_PTR();
    SET_VACANT_FREECELLS_AND_STACKS();

    /* Now let's check if a card that is under some other cards can be placed
     * in the foundations. */

    for (int stack_idx=0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
    {
        fcs_cards_column_t col = fcs_state_get_col(state, stack_idx);
        const int cards_num = fcs_col_len(col);
        /*
         * We starts from cards_num-2 because the top card is already covered
         * by move_top_stack_cards_to_founds.
         * */
        for (int c = cards_num-2 ; c >= 0 ; c--)
        {
            const fcs_card_t card = fcs_col_get_card(col, c);

            for (int deck = 0 ; deck < INSTANCE_DECKS_NUM ; deck++)
            {
                if (fcs_foundation_value(state, deck*4+fcs_card_suit(card)) == fcs_card_rank(card)-1)
                {
                    /* The card is foundation-able. Now let's check if we
                     * can move the cards above it to the freecells and
                     * stacks */

                    if ((num_vacant_freecells +
                        ((tests__is_filled_by_any_card()) ?
                            num_vacant_stacks :
                            0
                        ))
                            >= cards_num-(c+1))
                    {
                        int cols_indexes[3];
                        fcs_cards_column_t new_src_col;
                        /* We can move it */

                        sfs_check_state_begin()

                        my_copy_stack(stack_idx);

                        cols_indexes[0] = stack_idx;
                        cols_indexes[1] = -1;
                        cols_indexes[2] = -1;

                        empty_two_cols_from_new_state(
                            soft_thread,
                            NEW_STATE_BY_REF(),
                            moves,
                            cols_indexes,
                            cards_num-(c+1),
                            0
                        );

                        new_src_col = fcs_state_get_col(new_state, stack_idx);

                        fcs_card_t top_card;
                        fcs_col_pop_card(new_src_col, top_card);
                        fcs_increment_foundation(new_state, deck*4+fcs_card_suit(top_card));

                        fcs_internal_move_t temp_move;
                        fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_FOUNDATION);
                        fcs_int_move_set_src_stack(temp_move,stack_idx);
                        fcs_int_move_set_foundation(temp_move,deck*4+fcs_card_suit(top_card));

                        fcs_move_stack_push(moves, temp_move);

                        fcs_flip_top_card(stack_idx);

                        sfs_check_state_end()
                    }
                    break;
                }
            }
        }
    }

    return;
}


DECLARE_MOVE_FUNCTION(fc_solve_sfs_move_stack_cards_to_a_parent_on_the_same_stack)
{
    tests_define_accessors();
    tests_define_seqs_built_by();
    tests_define_empty_stacks_fill();

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)))
    SET_GAME_PARAMS();
#endif

    SET_VACANT_PTR();
    SET_VACANT_FREECELLS_AND_STACKS();

    /*
     * Now let's try to move a stack card to a parent card which is found
     * on the same stack.
     * */
    for (int stack_idx = 0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
    {
        fcs_cards_column_t col = fcs_state_get_col(state, stack_idx);
        const int cards_num = fcs_col_len(col);

        for (int c=0 ; c<cards_num ; c++)
        {
            /* Find a card which this card can be put on; */

            const fcs_card_t card = fcs_col_get_card(col, c);

            /* Do not move cards that are already found above a suitable
             * parent */
            /* TODO : is this code safe for variants that are not Freecell? */
            fcs_bool_t should_perform_move = TRUE;
            if (c != 0)
            {
                fcs_card_t prev_card = fcs_col_get_card(col, c-1);
                if ((fcs_card_rank(prev_card) == fcs_card_rank(card)+1) &&
                    ((fcs_card_suit(prev_card) & 0x1) != (fcs_card_suit(card) & 0x1)))
                {
                   should_perform_move = FALSE;
                }
            }
            if (should_perform_move)
            {
#define ds stack_idx
#define dest_col col
#define dest_cards_num cards_num
                /* Check if it can be moved to something on the same stack */
                for (int dc = 0 ; dc < c-1 ; dc++)
                {
                    fcs_card_t dest_card = fcs_col_get_card(dest_col, dc);
                    if (fcs_is_parent_card(card, dest_card))
                    {
                        /* Corresponding cards - see if it is feasible to move
                           the source to the destination. */

                        fcs_bool_t is_seq_in_dest = FALSE;
                        fcs_card_t dest_below_card = fcs_col_get_card(dest_col, dc+1);
                        if (fcs_is_parent_card(dest_below_card, dest_card))
                        {
                            is_seq_in_dest = TRUE;
                        }

                        if (!is_seq_in_dest)
                        {
                            int num_cards_to_relocate = dest_cards_num - dc - 1;

                            int freecells_to_fill = min(num_cards_to_relocate, num_vacant_freecells);

                            num_cards_to_relocate -= freecells_to_fill;

                            int freestacks_to_fill;
                            if (tests__is_filled_by_any_card())
                            {
                                freestacks_to_fill = min(num_cards_to_relocate, num_vacant_stacks);

                                num_cards_to_relocate -= freestacks_to_fill;
                            }
                            else
                            {
                                freestacks_to_fill = 0;
                            }

                            if (num_cards_to_relocate == 0)
                            {
                                /* We can move it */
                                sfs_check_state_begin()

                                my_copy_stack(ds);

                                fcs_cards_column_t new_dest_col = fcs_state_get_col(new_state, ds);

                                int cols_indexes[3];
                                cols_indexes[0] = ds;
                                cols_indexes[1] = -1;
                                cols_indexes[2] = -1;

                                int last_dest = empty_two_cols_from_new_state(
                                    soft_thread,
                                    NEW_STATE_BY_REF(),
                                    moves,
                                    cols_indexes,
                                    /* We're moving one extra card */
                                    cards_num - c,
                                    0
                                );

                                int source_index = last_dest & 0xFF;

                                empty_two_cols_from_new_state(
                                    soft_thread,
                                    NEW_STATE_BY_REF(),
                                    moves,
                                    cols_indexes,
                                    c - dc - 1,
                                    0
                                );

                                fcs_card_t moved_card;
                                if (!( (last_dest >> 8)&0x1))
                                {
                                    moved_card = fcs_freecell_card(new_state, source_index);
                                    fcs_empty_freecell(new_state, source_index);


                                    fcs_internal_move_t temp_move;
                                    fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_FREECELL_TO_STACK);
                                    fcs_int_move_set_src_freecell(temp_move,source_index);
                                    fcs_int_move_set_dest_stack(temp_move,ds);
                                    fcs_move_stack_push(moves, temp_move);
                                }
                                else
                                {
                                    fcs_cards_column_t new_source_col;

                                    new_source_col = fcs_state_get_col(new_state, source_index);

                                    fcs_col_pop_card(new_source_col, moved_card);

                                    fcs_internal_move_t temp_move;
                                    fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_STACK);
                                    fcs_int_move_set_src_stack(temp_move,source_index);
                                    fcs_int_move_set_dest_stack(temp_move,ds);
                                    fcs_int_move_set_num_cards_in_seq(temp_move,1);
                                    fcs_move_stack_push(moves, temp_move);
                                }

                                fcs_col_push_card(new_dest_col, moved_card);

                                sfs_check_state_end()
                            }
                        }
                    }
                }
            }
        }
    }

    return;
}
#undef ds
#undef dest_col
#undef dest_cards_num


DECLARE_MOVE_FUNCTION(fc_solve_sfs_move_stack_cards_to_different_stacks)
{
    tests_define_accessors();
    tests_define_seqs_built_by();
    tests_define_empty_stacks_fill();

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)) || (!defined(HARD_CODED_NUM_DECKS)))
    SET_GAME_PARAMS();
#endif

    SET_VACANT_PTR();
    SET_VACANT_FREECELLS_AND_STACKS();

    const int initial_derived_states_num_states =
        derived_states_list->num_states;

    CALC_POSITIONS_BY_RANK();

    /* Now let's try to move a card from one stack to the other     *
     * Note that it does not involve moving cards lower than king   *
     * to empty stacks                                              */

    for (int stack_idx = 0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
    {
        const fcs_cards_column_t col = fcs_state_get_col(state, stack_idx);

        int seq_end;
        for (int c=0 ; c<fcs_col_len(col) ; c=seq_end+1)
        {
            /* Check if there is a sequence here. */
            for(seq_end=c ; seq_end<fcs_col_len(col)-1 ; seq_end++)
            {
                const fcs_card_t this_card = fcs_col_get_card(col, seq_end+1);
                const fcs_card_t prev_card = fcs_col_get_card(col, seq_end);

                if (fcs_is_parent_card(this_card,prev_card))
                {
                }
                else
                {
                    break;
                }
            }

            if (tests__should_not_empty_columns())
            {
                if (c == 0)
                {
                    continue;
                }
            }

            /* Find a card which this card can be put on; */

            const fcs_card_t card = fcs_col_get_card(col, c);

#ifndef FCS_WITHOUT_CARD_FLIPPING
            /* Make sure the card is not flipped or else we can't move it */
            if (fcs_card_get_flipped(card))
            {
                continue;
            }
#endif

            /* Skip if it's a King - nothing to put it on. */
            if (unlikely(fcs_card_rank(card) == 13))
            {
                continue;
            }

            FCS_POS_IDX_TO_CHECK_START_LOOP(card)
            {
                const int ds = pos_idx_to_check[0];

                if ((ds >= 0) && (ds != stack_idx))
                {
                const int dc = pos_idx_to_check[1];
                const fcs_cards_column_t dest_col = fcs_state_get_col(state, ds);

                int num_cards_to_relocate = fcs_col_len(dest_col) - dc - 1 + fcs_col_len(col) - seq_end - 1;

                const int freecells_to_fill = min(num_cards_to_relocate, num_vacant_freecells);

                num_cards_to_relocate -= freecells_to_fill;

                int freestacks_to_fill;
                if (tests__is_filled_by_any_card())
                {
                    freestacks_to_fill = min(num_cards_to_relocate, num_vacant_stacks);

                    num_cards_to_relocate -= freestacks_to_fill;
                }
                else
                {
                    freestacks_to_fill = 0;
                }

                if (unlikely((num_cards_to_relocate == 0) &&
                   (calc_max_sequence_move(num_vacant_freecells-freecells_to_fill, num_vacant_stacks-freestacks_to_fill) >=
                    seq_end - c + 1)))
                {
                    sfs_check_state_begin()

                    my_copy_stack(stack_idx);
                    my_copy_stack(ds);

                    int cols_indexes[3];
                    cols_indexes[0] = ds;
                    cols_indexes[1] = stack_idx;
                    cols_indexes[2] = -1;

                    empty_two_cols_from_new_state(
                        soft_thread,
                        NEW_STATE_BY_REF(),
                        moves,
                        cols_indexes,
                        fcs_col_len(dest_col) - dc - 1,
                        fcs_col_len(col) - seq_end - 1
                    );

                    fcs_move_sequence(ds, stack_idx, c, seq_end);

                    fcs_flip_top_card(stack_idx);

                    /*
                     * This is to preserve the order that the
                     * initial (non-optimized) version of the
                     * function used - for backwards-compatibility
                     * and consistency.
                     * */
                    state_context_value = ((((((stack_idx << 8) | c) << 8) | ds) << 8) | dc);

                    sfs_check_state_end()
                }
                }
            }
        }
    }

    sort_derived_states(derived_states_list, initial_derived_states_num_states);

    return;
}

DECLARE_MOVE_FUNCTION(fc_solve_sfs_move_sequences_to_free_stacks)
{
    tests_define_accessors();
    tests_define_empty_stacks_fill();
    tests_define_seqs_built_by();

    if (tests__is_filled_by_none())
    {
        return;
    }

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)))
    SET_GAME_PARAMS();
#endif
    SET_VACANT_PTR();
    SET_VACANT_FREECELLS_AND_STACKS();

    const int max_sequence_len = calc_max_sequence_move(num_vacant_freecells, num_vacant_stacks-1);

    /* Now try to move sequences to empty stacks */

    if (num_vacant_stacks > 0)
    {
        int dest_stack_idx = -1;
        for (int stack_idx = 0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
        {
            fcs_cards_column_t col = fcs_state_get_col(state, stack_idx);
            const int cards_num = fcs_col_len(col);

            int seq_end;
            for (int c=0; c < cards_num; c = seq_end+1)
            {
                /* Check if there is a sequence here. */
                for(seq_end=c ; seq_end<cards_num-1; seq_end++)
                {
                    fcs_card_t this_card = fcs_col_get_card(col, seq_end+1);
                    fcs_card_t prev_card = fcs_col_get_card(col, seq_end);

                    if (! fcs_is_parent_card(this_card, prev_card))
                    {
                        break;
                    }
                }

                if ((fcs_col_get_rank(col, c) != 13) &&
                    (tests__is_filled_by_kings_only()))
                {
                    continue;
                }

                if (seq_end == cards_num -1)
                {
                    /* One stack is the destination stack, so we have one     *
                     * less stack in that case                                */
                    while ((max_sequence_len < cards_num -c) && (c > 0))
                    {
                        c--;
                    }

                    if (
                        (c > 0) &&
                        ((tests__is_filled_by_kings_only()) ?
                            (fcs_col_get_rank(col, c) == 13) :
                            1
                        )
                       )
                    {
                        sfs_check_state_begin();

                        if (dest_stack_idx < 0)
                        {
                            for (dest_stack_idx = 0 ;
                                 dest_stack_idx < LOCAL_STACKS_NUM;
                                 dest_stack_idx++)
                            {
                                if (fcs_col_len(
                                    fcs_state_get_col(new_state, dest_stack_idx)
                                    ) == 0)
                                {
                                    break;
                                }
                            }
                        }

                        my_copy_stack(dest_stack_idx);
                        my_copy_stack(stack_idx);

                        fcs_move_sequence(
                            dest_stack_idx,
                            stack_idx, c, cards_num-1
                        );

                        sfs_check_state_end()
                    }
                }
                else
                {
                    int num_cards_to_relocate = cards_num - seq_end - 1;

                    const int freecells_to_fill = min(num_cards_to_relocate, num_vacant_freecells);

                    num_cards_to_relocate -= freecells_to_fill;

                    int freestacks_to_fill;
                    if (tests__is_filled_by_any_card())
                    {
                        freestacks_to_fill = min(num_cards_to_relocate, num_vacant_stacks);

                        num_cards_to_relocate -= freestacks_to_fill;
                    }
                    else
                    {
                        freestacks_to_fill = 0;
                    }

                    if ((num_cards_to_relocate == 0) && (num_vacant_stacks-freestacks_to_fill > 0))
                    {
                        /* We can move it */
                        int seq_start = c;
                        while (
                            (calc_max_sequence_move(
                                num_vacant_freecells-freecells_to_fill,
                                num_vacant_stacks-freestacks_to_fill-1) < seq_end-seq_start+1)
                                &&
                            (seq_start <= seq_end)
                            )
                        {
                            seq_start++;
                        }
                        if ((seq_start <= seq_end) &&
                            ((tests__is_filled_by_kings_only()) ?
                                (fcs_col_get_rank(col, seq_start)
                                    == 13) :
                                1
                            )
                        )
                        {
                            int cols_indexes[3];
                            int empty_ret;

                            sfs_check_state_begin();

                            /* Fill the freecells with the top cards */

                            my_copy_stack(stack_idx);

                            cols_indexes[0] = stack_idx;
                            cols_indexes[1] = -1;
                            cols_indexes[2] = -1;

                            empty_ret = empty_two_cols_from_new_state(
                                soft_thread,
                                NEW_STATE_BY_REF(),
                                moves,
                                cols_indexes,
                                freecells_to_fill + freestacks_to_fill,
                                0
                            );

                            int b;
                            /* Find a vacant stack */
                            for (
                                    b=(
                                        ((empty_ret >> 8)&0x1)
                                        ? (empty_ret&0xFF) + 1
                                        : 0
                                      )
                                    ;
                                    b < LOCAL_STACKS_NUM
                                    ;
                                    b++
                                    )
                            {
                                if (fcs_col_len(
                                    fcs_state_get_col(new_state, b)
                                    ) == 0)
                                {
                                    break;
                                }
                            }

                            my_copy_stack(b);

                            fcs_move_sequence(b, stack_idx, seq_start, seq_end);

                            sfs_check_state_end();
                        }
                    }
                }
            }
        }
    }

    return;
}

/* Let's try to put cards that occupy freecells on an empty stack */
DECLARE_MOVE_FUNCTION(fc_solve_sfs_move_freecell_cards_to_empty_stack)
{
    tests_define_accessors();
    tests_define_empty_stacks_fill();

    if (tests__is_filled_by_none())
    {
        return;
    }

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)))
    SET_GAME_PARAMS();
#endif

    SET_VACANT_PTR();
    SET_VACANT_STACKS();

    if (num_vacant_stacks)
    {
        int stack_idx;
        for (stack_idx = 0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
        {
            if (fcs_col_len(
                fcs_state_get_col(state, stack_idx)
                ) == 0)
            {
                break;
            }
        }

        for (int fc = 0 ; fc < LOCAL_FREECELLS_NUM ; fc++)
        {
            const fcs_card_t card = fcs_freecell_card(state, fc);

            if (
                (tests__is_filled_by_kings_only())
                ? (fcs_card_rank(card) == 13)
                : fcs_card_is_valid(card)
               )
            {
                fcs_cards_column_t new_src_col;
                /* We can move it */

                sfs_check_state_begin();

                my_copy_stack(stack_idx);

                new_src_col = fcs_state_get_col(new_state, stack_idx);

                fcs_col_push_card(new_src_col, card);
                fcs_empty_freecell(new_state, fc);

                fcs_internal_move_t temp_move;
                fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_FREECELL_TO_STACK);
                fcs_int_move_set_src_freecell(temp_move,fc);
                fcs_int_move_set_dest_stack(temp_move,stack_idx);
                fcs_move_stack_push(moves, temp_move);

                sfs_check_state_end()
            }
        }
    }

    return;
}

DECLARE_MOVE_FUNCTION(fc_solve_sfs_move_cards_to_a_different_parent)
{
    tests_define_accessors();
    tests_define_seqs_built_by();
    tests_define_empty_stacks_fill();

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)) || (!defined(HARD_CODED_NUM_DECKS)))
    SET_GAME_PARAMS();
#endif
    SET_VACANT_PTR();
    SET_VACANT_FREECELLS_AND_STACKS();

    const int initial_derived_states_num_states = derived_states_list->num_states;

    CALC_POSITIONS_BY_RANK();

    /* This time try to move cards that are already on top of a parent to a different parent */

    for (int stack_idx = 0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
    {
        fcs_cards_column_t col = fcs_state_get_col(state, stack_idx);
        const int cards_num = fcs_col_len(col);

        /*
         * If there's only one card in the column, then it won't be above a
         * parent, so there's no sense in moving it.
         *
         * If there are no cards in the column, then there's nothing to do
         * here, and the algorithm will be confused
         * */
        if (cards_num < 2)
        {
            continue;
        }

        fcs_card_t upper_card = fcs_col_get_card(col, cards_num-1);

        /*
         * min_card_height is the minimal height of the card that is above
         * a true parent.
         *
         * It must be:
         *
         * 1. >= 1 - because the height 0 should not be moved.
         *
         * 2. <= cards_num-1 - because the height 0
         */
        int min_card_height;
        fcs_card_t lower_card;
        for (min_card_height = cards_num-2
            ; min_card_height >= 0
            ; upper_card = lower_card, min_card_height--
            )
        {
            lower_card = fcs_col_get_card(col, min_card_height);
            if (! fcs_is_parent_card(upper_card, lower_card))
            {
                break;
            }
        }

        min_card_height += 2;

        for(int c=min_card_height ; c < cards_num ; c++)
        {
            /* Find a card which this card can be put on; */

            const fcs_card_t card = fcs_col_get_card(col, c);

#ifndef FCS_WITHOUT_CARD_FLIPPING
            /*
             * Do not move cards that are flipped.
             * */
            if (fcs_card_get_flipped(card))
            {
                continue;
            }
#endif

            FCS_POS_IDX_TO_CHECK_START_LOOP(card)
            {
                const int ds = pos_idx_to_check[0];
                if ((ds == -1) || (ds == stack_idx))
                {
                    continue;
                }
                const int dc = pos_idx_to_check[1];

                fcs_cards_column_t dest_col = fcs_state_get_col(state, ds);
                int dest_cards_num = fcs_col_len(dest_col);
                fcs_card_t dest_card = fcs_col_get_card(dest_col, dc);

                /* Corresponding cards - see if it is feasible to move
                   the source to the destination. */

                /* Don't move if there's a sequence of cards in the
                 * destination.
                 * */
                if ((dc + 1 < dest_cards_num)
                        &&
                    fcs_is_parent_card(
                        fcs_col_get_card(dest_col, dc+1),
                        dest_card
                    )
                   )
                {
                    continue;
                }

                int num_cards_to_relocate = dest_cards_num - dc - 1;

                const int freecells_to_fill = min(num_cards_to_relocate, num_vacant_freecells);

                num_cards_to_relocate -= freecells_to_fill;

                int freestacks_to_fill;
                if (tests__is_filled_by_any_card())
                {
                    freestacks_to_fill = min(num_cards_to_relocate, num_vacant_stacks);

                    num_cards_to_relocate -= freestacks_to_fill;
                }
                else
                {
                    freestacks_to_fill = 0;
                }

                if (!(
                    (num_cards_to_relocate == 0)
                    && (calc_max_sequence_move(
                            num_vacant_freecells - freecells_to_fill,
                            num_vacant_stacks - freestacks_to_fill
                            )
                            >=
                        cards_num - c
                       )
                    ))
                {
                    continue;
                }

                /* We can move it */

                {
                    int cols_indexes[3];

                    sfs_check_state_begin()

                    /* Fill the freecells with the top cards */

                    my_copy_stack(ds);

                    cols_indexes[0] = ds;
                    cols_indexes[1] = -1;
                    cols_indexes[2] = -1;

                    empty_two_cols_from_new_state(
                        soft_thread,
                        NEW_STATE_BY_REF(),
                        moves,
                        cols_indexes,
                        freestacks_to_fill + freecells_to_fill,
                        0
                    );

                    my_copy_stack(stack_idx);

                    fcs_move_sequence(ds, stack_idx, c, cards_num-1);

                    state_context_value =
                        ((((((stack_idx << 8) | c) << 8) | ds) << 8) | dc);

                    sfs_check_state_end()
                }
            }
        }
    }

    sort_derived_states(derived_states_list, initial_derived_states_num_states);

    return;
}

DECLARE_MOVE_FUNCTION(fc_solve_sfs_empty_stack_into_freecells)
{
    tests_define_accessors();
    tests_define_empty_stacks_fill();

    if (tests__is_filled_by_none())
    {
        return;
    }

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)))
    SET_GAME_PARAMS();
#endif

    SET_VACANT_PTR();
    SET_VACANT_FREECELLS_AND_STACKS();

    /* Now, let's try to empty an entire stack into the freecells, so other cards can
     * inhabit it */

    if (num_vacant_stacks == 0)
    {
        for (int stack_idx = 0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
        {
            fcs_cards_column_t col = fcs_state_get_col(state, stack_idx);
            const int cards_num = fcs_col_len(col);

            if (cards_num <= num_vacant_freecells)
            {
                /* We can empty it */
                sfs_check_state_begin()

                my_copy_stack(stack_idx);

                fcs_cards_column_t new_src_col = fcs_state_get_col(new_state, stack_idx);

                int b = 0;
                for (int c = 0 ; c < cards_num ; c++, b++)
                {
                    /* Find a vacant freecell */
                    for ( ; b < LOCAL_FREECELLS_NUM ; b++)
                    {
                        if (fcs_freecell_is_empty(new_state, b))
                        {
                            break;
                        }
                    }
                    fcs_card_t top_card;
                    fcs_col_pop_card(new_src_col, top_card);

                    fcs_put_card_in_freecell(new_state, b, top_card);

                    fcs_internal_move_t temp_move;
                    fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_FREECELL);
                    fcs_int_move_set_src_stack(temp_move,stack_idx);
                    fcs_int_move_set_dest_freecell(temp_move,b);
                    fcs_move_stack_push(moves, temp_move);
                }

                sfs_check_state_end()
            }
        }
    }

    return;

}

DECLARE_MOVE_FUNCTION(fc_solve_sfs_yukon_do_nothing)
{
    return;
}

/* Disabling Yukon solving for the time being. */
#if 0
DECLARE_MOVE_FUNCTION(fc_solve_sfs_yukon_move_card_to_parent)
{
    int stack_idx, cards_num, c, ds;
    int dest_cards_num;
    fcs_card_t card;
    fcs_card_t dest_card;
    fcs_cards_column_t dest_col, col;

#ifndef HARD_CODED_NUM_STACKS
    int stacks_num;
#endif

    fcs_internal_move_t temp_move;

    tests_define_accessors();

    temp_move = fc_solve_empty_move;

#ifndef HARD_CODED_NUM_STACKS
    stacks_num = INSTANCE_STACKS_NUM;
#endif

    for( ds=0 ; ds < LOCAL_STACKS_NUM ; ds++ )
    {
        dest_col = fcs_state_get_col(state, ds);
        dest_cards_num = fcs_col_len(dest_col);
        if (dest_cards_num > 0)
        {
            dest_card = fcs_col_get_card(dest_col, dest_cards_num-1);
            for( stack_idx=0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
            {
                if (stack_idx == ds)
                {
                    continue;
                }
                col = fcs_state_get_col(state, stack_idx);
                cards_num = fcs_col_len(col);
                for( c=cards_num-1 ; c >= 0 ; c--)
                {
                    card = fcs_col_get_card(col, c);
#ifndef FCS_WITHOUT_CARD_FLIPPING
                    if (fcs_card_get_flipped(card))
                    {
                        break;
                    }
#endif
                    if (fcs_is_parent_card(card, dest_card))
                    {

                        /* We can move it there - now let's check to see
                         * if it is already above a suitable parent. */
                        if ((c == 0) ||
                            (! fcs_is_parent_card(card, fcs_col_get_card(col, c-1))))
                        {
                            /* Let's move it */
                            sfs_check_state_begin();

                            my_copy_stack(stack_idx);
                            my_copy_stack(ds);

                            fcs_move_sequence(ds, stack_idx, c, cards_num-1);

                            fcs_flip_top_card(stack_idx);

                            sfs_check_state_end();
                        }

                    }
                }
            }
        }
    }

    return;
}

DECLARE_MOVE_FUNCTION(fc_solve_sfs_yukon_move_kings_to_empty_stack)
{
    int stack_idx, cards_num, c, ds;
    fcs_card_t card;
    fcs_cards_column_t col;

#ifndef HARD_CODED_NUM_STACKS
    int stacks_num;
#endif
    fcs_internal_move_t temp_move;

    tests_define_accessors();

    SET_VACANT_STACKS();
    temp_move = fc_solve_empty_move;

    if (num_vacant_stacks == 0)
    {
        return;
    }

#ifndef HARD_CODED_NUM_STACKS
    stacks_num = INSTANCE_STACKS_NUM;
#endif

    for(ds=0;ds<LOCAL_STACKS_NUM;ds++)
    {
        if (fcs_col_len(
            fcs_state_get_col(state, ds)
            ) == 0)
        {
            break;
        }
    }

    for( stack_idx=0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
    {
        col = fcs_state_get_col(state, stack_idx);
        cards_num = fcs_col_len(col);
        for( c=cards_num-1 ; c >= 1 ; c--)
        {
            card = fcs_col_get_card(col, c);
#ifndef FCS_WITHOUT_CARD_FLIPPING
            if (fcs_card_get_flipped(card))
            {
                break;
            }
#endif
            if (fcs_card_rank(card) == 13)
            {
                /* It's a King - so let's move it */
                sfs_check_state_begin();

                my_copy_stack(stack_idx);
                my_copy_stack(ds);
                fcs_move_sequence(ds, stack_idx, c, cards_num-1);

                fcs_flip_top_card(stack_idx);

                sfs_check_state_end();
            }
        }
    }

    return;
}
#endif

DECLARE_MOVE_FUNCTION(fc_solve_sfs_atomic_move_card_to_empty_stack)
{
    tests_define_accessors();
    tests_define_empty_stacks_fill();

    if (tests__is_filled_by_none())
    {
        return;
    }

    SET_VACANT_PTR();
    SET_VACANT_STACKS();

    if (num_vacant_stacks == 0)
    {
        return;
    }

#ifndef HARD_CODED_NUM_STACKS
    SET_GAME_PARAMS();
#endif

    int empty_stack_idx;
    for (empty_stack_idx = 0 ; empty_stack_idx < LOCAL_STACKS_NUM ; empty_stack_idx++)
    {
        if (fcs_col_len(
            fcs_state_get_col(state, empty_stack_idx)
            ) == 0)
        {
            break;
        }
    }

    for (int stack_idx = 0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
    {
        fcs_cards_column_t col = fcs_state_get_col(state, stack_idx);
        const int cards_num = fcs_col_len(col);

        /* Bug fix: if there's only one card in a column, there's no
         * point moving it to a new empty column.
         * */
        if (cards_num > 1)
        {
            const fcs_card_t card = fcs_col_get_card(col, cards_num-1);

            if (tests__is_filled_by_kings_only() &&
                (fcs_card_rank(card) != 13))
            {
                continue;
            }
            /* Let's move it */
            {
                sfs_check_state_begin();

                my_copy_stack(stack_idx);

                fcs_cards_column_t new_src_col = fcs_state_get_col(new_state, stack_idx);

                fcs_col_pop_top(new_src_col);

                my_copy_stack(empty_stack_idx);

                fcs_cards_column_t empty_stack_col = fcs_state_get_col(new_state, empty_stack_idx);
                fcs_col_push_card(empty_stack_col, card);

                fcs_internal_move_t temp_move;
                fcs_int_move_set_type(temp_move, FCS_MOVE_TYPE_STACK_TO_STACK);
                fcs_int_move_set_src_stack(temp_move, stack_idx);
                fcs_int_move_set_dest_stack(temp_move, empty_stack_idx);
                fcs_int_move_set_num_cards_in_seq(temp_move, 1);

                fcs_move_stack_push(moves, temp_move);

                sfs_check_state_end()
            }
        }
    }

    return;
}

DECLARE_MOVE_FUNCTION(fc_solve_sfs_atomic_move_card_to_parent)
{
    tests_define_accessors();
    tests_define_seqs_built_by();
    tests_define_empty_stacks_fill();

#ifndef HARD_CODED_NUM_STACKS
    SET_GAME_PARAMS();
#endif

    const int num_cards_in_col_threshold = tests__should_not_empty_columns() ? 1 : 0;

    for (int stack_idx = 0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
    {
        fcs_cards_column_t col = fcs_state_get_col(state, stack_idx);
        const int cards_num = fcs_col_len(col);

        if (cards_num > num_cards_in_col_threshold)
        {
            const fcs_card_t card = fcs_col_get_card(col, cards_num-1);

            for (int ds = 0 ; ds < LOCAL_STACKS_NUM ; ds++)
            {
                if (ds == stack_idx)
                {
                    continue;
                }

                fcs_cards_column_t dest_col = fcs_state_get_col(state, ds);

                if (fcs_col_len(dest_col) > 0)
                {
                    fcs_card_t dest_card = fcs_col_get_card(dest_col,
                            fcs_col_len(dest_col)-1);
                    if (fcs_is_parent_card(card, dest_card))
                    {
                        /* Let's move it */
                        {
                            sfs_check_state_begin();

                            my_copy_stack(stack_idx);
                            my_copy_stack(ds);

                            fcs_cards_column_t new_src_col = fcs_state_get_col(new_state, stack_idx);
                            fcs_cards_column_t new_dest_col = fcs_state_get_col(new_state, ds);

                            fcs_col_pop_top(new_src_col);

                            fcs_col_push_card(new_dest_col, card);

                            fcs_internal_move_t temp_move;
                            fcs_int_move_set_type(temp_move, FCS_MOVE_TYPE_STACK_TO_STACK);
                            fcs_int_move_set_src_stack(temp_move, stack_idx);
                            fcs_int_move_set_dest_stack(temp_move, ds);
                            fcs_int_move_set_num_cards_in_seq(temp_move, 1);

                            fcs_move_stack_push(moves, temp_move);

                            sfs_check_state_end()
                        }
                    }
                }
            }
        }
    }

    return;
}

DECLARE_MOVE_FUNCTION(fc_solve_sfs_atomic_move_card_to_freecell)
{
    tests_define_accessors();
    tests_define_empty_stacks_fill();

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)))
    SET_GAME_PARAMS();
#endif

    SET_VACANT_PTR();
    SET_VACANT_FREECELLS();

    if (num_vacant_freecells == 0)
    {
        return;
    }

    const int num_cards_in_col_threshold = tests__should_not_empty_columns() ? 1 : 0;

    int ds;
    for (ds = 0 ; ds < LOCAL_FREECELLS_NUM ; ds++)
    {
        if (fcs_freecell_is_empty(state, ds))
        {
            break;
        }
    }

    for (int stack_idx = 0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
    {
        fcs_cards_column_t col = fcs_state_get_col(state, stack_idx);
        const int cards_num = fcs_col_len(col);
        if (cards_num > num_cards_in_col_threshold)
        {
            const fcs_card_t card = fcs_col_get_card(col, cards_num-1);

            /* Let's move it */
            {
                sfs_check_state_begin();

                my_copy_stack(stack_idx);
                fcs_cards_column_t new_src_col =
                    fcs_state_get_col(new_state, stack_idx);

                fcs_col_pop_top(new_src_col);

                fcs_put_card_in_freecell(new_state, ds, card);

                fcs_internal_move_t temp_move;
                fcs_int_move_set_type(temp_move, FCS_MOVE_TYPE_STACK_TO_FREECELL);
                fcs_int_move_set_src_stack(temp_move, stack_idx);
                fcs_int_move_set_dest_freecell(temp_move, ds);

                fcs_move_stack_push(moves, temp_move);

                sfs_check_state_end()
            }
        }
    }

    return;
}

DECLARE_MOVE_FUNCTION(fc_solve_sfs_atomic_move_freecell_card_to_parent)
{
    tests_define_accessors();
    tests_define_seqs_built_by();

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)))
    SET_GAME_PARAMS();
#endif

    for (int fc = 0 ; fc < LOCAL_FREECELLS_NUM ; fc++)
    {
        const fcs_card_t card = fcs_freecell_card(state, fc);

        if (fcs_card_is_empty(card))
        {
            continue;
        }

        for (int ds = 0 ; ds < LOCAL_STACKS_NUM ; ds++)
        {
            fcs_cards_column_t dest_col = fcs_state_get_col(state, ds);
            if (fcs_col_len(dest_col) > 0)
            {
                fcs_card_t dest_card = fcs_col_get_card(dest_col, fcs_col_len(dest_col)-1);
                if (fcs_is_parent_card(card, dest_card))
                {
                    /* Let's move it */
                    sfs_check_state_begin();

                    my_copy_stack(ds);

                    fcs_cards_column_t new_dest_col = fcs_state_get_col(new_state, ds);

                    fcs_empty_freecell(new_state, fc);

                    fcs_col_push_card(new_dest_col, card);

                    fcs_internal_move_t temp_move;
                    fcs_int_move_set_type(temp_move, FCS_MOVE_TYPE_FREECELL_TO_STACK);
                    fcs_int_move_set_src_freecell(temp_move, fc);
                    fcs_int_move_set_dest_stack(temp_move, ds);
                    fcs_int_move_set_num_cards_in_seq(temp_move, 1);

                    fcs_move_stack_push(moves, temp_move);

                    sfs_check_state_end()
                }
            }
        }
    }

    return;
}

DECLARE_MOVE_FUNCTION(fc_solve_sfs_atomic_move_freecell_card_to_empty_stack)
{
    tests_define_accessors();
    tests_define_empty_stacks_fill();

#if ((!defined(HARD_CODED_NUM_FREECELLS)) || (!defined(HARD_CODED_NUM_STACKS)))
    SET_GAME_PARAMS();
#endif

    if (tests__is_filled_by_none())
    {
        return;
    }

    SET_VACANT_PTR();
    SET_VACANT_STACKS();

    if (num_vacant_stacks == 0)
    {
        return;
    }

    /* Find a vacant stack */
    int ds;
    for (ds = 0 ; ds < LOCAL_STACKS_NUM ; ds++)
    {
        if (fcs_col_len(
            fcs_state_get_col(state, ds)
            ) == 0)
        {
            break;
        }
    }

    for (int fc = 0 ; fc < LOCAL_FREECELLS_NUM ; fc++)
    {
        const fcs_card_t card = fcs_freecell_card(state, fc);

        if (fcs_card_is_empty(card) ||
            (tests__is_filled_by_kings_only() &&
             (fcs_card_rank(card) != 13))
           )
        {
            continue;
        }

        {
            sfs_check_state_begin();

            my_copy_stack(ds);

            fcs_cards_column_t new_dest_col = fcs_state_get_col(new_state, ds);

            fcs_empty_freecell(new_state, fc);

            fcs_col_push_card(new_dest_col, card);

            fcs_internal_move_t temp_move;
            fcs_int_move_set_type(temp_move, FCS_MOVE_TYPE_FREECELL_TO_STACK);
            fcs_int_move_set_src_freecell(temp_move, fc);
            fcs_int_move_set_dest_stack(temp_move, ds);
            fcs_int_move_set_num_cards_in_seq(temp_move, 1);

            fcs_move_stack_push(moves, temp_move);

            sfs_check_state_end()
        }
    }

    return;
}

#define CALC_FOUNDATION_TO_PUT_CARD_ON__STATE_PARAMS() pass_new_state.key, card

#define CALC_FOUNDATION_TO_PUT_CARD_ON() calc_foundation_to_put_card_on(soft_thread, CALC_FOUNDATION_TO_PUT_CARD_ON__STATE_PARAMS())

#ifdef FCS_FREECELL_ONLY
#define SEQS_ARE_BUILT_BY_RANK() FALSE
#else
#define SEQS_ARE_BUILT_BY_RANK() (sequences_are_built_by == FCS_SEQ_BUILT_BY_RANK)
#endif

static GCC_INLINE int calc_foundation_to_put_card_on(
    fc_solve_soft_thread_t * soft_thread,
    fcs_state_t * my_ptr_state,
    const fcs_card_t card
)
{
#ifndef FCS_FREECELL_ONLY
    fc_solve_instance_t * instance = soft_thread->hard_thread->instance;
#endif

    tests_define_seqs_built_by();

    for (int deck = 0 ; deck < INSTANCE_DECKS_NUM ; deck++)
    {
        if (fcs_foundation_value(*my_ptr_state, (deck<<2)+fcs_card_suit(card)) == fcs_card_rank(card) - 1)
        {
            int ret_val = (deck<<2)+fcs_card_suit(card);
            /* Always put on the foundation if it is built-by-suit */
#ifndef FCS_FREECELL_ONLY
            if (sequences_are_built_by == FCS_SEQ_BUILT_BY_SUIT)
            {
                return ret_val;
            }
#endif

            int other_deck_idx;
            for (other_deck_idx = 0 ; other_deck_idx < (INSTANCE_DECKS_NUM << 2) ; other_deck_idx++)
            {
                if (fcs_foundation_value(*my_ptr_state, other_deck_idx)
                        < fcs_card_rank(card) - 2 -
                        (
                            SEQS_ARE_BUILT_BY_RANK()
                            ? 0
                            : ((other_deck_idx&0x1) == (fcs_card_suit(card)&0x1))
                        )
                   )
                {
                    break;
                }
            }
            if (other_deck_idx == (INSTANCE_DECKS_NUM << 2))
            {
                return ret_val;
            }
        }
    }

    return -1;
}

extern int fc_solve_sfs_raymond_prune(
    fc_solve_soft_thread_t * soft_thread,
    fcs_kv_state_t * raw_ptr_state_raw,
    fcs_collectible_state_t * * ptr_next_state_val
)
{
    tests_define_accessors();
#ifndef HARD_CODED_NUM_STACKS
    SET_GAME_PARAMS();
#endif

    fcs_derived_states_list_t derived_states_list_struct;
    derived_states_list_struct.states = NULL;
    derived_states_list_struct.num_states = 0;

    sfs_check_state_begin();

    int num_total_cards_moved = 0;
    int num_cards_moved = 0;
    do {
        num_cards_moved = 0;
        for ( int stack_idx=0 ; stack_idx < LOCAL_STACKS_NUM ; stack_idx++)
        {
            fcs_cards_column_t col = fcs_state_get_col(new_state, stack_idx);
            const int cards_num = fcs_col_len(col);

            if (cards_num)
            {
                /* Get the top card in the stack */
                const fcs_card_t card = fcs_col_get_card(col, cards_num-1);

                const int dest_foundation = CALC_FOUNDATION_TO_PUT_CARD_ON();
                if (dest_foundation >= 0)
                {
                    /* We can safely move it. */
                    num_cards_moved++;

                    my_copy_stack(stack_idx);
                    {
                        fcs_cards_column_t new_temp_col;
                        new_temp_col = fcs_state_get_col(new_state, stack_idx);
                        fcs_col_pop_top(new_temp_col);
                    }

                    fcs_increment_foundation(new_state, dest_foundation);

                    fcs_internal_move_t temp_move;
                    fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_FOUNDATION);
                    fcs_int_move_set_src_stack(temp_move,stack_idx);
                    fcs_int_move_set_foundation(temp_move,dest_foundation);

                    fcs_move_stack_push(moves, temp_move);

                    fcs_flip_top_card(stack_idx);
                }
            }
        }

        /* Now check the same for the free cells */
        for ( int fc=0 ; fc < LOCAL_FREECELLS_NUM ; fc++)
        {
            const fcs_card_t card = fcs_freecell_card(new_state, fc);
            if (fcs_card_is_valid(card))
            {
                const int dest_foundation = CALC_FOUNDATION_TO_PUT_CARD_ON();
                if (dest_foundation >= 0)
                {
                    num_cards_moved++;

                    /* We can put it there */

                    fcs_empty_freecell(new_state, fc);

                    fcs_internal_move_t temp_move;
                    fcs_increment_foundation(new_state, dest_foundation);

                    fcs_int_move_set_type(temp_move,FCS_MOVE_TYPE_FREECELL_TO_FOUNDATION);
                    fcs_int_move_set_src_freecell(temp_move,fc);
                    fcs_int_move_set_foundation(temp_move,dest_foundation);

                    fcs_move_stack_push(moves, temp_move);
                }
            }
        }
        num_total_cards_moved += num_cards_moved;
    } while (num_cards_moved);

#define derived_states_list (&derived_states_list_struct)
    sfs_check_state_end();
#undef derived_states_list

    {
        register int ret_code;

        if (num_total_cards_moved)
        {
            register fcs_collectible_state_t * ptr_next_state;

            *ptr_next_state_val
                = ptr_next_state
                = derived_states_list_struct.states[0].state_ptr;

            /*
             * Set the GENERATED_BY_PRUNING flag uncondtionally. It won't
             * hurt if it's already there, and if it's a state that was
             * found by other means, we still shouldn't prune it, because
             * it is already "prune-perfect".
             * */
            FCS_S_VISITED(ptr_next_state) |= FCS_VISITED_GENERATED_BY_PRUNING;

            ret_code = PRUNE_RET_FOLLOW_STATE;
        }
        else
        {
            *ptr_next_state_val = NULL;
            ret_code = PRUNE_RET_NOT_FOUND;
        }

        free(derived_states_list_struct.states);

        return ret_code;
    }
}