Commits

Shlomi Fish committed a19eee4

Progress, but it broke the code.

  • Participants
  • Parent commits 5160974
  • Branches merge-all-in-a-row-and-black-hole-code-trial

Comments (0)

Files changed (4)

File black-hole-solitaire/all-in-a-row-c-solver/black_hole_solver.c

      * This is the ranks of the cards in the columns. It remains constant
      * for the duration of the game.
      * */
-    bhs_rank_t board_values[MAX_NUM_COLUMNS][MAX_NUM_CARDS_IN_COL];
+    bhs_rank_t board_values[BHS__MAX_NUM_COLUMNS][BHS__MAX_NUM_CARDS_IN_COL];
 
     bhs_rank_t initial_foundation;
 
     bh_solve_hash_t positions;
 
     bhs_card_string_t initial_foundation_string;
-    bhs_card_string_t initial_board_card_strings[MAX_NUM_COLUMNS][MAX_NUM_CARDS_IN_COL];
-    int initial_lens[MAX_NUM_COLUMNS];
+    bhs_card_string_t initial_board_card_strings[BHS__MAX_NUM_COLUMNS][BHS__MAX_NUM_CARDS_IN_COL];
+    int initial_lens[BHS__MAX_NUM_COLUMNS];
 
     bhs_state_key_value_pair_t init_state;
     bhs_state_key_value_pair_t final_state;
     int num_states_in_solution, current_state_in_solution_idx;
 
     long iterations_num, num_states_in_collection, max_iters_limit;
