~ubuntu-branches/ubuntu/natty/freecell-solver/natty

« back to all changes in this revision

Viewing changes to freecell.c

  • Committer: Bazaar Package Importer
  • Author(s): RISKO Gergely
  • Date: 2005-03-30 20:12:47 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050330201247-8qdt6jhg7kxr3gjy
Tags: 2.8.10-1
* New upstream release
* fixed override disparity

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
#include <string.h>
11
11
#include <stdio.h>
12
12
#include <math.h>
 
13
#include <limits.h>
 
14
 
 
15
 
 
16
#include "config.h"
 
17
 
13
18
#if FCS_STATE_STORAGE==FCS_STATE_STORAGE_LIBREDBLACK_TREE
14
19
#include <search.h>
15
20
#endif
16
 
#include <limits.h>
17
 
 
18
 
 
19
 
#include "config.h"
 
21
 
20
22
#include "state.h"
21
23
#include "card.h"
22
24
#include "fcs_dm.h"
24
26
 
25
27
#include "fcs_isa.h"
26
28
#include "tests.h"
 
29
#include "ms_ca.h"
27
30
 
28
31
#ifdef DMALLOC
29
32
#include "dmalloc.h"
55
58
int freecell_solver_sfs_move_top_stack_cards_to_founds(
56
59
        freecell_solver_soft_thread_t * soft_thread,
57
60
        fcs_state_with_locations_t * ptr_state_with_locations,
58
 
        int depth,
59
61
        int num_freestacks,
60
62
        int num_freecells,
61
63
        fcs_derived_states_list_t * derived_states_list,
62
64
        int reparent
63
65
        )
64
66
{
65
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
66
 
    freecell_solver_instance_t * instance = hard_thread->instance;
67
 
 
68
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
 
67
    tests_declare_accessors();
69
68
    int stack;
70
69
    int cards_num;
71
70
    int deck;
74
73
    int check;
75
74
    int state_stacks_num;
76
75
 
77
 
    fcs_move_stack_t * moves;
78
76
    fcs_move_t temp_move;
79
77
 
80
 
    fcs_move_stack_alloc_into_var(moves);
 
78
    tests_define_accessors();
 
79
 
 
80
    moves = hard_thread->reusable_move_stack;
 
81
    indirect_stacks_buffer = hard_thread->indirect_stacks_buffer;
81
82
 
82
83
    state_stacks_num = instance->stacks_num;
83
84
 
96
97
 
97
98
                    sfs_check_state_begin();
98
99
 
99
 
                    fcs_move_stack_reset(moves);
100
100
 
 
101
                    my_copy_stack(stack);
101
102
                    fcs_pop_stack_card(new_state, stack, temp_card);
102
103
 
103
104
                    fcs_increment_foundation(new_state, deck*4+fcs_card_suit(card));
126
127
        }
127
128
    }
128
129
 
129
 
    fcs_move_stack_destroy(moves);
130
 
 
131
130
    return FCS_STATE_IS_NOT_SOLVEABLE;
132
131
}
133
132
 
139
138
int freecell_solver_sfs_move_freecell_cards_to_founds(
140
139
        freecell_solver_soft_thread_t * soft_thread,
141
140
        fcs_state_with_locations_t * ptr_state_with_locations,
142
 
        int depth,
143
141
        int num_freestacks,
144
142
        int num_freecells,
145
143
        fcs_derived_states_list_t * derived_states_list,
146
144
        int reparent
147
145
        )
148
146
{
149
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
150
 
    freecell_solver_instance_t * instance = hard_thread->instance;
151
 
 
152
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
 
147
    tests_declare_accessors();
153
148
    int fc;
154
149
    int deck;
155
150
    fcs_card_t card;
156
151
    int check;
157
 
    fcs_move_stack_t * moves;
158
152
    fcs_move_t temp_move;
159
153
    int state_freecells_num;
160
154
 
161
 
    fcs_move_stack_alloc_into_var(moves);
 
155
    tests_define_accessors();
162
156
 
163
157
    state_freecells_num = instance->freecells_num;
164
158
 
179
173
 
180
174
                    fcs_increment_foundation(new_state, deck*4+fcs_card_suit(card));
181
175
 
182
 
                    fcs_move_stack_reset(moves);
183
176
                    fcs_move_set_type(temp_move,FCS_MOVE_TYPE_FREECELL_TO_FOUNDATION);
184
177
                    fcs_move_set_src_freecell(temp_move,fc);
185
178
                    fcs_move_set_foundation(temp_move,deck*4+fcs_card_suit(card));
195
188
        }
196
189
    }
197
190
 
198
 
    fcs_move_stack_destroy(moves);
199
 
 
200
191
    return FCS_STATE_IS_NOT_SOLVEABLE;
201
192
}
202
193
 
203
194
int freecell_solver_sfs_move_freecell_cards_on_top_of_stacks(
204
195
        freecell_solver_soft_thread_t * soft_thread,
205
196
        fcs_state_with_locations_t * ptr_state_with_locations,
206
 
        int depth,
207
197
        int num_freestacks,
208
198
        int num_freecells,
209
199
        fcs_derived_states_list_t * derived_states_list,
210
200
        int reparent
211
201
        )
212
202
{
213
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
214
 
    freecell_solver_instance_t * instance = hard_thread->instance;
215
 
 
216
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
 
203
    tests_declare_accessors();
 
204
    
217
205
    int dest_cards_num;
218
206
    int ds, fc, dc;
219
207
    fcs_card_t dest_card, src_card, temp_card, dest_below_card;
220
208
    int check;
221
 
    fcs_move_stack_t * moves;
 
209
 
222
210
    fcs_move_t temp_move;
223
211
    int is_seq_in_dest;
224
212
    int num_cards_to_relocate;
226
214
    int a,b;
227
215
    int state_freecells_num, state_stacks_num, sequences_are_built_by;
228
216
 
229
 
    fcs_move_stack_alloc_into_var(moves);
 
217
    tests_define_accessors();
230
218
 
231
219
    state_freecells_num = instance->freecells_num;
232
220
    state_stacks_num = instance->stacks_num;
299
287
 
300
288
                                sfs_check_state_begin()
301
289
 
302
 
                                fcs_move_stack_reset(moves);
303
290
 
304
291
                                /* Fill the freecells with the top cards */
305
292
 
 
293
                                my_copy_stack(ds);
 
294
 
306
295
                                for(a=0 ; a<freecells_to_fill ; a++)
307
296
                                {
308
297
                                    /* Find a vacant freecell */
334
323
                                            break;
335
324
                                        }
336
325
                                    }
 
326
                                    my_copy_stack(b);
337
327
 
338
328
                                    fcs_pop_stack_card(new_state, ds, temp_card);
339
329
                                    fcs_push_card_into_stack(new_state, b, temp_card);
345
335
                                }
346
336
 
347
337
                                /* Now put the freecell card on top of the stack */
 
338
                                
