1. Shlomi Fish
  2. black-hole-solitaire

Commits

Shlomi Fish  committed 78852d0

Add the --iters-display-step flag.

  • Participants
  • Parent commits c8dfd3d
  • Branches default

Comments (0)

Files changed (4)

File black-hole-solitaire/c-solver/black_hole_solver.c

View file
     int num_states_in_solution, current_state_in_solution_idx;
 
     long iterations_num, num_states_in_collection, max_iters_limit;
+    long iters_display_step;
 
     int num_columns;
     int bits_per_column;
         ret->num_states_in_collection = 0;
         ret->max_iters_limit = -1;
         ret->is_rank_reachability_prune_enabled = FALSE;
+        ret->iters_display_step = 0;
+        ret->num_columns = 0;
 
         bh_solve_hash_init(&(ret->positions));
 
     int ret_code, col_idx;
     int line_num = 1;
 
+    if (num_columns > BHS__MAX_NUM_COLUMNS)
+    {
+        return BLACK_HOLE_SOLVER__INVALID_INPUT;
+    }
+
     solver = (bhs_solver_t *)instance_proto;
 
     solver->num_columns = num_columns;
     return BLACK_HOLE_SOLVER__SUCCESS;
 }
 
+DLLEXPORT extern int black_hole_solver_set_iters_display_step(
+    black_hole_solver_instance_t * const instance_proto,
+    const long iters_display_step
+)
+{
+    if (iters_display_step < 0)
+    {
+        return BLACK_HOLE_SOLVER__INVALID_INPUT;
+    }
+    bhs_solver_t * const solver = (bhs_solver_t *)instance_proto;
+    solver->iters_display_step = iters_display_step;
+
+    return BLACK_HOLE_SOLVER__SUCCESS;
+}
+
 static GCC_INLINE void queue_item_populate_packed(
     bhs_solver_t * solver,
     bhs_queue_item_t * queue_item
     bhs_rank_t card;
     long iterations_num;
     long max_iters_limit;
+    long iters_display_step;
+    long next_iterations_display_point;
     int num_columns;
     int i;
 
     init_state = &(solver->init_state);
 
     max_iters_limit = solver->max_iters_limit;
+    iters_display_step = solver->iters_display_step;
+
+    if (iters_display_step > 0)
+    {
+        next_iterations_display_point = iters_display_step;
+    }
+    else
+    {
+        next_iterations_display_point = LONG_MAX;
+    }
 
     if (max_iters_limit < 0)
     {
                 return BLACK_HOLE_SOLVER__OUT_OF_ITERS;
             }
         }
+
+        if (iterations_num == next_iterations_display_point)
+        {
+            printf("Iteration: %ld\n", iterations_num);
+            fflush(stdout);
+            next_iterations_display_point += iters_display_step;
+        }
     }
 
     solver->iterations_num = iterations_num;

File black-hole-solitaire/c-solver/black_hole_solver.h

View file
     BLACK_HOLE_SOLVER__TOO_MANY_CARDS,
     BLACK_HOLE_SOLVER__NOT_SOLVABLE,
     BLACK_HOLE_SOLVER__END,
-    BLACK_HOLE_SOLVER__OUT_OF_ITERS
+    BLACK_HOLE_SOLVER__OUT_OF_ITERS,
+    BLACK_HOLE_SOLVER__INVALID_INPUT
 };
 
 typedef struct
     long limit
 );
 
+DLLEXPORT extern int black_hole_solver_set_iters_display_step(
+    black_hole_solver_instance_t * const instance_proto,
+    const long display_step
+);
+
 DLLEXPORT extern int black_hole_solver_enable_rank_reachability_prune(
     black_hole_solver_instance_t * instance_proto,
     fcs_bool_t enabled_status

File black-hole-solitaire/c-solver/black_hole_solver_main.c

View file
     FILE * fh;
     int arg_idx;
     long max_iters_limit = -1;
+    long iters_display_step = 0;
     enum GAME_TYPE game_type = GAME__UNKNOWN;
     fcs_bool_t display_boards = FALSE;
     fcs_bool_t is_rank_reachability_prune_enabled = FALSE;
             arg_idx++;
             is_rank_reachability_prune_enabled = TRUE;
         }
