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