348
339
                                fcs_push_card_into_stack(new_state, ds, src_card);
349
340
                                fcs_empty_freecell(new_state, fc);
350
341
                                fcs_move_set_type(temp_move,FCS_MOVE_TYPE_FREECELL_TO_STACK);
361
352
        }
362
353
    }
363
354
 
364
 
    fcs_move_stack_destroy(moves);
365
 
 
366
355
    return FCS_STATE_IS_NOT_SOLVEABLE;
367
356
}
368
357
 
371
360
int freecell_solver_sfs_move_non_top_stack_cards_to_founds(
372
361
        freecell_solver_soft_thread_t * soft_thread,
373
362
        fcs_state_with_locations_t * ptr_state_with_locations,
374
 
        int depth,
375
363
        int num_freestacks,
376
364
        int num_freecells,
377
365
        fcs_derived_states_list_t * derived_states_list,
378
366
        int reparent
379
367
        )
380
368
{
381
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
382
 
    freecell_solver_instance_t * instance = hard_thread->instance;
383
 
 
384
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
 
369
    tests_declare_accessors();
 
370
    
385
371
    int check;
386
372
 
387
373
    int stack;
392
378
    int state_freecells_num;
393
379
    int state_stacks_num;
394
380
 
395
 
    fcs_move_stack_t * moves;
396
381
    fcs_move_t temp_move;
397
382
 
 
383
    tests_define_accessors();
 
384
 
398
385
    state_freecells_num = instance->freecells_num;
399
386
    state_stacks_num = instance->stacks_num;
400
387
 
401
 
    fcs_move_stack_alloc_into_var(moves);
402
388
 
403
389
 
404
390
    /* Now let's check if a card that is under some other cards can be placed
433
419
 
434
420
                        sfs_check_state_begin()
435
421
 
436
 
                        fcs_move_stack_reset(moves);
 
422
                        my_copy_stack(stack);
 
423
 
437
424
 
438
425
                        /* Fill the freecells with the top cards */
439
426
                        for(a=0 ; a<min(num_freecells, cards_num-(c+1)) ; a++)
471
458
                                }
472
459
                            }
473
460
 
 
461
                            my_copy_stack(b);
 
462
 
474
463
                            fcs_pop_stack_card(new_state, stack, temp_card);
475
464
                            fcs_push_card_into_stack(new_state, b, temp_card);
476
465
 
503
492
        }
504
493
    }
505
494
 
506
 
    fcs_move_stack_destroy(moves);
507
 
 
508
495
    return FCS_STATE_IS_NOT_SOLVEABLE;
509
496
}
510
497
 
514
501
int freecell_solver_sfs_move_stack_cards_to_a_parent_on_the_same_stack(
515
502
        freecell_solver_soft_thread_t * soft_thread,
516
503
        fcs_state_with_locations_t * ptr_state_with_locations,
517
 
        int depth,
518
504
        int num_freestacks,
519
505
        int num_freecells,
520
506
        fcs_derived_states_list_t * derived_states_list,
521
507
        int reparent
522
508
        )
523
509
{
524
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
525
 
    freecell_solver_instance_t * instance = hard_thread->instance;
526
 
 
527
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
 
510
    tests_declare_accessors();
528
511
    int check;
529
512
 
530
513
    int stack, c, cards_num, a, dc,b;
537
520
    int state_stacks_num;
538
521
    int sequences_are_built_by;
539
522
 
540
 
    fcs_move_stack_t * moves;
541
523
    fcs_move_t temp_move;
542
524
 
 
525
    tests_define_accessors();
 
526
 
543
527
    state_freecells_num = instance->freecells_num;
544
528
    state_stacks_num = instance->stacks_num;
545
529
    sequences_are_built_by = instance->sequences_are_built_by;
546
530
 
547
 
    fcs_move_stack_alloc_into_var(moves);
548
 
 
549
 
 
550
531
    /*
551
532
     * Now let's try to move a stack card to a parent card which is found
552
533
     * on the same stack.
618
599
 
619
600
                                sfs_check_state_begin()
620
601
 
621
 
                                fcs_move_stack_reset(moves);
622
602
 
623
603
                                {
624
604
                                    int i_card_pos;
627
607
 
628
608
                                    i_card_pos = fcs_stack_len(new_state,stack)-1;
629
609
                                    a = 0;
 
610
 
 
611
                                    my_copy_stack(ds);
630
612
                                    while(i_card_pos>c)
631
613
                                    {
632
614
                                        if (a < freecells_to_fill)
660
642
                                                }
661
643
                                            }
662
644
 
 
645
                                            my_copy_stack(b);
 
646
 
663
647
                                            fcs_pop_stack_card(new_state, ds, temp_card);
664
648
                                            fcs_push_card_into_stack(new_state, b, temp_card);
665
649
 
703
687
                                                break;
704
688
                                            }
705
689
                                        }
 
690
 
 
691
                                        my_copy_stack(b);
706
692
                                        fcs_push_card_into_stack(new_state, b, moved_card);
707
693
 
708
694
                                        fcs_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_STACK);
750
736
                                            }
751
737
 
752
738
                                            fcs_pop_stack_card(new_state, ds, temp_card);
 
739
                                            my_copy_stack(b);
753
740
                                            fcs_push_card_into_stack(new_state, b, temp_card);
754
741
 
755
742
                                            fcs_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_STACK);
777
764
                                    }
778
765
                                    else
779
766
                                    {
 
767
                                        my_copy_stack(source_index);
780
768
                                        fcs_pop_stack_card(new_state, source_index, moved_card);
781
769
 
782
770
                                        fcs_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_STACK);
798
786
        }
799
787
    }
800
788
 
801
 
    fcs_move_stack_destroy(moves);
802
 
 
803
789
    return FCS_STATE_IS_NOT_SOLVEABLE;
804
790
}
805
791
#undef ds
808
794
int freecell_solver_sfs_move_stack_cards_to_different_stacks(
809
795
        freecell_solver_soft_thread_t * soft_thread,
810
796
        fcs_state_with_locations_t * ptr_state_with_locations,
811
 
        int depth,
812
797
        int num_freestacks,
813
798
        int num_freecells,
814
799
        fcs_derived_states_list_t * derived_states_list,
815
800
        int reparent
816
801
        )
817
802
{
818
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
819
 
    freecell_solver_instance_t * instance = hard_thread->instance;
 
803
    tests_declare_accessors();
820
804
 
821
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
822
805
    int check;
823
806
 
824
807
    int stack, c, cards_num, a, dc, ds,b;
832
815
    int state_stacks_num;
833
816
    int sequences_are_built_by;
834
817
 
835
 
    fcs_move_stack_t * moves;
836
818
    fcs_move_t temp_move;
837
819
 
 
820
    tests_define_accessors();
 
821
 
838
822
    state_freecells_num = instance->freecells_num;
839
823
    state_stacks_num = instance->stacks_num;
840
824
    sequences_are_built_by = instance->sequences_are_built_by;
841
825
 
842
 
    fcs_move_stack_alloc_into_var(moves);
843
 
 
844
 
 
845
826
    /* Now let's try to move a card from one stack to the other     *
846
827
     * Note that it does not involve moving cards lower than king   *
847
828
     * to empty stacks                                              */
926
907
 
927
908
                                        sfs_check_state_begin()
928
909
 
929
 
                                        fcs_move_stack_reset(moves);
930
910
 
931
911
                                        /* Fill the freecells with the top cards */
932
912
 
 
913
                                        my_copy_stack(stack);
 
914
                                        my_copy_stack(ds);
 
915
 
933
916
                                        for(a=0 ; a<freecells_to_fill ; a++)
934
917
                                        {
935
918
                                            /* Find a vacant freecell */
949
932
                                            {
950
933
                                                from_which_stack = ds;
951
934
                                            }
 
935
                                            my_copy_stack(from_which_stack);
 
936
                                            
952
937
                                            fcs_pop_stack_card(new_state, from_which_stack, temp_card);
953
938
 
954
939
                                            fcs_put_card_in_freecell(new_state, b, temp_card);
981
966
                                            {
982
967
                                                from_which_stack = ds;
983
968
                                            }
984
 
 
985
 
 
 
969
                                            
 
970
                                            my_copy_stack(b);
986
971
                                            fcs_pop_stack_card(new_state, from_which_stack, temp_card);
987
972
                                            fcs_push_card_into_stack(new_state, b, temp_card);
988
973
                                            fcs_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_STACK);
1022
1007
        }
