summaryrefslogtreecommitdiffstats
path: root/kpat/freecell-solver/tests.h
blob: fbf7826219dd0f47c58f3a549ce63ea5e1bd57ae (plain)
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
/*
 * fcs.h - header file of the test functions for Freecell Solver.
 *
 * The test functions code is found in freecell.c
 *
 * Written by Shlomi Fish ([email protected]), 2000
 *
 * This file is in the public domain (it's uncopyrighted).
 */

#ifndef FC_SOLVE__TESTS_H
#define FC_SOLVE__TESTS_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>
#include <limits.h>
#include <string.h>

#include "fcs_isa.h"
#include "fcs.h"

#include "test_arr.h"


/*
 * The number of cards that can be moved is
 * (freecells_number + 1) * 2 ^ (free_stacks_number)
 *
 * See the Freecell FAQ and the source code of PySol
 *
 * */
#define calc_max_sequence_move(fc_num, fs_num)                    \
    ((instance->empty_stacks_fill == FCS_ES_FILLED_BY_ANY_CARD) ? \
        (                                                         \
            (instance->unlimited_sequence_move) ?                 \
                INT_MAX :                                         \
                (((fc_num)+1)<<(fs_num))                          \
        ) :                                                       \
        ((fc_num)+1)                                              \
    )

#include "caas.h"

/*
 *  These are some macros to make it easier for the programmer.
 * */
#define state_with_locations (*ptr_state_with_locations)
#define state (ptr_state_with_locations->s)
#define new_state_with_locations (*ptr_new_state_with_locations)
#define new_state (ptr_new_state_with_locations->s)

#define sfs_check_state_begin()                                                \
    fcs_state_ia_alloc_into_var(ptr_new_state_with_locations, hard_thread);    \
    fcs_duplicate_state(new_state_with_locations, state_with_locations);       \
    /* Some A* and BFS parameters that need to be initialized in               \
     * the derived state.                                                      \
     * */                                                                      \
    ptr_new_state_with_locations->tqparent = ptr_state_with_locations;           \
    ptr_new_state_with_locations->moves_to_parent = moves;                     \
    /* Make sure depth is consistent with the game graph.                      \
     * I.e: the depth of every newly discovered state is derived from          \
     * the state from which it was discovered. */                              \
    ptr_new_state_with_locations->depth = ptr_state_with_locations->depth + 1; \
    /* Mark this state as a state that was not yet visited */                  \
    ptr_new_state_with_locations->visited = 0;                                 \
    /* It's a newly created state which does not have tqchildren yet. */         \
    ptr_new_state_with_locations->num_active_tqchildren = 0;                     \
    memset(ptr_new_state_with_locations->scan_visited, '\0',                   \
        sizeof(ptr_new_state_with_locations->scan_visited)                     \
        );                                                                     \
    fcs_move_stack_reset(moves);                                               \

    
    

#define sfs_check_state_end()                                             \
/* The last move in a move stack should be FCS_MOVE_TYPE_CANONIZE         \
 * because it indicates that the order of the stacks and freecells        \
 * need to be recalculated                                                \
 * */                                                                     \
