diff options
Diffstat (limited to 'src/commands.c')
| -rw-r--r-- | src/commands.c | 107 |
1 files changed, 92 insertions, 15 deletions
diff --git a/src/commands.c b/src/commands.c index 02af19f..fc63e7c 100644 --- a/src/commands.c +++ b/src/commands.c | |||
| @@ -8,12 +8,14 @@ CommandArgs * print_parse_args(int c, char **v); | |||
| 8 | CommandArgs * parse_only_scramble(int c, char **v); | 8 | CommandArgs * parse_only_scramble(int c, char **v); |
| 9 | CommandArgs * parse_no_arg(int c, char **v); | 9 | CommandArgs * parse_no_arg(int c, char **v); |
| 10 | CommandArgs * solve_parse_args(int c, char **v); | 10 | CommandArgs * solve_parse_args(int c, char **v); |
| 11 | CommandArgs * scramble_parse_args(int c, char **v); | ||
| 11 | 12 | ||
| 12 | /* Exec functions ************************************************************/ | 13 | /* Exec functions ************************************************************/ |
| 13 | 14 | ||
| 14 | static void gen_exec(CommandArgs *args); | 15 | static void gen_exec(CommandArgs *args); |
| 15 | static void invert_exec(CommandArgs *args); | 16 | static void invert_exec(CommandArgs *args); |
| 16 | static void solve_exec(CommandArgs *args); | 17 | static void solve_exec(CommandArgs *args); |
| 18 | static void scramble_exec(CommandArgs *args); | ||
| 17 | static void steps_exec(CommandArgs *args); | 19 | static void steps_exec(CommandArgs *args); |
| 18 | static void commands_exec(CommandArgs *args); | 20 | static void commands_exec(CommandArgs *args); |
| 19 | static void print_exec(CommandArgs *args); | 21 | static void print_exec(CommandArgs *args); |
| @@ -26,6 +28,7 @@ static void version_exec(CommandArgs *args); | |||
| 26 | /* Local functions ***********************************************************/ | 28 | /* Local functions ***********************************************************/ |
| 27 | 29 | ||
| 28 | static bool read_step(CommandArgs *args, char *str); | 30 | static bool read_step(CommandArgs *args, char *str); |
| 31 | static bool read_scrtype(CommandArgs *args, char *str); | ||
| 29 | static bool read_scramble(int c, char **v, CommandArgs *args); | 32 | static bool read_scramble(int c, char **v, CommandArgs *args); |
| 30 | 33 | ||
| 31 | /* Commands ******************************************************************/ | 34 | /* Commands ******************************************************************/ |
| @@ -40,6 +43,15 @@ solve_cmd = { | |||
| 40 | }; | 43 | }; |
| 41 | 44 | ||
| 42 | Command | 45 | Command |
| 46 | scramble_cmd = { | ||
| 47 | .name = "scramble", | ||
| 48 | .usage = "scramble [TYPE] [-n N]", | ||
| 49 | .description = "Get a random-position scramble", | ||
| 50 | .parse_args = scramble_parse_args, | ||
| 51 | .exec = scramble_exec, | ||
| 52 | }; | ||
| 53 | |||
| 54 | Command | ||
| 43 | gen_cmd = { | 55 | gen_cmd = { |
| 44 | .name = "gen", | 56 | .name = "gen", |
| 45 | .usage = "gen [-t N]", | 57 | .usage = "gen [-t N]", |
| @@ -137,6 +149,7 @@ Command *commands[NCOMMANDS] = { | |||
| 137 | &print_cmd, | 149 | &print_cmd, |
| 138 | &quit_cmd, | 150 | &quit_cmd, |
| 139 | &solve_cmd, | 151 | &solve_cmd, |
| 152 | &scramble_cmd, | ||
| 140 | &steps_cmd, | 153 | &steps_cmd, |
| 141 | &twophase_cmd, | 154 | &twophase_cmd, |
| 142 | &unniss_cmd, | 155 | &unniss_cmd, |
| @@ -243,6 +256,37 @@ solve_parse_args(int c, char **v) | |||
| 243 | } | 256 | } |
| 244 | 257 | ||
| 245 | CommandArgs * | 258 | CommandArgs * |
| 259 | scramble_parse_args(int c, char **v) | ||
| 260 | { | ||
| 261 | int i; | ||
| 262 | long val; | ||
| 263 | |||
| 264 | CommandArgs *a = new_args(); | ||
| 265 | |||
| 266 | a->success = true; | ||
| 267 | a->n = 1; | ||
| 268 | a->scrt = -1; | ||
| 269 | |||
| 270 | for (i = 0; i < c; i++) { | ||
| 271 | if (!strcmp(v[i], "-n") && i+1 < c) { | ||
| 272 | val = strtol(v[++i], NULL, 10); | ||
| 273 | if (val < 1 || val > 1000000) { | ||
| 274 | fprintf(stderr, | ||
| 275 | "Invalid number of scrambles.\n"); | ||
| 276 | a->success = false; | ||
| 277 | return a; | ||
| 278 | } | ||
| 279 | a->n = val; | ||
| 280 | } else if (!read_scrtype(a, v[i])) { | ||
| 281 | a->success = false; | ||
| 282 | return a; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | return a; | ||
| 287 | } | ||
| 288 | |||
| 289 | CommandArgs * | ||
| 246 | gen_parse_args(int c, char **v) | 290 | gen_parse_args(int c, char **v) |
| 247 | { | 291 | { |
| 248 | int val; | 292 | int val; |
| @@ -342,6 +386,26 @@ solve_exec(CommandArgs *args) | |||
| 342 | } | 386 | } |
| 343 | 387 | ||
| 344 | static void | 388 | static void |
| 389 | scramble_exec(CommandArgs *args) | ||
| 390 | { | ||
| 391 | Cube cube; | ||
| 392 | Alg *scr; | ||
| 393 | int i; | ||
| 394 | |||
| 395 | init_movesets(); | ||
| 396 | init_symcoord(); | ||
| 397 | |||
| 398 | srand(time(NULL)); | ||
| 399 | |||
| 400 | for (i = 0; i < args->n; i++) { | ||
| 401 | cube = random_cube(args->scrt); | ||
| 402 | scr = solve_2phase(cube, 1); | ||
| 403 | print_alg(scr, false); | ||
| 404 | free_alg(scr); | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | static void | ||
| 345 | gen_exec(CommandArgs *args) | 409 | gen_exec(CommandArgs *args) |
| 346 | { | 410 | { |
| 347 | int i; | 411 | int i; |
| @@ -454,21 +518,6 @@ version_exec(CommandArgs *args) | |||
| 454 | /* Local functions implementation ********************************************/ | 518 | /* Local functions implementation ********************************************/ |
| 455 | 519 | ||
| 456 | static bool | 520 | static bool |
| 457 | read_step(CommandArgs *args, char *str) | ||
| 458 | { | ||
| 459 | int i; | ||
| 460 | |||
| 461 | for (i = 0; i < NSTEPS; i++) { | ||
| 462 | if (steps[i] != NULL && !strcmp(steps[i]->shortname, str)) { | ||
| 463 | args->step = steps[i]; | ||
| 464 | return true; | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | return false; | ||
| 469 | } | ||
| 470 | |||
| 471 | static bool | ||
| 472 | read_scramble(int c, char **v, CommandArgs *args) | 521 | read_scramble(int c, char **v, CommandArgs *args) |
| 473 | { | 522 | { |
| 474 | int i, k, n; | 523 | int i, k, n; |
| @@ -510,6 +559,34 @@ read_scramble(int c, char **v, CommandArgs *args) | |||
| 510 | return args->scramble->len > 0; | 559 | return args->scramble->len > 0; |
| 511 | } | 560 | } |
| 512 | 561 | ||
| 562 | static bool | ||
| 563 | read_scrtype(CommandArgs *args, char *str) | ||
| 564 | { | ||
| 565 | int i; | ||
| 566 | |||
| 567 | args->scrt = -1; | ||
| 568 | for (i = 0; i < NSCRTYPES; i++) | ||
| 569 | if (!strcmp(scrtypes[i], str)) | ||
| 570 | args->scrt = i; | ||
| 571 | |||
| 572 | return args->scrt != -1; | ||
| 573 | } | ||
| 574 | |||
| 575 | static bool | ||
| 576 | read_step(CommandArgs *args, char *str) | ||
| 577 | { | ||
| 578 | int i; | ||
| 579 | |||
| 580 | for (i = 0; i < NSTEPS; i++) { | ||
| 581 | if (steps[i] != NULL && !strcmp(steps[i]->shortname, str)) { | ||
| 582 | args->step = steps[i]; | ||
| 583 | return true; | ||
| 584 | } | ||
| 585 | } | ||
| 586 | |||
| 587 | return false; | ||
| 588 | } | ||
| 589 | |||
| 513 | /* Public functions implementation *******************************************/ | 590 | /* Public functions implementation *******************************************/ |
| 514 | 591 | ||
| 515 | void | 592 | void |