1023
1008
    }
1024
1009
 
1025
 
    fcs_move_stack_destroy(moves);
1026
 
 
1027
1010
    return FCS_STATE_IS_NOT_SOLVEABLE;
1028
1011
}
1029
1012
 
1032
1015
int freecell_solver_sfs_move_sequences_to_free_stacks(
1033
1016
        freecell_solver_soft_thread_t * soft_thread,
1034
1017
        fcs_state_with_locations_t * ptr_state_with_locations,
1035
 
        int depth,
1036
1018
        int num_freestacks,
1037
1019
        int num_freecells,
1038
1020
        fcs_derived_states_list_t * derived_states_list,
1039
1021
        int reparent
1040
1022
        )
1041
1023
{
1042
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
1043
 
    freecell_solver_instance_t * instance = hard_thread->instance;
1044
 
 
1045
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
 
1024
    tests_declare_accessors();
1046
1025
    int check;
1047
1026
 
1048
1027
    int stack, cards_num, c, ds, a, b, seq_end;
1053
1032
    int state_stacks_num;
1054
1033
    int sequences_are_built_by;
1055
1034
 
1056
 
    fcs_move_stack_t * moves;
1057
1035
    fcs_move_t temp_move;
1058
1036
 
 
1037
    tests_define_accessors();
 
1038
 
1059
1039
    if (instance->empty_stacks_fill == FCS_ES_FILLED_BY_NONE)
1060
1040
    {
1061
1041
        return FCS_STATE_IS_NOT_SOLVEABLE;
1065
1045
    state_stacks_num = instance->stacks_num;
1066
1046
    sequences_are_built_by = instance->sequences_are_built_by;
1067
1047
 
1068
 
    fcs_move_stack_alloc_into_var(moves);
1069
 
 
1070
1048
    max_sequence_len = calc_max_sequence_move(num_freecells, num_freestacks-1);
1071
1049
 
1072
1050
    /* Now try to move sequences to empty stacks */
1116
1094
                    {
1117
1095
                        sfs_check_state_begin();
1118
1096
 
1119
 
                        fcs_move_stack_reset(moves);
1120
1097
 
1121
1098
                        for(ds=0;ds<state_stacks_num;ds++)
1122
1099
                        {
1124
1101
                                break;
1125
1102
                        }
1126
1103
 
 
1104
                        my_copy_stack(ds);
 
1105
 
1127
1106
                        for(a=c ; a <= cards_num-1 ; a++)
1128
1107
                        {
1129
1108
                            fcs_push_stack_card_into_stack(new_state, ds, stack, a);
1130
1109
                        }
1131
1110
 
 
1111
                        my_copy_stack(stack);
 
1112
 
1132
1113
                        for(a=0;a<cards_num-c;a++)
1133
1114
                        {
1134
1115
                            fcs_pop_stack_card(new_state, stack, temp_card);
1187
1168
                        {
1188
1169
                            sfs_check_state_begin();
1189
1170
 
1190
 
                            fcs_move_stack_reset(moves);
1191
1171
 
1192
1172
                            /* Fill the freecells with the top cards */
1193
1173
 
 
1174
                            my_copy_stack(stack);
 
1175
 
1194
1176
                            for(a=0; a<freecells_to_fill ; a++)
1195
1177
                            {
1196
1178
                                /* Find a vacant freecell */
1210
1192
                                fcs_move_stack_push(moves, temp_move);
1211
1193
                            }
1212
1194
 
 
1195
                            my_copy_stack(stack);
 
1196
 
1213
1197
                            /* Fill the free stacks with the cards below them */
1214
1198
                            for(a=0; a < freestacks_to_fill ; a++)
1215
1199
                            {
1221
1205
                                        break;
1222
1206
                                    }
1223
1207
                                }
 
1208
 
 
1209
                                my_copy_stack(b);
1224
1210
                                fcs_pop_stack_card(new_state, stack, temp_card);
1225
1211
                                fcs_push_card_into_stack(new_state, b, temp_card);
1226
1212
                                fcs_move_set_type(temp_move,FCS_MOVE_TYPE_STACK_TO_STACK);
1239
1225
                                }
1240
1226
                            }
1241
1227
 
 
1228
                            my_copy_stack(b);
 
1229
 
1242
1230
                            for(a=seq_start ; a <= seq_end ; a++)
1243
1231
                            {
1244
1232
                                fcs_push_stack_card_into_stack(new_state, b, stack, a);
1262
1250
        }
1263
1251
    }
1264
1252
 
1265
 
    fcs_move_stack_destroy(moves);
1266
 
 
1267
1253
    return FCS_STATE_IS_NOT_SOLVEABLE;
1268
1254
}
1269
1255
 
1272
1258
int freecell_solver_sfs_move_freecell_cards_to_empty_stack(
1273
1259
        freecell_solver_soft_thread_t * soft_thread,
1274
1260
        fcs_state_with_locations_t * ptr_state_with_locations,
1275
 
        int depth,
1276
1261
        int num_freestacks,
1277
1262
        int num_freecells,
1278
1263
        fcs_derived_states_list_t * derived_states_list,
1279
1264
        int reparent
1280
1265
        )