fcs_move_set_type(temp_move,FCS_MOVE_TYPE_CANONIZE);                      \
fcs_move_stack_push(moves, temp_move);                                    \
                                                                          \
{                                                                         \
    fcs_state_with_locations_t * existing_state;                          \
    check = freecell_solver_check_and_add_state(                          \
        soft_thread,                                                      \
        ptr_new_state_with_locations,                                     \
        &existing_state                                                   \
        );                                                                \
    if ((check == FCS_STATE_BEGIN_SUSPEND_PROCESS) ||                     \
        (check == FCS_STATE_SUSPEND_PROCESS))                             \
    {                                                                     \
        /* This state is not going to be used, so                         \
         * let's clean it. */                                             \
        fcs_state_ia_release(hard_thread);                                \
        return check;                                                     \
    }                                                                     \
    else if (check == FCS_STATE_ALREADY_EXISTS)                           \
    {                                                                     \
        fcs_state_ia_release(hard_thread);                                \
        calculate_real_depth(existing_state);                             \
        /* Re-tqparent the existing state to this one.                      \
         *                                                                \
         * What it means is that if the depth of the state if it          \
         * can be reached from this one is lower than what it             \
         * already have, then re-assign its tqparent to this state.         \
         * */                                                             \
        if (reparent &&                                                   \
           (existing_state->depth > ptr_state_with_locations->depth+1))   \
        {                                                                 \
            /* Make a copy of "moves" because "moves" will be destroyed */\
            existing_state->moves_to_parent =                             \
                freecell_solver_move_stack_compact_allocate(              \
                    hard_thread, moves                                    \
                    );                                                    \
            if (!(existing_state->visited & FCS_VISITED_DEAD_END))        \
            {                                                             \
                if ((--existing_state->tqparent->num_active_tqchildren) == 0) \
                {                                                         \
                    mark_as_dead_end(                                     \
                        existing_state->tqparent                            \
                        );                                                \
                }                                                         \
                ptr_state_with_locations->num_active_tqchildren++;          \
            }                                                             \
            existing_state->tqparent = ptr_state_with_locations;            \
            existing_state->depth = ptr_state_with_locations->depth + 1;  \
        }                                                                 \
        fcs_derived_states_list_add_state(                                \
            derived_states_list,                                          \
            existing_state                                                \
            );                                                            \
    }                                                                     \
    else                                                                  \
    {                                                                     \
        fcs_derived_states_list_add_state(                                \
            derived_states_list,                                          \
            ptr_new_state_with_locations                                  \
            );                                                            \
   }                                                                      \
}


/*
    This macro checks if the top card in the stack is a flipped card
    , and if so flips it so its face is up.
  */
#define fcs_flip_top_card(stack)                                   \
{                                                                  \
    int cards_num;                                                 \
    cards_num = fcs_stack_len(new_state,stack);                    \
                                                                   \
    if (cards_num > 0)                                             \
    {                                                              \
        if (fcs_card_get_flipped(                                  \
                fcs_stack_card(                                    \
                    new_state,                                     \
                    stack,                                         \
                    cards_num-1)                                   \
                ) == 1                                             \
           )                                                       \
        {                                                          \
            fcs_flip_stack_card(new_state,stack,cards_num-1);      \
            fcs_move_set_type(temp_move, FCS_MOVE_TYPE_FLIP_CARD); \
            fcs_move_set_src_stack(temp_move, stack);              \
                                                                   \
            fcs_move_stack_push(moves, temp_move);                 \
        }                                                          \
    }                                                              \
}


/*
 * dest is the destination stack
 * source is the source stack
 * start is the start height
 * end is the end height
 * a is the iterator
 * */
#define fcs_move_sequence(dest, source, start, end, a)              \
{                                                                   \
    for ( a = (start) ; a <= (end) ; a++)                           \
    {                                                               \
        fcs_push_stack_card_into_stack(new_state, dest, source, a); \
    }                                                               \
                                                                    \
    for ( a = (start) ; a <= (end) ; a++)                           \
    {                                                               \
        fcs_pop_stack_card(new_state, source, temp_card);           \
    }                                                               \
                                                                    \
    fcs_move_set_type(temp_move, FCS_MOVE_TYPE_STACK_TO_STACK);     \
    fcs_move_set_src_stack(temp_move, source);                      \
    fcs_move_set_dest_stack(temp_move, dest);                       \
    fcs_move_set_num_cards_in_seq(temp_move, (end)-(start)+1);      \
                                                                    \
    fcs_move_stack_push(moves, temp_move);                          \
}

/*
 * This test declares a few access variables that are used in all
 * the tests.
 * */
