Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix memory leaks #3

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions cost.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ void find_coord(Puzzle *puzzle, int value, int *x, int *y) {
}
}

int manhattan(int x1, int y1, int x2, int y2) {
return abs(x1 - x2) + abs(y1 - y2);
int manhattan_cost(Puzzle *current, Puzzle *goal) {
int cost = 0;
for (int j = 0; j < PUZZLE_DIMENSION; j++) {
if (current->board[j] != 0) {
int x1, y1, x2, y2;
find_coord(current, current->board[j], &x1, &y1);
find_coord(goal, current->board[j], &x2, &y2);
cost += abs(x1 - x2) + abs(y1 - y2);
}
}
return cost;
}
2 changes: 1 addition & 1 deletion cost.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
#include "puzzle.h"

void find_coord(Puzzle *puzzle, int value, int *x, int *y);
int manhattan(int x1, int y1, int x2, int y2);
int manhattan_cost(Puzzle *current, Puzzle *goal);

#endif // COST_H_
17 changes: 9 additions & 8 deletions node.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,39 @@ Node *generate_child_node(const Node *parent, const Direction direction) {
return NULL;
}

Node *child = (Node *)malloc(sizeof(Node));
if (!child) return NULL;

Puzzle *new_state = (Puzzle *)malloc(sizeof(Puzzle));
if (!new_state) return NULL;

*new_state = *(parent->state);

move(new_state, direction);

Node *child = (Node *)malloc(sizeof(Node));
if (!child) {
// Free new_state if move fails
if (!move(new_state, direction)) {
free(new_state);
return NULL;
}

child->parent = (Node *)parent;
child->state = new_state;
child->move = direction;
child->cost = 0;
child->cost = parent->cost + 1;

return child;
}

Node **generate_children(const Node *parent, int *num_children) {
Node **children = (Node **)malloc(DIRECTION_COUNT * sizeof(Node *));
if (!children) return NULL;

*num_children = 0;

Direction directions[DIRECTION_COUNT] = {UP, DOWN, LEFT, RIGHT};

for (int i = 0; i < DIRECTION_COUNT; i++) {
Node *child = generate_child_node(parent, directions[i]);
if (child != NULL) {
children[(*num_children)++] = child;
}
if (child) children[(*num_children)++] = child;
}

return children;
Expand Down
8 changes: 5 additions & 3 deletions puzzle.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int is_valid_move(const Puzzle *p, const Direction direction) {
}
}

void move(Puzzle *p, const Direction direction) {
int move(Puzzle *p, const Direction direction) {
int blank_row = p->blank_index / PUZZLE_SIZE;
int blank_col = p->blank_index % PUZZLE_SIZE;

Expand All @@ -106,16 +106,18 @@ void move(Puzzle *p, const Direction direction) {
new_col++;
break;
case NONE:
return;
return 0; // No move made
}

if (new_row < 0 || new_row >= PUZZLE_SIZE || new_col < 0 || new_col >= PUZZLE_SIZE) {
return;
return 0; // Move is out of bounds
}

int new_index = new_row * PUZZLE_SIZE + new_col;
swap(&p->board[p->blank_index], &p->board[new_index]);
p->blank_index = new_index;

return 1; // Move successful
}

void print_puzzle(const Puzzle *p, int debug) {
Expand Down
2 changes: 1 addition & 1 deletion puzzle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int get_blank_index(const Puzzle *p);
int get_inversion_count(const Puzzle puzzle);
int is_solvable(const Puzzle puzzle);
int is_valid_move(const Puzzle *p, const Direction direction);
void move(Puzzle *p, const Direction direction);
int move(Puzzle *p, const Direction direction);
void print_puzzle(const Puzzle *p, int debug);
void shuffle(Puzzle *p);
void swap(int *a, int *b);
Expand Down
22 changes: 10 additions & 12 deletions solve/bfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ Node* bfs_solve(Puzzle* start, Puzzle* goal) {

Node* current = dequeue(queue);

// Check if the goal state is reached
if (compare_puzzles(current->state, goal)) {
// Free the remaining nodes in the queue
while (!is_queue_empty(queue)) {
Node* temp = dequeue(queue);
free(temp);
free(temp->state); // Free the state associated with the node
free(temp); // Free the node itself
}
free_queue(queue);
free_hashset(&visited); // Free the HashSet memory

// Print statistics
printf("Solution found at depth %d after expanding %d nodes.\n", depth, nodes_expanded);
return current; // Found the goal

return current; // Return the solution node
}

// Generate the children nodes of the current node
Expand All @@ -50,22 +55,15 @@ Node* bfs_solve(Puzzle* start, Puzzle* goal) {

for (int i = 0; i < num_children; i++) {
// Calculate cost of the child node
int x1, y1, x2, y2;

for (int j = 0; j < PUZZLE_DIMENSION; j++) {
if (current->state->board[j] != 0) {
find_coord(current->state, current->state->board[j], &x1, &y1);
find_coord(goal, current->state->board[j], &x2, &y2);
children[i]->cost += manhattan(x1, y1, x2, y2);
}
}
children[i]->cost = manhattan_cost(children[i]->state, goal);

// Check if the child state has been visited
if (!contains(&visited, children[i]->state)) {
enqueue(queue, children[i]); // Enqueue unvisited child
insert(&visited, children[i]->state); // Mark as visited
} else {
free(children[i]); // Free memory for already visited child
free(children[i]->state); // Free state memory for already visited child
free(children[i]); // Free memory for already visited child
}
}

Expand Down
Loading