1281
1266
{
1282
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
1283
 
    freecell_solver_instance_t * instance = hard_thread->instance;
1284
 
 
1285
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
 
1267
    tests_declare_accessors();
1286
1268
    int check;
1287
1269
    int fc, stack;
1288
1270
    fcs_card_t card;
1289
1271
 
1290
 
    fcs_move_stack_t * moves;
1291
1272
    fcs_move_t temp_move;
1292
1273
 
1293
1274
    int state_freecells_num;
1295
1276
 
1296
1277
    /* Let's try to put cards that occupy freecells on an empty stack */
1297
1278
 
 
1279
    tests_define_accessors();
 
1280
 
1298
1281
    if (instance->empty_stacks_fill == FCS_ES_FILLED_BY_NONE)
1299
1282
    {
1300
1283
        return FCS_STATE_IS_NOT_SOLVEABLE;
1303
1286
    state_freecells_num = instance->freecells_num;
1304
1287
    state_stacks_num = instance->stacks_num;
1305
1288
 
1306
 
    fcs_move_stack_alloc_into_var(moves);
1307
 
 
1308
1289
    for(fc=0;fc<state_freecells_num;fc++)
1309
1290
    {
1310
1291
        card = fcs_freecell_card(state, fc);
1327
1308
 
1328
1309
                sfs_check_state_begin();
1329
1310
 
1330
 
                fcs_move_stack_reset(moves);
 
1311
                my_copy_stack(stack);
1331
1312
 
1332
1313
                fcs_push_card_into_stack(new_state, stack, card);
1333
1314
                fcs_empty_freecell(new_state, fc);
1342
1323
        }
1343
1324
    }
1344
1325
 
1345
 
    fcs_move_stack_destroy(moves);
1346
 
 
1347
1326
    return FCS_STATE_IS_NOT_SOLVEABLE;
1348
1327
}
1349
1328
 
1350
1329
int freecell_solver_sfs_move_cards_to_a_different_parent(
1351
1330
        freecell_solver_soft_thread_t * soft_thread,
1352
1331
        fcs_state_with_locations_t * ptr_state_with_locations,
1353
 
        int depth,
1354
1332
        int num_freestacks,
1355
1333
        int num_freecells,
1356
1334
        fcs_derived_states_list_t * derived_states_list,
1357
1335
        int reparent
1358
1336
        )
1359
1337
{
1360
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
1361
 
    freecell_solver_instance_t * instance = hard_thread->instance;
 
1338
    tests_declare_accessors();
1362
1339
 
1363
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
1364
1340
    int check;
1365
1341
 
1366
1342
    int stack, cards_num, c, a, b, ds, dc;
1371
1347
    fcs_card_t dest_card, dest_below_card;
1372
1348
    int freecells_to_fill, freestacks_to_fill;
1373
1349
 
1374
 
    fcs_move_stack_t * moves;
1375
1350
    fcs_move_t temp_move;
1376
1351
 
1377
1352
    int state_freecells_num;
1378
1353
    int state_stacks_num;
1379
1354
    int sequences_are_built_by;
1380
1355
 
 
1356
    tests_define_accessors();
 
1357
 
1381
1358
    state_freecells_num = instance->freecells_num;
1382
1359
    state_stacks_num = instance->stacks_num;
1383
1360
    sequences_are_built_by = instance->sequences_are_built_by;
1384
1361
 
1385
 
    fcs_move_stack_alloc_into_var(moves);
1386
 
 
1387
 
 
1388
1362
    /* This time try to move cards that are already on top of a parent to a different parent */
1389
1363
 
1390
1364
    for (stack=0;stack<state_stacks_num;stack++)
1489
1463
 
1490
1464
                                            sfs_check_state_begin()
1491
1465
 
1492
 
                                            fcs_move_stack_reset(moves);
1493
1466
 
1494
1467
                                            /* Fill the freecells with the top cards */
 
1468
 
 
1469
                                            my_copy_stack(ds);
1495
1470
                                            for(a=0 ; a<freecells_to_fill ; a++)
1496
1471
                                            {
1497
1472
                                                /* Find a vacant freecell */
1502
1477
                                                        break;
1503
1478
                                                    }
1504
1479
                                                }
1505
 
 
 
1480
                                                
1506
1481
                                                fcs_pop_stack_card(new_state, ds, temp_card);
1507
1482
 
1508
1483
                                                fcs_put_card_in_freecell(new_state, b, temp_card);
1525
1500
                                                    }
1526
1501
                                                }
1527
1502
 
 
1503
                                                my_copy_stack(b);
 
1504
 
1528
1505
                                                fcs_pop_stack_card(new_state, ds, temp_card);
1529
1506
                                                fcs_push_card_into_stack(new_state, b, temp_card);
1530
1507
 
1535
1512
                                                fcs_move_stack_push(moves, temp_move);
1536
1513
                                            }
1537
1514
 
 
1515
                                            my_copy_stack(stack);
 
1516
 
1538
1517
                                            for(a=c ; a <= cards_num-1 ; a++)
1539
1518
                                            {
1540
1519
                                                fcs_push_stack_card_into_stack(new_state, ds, stack, a);
1563
1542
        }
1564
1543
    }
1565
1544
 
1566
 
    fcs_move_stack_destroy(moves);
1567
 
 
1568
1545
    return FCS_STATE_IS_NOT_SOLVEABLE;
1569
1546
}
1570
1547
 
1574
1551
int freecell_solver_sfs_empty_stack_into_freecells(
1575
1552
        freecell_solver_soft_thread_t * soft_thread,
1576
1553
        fcs_state_with_locations_t * ptr_state_with_locations,
1577
 
        int depth,
1578
1554
        int num_freestacks,
1579
1555
        int num_freecells,
1580
1556
        fcs_derived_states_list_t * derived_states_list,
1581
1557
        int reparent
1582
1558
        )
