diff options
Diffstat (limited to '')
| -rw-r--r-- | src/main.c | 211 |
1 files changed, 142 insertions, 69 deletions
| @@ -8,51 +8,6 @@ | |||
| 8 | #include "solver.h" | 8 | #include "solver.h" |
| 9 | #include "string.h" | 9 | #include "string.h" |
| 10 | #include "helppages.h" | 10 | #include "helppages.h" |
| 11 | |||
| 12 | char *commands[][10] = { | ||
| 13 | {"help", "[COMMAND]", | ||
| 14 | "Print this help, or a help page for COMMAND."}, | ||
| 15 | {"scramble", "[OPTIONS]", | ||
| 16 | "Prints a random-state scramble."}, | ||
| 17 | {"save", "[MOVES|@ID|$ID]", | ||
| 18 | "Save or copy a scramble."}, | ||
| 19 | {"change", "$ID1 [MOVES|$ID2|@ID2]", | ||
| 20 | "Change a memorized scramble."}, | ||
| 21 | {"print", "[$ID|@ID]", | ||
| 22 | "Print memorized sequences."}, | ||
| 23 | {"add", "[MOVES|$ID1|@ID1] $ID2", | ||
| 24 | "Add moves at the end of a memorized scramble."}, | ||
| 25 | {"invert", "[MOVES|$ID|@ID]", | ||
| 26 | "Inverts the given sequence of moves."}, | ||
| 27 | {"unniss", "[MOVES|$ID|@ID]}", | ||
| 28 | "Removes NISS: A (B) -> B\' A."}, | ||
| 29 | {"pic", "[MOVES|$ID|@ID]", | ||
| 30 | "Show a text description of the scrambled cube."}, | ||
| 31 | {"solve", "[MOVES|$ID|@ID]", | ||
| 32 | "Solves a scramble."}, | ||
| 33 | {"replace", "[MOVES|$ID|@ID]", | ||
| 34 | "Find non-optimal subsequences."}, | ||
| 35 | {"clear", "", | ||
| 36 | "Delete saved scrambles and output sequences."}, | ||
| 37 | {"eo", "[MOVES|$ID|@ID]", | ||
| 38 | "Solves EO."}, | ||
| 39 | {"dr", "[MOVES|$ID|@ID]", | ||
| 40 | "Solves DR, either directly or from eo."}, | ||
| 41 | {"htr", "[MOVES|$ID|@ID]", | ||
| 42 | "Solves HTR from DR."}, | ||
| 43 | {"drfinish", "[MOVES|$ID|@ID]", | ||
| 44 | "Solves the cube after DR."}, | ||
| 45 | {"htrfinish", "[MOVES|$ID|@ID]", | ||
| 46 | "Solves the cube using only half turns."}, | ||
| 47 | {"drcorners", "[MOVES|$ID|@ID]", | ||
| 48 | "Solves corners after DR."}, | ||
| 49 | {"exit", "", | ||
| 50 | "Exit nissy."}, | ||
| 51 | {"quit", "", | ||
| 52 | "Exit nissy."}, | ||
| 53 | {"", "", ""} | ||
| 54 | }; | ||
| 55 | |||
| 56 | /* Saved sequences of moves */ | 11 | /* Saved sequences of moves */ |
| 57 | int scr_count=1, tmp_count=1, max_tmp=999; | 12 | int scr_count=1, tmp_count=1, max_tmp=999; |
| 58 | int scrambles[255][255], tmp[1000][255]; | 13 | int scrambles[255][255], tmp[1000][255]; |
| @@ -122,28 +77,6 @@ int parsecmd(char *cmd, char cmdtok[][100]) { | |||
| 122 | return n; | 77 | return n; |
| 123 | } | 78 | } |
| 124 | 79 | ||
| 125 | void help_cmd(int n, char cmdtok[][100]) { | ||
| 126 | if (n == 1) { | ||
| 127 | printf("\n"); | ||
| 128 | for (int i = 0; commands[i][0][0]; i++) | ||
| 129 | printf("%-10s%-25s%s\n", commands[i][0], commands[i][1], commands[i][2]); | ||
| 130 | printf("\n"); | ||
| 131 | printf("Type \'help\' followed by a command for a detailed help page.\n"); | ||
| 132 | printf("Type \'help nissy\' for a general user guide.\n"); | ||
| 133 | } else if (n == 2) { | ||
| 134 | for (int i = 0; i < Npages; i++) { | ||
| 135 | if (!strcmp(helppages[i][0], cmdtok[1])) { | ||
| 136 | printf("%s", helppages[i][1]); | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | printf("No help page for %s.\n", cmdtok[1]); | ||
| 141 | return; | ||
| 142 | } else { | ||
| 143 | printf("help: wrong syntax.\n"); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | void scramble_cmd(int n, char cmdtok[][100]) { | 80 | void scramble_cmd(int n, char cmdtok[][100]) { |
| 148 | int c = 0, e = 0, dr = 0; | 81 | int c = 0, e = 0, dr = 0; |
| 149 | 82 | ||
| @@ -528,6 +461,69 @@ void eo_cmd(int n, char cmdtok[][100]) { | |||
| 528 | print_results(neo, eo_list); | 461 | print_results(neo, eo_list); |
| 529 | } | 462 | } |
| 530 | 463 | ||
| 464 | void 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 | |||
| 531 | void dr_cmd(int n, char cmdtok[][100]) { | 527 | void dr_cmd(int n, char cmdtok[][100]) { |
| 532 | 528 | ||
| 533 | /* Default values */ | 529 | /* Default values */ |
| @@ -763,7 +759,7 @@ void htrfinish_cmd(int n, char cmdtok[][100]) { | |||
| 763 | 759 | ||
| 764 | void drcorners_cmd(int n, char cmdtok[][100]) { | 760 | void drcorners_cmd(int n, char cmdtok[][100]) { |
| 765 | /* Default values */ | 761 | /* Default values */ |
| 766 | int m = 1, b = 20, ignore=0; | 762 | int m = 1, b = 20, ignore = 0; |
| 767 | int from = 0; /* 0: unspecified; {1,2,3}: from {ud,fb,rl} */ | 763 | int from = 0; /* 0: unspecified; {1,2,3}: from {ud,fb,rl} */ |
| 768 | int scram[255] = {[0] = 0}; | 764 | int scram[255] = {[0] = 0}; |
| 769 | int scram_unnissed[255]; | 765 | int scram_unnissed[255]; |
| @@ -823,15 +819,89 @@ void exit_quit_cmd(int n, char cmdtok[][100]) { | |||
| 823 | printf("%s: wrong synstax.\n", cmdtok[0]); | 819 | printf("%s: wrong synstax.\n", cmdtok[0]); |
| 824 | } | 820 | } |
| 825 | 821 | ||
| 822 | /***************************************************************/ | ||
| 823 | /* List of all commands */ | ||
| 824 | /* Important: they must be in the same order in the two arrays */ | ||
| 825 | /***************************************************************/ | ||
| 826 | |||
| 827 | char *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 | |||
| 873 | void 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 | |||
| 826 | void (*cmd_list[])(int n, char cmdtok[][100]) = { | 895 | void (*cmd_list[])(int n, char cmdtok[][100]) = { |
| 827 | help_cmd, scramble_cmd, save_cmd, change_cmd, print_cmd, | 896 | help_cmd, scramble_cmd, save_cmd, change_cmd, print_cmd, |
| 828 | add_cmd, invert_cmd, unniss_cmd, pic_cmd, | 897 | add_cmd, invert_cmd, unniss_cmd, pic_cmd, |
| 829 | solve_cmd, replace_cmd, clear_cmd, | 898 | solve_cmd, replace_cmd, clear_cmd, |
| 830 | eo_cmd, dr_cmd, htr_cmd, | 899 | eo_cmd, co_cmd, dr_cmd, htr_cmd, |
| 831 | drfinish_cmd, htrfinish_cmd, drcorners_cmd, | 900 | drfinish_cmd, htrfinish_cmd, drcorners_cmd, |
| 832 | exit_quit_cmd, exit_quit_cmd, NULL | 901 | exit_quit_cmd, exit_quit_cmd, NULL |
| 833 | }; | 902 | }; |
| 834 | 903 | ||
| 904 | |||
| 835 | void execcmd(int n, char cmdtok[][100]) { | 905 | void execcmd(int n, char cmdtok[][100]) { |
| 836 | int i = 0; | 906 | int i = 0; |
| 837 | while (strcmp(commands[i][0], cmdtok[0]) && strcmp(commands[i][0], "")) | 907 | while (strcmp(commands[i][0], cmdtok[0]) && strcmp(commands[i][0], "")) |
| @@ -842,6 +912,9 @@ void execcmd(int n, char cmdtok[][100]) { | |||
| 842 | printf("%s: not a command.\n", cmdtok[0]); | 912 | printf("%s: not a command.\n", cmdtok[0]); |
| 843 | } | 913 | } |
| 844 | 914 | ||
| 915 | |||
| 916 | /* Main loop */ | ||
| 917 | |||
| 845 | int main() { | 918 | int main() { |
| 846 | init_transition_table(); | 919 | init_transition_table(); |
| 847 | init_possible_next(); | 920 | init_possible_next(); |
