-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
73 lines (59 loc) · 1.63 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "solve/bfs.h"
int main(int argc, char* argv[]) {
// Command-line arguments
char* mode = "bfs"; // Default mode
char* puzzle = "";
// Parse command-line arguments
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--mode") == 0 || strcmp(argv[i], "-m") == 0) {
mode = argv[++i];
} else if (strcmp(argv[i], "--puzzle") == 0 || strcmp(argv[i], "-p") == 0) {
puzzle = argv[++i];
} else {
printf("Unknown argument: %s\n", argv[i]);
return 1;
}
}
// Display the mode
printf("Mode: %s\n", mode);
// Seed the random number generator
srand(time(NULL));
// Initialize the puzzle
Puzzle start;
if (strlen(puzzle) == 9) {
start = create_puzzle(puzzle);
} else {
start = create_random_puzzle();
}
print_puzzle(&start, 0);
// Check if the puzzle is solvable
if (is_solvable(start)) {
printf("Solving puzzle...\n");
} else {
printf("Puzzle is not solvable\n");
return 1;
}
Puzzle goal = create_goal_puzzle("inline");
Node* solution = NULL;
// Call the BFS solver
if (strcmp(mode, "bfs") == 0) {
solution = bfs_solve(&start, &goal);
} else {
printf("Unknown mode: %s\n", mode);
return 1;
}
if (solution) {
Node* current = solution;
while (current != NULL) {
print_node(current, 1);
current = current->parent;
}
} else {
printf("No solution found.\n");
}
return 0;
}