1583
1559
{
1584
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
1585
 
    freecell_solver_instance_t * instance = hard_thread->instance;
 
1560
    tests_declare_accessors();
1586
1561
 
1587
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
1588
1562
    int check;
1589
1563
 
1590
1564
    int stack, cards_num, c, b;
1592
1566
    int state_stacks_num;
1593
1567
    int state_freecells_num;
1594
1568
 
1595
 
    fcs_move_stack_t * moves;
1596
1569
    fcs_move_t temp_move;
1597
1570
 
 
1571
    tests_define_accessors();
 
1572
 
1598
1573
    if (instance->empty_stacks_fill == FCS_ES_FILLED_BY_NONE)
1599
1574
    {
1600
1575
        return FCS_STATE_IS_NOT_SOLVEABLE;
1603
1578
    state_stacks_num = instance->stacks_num;
1604
1579
    state_freecells_num = instance->freecells_num;
1605
1580
 
1606
 
    fcs_move_stack_alloc_into_var(moves);
1607
 
 
1608
 
 
1609
1581
 
1610
1582
    /* Now, let's try to empty an entire stack into the freecells, so other cards can
1611
1583
     * inhabit it */
1620
1592
                /* We can empty it */
1621
1593
 
1622
1594
                sfs_check_state_begin()
1623
 
 
1624
 
                fcs_move_stack_reset(moves);
 
1595
                
 
1596
                my_copy_stack(stack);
1625
1597
 
1626
1598
                for(c=0;c<cards_num;c++)
1627
1599
                {
1648
1620
        }
1649
1621
    }
1650
1622
 
1651
 
    fcs_move_stack_destroy(moves);
1652
 
 
1653
1623
    return FCS_STATE_IS_NOT_SOLVEABLE;
1654
1624
 
1655
1625
}
1657
1627
int freecell_solver_sfs_yukon_do_nothing(
1658
1628
        freecell_solver_soft_thread_t * soft_thread,
1659
1629
        fcs_state_with_locations_t * ptr_state_with_locations,
1660
 
        int depth,
1661
1630
        int num_freestacks,
1662
1631
        int num_freecells,
1663
1632
        fcs_derived_states_list_t * derived_states_list,
1670
1639
int freecell_solver_sfs_yukon_move_card_to_parent(
1671
1640
        freecell_solver_soft_thread_t * soft_thread,
1672
1641
        fcs_state_with_locations_t * ptr_state_with_locations,
1673
 
        int depth,
1674
1642
        int num_freestacks,
1675
1643
        int num_freecells,
1676
1644
        fcs_derived_states_list_t * derived_states_list,
1677
1645
        int reparent
1678
1646
        )
1679
1647
{
1680
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
1681
 
    freecell_solver_instance_t * instance = hard_thread->instance;
 
1648
    tests_declare_accessors();
1682
1649
 
1683
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
1684
1650
    int check;
1685
1651
 
1686
1652
    int stack, cards_num, c, a, ds;
1691
1657
    int state_stacks_num;
1692
1658
    int sequences_are_built_by;
1693
1659
 
1694
 
    fcs_move_stack_t * moves;
1695
1660
    fcs_move_t temp_move;
1696
1661
 
 
1662
    tests_define_accessors();
 
1663
 
1697
1664
    state_stacks_num = instance->stacks_num;
1698
1665
    sequences_are_built_by = instance->sequences_are_built_by;
1699
1666
 
1700
 
    fcs_move_stack_alloc_into_var(moves);
1701
 
 
1702
1667
    for( ds=0 ; ds < state_stacks_num ; ds++ )
1703
1668
    {
1704
1669
        dest_cards_num = fcs_stack_len(state, ds);
1729
1694
                            /* Let's move it */
1730
1695
                            sfs_check_state_begin();
1731
1696
 
1732
 
                            fcs_move_stack_reset(moves);
 
1697
                            my_copy_stack(stack);
 
1698
                            my_copy_stack(ds);
1733
1699
 
1734
1700
                            fcs_move_sequence(ds, stack, c, cards_num-1, a);
1735
1701
 
1744
1710
        }
1745
1711
    }
1746
1712
 
1747
 
    fcs_move_stack_destroy(moves);
1748
 
 
1749
1713
    return FCS_STATE_IS_NOT_SOLVEABLE;
1750
1714
}
1751
1715
 
1752
1716
int freecell_solver_sfs_yukon_move_kings_to_empty_stack(
1753
1717
        freecell_solver_soft_thread_t * soft_thread,
1754
1718
        fcs_state_with_locations_t * ptr_state_with_locations,
1755
 
        int depth,
1756
1719
        int num_freestacks,
1757
1720
        int num_freecells,
1758
1721
        fcs_derived_states_list_t * derived_states_list,
1759
1722
        int reparent
1760
1723
        )
1761
1724
{
1762
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
1763
 
    freecell_solver_instance_t * instance = hard_thread->instance;
 
1725
    tests_declare_accessors();
1764
1726
 
1765
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
1766
1727
    int check;
1767
1728
 
1768
1729
    int stack, cards_num, c, a, ds;
1770
1731
 
1771
1732
    int state_stacks_num;
1772
1733
 
1773
 
    fcs_move_stack_t * moves;
1774
1734
    fcs_move_t temp_move;
1775
1735
 
 
1736
    tests_define_accessors();
 
1737
 
1776
1738
    if (num_freestacks == 0)
1777
1739
    {
1778
1740
        return FCS_STATE_IS_NOT_SOLVEABLE;
1780
1742
 
1781
1743
    state_stacks_num = instance->stacks_num;
1782
1744
 
1783
 
    fcs_move_stack_alloc_into_var(moves);
1784
 
 
1785
 
 
1786
1745
    for( stack=0 ; stack < state_stacks_num ; stack++)
1787
1746
    {
1788
1747
        cards_num = fcs_stack_len(state, stack);
1798
1757
                /* It's a King - so let's move it */
1799
1758
                sfs_check_state_begin();
1800
1759
 
1801
 
                fcs_move_stack_reset(moves);
1802
1760
 
1803
1761
                for( ds=0 ; ds < state_stacks_num ; ds++)
1804
1762
                {
1807
1765
                        break;
1808
1766
                    }
1809
1767
                }
 
1768
                my_copy_stack(stack);
 
1769
                my_copy_stack(ds);
1810
1770
                fcs_move_sequence(ds, stack, c, cards_num-1, a);
1811
1771
 
1812
1772
 
1817
1777
        }
1818
1778
    }
1819
1779
 
1820
 
    fcs_move_stack_destroy(moves);
1821
 
 
1822
1780
    return FCS_STATE_IS_NOT_SOLVEABLE;
1823
1781
}
1824
1782
 
1832
1790
int freecell_solver_sfs_deal_gypsy_talon(
1833
1791
        freecell_solver_soft_thread_t * soft_thread,
1834
1792
        fcs_state_with_locations_t * ptr_state_with_locations,
1835
 
        int depth,
1836
1793
        int num_freestacks,
1837
1794
        int num_freecells,
1838
1795
        fcs_derived_states_list_t * derived_states_list,
1839
1796
        int reparent
1840
1797
        )
1841
1798
{
1842
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
1843
 
    freecell_solver_instance_t * instance = hard_thread->instance;
 
1799
    tests_declare_accessors();
1844
1800
 
1845
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
1846
1801
    int check;
1847
1802
 
1848
1803
    fcs_card_t temp_card;
1849
1804
    int a;
1850
1805
 
1851
 
    fcs_move_stack_t * moves;
1852
1806
    fcs_move_t temp_move;
1853
1807
 
 
1808
    tests_define_accessors();
 
1809
 
1854
1810
    if (instance->talon_type != FCS_TALON_GYPSY)
1855
1811
    {
1856
1812
        return FCS_STATE_IS_NOT_SOLVEABLE;
1857
1813
    }
1858
1814
 
1859
 
    fcs_move_stack_alloc_into_var(moves);
 
1815
    moves = hard_thread->reusable_move_stack;
 
1816
    indirect_stacks_buffer = hard_thread->indirect_stacks_buffer;
1860
1817
 
1861
1818
    if (fcs_talon_pos(state) < fcs_talon_len(state))
1862
1819
    {
1873
1830
        sfs_check_state_end()
1874
1831
    }
1875
1832
 
1876
 
    fcs_move_stack_destroy(moves);
1877
 
 
1878
1833
    return FCS_STATE_IS_NOT_SOLVEABLE;
1879
1834
}
1880
1835
 