+
+    int num_columns;
 } bhs_solver_t;
 
 int DLLEXPORT black_hole_solver_create(
 extern int DLLEXPORT black_hole_solver_read_board(
     black_hole_solver_instance_t * instance_proto,
     const char * board_string,
-    int * error_line_number
+    int * error_line_number,
+    int num_columns,
+    int max_num_cards_in_col
 )
 {
     const char * s, * match;
         return BLACK_HOLE_SOLVER__TRAILING_CHARS;
     }
 
-    for(col_idx = 0; col_idx < MAX_NUM_COLUMNS; col_idx++)
+    for(col_idx = 0; col_idx < num_columns; col_idx++)
     {
         int pos_idx = 0;
         while ((*s != '\n') && (*s != '\0'))
         {
-            if (pos_idx == MAX_NUM_CARDS_IN_COL)
+            if (pos_idx == max_num_cards_in_col)
             {
                 *error_line_number = 2+col_idx;
                 return BLACK_HOLE_SOLVER__TOO_MANY_CARDS;
     bhs_rank_t card;
     long iterations_num, num_states_in_collection;
     long max_iters_limit;
+    int num_columns;
 
     solver = (bhs_solver_t *)ret_instance;
 
+    num_columns = solver->num_columns;
+
     init_state = &(solver->init_state);
     memset(init_state, '\0', sizeof(*init_state));
     init_state->key.foundations = solver->initial_foundation;
         max_iters_limit = LONG_MAX;
     }
 
-    for (two_cols_idx = 0, two_cols_offset = 0; two_cols_idx < (MAX_NUM_COLUMNS / 2); two_cols_idx++, two_cols_offset += 2)
+    int cols_idx_limit =
+    (
+        (num_columns / BHS__ALL_IN_A_ROW__COLS_PER_BYTE)
+            +
+        (
+            (num_columns % BHS__ALL_IN_A_ROW__COLS_PER_BYTE ) ? 1 : 0
+        )
+    );
+
+    for (two_cols_idx = 0, two_cols_offset = 0;
+        two_cols_idx < cols_idx_limit ;
+        two_cols_idx++, two_cols_offset += BHS__ALL_IN_A_ROW__COLS_PER_BYTE)
     {
         init_state->key.data[two_cols_idx] =
             (unsigned char)
             (
               (solver->initial_lens[two_cols_offset])
-            | (solver->initial_lens[two_cols_offset+1] << 4)
+            | (solver->initial_lens[two_cols_offset+1] << BHS__ALL_IN_A_ROW__BITS_PER_COL)
             )
             ;
     }
 
         if (foundations == -1)
         {
-            for (col_idx = 0 ; col_idx < MAX_NUM_COLUMNS ; col_idx++)
+            for (col_idx = 0 ; col_idx < num_columns ; col_idx++)
             {
 #define BYTE_POS() (col_idx >> 1)
 #define BIT_OFFSET() ((col_idx&(2-1))<<2)
         }
         else
         {
-            for (col_idx = 0 ; col_idx < MAX_NUM_COLUMNS ; col_idx++)
+            for (col_idx = 0 ; col_idx < num_columns ; col_idx++)
             {
                 if ((pos = (
                     (state.key.data[BYTE_POS()] >> (BIT_OFFSET()))

File black-hole-solitaire/all-in-a-row-c-solver/black_hole_solver.h

 DLLEXPORT extern int black_hole_solver_read_board(
     black_hole_solver_instance_t * ret_instance,
     const char * board_string,
-    int * error_line_number
+    int * error_line_number,
+    int num_columns,
+    int max_num_cards_in_col
 );
 
 DLLEXPORT extern int black_hole_solver_set_max_iters_limit(

File black-hole-solitaire/all-in-a-row-c-solver/black_hole_solver_main.c

 
 #include "black_hole_solver.h"
 
+#include "state.h"
+
 #define MAX_LEN_BOARD_STRING 4092
 
 int main(int argc, char * argv[])
     if (black_hole_solver_read_board(
         solver,
         board,
-        &error_line_num
+        &error_line_num,
+        BHS__ALL_IN_A_ROW__NUM_COLUMNS,
+        BHS__ALL_IN_A_ROW__MAX_NUM_CARDS_IN_COL
         ))
     {
         fprintf(stderr, "Error reading the board at line No. %d!\n", error_line_num);

File black-hole-solitaire/all-in-a-row-c-solver/state.h

 #define BHS_STATE_H
 
 
-#define MAX_NUM_COLUMNS 13
-#define MAX_NUM_CARDS_IN_COL 4
+#define BHS__ALL_IN_A_ROW__NUM_COLUMNS 13
+#define BHS__ALL_IN_A_ROW__MAX_NUM_CARDS_IN_COL 4
+#define BHS__ALL_IN_A_ROW__BITS_PER_COL 4
+#define BHS__ALL_IN_A_ROW__COLS_PER_BYTE 2
 
+#define BHS__BLACK_HOLE__NUM_COLUMNS 17
+#define BHS__BLACK_HOLE__MAX_NUM_CARDS_IN_COL 3
+#define BHS__BLACK_HOLE__BITS_PER_COL 2
+#define BHS__BLACK_HOLE__COLS_PER_BYTE 4
 
 typedef char bhs_card_string_t[3];
 typedef char bhs_rank_t;
 
-/* We allocate 2-bits for the length of every column */
-#define NUM_DATA_CHARS (MAX_NUM_COLUMNS * 4 / 8 + 1)
+#define max(a,b) ( ( (a)>(b) ) ? (a) : (b) )
+
+#define BHS__MAX_NUM_COLUMNS (max(BHS__ALL_IN_A_ROW__NUM_COLUMNS , BHS__BLACK_HOLE__NUM_COLUMNS))
+#define BHS__MAX_NUM_CARDS_IN_COL (max(BHS__ALL_IN_A_ROW__MAX_NUM_CARDS_IN_COL , BHS__BLACK_HOLE__MAX_NUM_CARDS_IN_COL))
+
+#define BHS__NUM_BITS_PER_CHAR 8
+/* We allocate 4-bits for the length of every column */
+#define BHS__ALL_IN_A_ROW__NUM_DATA_CHARS (BHS__ALL_IN_A_ROW__NUM_COLUMNS * BHS__ALL_IN_A_ROW__BITS_PER_COL / BHS__NUM_BITS_PER_CHAR + 1)
+#define BHS__BLACK_HOLE__NUM_DATA_CHARS (BHS__BLACK_HOLE__NUM_COLUMNS * BHS__BLACK_HOLE__BITS_PER_COL / BHS__NUM_BITS_PER_CHAR + 1)
+
+#define BHS__NUM_DATA_CHARS (max(BHS__ALL_IN_A_ROW__NUM_DATA_CHARS, BHS__BLACK_HOLE__BITS_PER_COL))
+
 typedef struct
 {
-    unsigned char data[NUM_DATA_CHARS];
+    unsigned char data[BHS__NUM_DATA_CHARS];
     bhs_rank_t foundations;
 } bhs_state_key_t;