45
45
General use of this interface:
46
1. freecell_solver_alloc_instance()
46
1. fc_solve_alloc_instance()
47
47
2. Set the parameters of the game
48
48
3. If you wish to revert, go to step #11.
49
4. freecell_solver_init_instance()
50
5. Call freecell_solver_solve_instance() with the initial board.
49
4. fc_solve_init_instance()
50
5. Call fc_solve_solve_instance() with the initial board.
51
51
6. If it returns FCS_STATE_SUSPEND_PROCESS and you wish to proceed,
52
52
then increase the iteration limit and call
53
freecell_solver_resume_instance().
53
fc_solve_resume_instance().
54
54
7. Repeat Step #6 zero or more times.
55
55
8. If the last call to solve_instance() or resume_instance() returned
56
56
FCS_STATE_SUSPEND_PROCESS then call
57
freecell_solver_unresume_instance().
57
fc_solve_unresume_instance().
58
58
9. If the solving was successful you can use the move stacks or the
59
59
intermediate stacks. (Just don't destory them in any way).
60
10. Call freecell_solver_finish_instance().
61
11. Call freecell_solver_free_instance().
60
10. Call fc_solve_finish_instance().
61
11. Call fc_solve_free_instance().
63
63
The library functions inside lib.c (a.k.a fcs_user()) give an
64
64
easier approach for embedding Freecell Solver into your library. The
867
867
optimization_thread->ht_max_num_times = -1;
870
freecell_solver_a_star_or_bfs_do_solve_or_resume(
870
fc_solve_a_star_or_bfs_do_solve_or_resume(
871
871
optimization_thread->soft_threads[0],
872
instance->state_copy_ptr,
872
instance->state_copy_ptr_key,
873
instance->state_copy_ptr_val,
879
extern void freecell_solver_cache_talon(
880
freecell_solver_instance_t * instance,
881
fcs_state_with_locations_t * new_state
880
extern void fc_solve_cache_talon(
881
fc_solve_instance_t * instance,
882
fcs_state_t * new_state_key,
883
fcs_state_extra_info_t * new_state_val
885
887
This function starts the solution process _for the first time_. If one
886
888
wishes to proceed after the iterations limit was reached, one should
887
use freecell_solver_resume_instance.
889
use fc_solve_resume_instance.
890
int freecell_solver_solve_instance(
891
freecell_solver_instance_t * instance,
892
fcs_state_with_locations_t * init_state
892
int fc_solve_solve_instance(
893
fc_solve_instance_t * instance,
894
fcs_state_t * init_state_key,
895
fcs_state_extra_info_t * init_state_val
895
fcs_state_with_locations_t * state_copy_ptr;
898
fcs_state_t * state_copy_ptr_key;
899
fcs_state_extra_info_t * state_copy_ptr_val;
897
901
/* Allocate the first state and initialize it to init_state */
898
fcs_state_ia_alloc_into_var(state_copy_ptr, instance->hard_threads[0]);
902
fcs_state_ia_alloc_into_var(
903
state_copy_ptr_key, state_copy_ptr_val,
904
instance->hard_threads[0]
900
fcs_duplicate_state(*state_copy_ptr, *init_state);
907
fcs_duplicate_state(*state_copy_ptr_key, *state_copy_ptr_val,
908
*init_state_key, *init_state_val
904
913
for(a=0;a<instance->stacks_num;a++)
906
fcs_copy_stack(*state_copy_ptr, a, instance->hard_threads[0]->indirect_stacks_buffer);
915
fcs_copy_stack(*state_copy_ptr_key, *state_copy_ptr_val, a, instance->hard_threads[0]->indirect_stacks_buffer);
910
919
/* Initialize the state to be a base state for the game tree */
911
state_copy_ptr->depth = 0;
912
state_copy_ptr->moves_to_parent = NULL;
913
state_copy_ptr->visited = 0;
914
state_copy_ptr->parent = NULL;
915
memset(&(state_copy_ptr->scan_visited), '\0', sizeof(state_copy_ptr->scan_visited));
920
state_copy_ptr_val->depth = 0;
921
state_copy_ptr_val->moves_to_parent = NULL;
922
state_copy_ptr_val->visited = 0;
923
state_copy_ptr_val->parent_key = NULL;
924
state_copy_ptr_val->parent_val = NULL;
925
memset(&(state_copy_ptr_val->scan_visited), '\0', sizeof(state_copy_ptr_val->scan_visited));
917
instance->state_copy_ptr = state_copy_ptr;
927
instance->state_copy_ptr_key = state_copy_ptr_key;
928
instance->state_copy_ptr_val = state_copy_ptr_val;
919
930
/* Initialize the data structure that will manage the state collection */
920
931
#if (FCS_STATE_STORAGE == FCS_STATE_STORAGE_LIBREDBLACK_TREE)
921
instance->tree = rbinit(freecell_solver_state_compare_with_context, NULL);
932
instance->tree = rbinit(fc_solve_state_compare_with_context, NULL);
922
933
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_LIBAVL_AVL_TREE)
923
instance->tree = avl_create(freecell_solver_state_compare_with_context, NULL);
934
instance->tree = avl_create(fc_solve_state_compare_with_context, NULL);
924
935
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_LIBAVL_REDBLACK_TREE)
925
instance->tree = rb_create(freecell_solver_state_compare_with_context, NULL);
936
instance->tree = rb_create(fc_solve_state_compare_with_context, NULL);
926
937
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_GLIB_TREE)
927
instance->tree = g_tree_new(freecell_solver_state_compare);
938
instance->tree = g_tree_new(fc_solve_state_compare);
930
941
#if (FCS_STATE_STORAGE == FCS_STATE_STORAGE_GLIB_HASH)
931
942
instance->hash = g_hash_table_new(
932
freecell_solver_hash_function,
933
freecell_solver_state_compare_equal
943
fc_solve_hash_function,
944
fc_solve_state_compare_equal
935
946
#elif (FCS_STATE_STORAGE == FCS_STATE_STORAGE_INTERNAL_HASH)
936
instance->hash = freecell_solver_hash_init(
947
instance->hash = fc_solve_hash_init(
938
freecell_solver_state_compare_with_context,
949
fc_solve_state_compare_with_context,
1588
1612
#ifdef INDIRECT_STACK_STATES
1589
1613
#if FCS_STACK_STORAGE == FCS_STACK_STORAGE_INTERNAL_HASH
1591
freecell_solver_hash_free_with_callback(instance->stacks_hash, freecell_solver_stack_free);
1615
fc_solve_hash_free_with_callback(instance->stacks_hash, fc_solve_stack_free);
1593
freecell_solver_hash_free(instance->stacks_hash);
1617
fc_solve_hash_free(instance->stacks_hash);
1595
1619
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_LIBAVL_AVL_TREE)
1597
avl_destroy(instance->stacks_tree, freecell_solver_stack_free);
1621
avl_destroy(instance->stacks_tree, fc_solve_stack_free);
1599
1623
avl_destroy(instance->stacks_tree, NULL);
1601
1625
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_LIBAVL_REDBLACK_TREE)
1603
rb_destroy(instance->stacks_tree, freecell_solver_stack_free);
1627
rb_destroy(instance->stacks_tree, fc_solve_stack_free);
1605
1629
rb_destroy(instance->stacks_tree, NULL);
1607
1631
#elif (FCS_STACK_STORAGE == FCS_STACK_STORAGE_LIBREDBLACK_TREE)
1609
1633
rbwalk(instance->stacks_tree,
1610
freecell_solver_libredblack_walk_destroy_stack_action,
1634
fc_solve_libredblack_walk_destroy_stack_action,