1882
1837
int freecell_solver_sfs_get_card_from_klondike_talon(
1883
1838
        freecell_solver_soft_thread_t * soft_thread,
1884
1839
        fcs_state_with_locations_t * ptr_state_with_locations,
1885
 
        int depth,
1886
1840
        int num_freestacks,
1887
1841
        int num_freecells,
1888
1842
        fcs_derived_states_list_t * derived_states_list,
1889
1843
        int reparent
1890
1844
        )
1891
1845
{
1892
 
    freecell_solver_hard_thread_t * hard_thread = soft_thread->hard_thread;
1893
 
    freecell_solver_instance_t * instance = hard_thread->instance;
1894
 
 
1895
 
 
1896
 
    fcs_state_with_locations_t * ptr_new_state_with_locations;
 
1846
    tests_declare_accessors();
 
1847
 
 
1848
 
1897
1849
    fcs_state_with_locations_t * talon_temp;
1898
1850
 
1899
 
    fcs_move_stack_t * moves;
1900
1851
    fcs_move_t temp_move;
1901
1852
 
1902
1853
    int check;
1907
1858
    int cards_num;
1908
1859
    int a;
1909
1860
 
 
1861
    tests_define_accessors();
 
1862
 
1910
1863
    if (instance->talon_type != FCS_TALON_KLONDIKE)
1911
1864
    {
1912
1865
        return FCS_STATE_IS_NOT_SOLVEABLE;
1913
1866
    }
1914
1867
 
1915
 
    fcs_move_stack_alloc_into_var(moves);
1916
 
 
1917
1868
    /* Duplicate the talon and its parameters into talon_temp */
1918
1869
    talon_temp = malloc(sizeof(fcs_state_with_locations_t));
1919
1870
    talon_temp->s.talon = malloc(fcs_klondike_talon_len(state)+1);
2021
1972
    free(talon_temp->s.talon);
2022
1973
    free(talon_temp);
2023
1974
 
2024
 
    fcs_move_stack_destroy(moves);
2025
 
 
2026
1975
    return FCS_STATE_IS_NOT_SOLVEABLE;
2027
1976
 
2028
1977
}
2029
 
 
2030
 
 
 
1978
       
2031
1979
#endif
2032
1980
 
 
1981
int freecell_solver_sfs_atomic_move_card_to_empty_stack(
 
1982
        freecell_solver_soft_thread_t * soft_thread,
 
1983
        fcs_state_with_locations_t * ptr_state_with_locations,
 
1984
        int num_freestacks,
 
1985
        int num_freecells,
 
1986
        fcs_derived_states_list_t * derived_states_list,
 
1987
        int reparent
 
1988
        )
 
1989
{
 
1990
    tests_declare_accessors();
 
1991
    int empty_stacks_filled_by, state_stacks_num;
 
1992
    int stack, cards_num;
 
1993
    fcs_card_t card, temp_card;
 
1994
    fcs_move_t temp_move;
 
1995
    int check;
 
1996
    int empty_stack_idx;
 
1997
 
 
1998
    tests_define_accessors();
 
1999
 
 
2000
    if (num_freestacks == 0)
 
2001
    {
 
2002
        return FCS_STATE_IS_NOT_SOLVEABLE;
 
2003
    }
 
2004
 
 
2005
    state_stacks_num = instance->stacks_num;
 
2006
 
 
2007
    for(empty_stack_idx=0;empty_stack_idx<state_stacks_num;empty_stack_idx++)
 
2008
    {
 
2009
        if (fcs_stack_len(state, empty_stack_idx) == 0)
 
2010
        {
 
2011
            break;
 
2012
        }
 
2013
    }
 
2014
 
 
2015
    empty_stacks_filled_by = instance->empty_stacks_fill;
 
2016
 
 
2017
    if (empty_stacks_filled_by == FCS_ES_FILLED_BY_NONE)
 
2018
    {
 
2019
        return FCS_STATE_IS_NOT_SOLVEABLE;
 
2020
    }
 
2021
 
 
2022
    
 
2023
 
 
2024
    for(stack=0;stack<state_stacks_num;stack++)
 
2025
    {
 
2026
        cards_num = fcs_stack_len(state, stack);
 
2027
        if (cards_num > 0)
 
2028
        {
 
2029
            card = fcs_stack_card(state, stack, cards_num-1);
 
2030
            if ((empty_stacks_filled_by == FCS_ES_FILLED_BY_KINGS_ONLY) &&
 
2031
                (fcs_card_card_num(card) != 13))
 
2032
            {
 
2033
                continue;
 
2034
            }
 
2035
            /* Let's move it */
 
2036
            {
 
2037
                sfs_check_state_begin();
 
2038
 
 
2039
                my_copy_stack(stack);
 
2040
 
 
2041
                fcs_pop_stack_card(new_state, stack, temp_card);
 
2042
                
 
2043
 
 
2044
                my_copy_stack(empty_stack_idx);
 
2045
 
 
2046
                fcs_push_card_into_stack(new_state, empty_stack_idx, card);
 
2047
 
 
2048
                fcs_move_set_type(temp_move, FCS_MOVE_TYPE_STACK_TO_STACK);
 
2049
                fcs_move_set_src_stack(temp_move, stack);
 
2050
                fcs_move_set_dest_stack(temp_move, empty_stack_idx);
 
2051
                fcs_move_set_num_cards_in_seq(temp_move, 1);
 
2052
 
 
2053
                fcs_move_stack_push(moves, temp_move);
 
2054
 
 
2055
                fcs_move_set_type(temp_move,FCS_MOVE_TYPE_CANONIZE);
 
2056
                fcs_move_stack_push(moves, temp_move);
 
2057
                
 
2058
                sfs_check_state_end()                
 
2059
            }
 
2060
        }
 
2061
    }
 
2062
 
 
2063
    return FCS_STATE_IS_NOT_SOLVEABLE;
 
2064
}
 
2065
 
 
2066
int freecell_solver_sfs_atomic_move_card_to_parent(
 
2067
        freecell_solver_soft_thread_t * soft_thread,
 
2068
        fcs_state_with_locations_t * ptr_state_with_locations,
 
2069
        int num_freestacks,
 
2070
        int num_freecells,
 
2071
        fcs_derived_states_list_t * derived_states_list,
 
2072
        int reparent
 
2073
        )
 
