Commits

shl...@b384bcd7-cfd4-0310-aca0-d78b80f7b91b  committed 8b2c3b6

Add the set_max_iters_limit method.

  • Participants
  • Parent commits 0b05158

Comments (0)

Files changed (4)

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

 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <limits.h>
 
 #include "config.h"
 #include "black_hole_solver.h"
     bhs_state_key_value_pair_t * states_in_solution;
     int num_states_in_solution, current_state_in_solution_idx;
     
-    long iterations_num, num_states_in_collection;
+    long iterations_num, num_states_in_collection, max_iters_limit;
 } bhs_solver_t;
 
 int DLLEXPORT black_hole_solver_create(
     return BLACK_HOLE_SOLVER__SUCCESS;
 }
 
+DLLEXPORT extern int black_hole_solver_set_max_iters_limit(
+    black_hole_solver_instance_t * instance_proto,
+    long limit
+)
+{
+    bhs_solver_t * solver;
+
+    solver = (bhs_solver_t *)instance_proto;
+    solver->max_iters_limit = limit;
+
+    return BLACK_HOLE_SOLVER__SUCCESS;
+}
+
+
 extern int DLLEXPORT black_hole_solver_run(
     black_hole_solver_instance_t * ret_instance
 )
     int col_idx, pos;
     bhs_rank_t card;
     long iterations_num, num_states_in_collection;
+    long max_iters_limit;
 
     solver = (bhs_solver_t *)ret_instance;
 
     memset(init_state, '\0', sizeof(*init_state));
     init_state->key.foundations = solver->initial_foundation;
 
+    max_iters_limit = solver->max_iters_limit;
+
+    if (max_iters_limit < 0)
+    {
+        max_iters_limit = LONG_MAX;
+    }
+
     for (four_cols_idx = 0, four_cols_offset = 0; four_cols_idx < 4; four_cols_idx++, four_cols_offset += 4)
     {
         init_state->key.data[four_cols_idx] =
 
             return BLACK_HOLE_SOLVER__SUCCESS;
         }
+        else
+        {
+            if (iterations_num == max_iters_limit)
+            {
+                free(queue);
+                return BLACK_HOLE_SOLVER__OUT_OF_ITERS;
+            }
+        }
     }
 
     solver->iterations_num = iterations_num;

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

     BLACK_HOLE_SOLVER__NOT_ENOUGH_COLUMNS,
     BLACK_HOLE_SOLVER__TOO_MANY_CARDS,
     BLACK_HOLE_SOLVER__NOT_SOLVABLE,
-    BLACK_HOLE_SOLVER__END
+    BLACK_HOLE_SOLVER__END,
+    BLACK_HOLE_SOLVER__OUT_OF_ITERS
 };
 
 typedef struct
     int * error_line_number
 );
 
+DLLEXPORT extern int black_hole_solver_set_max_iters_limit(
+    black_hole_solver_instance_t * instance_proto,
+    long limit
+);
+
 DLLEXPORT extern int black_hole_solver_run(
     black_hole_solver_instance_t * ret_instance
 );

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

     black_hole_solver_instance_t * solver;
     char board[MAX_LEN_BOARD_STRING];
     int error_line_num;
-    int ret;
+    int ret, solver_ret_code;
     char * filename = NULL;
     FILE * fh;
+    int arg_idx;
+    long max_iters_limit = -1;
 
     if (black_hole_solver_create(&solver))
     {
         exit(-1);
     }
 
-    if (argc > 1)
+    arg_idx = 1;
+    if (argc > arg_idx)
     {
-        if (strcmp(argv[1], "-"))
+        if (!strcmp(argv[arg_idx], "--max-iters"))
         {
-            filename = argv[1];
+            arg_idx++;
+            if (argc == arg_idx)
+            {
+                fprintf(stderr, "Error! --max-iters requires an arguments.\n");
+                exit(-1);
+            }
+            max_iters_limit = atol(argv[arg_idx++]);
         }
     }
 
+    black_hole_solver_set_max_iters_limit(solver, max_iters_limit);
+    
+    if (argc > arg_idx)
+    {
+        if (strcmp(argv[arg_idx], "-"))
+        {
+            filename = argv[arg_idx];
+        }
+        arg_idx++;
+    }
+
     if (filename)
     {
         fh = fopen(filename, "rt");
     }
 
     ret = 0;
-    if (!black_hole_solver_run(solver))
+
+    solver_ret_code = black_hole_solver_run(solver);
+    if (!solver_ret_code)
     {
         int col_idx, card_rank, card_suit;
         int next_move_ret_code;
             ret = -1;
         }
     }
+    else if (solver_ret_code == BLACK_HOLE_SOLVER__OUT_OF_ITERS)
+    {
+        printf("Intractable!\n");
+        ret = -2;
+    }
     else
     {
         printf("Unsolved!\n");

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

 use strict;
 use warnings;
 
-use Test::More tests => 3;
+use Test::More tests => 6;
 use Test::Differences;
 
 use File::Spec;
 
 # TEST
 eq_or_diff ($trap->stdout(), $expected_output, "Right output.");
+
+
+trap
+{
+    system("./black-hole-solve",
+        "--max-iters", "10000",
+        File::Spec->catfile(
+            $bin_dir, "data", "26464608654870335080.bh.board.txt"
+        )
+    );
+};
+
+# TEST
+ok (! ($trap->exit), "Running --max-iters program successfully.");
+
+$expected_output = <<'EOF';
+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.");
+
+trap
+{
+    system("./black-hole-solve", 
+        File::Spec->catfile(
+            $bin_dir, "data", "1.bh.board.txt"
+        )
+    );
+};
+
+$expected_output = <<'EOF';
+Unsolved!
+
+
+--------------------
+Total number of states checked is 8.
+This scan generated 8 states.
+EOF
+
+# TEST
+eq_or_diff ($trap->stdout(), $expected_output, "Right output for --max-iters.");