board_struct *attempt_to_move(board_struct *parent_board, board_struct *current_board, success_failure *success_index, int row_coordinate, int col_coordinate, full_empty hashing_array[VERY_LARGE_NUMBER], int row, int col, int row_incr, int col_incr) { board_struct *new_board; //The two is as the space two away from the peg in the direction we wish to move must be empty in order to put a peg in. if(parent_board->board_array[row + row_incr][col + col_incr] == peg && parent_board->board_array[row + 2 * row_incr][col + 2 * col_incr] == space){ new_board = create_new_board_struct(); copy_in_parent_board(new_board, parent_board); generate_board(new_board, parent_board, current_board, row, col, row_incr, col_incr); check_for_success(new_board, row_coordinate, col_coordinate, success_index); count_board_pegs(new_board); generate_hash_string(new_board); if( check_modulus_hash_number(new_board, hashing_array) == seen_before){ if( check_unique_hash_number(new_board, current_board, new_board->number_pegs) == seen_before){ free(new_board); return(current_board); } } print_messages(new_board->number_pegs, success_index); return(new_board); } return(current_board); }
static RendererWidgetOptions *parse_args(int argc, char *argv[]) { GError *error = NULL; gboolean klein = FALSE; gboolean poincare = FALSE; gboolean editing = FALSE; gchar** levels = NULL; gchar *level = NULL; double scale = 1; gchar* scale_str = NULL; HyperbolicProjection projection = DEFAULT_PROJECTION; gboolean animation = FALSE; gboolean noanimation = FALSE; gboolean random = FALSE; GOptionContext *context = g_option_context_new(NULL); g_option_context_set_summary(context, RENDERER_SUMMARY); GOptionEntry options[] = { {"poincare", 'P', 0, G_OPTION_ARG_NONE, &poincare, "Poincare Projection", NULL}, {"klein", 'K', 0, G_OPTION_ARG_NONE, &klein, "Klein Projection (takes precedence)", NULL}, {"animation", 'A', 0, G_OPTION_ARG_NONE, &animation, "Animate Moves", NULL}, {"no-animation", 'N', 0, G_OPTION_ARG_NONE, &noanimation, "Don't Animate Moves", NULL}, {"scale", 'S', 0, G_OPTION_ARG_STRING, &scale_str, "Render at a lower resolution (SCALE of 2 means scale" " rendered image x2 before displaying)", "SCALE"}, {"random", 'R', 0, G_OPTION_ARG_NONE, &random, "Generate random level", NULL}, {"editing", 'E', 0, G_OPTION_ARG_NONE, &editing, "Editing mode", NULL}, {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &levels, "Level File", "LEVEL"}, { NULL, 0, 0, 0, NULL, NULL, NULL } }; g_option_context_add_main_entries(context, options, NULL); g_option_context_add_group(context, gtk_get_option_group(TRUE)); gboolean res = g_option_context_parse(context, &argc, &argv, &error); g_option_context_free(context); if (random) { if (!res) { fprintf(stderr, "Invalid command line options!\n"); goto FAIL; } } else { /* !random */ if (!res || levels == NULL || levels[0] == NULL || levels[1] != NULL) { fprintf(stderr, "Invalid command line options!\n"); goto FAIL; } } if ((klein && poincare) || (animation && noanimation) || (random && level)) { fprintf(stderr, "Mutually exclusive command line options specified!\n"); goto FAIL; } if (klein) { projection = PROJECTION_KLEIN; } else if (poincare) { projection = PROJECTION_POINCARE; } if (noanimation) { animation = FALSE; } else if (animation) { animation = TRUE; } else { animation = DEFAULT_ANIMATION; } if (scale_str != NULL) { if (!sscanf(scale_str, "%lf", &scale)) { fprintf(stderr, "Could not parse scale!\n"); } } Board *board; if (random) { board = generate_board(NULL); } else { level = levels[0]; FILE* levelfh = fopen(level, "r"); if (levelfh == NULL) { perror("Could not open level"); goto FAIL; } SavedTile *map = NULL; ConfigOption *cfg = NULL; level_parse_file(levelfh, &map, &cfg); fclose(levelfh); if ((map == NULL) || (cfg == NULL)) { fprintf(stderr, "Could not succesfully parse %s.\n", level); free(map); free(cfg); goto FAIL; } board = board_assemble_full(map, cfg); board->filename = strdup(level); free(map); free(cfg); if (board == NULL) { fprintf(stderr, "Could not succesfully create board from %s.\n", level); goto FAIL; } } if (levels) g_strfreev(levels); if (scale_str) g_free(scale_str); RendererWidgetOptions *opts = malloc(sizeof(RendererWidgetOptions)); opts->projection = projection; opts->board = board; g_atomic_int_set(&opts->drawing, FALSE); opts->animation = animation; opts->editing = editing; opts->scale = scale; return opts; FAIL: if (levels) g_strfreev(levels); if (scale_str) g_free(scale_str); return NULL; }
board() : SIZE_W_(32), SIZE_H_(32), board_(1024) { generate_board(); }
int main(void) { srand(time(NULL)); printf("The available commands are:\n" "I N - Inject an NxN board. This command will scan NxN letters to form a board\n" "G N - Generates a new NxN board where a letter doesn't appear more than N times\n" "P - Print the current board\n" "W N word - Insert a word into the dictionary with score N\n" "A N word - Insert a word and all prefixes in the dictionary. Non-proper prefixes get a score of 0\n" "R word - Delete a word\n" "S word - Search for a word and return its score (-1 if no such word exists)\n" "D - Dump the dictionary with the corresponding scores\n" "B - Find the best word (word with the highest score)\n" "Q - Quit\n" "> "); char **board = NULL; size_t board_dim = 0; trie = new_trie(); char op; while (scanf(" %c", &op) == 1) { if (op == 'I') { destroy_board(board, board_dim); scanf("%zu", &board_dim); board = inject_board(board_dim); } else if (op == 'P') { if (board == NULL) { printf("No board at the moment\n"); } else { print_board(board, board_dim); } } else if (op == 'G') { destroy_board(board, board_dim); scanf("%zu", &board_dim); board = generate_board(board_dim); } else if (op == 'W') { size_t word_score; scanf("%zu%s", &word_score, word_buff); insert_word(trie, word_buff, word_score); } else if (op == 'A') { size_t word_score; scanf("%zu%s", &word_score, word_buff); size_t i; for (i = 1; word_buff[i] != '\0'; i++) { char c = word_buff[i]; word_buff[i] = '\0'; insert_word(trie, word_buff, 0); word_buff[i] = c; } insert_word(trie, word_buff, word_score); } else if (op == 'R') { scanf("%s", word_buff); delete_word(trie, word_buff); } else if (op == 'S') { scanf("%s", word_buff); int s = word_score(trie, word_buff); if (s == -1) { printf("No such word: %s\n", word_buff); } else { printf("score(%s) = %d\n", word_buff, s); } } else if (op == 'D') { print_known_words(trie); } else if (op == 'B') { char *w = find_best_word(board, board_dim); if (w == NULL) { printf("No words found\n"); } else { printf("Best word: %s\n", w); } free(w); } else if (op == 'Q') { break; } else { fprintf(stderr, "Unrecognized operation: %c\n", op); } printf("> "); } destroy_trie(trie); destroy_board(board, board_dim); return 0; }