2074
{
 
2075
    tests_declare_accessors();
 
2076
    int state_stacks_num;
 
2077
    int stack, cards_num, ds, ds_cards_num;
 
2078
    fcs_card_t card, dest_card, temp_card;
 
2079
    fcs_move_t temp_move;
 
2080
    int check;
 
2081
    int sequences_are_built_by;
 
2082
 
 
2083
    tests_define_accessors();
 
2084
 
 
2085
    state_stacks_num = instance->stacks_num;
 
2086
 
 
2087
    sequences_are_built_by = instance->sequences_are_built_by;
 
2088
    
 
2089
 
 
2090
    for(stack=0;stack<state_stacks_num;stack++)
 
2091
    {
 
2092
        cards_num = fcs_stack_len(state, stack);
 
2093
        if (cards_num > 0)
 
2094
        {
 
2095
            card = fcs_stack_card(state, stack, cards_num-1);
 
2096
            
 
2097
            for(ds=0;ds<state_stacks_num;ds++)
 
2098
            {
 
2099
                if (ds == stack)
 
2100
                {
 
2101
                    continue;
 
2102
                }
 
2103
 
 
2104
                ds_cards_num = fcs_stack_len(state, ds);
 
2105
                if (ds_cards_num > 0)
 
2106
                {
 
2107
                    dest_card = fcs_stack_card(state, ds, ds_cards_num-1);
 
2108
                    if (fcs_is_parent_card(card, dest_card))
 
2109
                    {
 
2110
                        /* Let's move it */
 
2111
                        {
 
2112
                            sfs_check_state_begin();
 
2113
 
 
2114
                            my_copy_stack(stack);
 
2115
                            my_copy_stack(ds);
 
2116
 
 
2117
                            fcs_pop_stack_card(new_state, stack, temp_card);
 
2118
                            
 
2119
                            fcs_push_card_into_stack(new_state, ds, card);
 
2120
 
 
2121
                            fcs_move_set_type(temp_move, FCS_MOVE_TYPE_STACK_TO_STACK);
 
2122
                            fcs_move_set_src_stack(temp_move, stack);
 
2123
                            fcs_move_set_dest_stack(temp_move, ds);
 
2124
                            fcs_move_set_num_cards_in_seq(temp_move, 1);
 
2125
 
 
2126
                            fcs_move_stack_push(moves, temp_move);
 
2127
 
 
2128
                            fcs_move_set_type(temp_move,FCS_MOVE_TYPE_CANONIZE);
 
2129
                            fcs_move_stack_push(moves, temp_move);
 
2130
                            
 
2131
                            sfs_check_state_end()                
 
2132
                        }
 
2133
                    }
 
2134
                }
 
2135
            }
 
2136
        }
 
2137
    }
 
2138
 
 
2139
    return FCS_STATE_IS_NOT_SOLVEABLE;
 
2140
}
 
2141
 
 
2142
int freecell_solver_sfs_atomic_move_card_to_freecell(
 
2143
        freecell_solver_soft_thread_t * soft_thread,
 
2144
        fcs_state_with_locations_t * ptr_state_with_locations,
 
2145
        int num_freestacks,
 
2146
        int num_freecells,
 
2147
        fcs_derived_states_list_t * derived_states_list,
 
2148
        int reparent
 
2149
        )
 
2150
{
 
2151
    tests_declare_accessors();
 
2152
    int state_stacks_num;
 
2153
    int state_freecells_num;
 
2154
    int stack, cards_num, ds;
 
2155
    fcs_card_t card, temp_card;
 
2156
    fcs_move_t temp_move;
 
2157
    int check;
 
2158
    int sequences_are_built_by;
 
2159
 
 
2160
    tests_define_accessors();
 
2161
 
 
2162
    state_stacks_num = instance->stacks_num;
 
2163
    state_freecells_num = instance->freecells_num;
 
2164
 
 
2165
    sequences_are_built_by = instance->sequences_are_built_by;
 
2166
 
 
2167
    if (num_freecells == 0)
 
2168
    {
 
2169
        return FCS_STATE_IS_NOT_SOLVEABLE;
 
2170
    }
 
2171
 
 
2172
    for(ds=0;ds<state_freecells_num;ds++)
 
2173
    {
 
2174
        if (fcs_freecell_card_num(state, ds) == 0)
 
2175
        {
 
2176
            break;
 
2177
        }
 
2178
    }
 
2179
    
 
2180
    
 
2181
 
 
2182
    for(stack=0;stack<state_stacks_num;stack++)
 
2183
    {
 
2184
        cards_num = fcs_stack_len(state, stack);
 
2185
        if (cards_num > 0)
 
2186
        {
 
2187
            card = fcs_stack_card(state, stack, cards_num-1);
 
2188
 
 
2189
            /* Let's move it */
 
2190
            {
 
2191
                sfs_check_state_begin();
 
2192
 
 
2193
                my_copy_stack(stack);
 
2194
 
 
2195
                fcs_pop_stack_card(new_state, stack, temp_card);
 
2196
                
 
2197
                fcs_put_card_in_freecell(new_state, ds, card);
 
2198
 
 
2199
                fcs_move_set_type(temp_move, FCS_MOVE_TYPE_STACK_TO_FREECELL);
 
2200
                fcs_move_set_src_stack(temp_move, stack);
 
2201
                fcs_move_set_dest_freecell(temp_move, ds);
 
2202
 
 
2203
                fcs_move_stack_push(moves, temp_move);
 
2204
 
 
2205
                fcs_move_set_type(temp_move,FCS_MOVE_TYPE_CANONIZE);
 
2206
                fcs_move_stack_push(moves, temp_move);
 
2207
                
 
2208
                sfs_check_state_end()                
 
2209
            }
 
2210
        }
 
2211
    }
 
2212
 
 
2213
    return FCS_STATE_IS_NOT_SOLVEABLE;
 
2214
}
 
2215
 
 
2216
int freecell_solver_sfs_atomic_move_freecell_card_to_parent(
 
2217
        freecell_solver_soft_thread_t * soft_thread,
 
2218
        fcs_state_with_locations_t * ptr_state_with_locations,
 
2219
        int num_freestacks,
 
2220
        int num_freecells,
 
2221
        fcs_derived_states_list_t * derived_states_list,
 
2222
        int reparent
 
2223
        )
 
