Skip to content

Commit

Permalink
Add debug option for solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Vianpyro committed Sep 9, 2024
1 parent 81558ef commit 88d5468
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "solve/bfs.h"

int main() {
Puzzle start = {7, {1, 2, 3, 4, 5, 6, 7, 0, 8}};
Puzzle start = {0, {0, 2, 3, 4, 5, 6, 7, 1, 8}};
Puzzle goal = create_goal_puzzle("inline");

// Appelle la fonction BFS pour résoudre le puzzle
Expand All @@ -16,7 +16,7 @@ int main() {
printf("Solution found!\n");
Node* current = solution;
while (current != NULL) {
print_node(current);
print_node(current, 0);
current = current->parent;
}
} else {
Expand Down
6 changes: 2 additions & 4 deletions node.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ Node **generate_children(const Node *parent, int *num_children) {
return children;
}

void print_node(const Node *n) {
void print_node(const Node *n, int debug) {
printf("Move: ");
print_direction(n->move);

printf("Cost: %d\n", n->cost);
print_puzzle(n->state);
print_puzzle(n->state, debug);
}
2 changes: 1 addition & 1 deletion node.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ typedef struct Node {

Node* generate_child_node(const Node* parent, const Direction direction);
Node** generate_children(const Node* parent, int* num_children);
void print_node(const Node* n);
void print_node(const Node* n, int debug);

#endif // NODE_H_
7 changes: 5 additions & 2 deletions puzzle.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,17 @@ void move(Puzzle *p, const Direction direction) {
p->blank_index = new_index;
}

void print_puzzle(const Puzzle *p) {
void print_puzzle(const Puzzle *p, int debug) {
for (int i = 0; i < PUZZLE_DIMENSION; i++) {
printf("%d ", p->board[i]);
if (i % PUZZLE_SIZE == PUZZLE_SIZE - 1) {
printf("\n");
}
}
printf("Blank index: %d\n", p->blank_index);

if (debug) {
printf("Blank index: %d\n", p->blank_index);
}
}

void shuffle(Puzzle *p) {
Expand Down
2 changes: 1 addition & 1 deletion puzzle.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Puzzle create_goal_puzzle(const char *type);
Puzzle create_root_puzzle();
int is_valid_move(const Puzzle *p, const Direction direction);
void move(Puzzle *p, const Direction direction);
void print_puzzle(const Puzzle *p);
void print_puzzle(const Puzzle *p, int debug);
void shuffle(Puzzle *p);
void swap(int *a, int *b);

Expand Down

0 comments on commit 88d5468

Please sign in to comment.