Solver algorithm implemented
This commit is contained in:
parent
ae85d7dbff
commit
cf9d366133
24 changed files with 55 additions and 13 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
.vscode/
|
4
Makefile
4
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
|
||||
|
|
BIN
lib/my_putchar.o
Normal file
BIN
lib/my_putchar.o
Normal file
Binary file not shown.
BIN
lib/my_putstr.o
Normal file
BIN
lib/my_putstr.o
Normal file
Binary file not shown.
BIN
sokoban
Executable file
BIN
sokoban
Executable file
Binary file not shown.
1
solution.txt
Normal file
1
solution.txt
Normal file
|
@ -0,0 +1 @@
|
|||
drdrdrdrrruuuuuulllllddrdrdrDulululldRdRdrdRRllululuurdrdrDululldRuuluurrrrrdddddrddlUUUUUUruLLLLLulDDdrdddrdRRlluluurdrDulldruluulldRuuurrrrrdddddrddlUUUUUUruLLLLLulDDdrdrdddRRlluurDldRuulululldRdRluuuurrrrrdddddrddlUUUUUUruLLLLLulDDdrdrdrddRluulululldRdRluuuurrrrrdddddrddlUUUUUUruLLLLLulDD
|
56
src/ai/ai.c
56
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)
|
||||
|
|
BIN
src/ai/ai.o
Normal file
BIN
src/ai/ai.o
Normal file
Binary file not shown.
BIN
src/ai/hashtable.o
Normal file
BIN
src/ai/hashtable.o
Normal file
Binary file not shown.
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
BIN
src/ai/priority_queue.o
Normal file
BIN
src/ai/priority_queue.o
Normal file
Binary file not shown.
|
@ -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
|
||||
|
||||
|
|
BIN
src/ai/utils.o
Normal file
BIN
src/ai/utils.o
Normal file
Binary file not shown.
BIN
src/find_player.o
Normal file
BIN
src/find_player.o
Normal file
Binary file not shown.
BIN
src/helper.o
Normal file
BIN
src/helper.o
Normal file
Binary file not shown.
BIN
src/key_check.o
Normal file
BIN
src/key_check.o
Normal file
Binary file not shown.
BIN
src/loose_check.o
Normal file
BIN
src/loose_check.o
Normal file
Binary file not shown.
BIN
src/main.o
Normal file
BIN
src/main.o
Normal file
Binary file not shown.
BIN
src/map_check.o
Normal file
BIN
src/map_check.o
Normal file
Binary file not shown.
BIN
src/map_reading.o
Normal file
BIN
src/map_reading.o
Normal file
Binary file not shown.
BIN
src/movement.o
Normal file
BIN
src/movement.o
Normal file
Binary file not shown.
BIN
src/play.o
Normal file
BIN
src/play.o
Normal file
Binary file not shown.
BIN
src/win_check.o
Normal file
BIN
src/win_check.o
Normal file
Binary file not shown.
BIN
src/zone_check.o
Normal file
BIN
src/zone_check.o
Normal file
Binary file not shown.
Loading…
Reference in a new issue