aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano.tronto@gmail.com>2021-11-11 21:37:34 +0100
committerSebastiano Tronto <sebastiano.tronto@gmail.com>2021-11-11 21:37:34 +0100
commit3568412f8f230774d0d11d7ed1c897424f95d3ef (patch)
tree77223792d8c925a9b1fc32b3f4341e943b5f8209 /src/main.c
parent67e1b5e6e6a2c917a2fe58a37a1382c982b1e5c5 (diff)
downloadnissy-3568412f8f230774d0d11d7ed1c897424f95d3ef.tar.gz
nissy-3568412f8f230774d0d11d7ed1c897424f95d3ef.zip
Rewritten from scratch. Welocme nissy 2.0!
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c937
1 files changed, 0 insertions, 937 deletions
diff --git a/src/main.c b/src/main.c
deleted file mode 100644
index a14e365..0000000
--- a/src/main.c
+++ /dev/null
@@ -1,937 +0,0 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <time.h>
4#include "utils.h"
5#include "coordinates.h"
6#include "io.h"
7#include "moves.h"
8#include "solver.h"
9#include "string.h"
10#include "helppages.h"
11/* Saved sequences of moves */
12int scr_count=1, tmp_count=1, max_tmp=999;
13int scrambles[255][255], tmp[1000][255];
14
15int read_moves_from_variable(char *id, int *dst) {
16 char c = id[0];
17 if (c != '$' && c != '@')
18 return -1;
19 int n = atoi(id+1);
20 if (n <= 0 || n >= (c == '$' ? scr_count : tmp_count))
21 return -1;
22 copy_moves(c == '$' ? scrambles[n] : tmp[n], dst);
23 return n;
24}
25
26int read_moves_from_argument(int n, char tok[][100], int *dst) {
27 int r = read_moves_from_variable(tok[0], dst);
28 return (r != -1) ? r : read_moves_from_tok(n, tok, dst);
29}
30
31void print_results(int n, int res[][30]) {
32 if (n == -1)
33 printf("Pre-conditions not satisfied (or other error).\n");
34
35 if (n == 0)
36 printf("No result found (try different bounds).\n");
37
38 if (n > 1)
39 printf("Found %d results.\n", n);
40 tmp_count = 1; /* Reset temporary count */
41 for (int i = 0; i < n; i++) {
42 if (i < max_tmp) {
43 copy_moves(res[i], tmp[tmp_count]);
44 printf("@%d:\t", tmp_count++);
45 } else {
46 printf(" \t");
47 }
48 print_moves(res[i]);
49 printf("(%d)\n", len(res[i]));
50 }
51}
52
53/* Removes extra white spaces from the input string */
54int parsecmd(char *cmd, char cmdtok[][100]) {
55 char *i = cmd, *j = cmd;
56 while (*j != '\n' && *j != EOF) {
57 *i = *j;
58 if (*i == ' ' || *i == '\t')
59 *i = ' ';
60 ++j;
61 if (*i == ' ' || *i == '\t')
62 while (*j == ' ' || *j == '\t')
63 ++j;
64 ++i;
65 }
66 if (*(i-1) == ' ')
67 *(i-1) = 0;
68 else
69 *i = 0;
70
71 int n = 0;
72 char *s = strtok(cmd, " ");
73 while (s != NULL) {
74 strcpy(cmdtok[n++], s);
75 s = strtok(NULL, " ");
76 }
77 return n;
78}
79
80void scramble_cmd(int n, char cmdtok[][100]) {
81 int c = 0, e = 0, dr = 0;
82
83 if (n > 2 || cmdtok[0][0] != 's') { /* Second case avoids warning */
84 printf("scramble: wrong syntax\n");
85 return;
86 } else if (n == 2) {
87 if (!strcmp(cmdtok[1], "c")) {
88 c = 1;
89 } else if (!strcmp(cmdtok[1], "e")) {
90 e = 1;
91 } else if (!strcmp(cmdtok[1], "dr")) {
92 dr = 1;
93 } else {
94 printf("scramble: wrong syntax\n");
95 return;
96 }
97 }
98
99 int scram[2][30];
100
101 srand(time(NULL));
102 int eofb = rand() % pow2to11;
103 int coud = rand() % pow3to7;
104 int ep = rand() % factorial12;
105 int cp = rand() % factorial8;
106
107 if (c) {
108 eofb = ep = 0;
109 } else if (e) {
110 coud = cp = 0;
111 } else if (dr) {
112 eofb = coud = 0;
113 int epud = rand() % factorial8;
114 int epe = rand() % factorial4;
115 int ep_arr[12];
116 epud_int_to_array(epud, ep_arr);
117 epe_int_to_array(epe, ep_arr);
118 ep = ep_array_to_int(ep_arr);
119 while (perm_sign_int(ep, 12) != perm_sign_int(cp, 8))
120 cp = (cp+1) % factorial8;
121 } else {
122 while (perm_sign_int(ep, 12) != perm_sign_int(cp, 8))
123 cp = (cp+1) % factorial8;
124 }
125
126 reach_state(eofb, coud, ep, cp, scram);
127 /* Debug */
128 /* printf("State: %d %d %d %d\n", eofb, coud, ep, cp); */
129 print_results(1, scram);
130}
131
132void save_cmd(int n, char cmdtok[][100]) {
133 int scram[255];
134 if (n == 1) {
135 if (read_moves_from_prompt(scram) == -1) {
136 printf("save: error reading moves. Not saved.\n");
137 return;
138 }
139 } else if (read_moves_from_argument(n-1, cmdtok+1, scram) == -1) {
140 printf("save: error reading moves or ID. Not saved.\n");
141 return;
142 }
143
144 copy_moves(scram, scrambles[scr_count]);
145
146 printf("$%d:\t", scr_count);
147 print_moves(scrambles[scr_count]);
148 printf("\n");
149 scr_count++;
150}
151
152void change_cmd(int n, char cmdtok[][100]) {
153 int id, scram[255];
154 if (n == 1) {
155 printf("change: you must specify an $ID.\n");
156 return;
157 } else if (cmdtok[1][0] != '$') {
158 printf("change: invalid $ID.\n");
159 return;
160 } else {
161 id = atoi(cmdtok[1]+1);
162 if (id <= 0 || id >= scr_count) {
163 printf("change: invalid $ID.\n");
164 return;
165 }
166 if (n == 2) {
167 if (read_moves_from_prompt(scram) == -1) {
168 printf("change: error reading moves.\n");
169 return;
170 }
171 } else if (read_moves_from_argument(n-2, cmdtok+2, scram) == -1 ) {
172 printf("change: error reading moves or ID.\n");
173 return;
174 }
175 }
176
177 copy_moves(scram, scrambles[id]);
178
179 printf("$%d:\t", id);
180 print_moves(scrambles[id]);
181 printf("\n");
182}
183
184void print_cmd(int n, char cmdtok[][100]) {
185 if (n == 1) {
186 for (int i = 1; i < scr_count; i++) {
187 printf("$%d:\t", i);
188 print_moves(scrambles[i]);
189 printf("\n");
190 }
191 } else if (n == 2) {
192 int i = atoi(cmdtok[1]+1);
193 char sign = cmdtok[1][0];
194 if (sign != '$' && sign != '@') {
195 printf("print: invalid ID (must start with $ or @).\n");
196 return;
197 }
198 if (i > 0 && i < (sign == '$' ? scr_count : tmp_count)) {
199 printf("%c%d:\t", sign, i);
200 print_moves(sign == '$' ? scrambles[i] : tmp[i]);
201 printf("\n");
202 } else {
203 printf("print: invalid ID.\n");
204 return;
205 }
206 } else {
207 printf("print: wrong syntax.\n");
208 }
209}
210
211void add_cmd(int n, char cmdtok[][100]) {
212 int id, scram[255];
213 if (n == 1) {
214 printf("add: you must specify a destination $ID.\n");
215 return;
216 } else if (cmdtok[n-1][0] != '$') {
217 printf("add: invalid destination $ID.\n");
218 return;
219 } else {
220 id = atoi(cmdtok[n-1]+1);
221 if (id <= 0 || id >= scr_count) {
222 printf("add: invalid destination $ID.\n");
223 return;
224 }
225 if (n == 2) {
226 if (read_moves_from_prompt(scram) == -1) {
227 printf("add: error reading moves.\n");
228 return;
229 }
230 } else {
231 if (read_moves_from_argument(n-2, cmdtok+1, scram) == -1) {
232 printf("add: error reading moves or ID.\n");
233 return;
234 }
235 }
236 }
237
238 append_moves(scram, scrambles[id]);
239
240 printf("$%d:\t", id);
241 print_moves(scrambles[id]);
242 printf("\n");
243}
244
245void invert_cmd(int n, char cmdtok[][100]) {
246 int scram[255];
247 if (n == 1) {
248 if (read_moves_from_prompt(scram) == -1) {
249 printf("invert: error reading moves.\n");
250 return;
251 }
252 } else if (read_moves_from_argument(n-1, cmdtok+1, scram) == -1) {
253 printf("invert: error reading moves or ID.\n");
254 return;
255 }
256
257 if (uses_niss(scram)) {
258 printf("invert: cannot invert NISS.\n");
259 return;
260 }
261
262 invert(scram, tmp[1]);
263 tmp_count = 2;
264
265 printf("@1:\t");
266 print_moves(tmp[1]);
267 printf("\n");
268}
269
270void unniss_cmd(int n, char cmdtok[][100]) {
271 int scram[255];
272 if (n == 1) {
273 if (read_moves_from_prompt(scram) == -1) {
274 printf("unniss: error reading moves.\n");
275 return;
276 }
277 } else if (read_moves_from_argument(n-1, cmdtok+1, scram) == -1) {
278 printf("unniss: error reading moves or ID.\n");
279 return;
280 }
281
282 unniss(scram, tmp[1]);
283 tmp_count = 2;
284
285 printf("@1:\t");
286 print_moves(tmp[1]);
287 printf("\n");
288}
289
290void pic_cmd(int n, char cmdtok[][100]) {
291 int scram[255];
292 if (n == 1) {
293 if (read_moves_from_prompt(scram) == -1) {
294 printf("pic: error reading moves.\n");
295 return;
296 }
297 } else if (read_moves_from_argument(n-1, cmdtok+1, scram) == -1) {
298 printf("pic: error reading moves or ID.\n");
299 return;
300 }
301 print_cube_scram(scram);
302}
303
304void solve_cmd(int n, char cmdtok[][100]) {
305 int m = 1, b = 25, optimal = 0;
306 int scram[255] = {[0] = 0};
307 int scram_unnissed[255];
308
309 /* Parse options */
310 for (int i = 1; i < n && scram[0] == 0; i++) {
311 if (!strncmp(cmdtok[i], "b=", 2)) {
312 b = atoi(cmdtok[i]+2);
313 if (b <= 0) {
314 printf("solve: bad option b.\n");
315 return;
316 }
317 } else if (!strncmp(cmdtok[i], "n=", 2)) {
318 m = atoi(cmdtok[i]+2);
319 if (m <= 0) {
320 printf("solve: bad option n.\n");
321 return;
322 }
323 } else if (!strcmp(cmdtok[i], "o")) {
324 optimal = 1;
325 } else if (read_moves_from_argument(n-i, cmdtok+i, scram) == -1) {
326 printf("solve: error reading moves or ID.\n");
327 return;
328 }
329 }
330
331 if (scram[0] == 0) {
332 if (read_moves_from_prompt(scram) == -1) {
333 printf("solve: error reading moves.\n");
334 return;
335 }
336 }
337
338 /* Call solver and print results */
339 unniss(scram, scram_unnissed);
340 int sol[m+2][30];
341 int s = solve_scram(scram_unnissed, sol, m, b, optimal);
342 print_results(s, sol);
343}
344
345void replace_cmd(int n, char cmdtok[][100]) {
346 int m = 10; /* max length */
347 int scram[255] = {[0] = 0};
348 int scram_unnissed[255];
349
350 /* Parse options */
351 for (int i = 1; i < n && scram[0] == 0; i++) {
352 if (!strncmp(cmdtok[i], "b=", 2)) {
353 m = atoi(cmdtok[i]+2);
354 if (m <= 0) {
355 printf("replace: bad option n.\n");
356 return;
357 }
358 } else if (read_moves_from_argument(n-i, cmdtok+i, scram) == -1) {
359 printf("replace: error reading moves or ID.\n");
360 return;
361 }
362 }
363
364 if (scram[0] == 0) {
365 if (read_moves_from_prompt(scram) == -1) {
366 printf("replace: error reading moves.\n");
367 return;
368 }
369 }
370
371 unniss(scram, scram_unnissed);
372 int l = len(scram_unnissed);
373 int aux1[255], aux2[15][30], aux3[30];
374 for (int i = 0; i < l; i++) {
375 for (int j = 2; j <= m && i + j <= l; j++) {
376 copy_moves(scram_unnissed+i, aux1);
377 aux1[j] = 0;
378 int s = solve_scram(aux1, aux2, 10, j-1, 1);
379 for (int k = 0; k < s; k++) {
380 invert(aux2[k], aux3);
381 /* TODO: the following part should also chek for the case when
382 * the last moves are R L or similar. */
383 if (aux3[0] != aux1[0] && aux3[len(aux3)-1] != aux1[len(aux1)-1]) {
384 printf("Replace [ ");
385 print_moves(aux1);
386 printf("] (moves %d-%d) with: [ ", i+1, i+j);
387 print_moves(aux3);
388 printf("] (-%d+%d)\n", j, len(aux3));
389 }
390 }
391 }
392 }
393}
394
395void clear_cmd(int n, char cmdtok[][100]) {
396 if (n > 1 || cmdtok[0][0] != 'c') { /* Avoid unused variable warning */
397 printf("clear: syntax error.\n");
398 return;
399 }
400 scr_count = tmp_count = 1;
401}
402
403void eo_cmd(int n, char cmdtok[][100]) {
404
405 /* Default values */
406 int m = 1, b = 20;
407 int niss = 0, hide = 1;
408 int fb = 1, rl = 1, ud = 1;
409 int scram[255] = {[0] = 0};
410 int scram_unnissed[255];
411
412 /* Parse options */
413 for (int i = 1; i < n && scram[0] == 0; i++) {
414 if (!strcmp(cmdtok[i], "h")) {
415 hide = 0;
416 } else if (!strcmp(cmdtok[i], "niss")) {
417 niss = 1;
418 } else if (!strncmp(cmdtok[i], "axis=", 5)) {
419 fb = rl = ud = 0;
420 if (strstr(cmdtok[i], "fb") != NULL)
421 fb = 1;
422 if (strstr(cmdtok[i], "rl") != NULL)
423 rl = 1;
424 if (strstr(cmdtok[i], "ud") != NULL)
425 ud = 1;
426 if (fb + rl + ud == 0) {
427 printf("eo: bad axis option.\n");
428 return;
429 }
430 } else if (!strncmp(cmdtok[i], "n=", 2)) {
431 m = atoi(cmdtok[i]+2);
432 if (m <= 0) {
433 printf("eo: bad option n.\n");
434 return;
435 }
436 } else if (!strncmp(cmdtok[i], "b=", 2)) {
437 b = atoi(cmdtok[i]+2);
438 if (b <= 0) {
439 printf("eo: bad option b.\n");
440 return;
441 }
442 } else if (read_moves_from_argument(n-i, cmdtok+i, scram) == -1) {
443 printf("eo: error reading moves or ID.\n");
444 return;
445 }
446 }
447
448 if (scram[0] == 0) {
449 if (read_moves_from_prompt(scram) == -1) {
450 printf("eo: error reading moves.\n");
451 return;
452 }
453 }
454
455 unniss(scram, scram_unnissed);
456
457 /* Call solver and print results */
458 int eo_list[m+5][30];
459 int neo = eo_scram_spam(scram_unnissed, eo_list, fb, rl, ud, m, b, niss,
460 hide);
461 print_results(neo, eo_list);
462}
463
464void co_cmd(int n, char cmdtok[][100]) {
465
466 /* Default values */
467 int m = 1, b = 20, ignore = 0;
468 int niss = 0, hide = 1;
469 int fb = 1, rl = 1, ud = 1;
470 int scram[255] = {[0] = 0};
471 int scram_unnissed[255];
472
473 /* Parse options */
474 for (int i = 1; i < n && scram[0] == 0; i++) {
475 if (!strcmp(cmdtok[i], "h")) {
476 hide = 0;
477 } else if (!strcmp(cmdtok[i], "niss")) {
478 niss = 1;
479 } else if (!strcmp(cmdtok[i], "i")) {
480 ignore = 1;
481 } else if (!strncmp(cmdtok[i], "axis=", 5)) {
482 fb = rl = ud = 0;
483 if (strstr(cmdtok[i], "fb") != NULL)
484 fb = 1;
485 if (strstr(cmdtok[i], "rl") != NULL)
486 rl = 1;
487 if (strstr(cmdtok[i], "ud") != NULL)
488 ud = 1;
489 if (fb + rl + ud == 0) {
490 printf("co: bad axis option.\n");
491 return;
492 }
493 } else if (!strncmp(cmdtok[i], "n=", 2)) {
494 m = atoi(cmdtok[i]+2);
495 if (m <= 0) {
496 printf("co: bad option n.\n");
497 return;
498 }
499 } else if (!strncmp(cmdtok[i], "b=", 2)) {
500 b = atoi(cmdtok[i]+2);
501 if (b <= 0) {
502 printf("co: bad option b.\n");
503 return;
504 }
505 } else if (read_moves_from_argument(n-i, cmdtok+i, scram) == -1) {
506 printf("co: error reading moves or ID.\n");
507 return;
508 }
509 }
510
511 if (scram[0] == 0) {
512 if (read_moves_from_prompt(scram) == -1) {
513 printf("co: error reading moves.\n");
514 return;
515 }
516 }
517
518 unniss(scram, scram_unnissed);
519
520 /* Call solver and print results */
521 int co_list[m+5][30];
522 int nco = co_scram_spam(scram_unnissed, co_list, fb, rl, ud, m, b, niss,
523 hide, ignore);
524 print_results(nco, co_list);
525}
526
527void dr_cmd(int n, char cmdtok[][100]) {
528
529 /* Default values */
530 int m = 1, b = 20;
531 int niss = 0, hide = 1;
532 int from = 0; /* 0: direct dr; {1,2,3}: from {eofb,eorl,eoud} */
533 int fb = 1, rl = 1, ud = 1;
534 int scram[255] = {[0] = 0};
535 int scram_unnissed[255];
536
537 /* Parse options */
538 for (int i = 1; i < n && scram[0] == 0; i++) {
539 if (!strcmp(cmdtok[i], "h")) {
540 hide = 0;
541 } else if (!strcmp(cmdtok[i], "niss")) {
542 niss = 1;
543 } else if (!strncmp(cmdtok[i], "axis=", 5)) {
544 fb = rl = ud = 0;
545 if (strstr(cmdtok[i], "fb") != NULL)
546 fb = 1;
547 if (strstr(cmdtok[i], "rl") != NULL)
548 rl = 1;
549 if (strstr(cmdtok[i], "ud") != NULL)
550 ud = 1;
551 if (fb + rl + ud == 0) {
552 printf("dr: bad axis option.\n");
553 return;
554 }
555 } else if (!strncmp(cmdtok[i], "n=", 2)) {
556 m = atoi(cmdtok[i]+2);
557 if (m <= 0) {
558 printf("dr: bad option n.\n");
559 return;
560 }
561 } else if (!strncmp(cmdtok[i], "b=", 2)) {
562 b = atoi(cmdtok[i]+2);
563 if (b <= 0) {
564 printf("dr: bad option b.\n");
565 return;
566 }
567 } else if (!strcmp(cmdtok[i], "from")) {
568 i++;
569 char x[3][3] = {"fb", "rl", "ud"};
570 for (int j = 0; j < 3; j++)
571 if (!strcmp(cmdtok[i], x[j]))
572 from = j+1;
573 if (!from) {
574 printf("dr: bad from option.\n");
575 return;
576 }
577 } else if (read_moves_from_argument(n-i, cmdtok+i, scram) == -1) {
578 printf("dr: error reading moves or ID.\n");
579 return;
580 }
581 }
582
583 if (scram[0] == 0) {
584 if (read_moves_from_prompt(scram) == -1) {
585 printf("dr: error reading moves.\n");
586 return;
587 }
588 }
589
590 unniss(scram, scram_unnissed);
591
592 /* Call solver */
593 int dr_list[m+5][30], ndr;
594 if (from) {
595 ndr = drfrom_scram_spam(scram_unnissed, dr_list, from, fb, rl, ud,
596 m, b, niss, hide);
597 if (ndr == -1) {
598 printf("dr: from given, but EO not found (possibly other error).\n");
599 return;
600 }
601 } else {
602 if (niss)
603 printf("Warning: not using NISS for direct DR.\n");
604 ndr = dr_scram_spam(scram_unnissed, dr_list, fb, rl, ud, m, b, hide);
605 }
606 print_results(ndr, dr_list);
607}
608
609void htr_cmd(int n, char cmdtok[][100]) {
610
611 /* Default values */
612 int m = 1, b = 20;
613 int niss = 0, hide = 1;
614 int from = 0; /* 0: unspecified; {1,2,3}: from {ud,fb,rl} */
615 int scram[255] = {[0] = 0};
616 int scram_unnissed[255];
617
618 /* Parse options */
619 for (int i = 1; i < n && scram[0] == 0; i++) {
620 if (!strcmp(cmdtok[i], "h")) {
621 hide = 0;
622 } else if (!strcmp(cmdtok[i], "niss")) {
623 niss = 1;
624 } else if (!strncmp(cmdtok[i], "n=", 2)) {
625 m = atoi(cmdtok[i]+2);
626 if (m <= 0) {
627 printf("htr: bad option n.\n");
628 return;
629 }
630 } else if (!strncmp(cmdtok[i], "b=", 2)) {
631 b = atoi(cmdtok[i]+2);
632 if (b <= 0) {
633 printf("htr: bad option b.\n");
634 return;
635 }
636 } else if (!strcmp(cmdtok[i], "from")) {
637 i++;
638 char x[3][3] = {"ud", "fb", "rl"};
639 for (int j = 0; j < 3; j++)
640 if (!strcmp(cmdtok[i], x[j]))
641 from = j+1;
642 if (!from) {
643 printf("htr: bad from option.\n");
644 return;
645 }
646 } else if (read_moves_from_argument(n-i, cmdtok+i, scram) == -1) {
647 printf("htr: error reading moves or ID.\n");
648 return;
649 }
650 }
651
652 if (scram[0] == 0) {
653 if (read_moves_from_prompt(scram) == -1) {
654 printf("htr: error reading moves.\n");
655 return;
656 }
657 }
658
659 unniss(scram, scram_unnissed);
660
661 /* Call solver */
662 int htr_list[m+5][30], nhtr;
663 nhtr = htr_scram_spam(scram_unnissed, htr_list, from, m, b, niss, hide);
664 print_results(nhtr, htr_list);
665}
666
667void drfinish_cmd(int n, char cmdtok[][100]) {
668 /* Default values */
669 int m = 1, b = 20;
670 int from = 0; /* 0: unspecified; {1,2,3}: from {ud,fb,rl} */
671 int scram[255] = {[0] = 0};
672 int scram_unnissed[255];
673
674 /* Parse options */
675 for (int i = 1; i < n && scram[0] == 0; i++) {
676 if (!strncmp(cmdtok[i], "n=", 2)) {
677 m = atoi(cmdtok[i]+2);
678 if (m <= 0) {
679 printf("drfinish: bad option n.\n");
680 return;
681 }
682 } else if (!strncmp(cmdtok[i], "b=", 2)) {
683 b = atoi(cmdtok[i]+2);
684 if (b <= 0) {
685 printf("drfinish: bad option b.\n");
686 return;
687 }
688 } else if (!strcmp(cmdtok[i], "from")) {
689 i++;
690 char x[3][3] = {"ud", "fb", "rl"};
691 for (int j = 0; j < 3; j++)
692 if (!strcmp(cmdtok[i], x[j]))
693 from = j+1;
694 if (!from) {
695 printf("drfinish: bad from option.\n");
696 return;
697 }
698 } else if (read_moves_from_argument(n-i, cmdtok+i, scram) == -1) {
699 printf("drfinish: error reading moves or ID.\n");
700 return;
701 }
702 }
703
704 if (scram[0] == 0) {
705 if (read_moves_from_prompt(scram) == -1) {
706 printf("drfinish: error reading moves.\n");
707 return;
708 }
709 }
710
711 unniss(scram, scram_unnissed);
712
713 /* Call solver */
714 int c_list[m+5][30], nc;
715 nc = dr_finish_scram_spam(scram_unnissed, c_list, from, m, b);
716 print_results(nc, c_list);
717}
718
719void htrfinish_cmd(int n, char cmdtok[][100]) {
720 /* Default values */
721 int m = 1, b = 20;
722 int scram[255] = {[0] = 0};
723 int scram_unnissed[255];
724
725 /* Parse options */
726 for (int i = 1; i < n && scram[0] == 0; i++) {
727 if (!strncmp(cmdtok[i], "n=", 2)) {
728 m = atoi(cmdtok[i]+2);
729 if (m <= 0) {
730 printf("htrfinish: bad option n.\n");
731 return;
732 }
733 } else if (!strncmp(cmdtok[i], "b=", 2)) {
734 b = atoi(cmdtok[i]+2);
735 if (b <= 0) {
736 printf("htrfinish: bad option b.\n");
737 return;
738 }
739 } else if (read_moves_from_argument(n-i, cmdtok+i, scram) == -1) {
740 printf("htrfinish: error reading moves or ID.\n");
741 return;
742 }
743 }
744
745 if (scram[0] == 0) {
746 if (read_moves_from_prompt(scram) == -1) {
747 printf("htrfinish: error reading moves.\n");
748 return;
749 }
750 }
751
752 unniss(scram, scram_unnissed);
753
754 /* Call solver */
755 int c_list[m+5][30], nc;
756 nc = htr_finish_scram_spam(scram_unnissed, c_list, m, b);
757 print_results(nc, c_list);
758}
759
760void drcorners_cmd(int n, char cmdtok[][100]) {
761 /* Default values */
762 int m = 1, b = 20, ignore = 0;
763 int from = 0; /* 0: unspecified; {1,2,3}: from {ud,fb,rl} */
764 int scram[255] = {[0] = 0};
765 int scram_unnissed[255];
766
767 /* Parse options */
768 for (int i = 1; i < n && scram[0] == 0; i++) {
769 if (!strncmp(cmdtok[i], "n=", 2)) {
770 m = atoi(cmdtok[i]+2);
771 if (m <= 0) {
772 printf("drcorners: bad option n.\n");
773 return;
774 }
775 } else if (!strncmp(cmdtok[i], "b=", 2)) {
776 b = atoi(cmdtok[i]+2);
777 if (b <= 0) {
778 printf("drcorners: bad option b.\n");
779 return;
780 }
781 } else if (!strcmp(cmdtok[i], "i")) {
782 ignore = 1;
783 } else if (!strcmp(cmdtok[i], "from")) {
784 i++;
785 char x[3][3] = {"ud", "fb", "rl"};
786 for (int j = 0; j < 3; j++)
787 if (!strcmp(cmdtok[i], x[j]))
788 from = j+1;
789 if (!from) {
790 printf("drcorners: bad from option.\n");
791 return;
792 }
793 } else if (read_moves_from_argument(n-i, cmdtok+i, scram) == -1) {
794 printf("drcorners: error reading moves or ID.\n");
795 return;
796 }
797 }
798
799 if (scram[0] == 0) {
800 if (read_moves_from_prompt(scram) == -1) {
801 printf("drcorners: error reading moves.\n");
802 return;
803 }
804 }
805
806 unniss(scram, scram_unnissed);
807
808 /* Call solver */
809 int c_list[m+5][30], nc;
810 nc = dr_corners_scram_spam(scram_unnissed, c_list, from, m, b, ignore);
811 print_results(nc, c_list);
812}
813
814
815void exit_quit_cmd(int n, char cmdtok[][100]) {
816 if (n == 1)
817 exit(0);
818 else
819 printf("%s: wrong synstax.\n", cmdtok[0]);
820}
821
822/***************************************************************/
823/* List of all commands */
824/* Important: they must be in the same order in the two arrays */
825/***************************************************************/
826
827char *commands[][10] = {
828 {"help", "[COMMAND]",
829 "Print this help, or a help page for COMMAND."},
830 {"scramble", "[OPTIONS]",
831 "Prints a random-state scramble."},
832 {"save", "[MOVES|@ID|$ID]",
833 "Save or copy a scramble."},
834 {"change", "$ID1 [MOVES|$ID2|@ID2]",
835 "Change a memorized scramble."},
836 {"print", "[$ID|@ID]",
837 "Print memorized sequences."},
838 {"add", "[MOVES|$ID1|@ID1] $ID2",
839 "Add moves at the end of a memorized scramble."},
840 {"invert", "[MOVES|$ID|@ID]",
841 "Inverts the given sequence of moves."},
842 {"unniss", "[MOVES|$ID|@ID]}",
843 "Removes NISS: A (B) -> B\' A."},
844 {"pic", "[MOVES|$ID|@ID]",
845 "Show a text description of the scrambled cube."},
846 {"solve", "[MOVES|$ID|@ID]",
847 "Solves a scramble."},
848 {"replace", "[MOVES|$ID|@ID]",
849 "Find non-optimal subsequences."},
850 {"clear", "",
851 "Delete saved scrambles and output sequences."},
852 {"eo", "[MOVES|$ID|@ID]",
853 "Solves EO."},
854 {"co", "[MOVES|$ID|@ID]",
855 "Solves CO."},
856 {"dr", "[MOVES|$ID|@ID]",
857 "Solves DR, either directly or from eo."},
858 {"htr", "[MOVES|$ID|@ID]",
859 "Solves HTR from DR."},
860 {"drfinish", "[MOVES|$ID|@ID]",
861 "Solves the cube after DR."},
862 {"htrfinish", "[MOVES|$ID|@ID]",
863 "Solves the cube using only half turns."},
864 {"drcorners", "[MOVES|$ID|@ID]",
865 "Solves corners after DR."},
866 {"exit", "",
867 "Exit nissy."},
868 {"quit", "",
869 "Exit nissy."},
870 {"", "", ""}
871};
872
873void help_cmd(int n, char cmdtok[][100]) {
874 if (n == 1) {
875 printf("\n");
876 for (int i = 0; commands[i][0][0]; i++)
877 printf("%-10s%-25s%s\n", commands[i][0], commands[i][1], commands[i][2]);
878 printf("\n");
879 printf("Type \'help\' followed by a command for a detailed help page.\n");
880 printf("Type \'help nissy\' for a general user guide.\n");
881 } else if (n == 2) {
882 for (int i = 0; i < Npages; i++) {
883 if (!strcmp(helppages[i][0], cmdtok[1])) {
884 printf("%s", helppages[i][1]);
885 return;
886 }
887 }
888 printf("No help page for %s.\n", cmdtok[1]);
889 return;
890 } else {
891 printf("help: wrong syntax.\n");
892 }
893}
894
895void (*cmd_list[])(int n, char cmdtok[][100]) = {
896 help_cmd, scramble_cmd, save_cmd, change_cmd, print_cmd,
897 add_cmd, invert_cmd, unniss_cmd, pic_cmd,
898 solve_cmd, replace_cmd, clear_cmd,
899 eo_cmd, co_cmd, dr_cmd, htr_cmd,
900 drfinish_cmd, htrfinish_cmd, drcorners_cmd,
901 exit_quit_cmd, exit_quit_cmd, NULL
902};
903
904
905void execcmd(int n, char cmdtok[][100]) {
906 int i = 0;
907 while (strcmp(commands[i][0], cmdtok[0]) && strcmp(commands[i][0], ""))
908 i++;
909 if (strcmp(commands[i][0], ""))
910 (*cmd_list[i])(n, cmdtok);
911 else
912 printf("%s: not a command.\n", cmdtok[0]);
913}
914
915
916/* Main loop */
917
918int main() {
919 init_transition_table();
920 init_possible_next();
921
922 printf("Type help for a list of commands.\n");
923
924 char cmd[1000] = "";
925 while (1) {
926 printf("nissy-# ");
927 if (fgets(cmd, 1000, stdin) == NULL)
928 break;
929 char cmdtok[100][100];
930 int n = parsecmd(cmd, cmdtok);
931 if (n == 0)
932 continue;
933 execcmd(n, cmdtok);
934 }
935
936 return 0;
937}

Generated with cgit - Back to sebastiano.tronto.net