#define tests_declare_accessors()                              \
    freecell_solver_hard_thread_t * hard_thread;               \
    freecell_solver_instance_t * instance;                     \
    fcs_state_with_locations_t * ptr_new_state_with_locations; \
    fcs_move_stack_t * moves;                                  \
    char * indirect_stacks_buffer;                             \
    int calc_real_depth;                                       \
    int scans_synergy

/*
 * This macro defines these accessors to have some value.
 * */
#define tests_define_accessors()                                  \
    hard_thread = soft_thread->hard_thread;                       \
    instance = hard_thread->instance;                             \
    moves = hard_thread->reusable_move_stack;                     \
    indirect_stacks_buffer = hard_thread->indirect_stacks_buffer; \
    calc_real_depth = instance->calc_real_depth;                  \
    scans_synergy = instance->scans_synergy;



extern int freecell_solver_sfs_simple_simon_move_sequence_to_founds(
        freecell_solver_soft_thread_t * soft_thread,
        fcs_state_with_locations_t * ptr_state_with_locations,
        int num_freestacks,
        int num_freecells,
        fcs_derived_states_list_t * derived_states_list,
        int reparent
        );
extern int freecell_solver_sfs_simple_simon_move_sequence_to_true_parent(
        freecell_solver_soft_thread_t * soft_thread,
        fcs_state_with_locations_t * ptr_state_with_locations,
        int num_freestacks,
        int num_freecells,
        fcs_derived_states_list_t * derived_states_list,
        int reparent
        );

extern int freecell_solver_sfs_simple_simon_move_whole_stack_sequence_to_false_parent(
        freecell_solver_soft_thread_t * soft_thread,
        fcs_state_with_locations_t * ptr_state_with_locations,
        int num_freestacks,
        int num_freecells,
        fcs_derived_states_list_t * derived_states_list,
        int reparent
        );

extern int freecell_solver_sfs_simple_simon_move_sequence_to_true_parent_with_some_cards_above(
        freecell_solver_soft_thread_t * soft_thread,
        fcs_state_with_locations_t * ptr_state_with_locations,
        int num_freestacks,
        int num_freecells,
        fcs_derived_states_list_t * derived_states_list,
        int reparent
        );

extern int freecell_solver_sfs_simple_simon_move_sequence_with_some_cards_above_to_true_parent(
        freecell_solver_soft_thread_t * soft_thread,
        fcs_state_with_locations_t * ptr_state_with_locations,
        int num_freestacks,
        int num_freecells,
        fcs_derived_states_list_t * derived_states_list,
        int reparent
        );

extern int freecell_solver_sfs_simple_simon_move_sequence_with_junk_seq_above_to_true_parent_with_some_cards_above(
        freecell_solver_soft_thread_t * soft_thread,
        fcs_state_with_locations_t * ptr_state_with_locations,
        int num_freestacks,
        int num_freecells,
        fcs_derived_states_list_t * derived_states_list,
        int reparent
        );

extern int freecell_solver_sfs_simple_simon_move_whole_stack_sequence_to_false_parent_with_some_cards_above(
        freecell_solver_soft_thread_t * soft_thread,
        fcs_state_with_locations_t * ptr_state_with_locations,
        int num_freestacks,
        int num_freecells,
        fcs_derived_states_list_t * derived_states_list,
        int reparent
        );

extern int freecell_solver_sfs_simple_simon_move_sequence_to_parent_on_the_same_stack(
        freecell_solver_soft_thread_t * soft_thread,
        fcs_state_with_locations_t * ptr_state_with_locations,
        int num_freestacks,
        int num_freecells,
        fcs_derived_states_list_t * derived_states_list,
        int reparent
        );

#ifdef __cplusplus
}
#endif

#define my_copy_stack(idx) fcs_copy_stack(new_state_with_locations, idx, indirect_stacks_buffer);

#endif /* FC_SOLVE__TESTS_H */