2224
{
 
2225
    tests_declare_accessors();
 
2226
    int state_stacks_num, state_freecells_num;
 
2227
    int fc, ds, ds_cards_num;
 
2228
    fcs_card_t card, dest_card;
 
2229
    fcs_move_t temp_move;
 
2230
    int check;
 
2231
    int sequences_are_built_by;
 
2232
 
 
2233
    tests_define_accessors();
 
2234
 
 
2235
    state_stacks_num = instance->stacks_num;
 
2236
    state_freecells_num = instance->freecells_num;
 
2237
 
 
2238
    sequences_are_built_by = instance->sequences_are_built_by;
 
2239
 
 
2240
    
 
2241
    
 
2242
 
 
2243
    for(fc=0;fc<state_freecells_num;fc++)
 
2244
    {
 
2245
        card = fcs_freecell_card(state, fc);
 
2246
        if (fcs_card_card_num(card) == 0)
 
2247
        {
 
2248
            continue;
 
2249
        }
 
2250
        
 
2251
        for(ds=0;ds<state_stacks_num;ds++)
 
2252
        {
 
2253
            ds_cards_num = fcs_stack_len(state, ds);
 
2254
            if (ds_cards_num > 0)
 
2255
            {
 
2256
                dest_card = fcs_stack_card(state, ds, ds_cards_num-1);
 
2257
                if (fcs_is_parent_card(card, dest_card))
 
2258
                {
 
2259
                    /* Let's move it */
 
2260
                    {
 
2261
                        sfs_check_state_begin();
 
2262
 
 
2263
                        my_copy_stack(ds);
 
2264
 
 
2265
                        fcs_empty_freecell(new_state, fc);
 
2266
                        
 
2267
                        fcs_push_card_into_stack(new_state, ds, card);
 
2268
 
 
2269
                        fcs_move_set_type(temp_move, FCS_MOVE_TYPE_FREECELL_TO_STACK);
 
2270
                        fcs_move_set_src_freecell(temp_move, fc);
 
2271
                        fcs_move_set_dest_stack(temp_move, ds);
 
2272
                        fcs_move_set_num_cards_in_seq(temp_move, 1);
 
2273
 
 
2274
                        fcs_move_stack_push(moves, temp_move);
 
2275
 
 
2276
                        fcs_move_set_type(temp_move,FCS_MOVE_TYPE_CANONIZE);
 
2277
                        fcs_move_stack_push(moves, temp_move);
 
2278
                        
 
2279
                        sfs_check_state_end()                
 
2280
                    }
 
2281
                }
 
2282
            }
 
2283
        }
 
2284
    }
 
2285
 
 
2286
    return FCS_STATE_IS_NOT_SOLVEABLE;
 
2287
}
 
2288
 
 
2289
int freecell_solver_sfs_atomic_move_freecell_card_to_empty_stack(
 
2290
        freecell_solver_soft_thread_t * soft_thread,
 
2291
        fcs_state_with_locations_t * ptr_state_with_locations,
 
2292
        int num_freestacks,
 
2293
        int num_freecells,
 
2294
        fcs_derived_states_list_t * derived_states_list,
 
2295
        int reparent
 
2296
        )
 
2297
{
 
2298
    tests_declare_accessors();
 
2299
    int state_stacks_num, state_freecells_num;
 
2300
    int fc, ds;
 
2301
    fcs_card_t card;
 
2302
    fcs_move_t temp_move;
 
2303
    int check;
 
2304
    int sequences_are_built_by, empty_stacks_filled_by;
 
2305
 
 
2306
    tests_define_accessors();
 
2307
 
 
2308
    moves = hard_thread->reusable_move_stack;
 
2309
    indirect_stacks_buffer = hard_thread->indirect_stacks_buffer;
 
2310
 
 
2311
    state_stacks_num = instance->stacks_num;
 
2312
    state_freecells_num = instance->freecells_num;
 
2313
 
 
2314
    sequences_are_built_by = instance->sequences_are_built_by;
 
2315
 
 
2316
    if (num_freestacks == 0)
 
2317
    {
 
2318
        return FCS_STATE_IS_NOT_SOLVEABLE;
 
2319
    }
 
2320
 
 
2321
    empty_stacks_filled_by = instance->empty_stacks_fill;
 
2322
 
 
2323
    if (empty_stacks_filled_by == FCS_ES_FILLED_BY_NONE)
 
2324
    {
 
2325
        return FCS_STATE_IS_NOT_SOLVEABLE;
 
2326
    }
 
2327
    
 
2328
    for(ds=0;ds<state_stacks_num;ds++)
 
2329
    {
 
2330
        if (fcs_stack_len(state, ds) == 0)
 
2331
        {
 
2332
            break;
 
2333
        }
 
2334
    }
 
2335
 
 
2336
    for(fc=0;fc<state_freecells_num;fc++)
 
2337
    {
 
2338
        card = fcs_freecell_card(state, fc);
 
2339
        if (fcs_card_card_num(card) == 0)
 
2340
        {
 
2341
            continue;
 
2342
        }
 
2343
 
 
2344
        if ((empty_stacks_filled_by == FCS_ES_FILLED_BY_KINGS_ONLY) &&
 
2345
            (fcs_card_card_num(card) != 13))
 
2346
        {
 
2347
            continue;
 
2348
        }        
 
2349
 
 
2350
        {
 
2351
            sfs_check_state_begin();
 
2352
 
 
2353
            my_copy_stack(ds);
 
2354
 
 
2355
            fcs_empty_freecell(new_state, fc);
 
2356
            
 
2357
            fcs_push_card_into_stack(new_state, ds, card);
 
2358
 
 
2359
            fcs_move_set_type(temp_move, FCS_MOVE_TYPE_FREECELL_TO_STACK);
 
2360
            fcs_move_set_src_freecell(temp_move, fc);
 
2361
            fcs_move_set_dest_stack(temp_move, ds);
 
2362
            fcs_move_set_num_cards_in_seq(temp_move, 1);
 
2363
 
 
2364
            fcs_move_stack_push(moves, temp_move);
 
2365
 
 
2366
            fcs_move_set_type(temp_move,FCS_MOVE_TYPE_CANONIZE);
 
2367
            fcs_move_stack_push(moves, temp_move);
 
2368
            
 
2369
            sfs_check_state_end()                
 
2370
        }
 
2371
    }
 
2372
 
 
2373
    return FCS_STATE_IS_NOT_SOLVEABLE;
 
2374
}
 
2375
 
 
2376
 
2033
2377
#undef state_with_locations
2034
2378
#undef state
2035
2379
#undef new_state_with_locations
2056
2400
    freecell_solver_sfs_simple_simon_move_sequence_with_junk_seq_above_to_true_parent_with_some_cards_above,
2057
2401
    freecell_solver_sfs_simple_simon_move_whole_stack_sequence_to_false_parent_with_some_cards_above,
2058
2402
    freecell_solver_sfs_simple_simon_move_sequence_to_parent_on_the_same_stack,
 
2403
    freecell_solver_sfs_atomic_move_card_to_empty_stack,
 
2404
    freecell_solver_sfs_atomic_move_card_to_parent,
 
2405
    freecell_solver_sfs_atomic_move_card_to_freecell,
 
2406
    freecell_solver_sfs_atomic_move_freecell_card_to_parent,
 
2407
    freecell_solver_sfs_atomic_move_freecell_card_to_empty_stack,
 
2408
#if 0
2059
2409
    freecell_solver_sfs_move_top_stack_cards_to_founds,
2060
2410
    freecell_solver_sfs_yukon_move_card_to_parent,
2061
2411
    freecell_solver_sfs_yukon_move_kings_to_empty_stack,
2062
2412
    freecell_solver_sfs_yukon_do_nothing,
2063
2413
    freecell_solver_sfs_yukon_do_nothing,
 
2414
#endif
2064
2415
    freecell_solver_sfs_yukon_do_nothing,
2065
2416
    freecell_solver_sfs_yukon_do_nothing
2066
2417
#ifdef FCS_WITH_TALONS