+        else if (!strcmp(argv[arg_idx], "--iters-display-step"))
+        {
+            arg_idx++;
+            if (argc == arg_idx)
+            {
+                fprintf(stderr, "Error! --iters-display-step requires an arguments.\n");
+                exit(-1);
+            }
+            iters_display_step = atol(argv[arg_idx++]);
+
+            if (iters_display_step < 0)
+            {
+                fprintf(stderr, "Error! --iters-display-step should be positive or zero.\n");
+                exit(-1);
+            }
+        }
         else
         {
             break;
     }
 
     black_hole_solver_set_max_iters_limit(solver, max_iters_limit);
+    black_hole_solver_set_iters_display_step(solver, iters_display_step);
     black_hole_solver_enable_rank_reachability_prune(
         solver,
         is_rank_reachability_prune_enabled

File black-hole-solitaire/c-solver/t/black-hole-output.t

View file
 use strict;
 use warnings;
 
-use Test::More tests => 8;
+use Test::More tests => 10;
 use Test::Differences;
 
 use File::Spec;
     qr/\Ablack-hole-solver version (\d+(?:\.\d+){2})\nLibrary version \1\n\z/,
     "Right otuput for --version."
 );
+
+trap
+{
+    system('./black-hole-solve',
+        '--game', 'black_hole',
+        '--iters-display-step', '1000',
+        File::Spec->catfile(
+            $bin_dir, "data", "26464608654870335080.bh.board.txt"
+        )
+    );
+};
+
+# TEST
+ok (! ($trap->exit), "iters-display-step: running the program successfully.");
+
+$expected_output = <<'EOF';
+Iteration: 1000
+Iteration: 2000
+Iteration: 3000
+Iteration: 4000
+Iteration: 5000
+Iteration: 6000
+Iteration: 7000
+Iteration: 8000
+Solved!
+Move a card from stack 16 to the foundations
+
+Info: Card moved is 2D
+
+
+====================
+
+Move a card from stack 13 to the foundations
+
+Info: Card moved is 3H
+
+
+====================
+
+Move a card from stack 16 to the foundations
+
+Info: Card moved is 2S
+
+
+====================
+
+Move a card from stack 13 to the foundations
+
+Info: Card moved is 3C
+
+
+====================
+
+Move a card from stack 2 to the foundations
+
+Info: Card moved is 4H
+
+
+====================
+
+Move a card from stack 3 to the foundations
+
+Info: Card moved is 5S
+
+
+====================
+
+Move a card from stack 13 to the foundations
+
+Info: Card moved is 6D
+
+
+====================
+
+Move a card from stack 15 to the foundations
+
+Info: Card moved is 7C
+
+
+====================
+
+Move a card from stack 11 to the foundations
+
+Info: Card moved is 8C
+
+
+====================
+
+Move a card from stack 16 to the foundations
+
+Info: Card moved is 9H
+
+
+====================
+
+Move a card from stack 14 to the foundations
+
+Info: Card moved is TH
+
+
+====================
+
+Move a card from stack 3 to the foundations
+
+Info: Card moved is 9S
+
+
+====================
+
+Move a card from stack 5 to the foundations
+
+Info: Card moved is 8S
+
+
+====================
+
+Move a card from stack 5 to the foundations
+
+Info: Card moved is 9D
+
+
+====================
+
+Move a card from stack 12 to the foundations
+
+Info: Card moved is TC
+
+
+====================
+
+Move a card from stack 0 to the foundations
+
+Info: Card moved is JS
+
+
+====================
+
+Move a card from stack 9 to the foundations
+
+Info: Card moved is QC
+
+
+====================
+
+Move a card from stack 14 to the foundations
+
+Info: Card moved is KS
+
+
+====================
+
+Move a card from stack 7 to the foundations
+
+Info: Card moved is QH
+
+
+====================
+
+Move a card from stack 7 to the foundations
+
+Info: Card moved is JC
+
+
+====================
+
+Move a card from stack 8 to the foundations
+
+Info: Card moved is TS
+
+
+====================
+
+Move a card from stack 0 to the foundations
+
+Info: Card moved is JH
+
+
+====================
+
+Move a card from stack 9 to the foundations
+
+Info: Card moved is QS
+
+
+====================
+
+Move a card from stack 10 to the foundations
+
+Info: Card moved is KH
+
+
+====================
+
+Move a card from stack 7 to the foundations
+
+Info: Card moved is AC
+
+
+====================
+
+Move a card from stack 14 to the foundations
+
+Info: Card moved is 2C
+
+
+====================
+
+Move a card from stack 10 to the foundations
+
+Info: Card moved is 3D
+
+
+====================
+
+Move a card from stack 8 to the foundations
+
+Info: Card moved is 4S
+
+
+====================
+
+Move a card from stack 15 to the foundations
+
+Info: Card moved is 5D
+
+
+====================
+
+Move a card from stack 6 to the foundations
+
+Info: Card moved is 6S
+
+
+====================
+
+Move a card from stack 1 to the foundations
+
+Info: Card moved is 7D
+
+
+====================
+
+Move a card from stack 4 to the foundations
+
+Info: Card moved is 6H
+
+
+====================
+
+Move a card from stack 11 to the foundations
+
+Info: Card moved is 5C
+
+
+====================
+
+Move a card from stack 1 to the foundations
+
+Info: Card moved is 4C
+
+
+====================
+
+Move a card from stack 4 to the foundations
+
+Info: Card moved is 3S
+
+
+====================
+
+Move a card from stack 6 to the foundations
+
+Info: Card moved is 2H
+
+
+====================
+
+Move a card from stack 15 to the foundations
+
+Info: Card moved is AD
+
+
+====================
+
+Move a card from stack 12 to the foundations
+
+Info: Card moved is KC
+
+
+====================
+
+Move a card from stack 4 to the foundations
+
+Info: Card moved is AH
+
+
+====================
+
+Move a card from stack 0 to the foundations
+
+Info: Card moved is KD
+
+
+====================
+
+Move a card from stack 8 to the foundations
+
+Info: Card moved is QD
+
+
+====================
+
+Move a card from stack 3 to the foundations
+
+Info: Card moved is JD
+
+
+====================
+
+Move a card from stack 2 to the foundations
+
+Info: Card moved is TD
+
+
+====================
+
+Move a card from stack 5 to the foundations
+
+Info: Card moved is 9C
+
+
+====================
+
+Move a card from stack 10 to the foundations
+
+Info: Card moved is 8D
+
+
+====================
+
+Move a card from stack 6 to the foundations
+
+Info: Card moved is 7S
+
+
+====================
+
+Move a card from stack 1 to the foundations
+
+Info: Card moved is 8H
+
+
+====================
+
+Move a card from stack 2 to the foundations
+
+Info: Card moved is 7H
+
+
+====================
+
+Move a card from stack 9 to the foundations
+
+Info: Card moved is 6C
+
+
+====================
+
+Move a card from stack 11 to the foundations
+
+Info: Card moved is 5H
+
+
+====================
+
+Move a card from stack 12 to the foundations
+
+Info: Card moved is 4D
+
+
+====================
+
+
+
+--------------------
+Total number of states checked is 8636.
+This scan generated 8672 states.
+EOF
+
+# TEST
+eq_or_diff ($trap->stdout(), $expected_output, "Right output for iterations step.");