diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d74e21 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/ diff --git a/Makefile b/Makefile index 8a4630a..5b6737a 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,8 @@ ## Makefile ## -#CC = gcc -Wall -Wextra -O3 -g -CC = gcc -Wall -Wextra -g +CC = gcc -Wall -Wextra -O3 -g +#CC = gcc -Wall -Wextra -g RM = rm -f diff --git a/lib/my_putchar.o b/lib/my_putchar.o new file mode 100644 index 0000000..0a8815e Binary files /dev/null and b/lib/my_putchar.o differ diff --git a/lib/my_putstr.o b/lib/my_putstr.o new file mode 100644 index 0000000..c706ea4 Binary files /dev/null and b/lib/my_putstr.o differ diff --git a/sokoban b/sokoban new file mode 100755 index 0000000..aca7830 Binary files /dev/null and b/sokoban differ diff --git a/solution.txt b/solution.txt new file mode 100644 index 0000000..f14df7a --- /dev/null +++ b/solution.txt @@ -0,0 +1 @@ +drdrdrdrrruuuuuulllllddrdrdrDulululldRdRdrdRRllululuurdrdrDululldRuuluurrrrrdddddrddlUUUUUUruLLLLLulDDdrdddrdRRlluluurdrDulldruluulldRuuurrrrrdddddrddlUUUUUUruLLLLLulDDdrdrdddRRlluurDldRuulululldRdRluuuurrrrrdddddrddlUUUUUUruLLLLLulDDdrdrdrddRluulululldRdRluuuurrrrrdddddrddlUUUUUUruLLLLLulDD diff --git a/src/ai/ai.c b/src/ai/ai.c index 9618144..74da426 100644 --- a/src/ai/ai.c +++ b/src/ai/ai.c @@ -59,18 +59,14 @@ char* save_solution( node_t* solution_node ){ * Copy a src into a dst state */ void copy_state(sokoban_t* init_data, state_t* dst, state_t* src){ - dst->map = malloc(sizeof(char *) * init_data->lines); for ( int i = 0; i < init_data->lines; ++i ){ int width = strlen(src->map[i]) + 1; - dst->map[i] = malloc(width); + dst->map[i] = malloc(width); memcpy(dst->map[i], src->map[i], width); } - dst->player_x = src->player_x; - dst->player_y = src->player_y; - } /** @@ -145,7 +141,7 @@ void update_explore_table(node_t* n, unsigned expanded_nodes ){ void free_memory(unsigned expanded_nodes ){ for( unsigned i = 0; i < expanded_nodes; i++){ - free(expanded_nodes_table[ i ]); + free(expanded_nodes_table[i]); } free(expanded_nodes_table); } @@ -184,7 +180,6 @@ void find_solution(sokoban_t* init_data, bool show_solution) unsigned generated_nodes = 0; unsigned expanded_nodes = 0; unsigned duplicated_nodes = 0; - int max_depth = 0; unsigned solution_size = 0; // Choose initial capacity of PRIME NUMBER @@ -216,6 +211,39 @@ void find_solution(sokoban_t* init_data, bool show_solution) * FILL IN THE GRAPH ALGORITHM * */ + while (pq.count > 0) { + n = heap_delete(&pq); + update_explore_table(n, expanded_nodes); + expanded_nodes += 1; + if (winning_condition(init_data, &(n->state))) { + solution = save_solution(n); + solution_size = n->depth; + break; + } + + for (move_t action = left; action <= down; action++) { + node_t *new = malloc(sizeof(*new)); + bool player_moved = applyAction(init_data, n, &new, action); + generated_nodes += 1; + if (!player_moved || simple_corner_deadlock(init_data, &(new->state))) { + free(new->state.map); + free(new); + continue; + } + + flatten_map(init_data, &flat_map, new->state.map); + + if (ht_lookup(&hashTable, flat_map) != NULL) { + duplicated_nodes += 1; + free(new->state.map); + free(new); + continue; + } + + ht_insert(&hashTable, flat_map, &flat_map); + heap_push(&pq, new); + } + } //---------------------------- @@ -224,13 +252,24 @@ void find_solution(sokoban_t* init_data, bool show_solution) ht_destroy(&hashTable); free_memory(expanded_nodes); free(flat_map); + for (int i = 0; i < init_data->lines; i++) { + free((init_data->map)[i]); + free((init_data->map_save)[i]); + } + free(init_data->map); + free(init_data->map_save); + free(init_data->buffer); + emptyPQ(&pq); + // free(pq.heaparr); + // printf("%d\n", pq.count); + //---------------------------- // Stop clock clock_t end = clock(); double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; - // Show Soltion + // Show solution if( show_solution && solution != NULL ) play_solution( *init_data, solution); endwin(); @@ -257,7 +296,6 @@ void find_solution(sokoban_t* init_data, bool show_solution) printf("\tExpanded/seconds: %d\n", (int)(expanded_nodes/cpu_time_used) ); printf("\tTime (seconds): %f\n", cpu_time_used ); - } void solve(char const *path, bool show_solution) diff --git a/src/ai/ai.o b/src/ai/ai.o new file mode 100644 index 0000000..40028f7 Binary files /dev/null and b/src/ai/ai.o differ diff --git a/src/ai/hashtable.o b/src/ai/hashtable.o new file mode 100644 index 0000000..ed45f88 Binary files /dev/null and b/src/ai/hashtable.o differ diff --git a/src/ai/priority_queue.c b/src/ai/priority_queue.c index bb28c67..a71c428 100644 --- a/src/ai/priority_queue.c +++ b/src/ai/priority_queue.c @@ -101,7 +101,10 @@ node_t* heap_delete(struct heap* h) void emptyPQ(struct heap* pq) { while(pq->count != 0) { node_t* n = heap_delete(pq); + for (int i = size; i >= 0; i--) { + free(n->state.map[i]); + } + free(n->state.map); free(n); - //printf("<<%d", heap_delete(pq)); } } diff --git a/src/ai/priority_queue.o b/src/ai/priority_queue.o new file mode 100644 index 0000000..0302ed6 Binary files /dev/null and b/src/ai/priority_queue.o differ diff --git a/src/ai/utils.h b/src/ai/utils.h index 7acda5f..165c017 100644 --- a/src/ai/utils.h +++ b/src/ai/utils.h @@ -50,4 +50,3 @@ bool winning_condition(sokoban_t *init_data, state_t *state); void play_solution( sokoban_t init_data, char* solution ); void print_map(sokoban_t *init_data, state_t *state); #endif - diff --git a/src/ai/utils.o b/src/ai/utils.o new file mode 100644 index 0000000..76f5846 Binary files /dev/null and b/src/ai/utils.o differ diff --git a/src/find_player.o b/src/find_player.o new file mode 100644 index 0000000..37fd3b8 Binary files /dev/null and b/src/find_player.o differ diff --git a/src/helper.o b/src/helper.o new file mode 100644 index 0000000..92fe570 Binary files /dev/null and b/src/helper.o differ diff --git a/src/key_check.o b/src/key_check.o new file mode 100644 index 0000000..941ce77 Binary files /dev/null and b/src/key_check.o differ diff --git a/src/loose_check.o b/src/loose_check.o new file mode 100644 index 0000000..e7160ba Binary files /dev/null and b/src/loose_check.o differ diff --git a/src/main.o b/src/main.o new file mode 100644 index 0000000..10208f8 Binary files /dev/null and b/src/main.o differ diff --git a/src/map_check.o b/src/map_check.o new file mode 100644 index 0000000..b619ee3 Binary files /dev/null and b/src/map_check.o differ diff --git a/src/map_reading.o b/src/map_reading.o new file mode 100644 index 0000000..df7832a Binary files /dev/null and b/src/map_reading.o differ diff --git a/src/movement.o b/src/movement.o new file mode 100644 index 0000000..109e4b4 Binary files /dev/null and b/src/movement.o differ diff --git a/src/play.o b/src/play.o new file mode 100644 index 0000000..79e1141 Binary files /dev/null and b/src/play.o differ diff --git a/src/win_check.o b/src/win_check.o new file mode 100644 index 0000000..413161b Binary files /dev/null and b/src/win_check.o differ diff --git a/src/zone_check.o b/src/zone_check.o new file mode 100644 index 0000000..4059b88 Binary files /dev/null and b/src/zone_check.o differ