aboutsummaryrefslogtreecommitdiff
path: root/src/commands.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano.tronto@gmail.com>2021-12-26 22:42:37 +0100
committerSebastiano Tronto <sebastiano.tronto@gmail.com>2021-12-26 22:42:37 +0100
commit8cec37cb3e490b06639f2e05df8b947a9333d5d2 (patch)
treedcb1039625ae9f7afca90a0348891d29497edd17 /src/commands.c
parent975e3eafbae15b93a1a4f160dd781d359a6c717b (diff)
downloadnissy-8cec37cb3e490b06639f2e05df8b947a9333d5d2.tar.gz
nissy-8cec37cb3e490b06639f2e05df8b947a9333d5d2.zip
Added scramble command
Diffstat (limited to '')
-rw-r--r--src/commands.c107
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);
8CommandArgs * parse_only_scramble(int c, char **v); 8CommandArgs * parse_only_scramble(int c, char **v);
9CommandArgs * parse_no_arg(int c, char **v); 9CommandArgs * parse_no_arg(int c, char **v);
10CommandArgs * solve_parse_args(int c, char **v); 10CommandArgs * solve_parse_args(int c, char **v);
11CommandArgs * scramble_parse_args(int c, char **v);
11 12
12/* Exec functions ************************************************************/ 13/* Exec functions ************************************************************/
13 14
14static void gen_exec(CommandArgs *args); 15static void gen_exec(CommandArgs *args);
15static void invert_exec(CommandArgs *args); 16static void invert_exec(CommandArgs *args);
16static void solve_exec(CommandArgs *args); 17static void solve_exec(CommandArgs *args);
18static void scramble_exec(CommandArgs *args);
17static void steps_exec(CommandArgs *args); 19static void steps_exec(CommandArgs *args);
18static void commands_exec(CommandArgs *args); 20static void commands_exec(CommandArgs *args);
19static void print_exec(CommandArgs *args); 21static void print_exec(CommandArgs *args);
@@ -26,6 +28,7 @@ static void version_exec(CommandArgs *args);
26/* Local functions ***********************************************************/ 28/* Local functions ***********************************************************/
27 29
28static bool read_step(CommandArgs *args, char *str); 30static bool read_step(CommandArgs *args, char *str);
31static bool read_scrtype(CommandArgs *args, char *str);
29static bool read_scramble(int c, char **v, CommandArgs *args); 32static 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
42Command 45Command
46scramble_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
54Command
43gen_cmd = { 55gen_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
245CommandArgs * 258CommandArgs *
259scramble_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
289CommandArgs *
246gen_parse_args(int c, char **v) 290gen_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
344static void 388static void
389scramble_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
408static void
345gen_exec(CommandArgs *args) 409gen_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
456static bool 520static bool
457read_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
471static bool
472read_scramble(int c, char **v, CommandArgs *args) 521read_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
562static bool
563read_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
575static bool
576read_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
515void 592void

Generated with cgit - Back to sebastiano.tronto.net