//finds route to target location and returns a pointer to the board that found it Node* find_solution (Node *start, int height_coordinate, int width_coordinate){ hash *hash_array = create_hash_table (HASH_TABLE_SIZE); set_hash_table (hash_array); Node *current, *previous, *parent; int target_found = 0; current = start; parent = start; while (!target_found){ previous = current; current->next = allocate_new_node (parent, previous); if (find_next_move (hash_array, start, current->next, parent->board, current->next->board) == 0){ if (parent->next == NULL){ printf("Couldn't find a solution\n"); exit (1); } parent = parent->next; current->next->parent = parent; copy_board (current->next->board, parent->board); } target_found = check_for_target (current->next->board, height_coordinate, width_coordinate); current = current->next; } return current; }
node *get_node_1(const char *message_id, unsigned int group_id, int get_new) { int string_length = strlen(message_id); int search = hash(message_id, string_length, node_table_length); int offset = 0; node *g; #if 0 printf("Entering node '%s', %d\n", message_id, group_id); #endif previous_instance_node = 0; while (1) { offset = node_table[search]; if (! offset) break; else if (offset && ! strcmp(message_id, get_string(nodes[offset].message_id))) break; if (search++ >= node_table_length) search = 0; } if (! offset) return allocate_new_node(message_id, group_id, search); else { g = &nodes[offset]; /* Try to find the parent in the same group. */ while (1) { if (g->group_id == group_id) return g; previous_instance_node = g->id; if (g->next_instance == 0) break; g = &nodes[g->next_instance]; } if (get_new) return allocate_new_node(message_id, group_id, search); else /* We didn't find any, so we just return the first instance of the parent. */ return &nodes[offset]; } }
node * add_child(node *parent, int value) { if(parent == NULL){ return NULL; } if(parent->node_number > MAX_CHILD_NODES) { return NULL; } new_child_node = allocate_new_node(value); if(new_child_node != NULL) { parent->children[parent->node_number] = new_child_node; parent->node_number++; } return new_child_node; }