forked from brownan/Rubiks-Cube-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
152 lines (137 loc) · 4.45 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright © 2009 Andrew Brown <brownan@gmail.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include "cornertable.h"
#include "edgetable.h"
#include "goal.h"
#include "cube.h"
int make_corner()
{
FILE *output;
unsigned char *cornertable = CORNER_TABLE_NEW;
corner_generate(cornertable, cube_solved);
output = fopen("table_corner.rht", "w");
corner_write(cornertable, output);
fclose(output);
return 0;
}
int make_edge(int t)
{
FILE *output;
unsigned char *edgetable = EDGE_TABLE_NEW;
edge_generate(edgetable, cube_solved, t);
if (t==1)
output = fopen("table_edge1.rht", "w");
else
output = fopen("table_edge2.rht", "w");
edge_write(edgetable, output);
fclose(output);
return 0;
}
/* Reads in tables and starts the solver */
int solve(char *tosolve)
{
FILE *input;
unsigned char *corner_table, *edge_table, *edge_table2;
cube_type tosolve_converted;
fprintf(stderr, "Loading tables...\n");
corner_table = CORNER_TABLE_NEW;
input = fopen("table_corner.rht", "r");
if (!corner_read(corner_table, input)) {
printf("Warning: could not open corner table\n");
free(corner_table);
corner_table = NULL;
}
fclose(input);
edge_table = EDGE_TABLE_NEW;
input = fopen("table_edge1.rht", "r");
if (!edge_read(edge_table, input)) {
printf("Warning: could not open edge table\n");
free(edge_table);
edge_table = NULL;
}
fclose(input);
edge_table2 = EDGE_TABLE_NEW;
input = fopen("table_edge2.rht", "r");
if (!edge_read(edge_table2, input)) {
printf("Warning: could not open second edge table\n");
free(edge_table2);
edge_table2 = NULL;
}
fclose(input);
cube_120convert(tosolve, tosolve_converted);
goal_solve(tosolve_converted, cube_solved, corner_table, edge_table,
edge_table2);
return 1;
}
int main(int argc, char **argv)
{
int number;
char cube_raw[121];
// Was the cube string specified on the command line? If not, present a
// menu
if (argc == 2) {
solve(argv[1]);
exit(0);
}
menu_top:
printf("What would you like to do?\n");
printf("1) Solve a cube\n");
printf("2) Generate the corner table\n");
printf("3) Generate the first edge table\n");
printf("4) Generate the second edge table\n");
printf("Choose: ");
while (scanf("%d", &number) != 1 || number > 4 || number < 1) {
printf("\n");
printf("Please enter a valid choice.\n");
printf("Choose: ");
number = getchar();
}
printf("\n");
switch (number) {
case 1:
printf("\nPlease enter a raw cube string.\n" \
"Raw cube strings are 120 characters long and designate\n"\
"the colors on each side of all 20 cubies.\n"\
"You can generate this with the cube_convert.py script\n");
int result = scanf("%120s", cube_raw);
if (result < 1) {
printf("Cube string error");
goto menu_top;
}
printf("\n");
solve(cube_raw);
break;
case 2:
printf("Generating corner cube table\n");
printf("This can take upwards of 40 minutes. Ctrl-C to cancel\n");
printf("\n");
make_corner();
break;
case 3:
printf("Generating edge cube table\n");
printf("This can take upwards of 20 minutes. Ctrl-C to cancel\n");
printf("\n");
make_edge(1);
break;
case 4:
printf("Generating edge cube table\n");
printf("This can take upwards of 20 minutes. Ctrl-C to cancel\n");
printf("\n");
make_edge(2);
break;
}
return 0;
}