-
Notifications
You must be signed in to change notification settings - Fork 17
/
cube.c
729 lines (671 loc) · 21.7 KB
/
cube.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#pragma pack(1)
typedef struct tagCUBE { // CUBE state node
struct tagCUBE *parent; // pointer to parent node
char f[3][3]; // front
char b[3][3]; // back
char u[3][3]; // up
char d[3][3]; // down
char l[3][3]; // left
char r[3][3]; // right
char op;
} CUBE;
#pragma pack()
typedef struct { // LINEITEM used to implement cube line rotation
int stride;
char *buffer;
} LINEITEM;
static void line_rotate90(LINEITEM item[5]) // do line rotation 90 degree
{
int i, j;
for (i=0; i<5; i++) {
int dst_idx = (i+0) % 5;
int src_idx = (i+1) % 5;
char *dst_buf = item[dst_idx].buffer;
char *src_buf = item[src_idx].buffer;
int dst_stride = item[dst_idx].stride;
int src_stride = item[src_idx].stride;
for (j=0; j<3; j++) {
*dst_buf = *src_buf;
dst_buf += dst_stride;
src_buf += src_stride;
}
}
}
static void surface_rotate90(char buf[3][3]) // do surface rotation 90 degree
{
char tmp[3][3];
int i, j;
memcpy(tmp, buf, sizeof(tmp));
for (i=0; i<3; i++) {
for (j=0; j<3; j++) {
buf[i][j] = tmp[3-j-1][i];
}
}
}
static void cube_init(CUBE *c)
{
memset(c->f, 'w', sizeof(c->f)); // 'w' - white
memset(c->b, 'y', sizeof(c->b)); // 'y' - yellow
memset(c->u, 'b', sizeof(c->u)); // 'b' - blue
memset(c->d, 'g', sizeof(c->d)); // 'g' - green
memset(c->l, 'o', sizeof(c->l)); // 'o' - orange
memset(c->r, 'r', sizeof(c->r)); // 'r' - red
}
static void cube_f(CUBE *c) // f operation
{
char temp[3];
LINEITEM lines[5] = {
{ 1, temp },
{ 3, &(c->l[0][2]) },
{ 1, &(c->d[0][0]) },
{-3, &(c->r[2][0]) },
{-1, &(c->u[2][2]) },
};
line_rotate90(lines);
surface_rotate90(c->f);
}
static void cube_b(CUBE *c) // b operation
{
char temp[3];
LINEITEM lines[5] = {
{ 1, temp },
{ 3, &(c->r[0][2]) },
{-1, &(c->d[2][2]) },
{-3, &(c->l[2][0]) },
{ 1, &(c->u[0][0]) },
};
line_rotate90(lines);
surface_rotate90(c->b);
}
static void cube_u(CUBE *c) // u operation
{
char temp[3];
LINEITEM lines[5] = {
{ 1, temp },
{ 1, &(c->l[0][0]) },
{ 1, &(c->f[0][0]) },
{ 1, &(c->r[0][0]) },
{ 1, &(c->b[0][0]) },
};
line_rotate90(lines);
surface_rotate90(c->u);
}
static void cube_d(CUBE *c) // d operation
{
char temp[3];
LINEITEM lines[5] = {
{ 1, temp },
{ 1, &(c->b[2][0]) },
{ 1, &(c->r[2][0]) },
{ 1, &(c->f[2][0]) },
{ 1, &(c->l[2][0]) },
};
line_rotate90(lines);
surface_rotate90(c->d);
}
static void cube_l(CUBE *c) // l operation
{
char temp[3];
LINEITEM lines[5] = {
{ 1, temp },
{ 3, &(c->d[0][0]) },
{ 3, &(c->f[0][0]) },
{ 3, &(c->u[0][0]) },
{-3, &(c->b[2][2]) },
};
line_rotate90(lines);
surface_rotate90(c->l);
}
static void cube_r(CUBE *c) // r operation
{
char temp[3];
LINEITEM lines[5] = {
{ 1, temp },
{ 3, &(c->u[0][2]) },
{ 3, &(c->f[0][2]) },
{ 3, &(c->d[0][2]) },
{-3, &(c->b[2][0]) },
};
line_rotate90(lines);
surface_rotate90(c->r);
}
enum {
CUBE_OP_F,
CUBE_OP_B,
CUBE_OP_U,
CUBE_OP_D,
CUBE_OP_L,
CUBE_OP_R,
};
static void (*g_op_tab[])(CUBE *c) = { cube_f, cube_b, cube_u, cube_d, cube_l, cube_r }; // define g_op_tab, an operation functions table
static void cube_op (CUBE *c, int op) { (g_op_tab[op])(c); } // cube_op is operation function, parameter op indicate the operation
static void cube_rand(CUBE *c, int n ) { while (n-- > 0) cube_op(c, rand() % 6); } // random the cube
static void cube_render(CUBE *c) // draw cube on screen, now only working on win32 console
{
char buffer[9][12] = {0};
int i, j;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
WORD wOldColorAttrs;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
// save the current color
GetConsoleScreenBufferInfo(h, &csbiInfo);
wOldColorAttrs = csbiInfo.wAttributes;
for (i=0; i<3; i++) for (j=0; j<3; j++) buffer[3+i][3+j] = c->f[i][j];
for (i=0; i<3; i++) for (j=0; j<3; j++) buffer[3+i][9+j] = c->b[i][j];
for (i=0; i<3; i++) for (j=0; j<3; j++) buffer[0+i][3+j] = c->u[i][j];
for (i=0; i<3; i++) for (j=0; j<3; j++) buffer[6+i][3+j] = c->d[i][j];
for (i=0; i<3; i++) for (j=0; j<3; j++) buffer[3+i][0+j] = c->l[i][j];
for (i=0; i<3; i++) for (j=0; j<3; j++) buffer[3+i][6+j] = c->r[i][j];
for (i=0; i<9; i++) {
for (j=0; j<12; j++) {
switch (buffer[i][j]) {
case 'w': case 'W': SetConsoleTextAttribute(h, FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE); break;
case 'y': case 'Y': SetConsoleTextAttribute(h, FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN); break;
case 'b': case 'B': SetConsoleTextAttribute(h, FOREGROUND_INTENSITY|FOREGROUND_BLUE ); break;
case 'g': case 'G': SetConsoleTextAttribute(h, FOREGROUND_INTENSITY|FOREGROUND_GREEN); break;
case 'o': case 'O': SetConsoleTextAttribute(h, FOREGROUND_RED|FOREGROUND_BLUE ); break;
case 'r': case 'R': SetConsoleTextAttribute(h, FOREGROUND_INTENSITY|FOREGROUND_RED ); break;
}
printf(buffer[i][j] ? "# " : " ");
}
printf("\n");
}
// restore the original color
SetConsoleTextAttribute(h, wOldColorAttrs);
}
static int cube_check_fcross0(CUBE *cube) // check front cross state, in total 4 squares need be solved, if solved score should be 4
{
int checklist[][2] = {
{ cube->f[1][1], cube->f[0][1] },
{ cube->f[1][1], cube->f[1][0] },
{ cube->f[1][1], cube->f[1][2] },
{ cube->f[1][1], cube->f[2][1] },
};
int value = 0, i;
for (i=0; i<4; i++) {
value += (checklist[i][0] == checklist[i][1]);
}
return value;
}
static int cube_check_fcross1(CUBE *cube) // check first layer cross, in total 4 squares need be solved, if solved score should be 4
{
int checklist[][2] = {
{ cube->l[1][1], cube->l[1][2] },
{ cube->u[1][1], cube->u[2][1] },
{ cube->r[1][1], cube->r[1][0] },
{ cube->d[1][1], cube->d[0][1] },
};
int value = 0, i;
for (i=0; i<4; i++) {
value += (checklist[i][0] == checklist[i][1]);
}
return value;
}
static int cube_check_fcorners(CUBE *cube) // check front corners
{
int checklist[][3][2] = {
{
{ cube->f[1][1], cube->f[0][0] },
{ cube->l[1][1], cube->l[0][2] },
{ cube->u[1][1], cube->u[2][0] },
},
{
{ cube->f[1][1], cube->f[0][2] },
{ cube->u[1][1], cube->u[2][2] },
{ cube->r[1][1], cube->r[0][0] },
},
{
{ cube->f[1][1], cube->f[2][0] },
{ cube->l[1][1], cube->l[2][2] },
{ cube->d[1][1], cube->d[0][0] },
},
{
{ cube->f[1][1], cube->f[2][2] },
{ cube->r[1][1], cube->r[2][0] },
{ cube->d[1][1], cube->d[0][2] },
},
};
int value = 0, i, j;
for (j=0; j<4; j++) {
value++;
for (i=0; i<3; i++) {
if (checklist[j][i][0] != checklist[j][i][1]) { value--; break; }
}
}
return value;
}
static int cube_check_medges(CUBE *cube) // check middle edges
{
int checklist[][2][2] = {
{
{ cube->l[1][1], cube->l[0][1] },
{ cube->u[1][1], cube->u[1][0] },
},
{
{ cube->u[1][1], cube->u[1][2] },
{ cube->r[1][1], cube->r[0][1] },
},
{
{ cube->l[1][1], cube->l[2][1] },
{ cube->d[1][1], cube->d[1][0] },
},
{
{ cube->r[1][1], cube->r[2][1] },
{ cube->d[1][1], cube->d[1][2] },
},
};
int value = 0, i, j;
for (j=0; j<4; j++) {
value++;
for (i=0; i<2; i++) {
if (checklist[j][i][0] != checklist[j][i][1]) { value--; break; }
}
}
return value;
}
static int cube_check_bcross(CUBE *cube) // check back cross
{
int checklist[][2][2] = {
{
{ cube->b[1][1], cube->b[0][1] },
{ cube->b[1][1], cube->b[2][1] },
},
{
{ cube->b[1][1], cube->b[1][0] },
{ cube->b[1][1], cube->b[1][2] },
},
};
int value = 0, i, j;
for (j=0; j<2; j++) {
value += 2;
for (i=0; i<2; i++) {
if (checklist[j][i][0] != checklist[j][i][1]) { value -= 2; break; }
}
}
return value;
}
static int cube_check_bsurface(CUBE *cube) // check back surface
{
int checklist[][2] = {
{ cube->b[1][1], cube->b[0][0] },
{ cube->b[1][1], cube->b[0][2] },
{ cube->b[1][1], cube->b[2][0] },
{ cube->b[1][1], cube->b[2][2] },
};
int value = 0, i;
for (i=0; i<4; i++) {
value += (checklist[i][0] == checklist[i][1]);
}
if (value == 2) value = 0;
return value;
}
static int cube_check_bcorners(CUBE *cube) // check back corners
{
int checklist[][3][2] = {
{
{ cube->b[1][1], cube->b[0][2] },
{ cube->l[1][1], cube->l[0][0] },
{ cube->u[1][1], cube->u[0][0] },
},
{
{ cube->b[1][1], cube->b[0][0] },
{ cube->u[1][1], cube->u[0][2] },
{ cube->r[1][1], cube->r[0][2] },
},
{
{ cube->b[1][1], cube->b[2][0] },
{ cube->r[1][1], cube->r[2][2] },
{ cube->d[1][1], cube->d[2][2] },
},
{
{ cube->b[1][1], cube->b[2][2] },
{ cube->l[1][1], cube->l[2][0] },
{ cube->d[1][1], cube->d[2][0] },
},
};
int returned[4] = { 1, 1, 1, 1 };
int value = 0, flag = 0, i, j;
for (j=0; j<4; j++) {
value++;
for (i=0; i<3; i++) {
if (checklist[j][i][0] != checklist[j][i][1]) { value--; returned[j] = 0; break; }
}
}
for (i=0; i<4; i++) {
if (returned[i] && returned[(i+1)%4]) flag = 1;
}
return flag ? value : 0;
}
static int cube_check_bedges(CUBE *cube) // check back edges
{
int checklist[][2][2] = {
{
{ cube->u[1][1], cube->u[0][1] },
{ cube->b[1][1], cube->b[0][1] },
},
{
{ cube->d[1][1], cube->d[2][1] },
{ cube->b[1][1], cube->b[2][1] },
},
{
{ cube->l[1][1], cube->l[1][0] },
{ cube->b[1][1], cube->b[1][2] },
},
{
{ cube->r[1][1], cube->r[1][2] },
{ cube->b[1][1], cube->b[1][0] },
},
};
int value = 0, i, j;
for (j=0; j<4; j++) {
value++;
for (i=0; i<2; i++) {
if (checklist[j][i][0] != checklist[j][i][1]) { value--; break; }
}
}
return value;
}
static int cube_check_state(CUBE *cube, int flag) // flag: 0 - calculate score stop when score of one step is not 4, >0 - calulate score stop when cur >= flag
{
int (*pfn_check_tab[])(CUBE* cube) = {
cube_check_fcross0 ,
cube_check_fcross1 ,
cube_check_fcorners,
cube_check_medges ,
cube_check_bcross ,
cube_check_bsurface,
cube_check_bcorners,
cube_check_bedges ,
NULL,
};
int value = 0, cur = 0, i;
for (i=0; pfn_check_tab[i]; i++) {
if (flag) {
value += pfn_check_tab[i](cube);
cur += 4;
if (cur >= flag) break;
} else {
cur = pfn_check_tab[i](cube);
value += cur;
if (cur != 4) break;
}
}
return value;
}
typedef struct {
int open ;
int close;
int size ;
CUBE *cubes;
} TABLE;
static int search_table_create(TABLE *table, int size)
{
table->size = size;
table->cubes = malloc(size * sizeof(CUBE));
return table->cubes ? 0 : -1;
}
static void search_table_destroy(TABLE *table)
{
free(table->cubes);
table->cubes = NULL;
}
static int is_4same_ops(CUBE *cube) // check whether parent path is 4 same operations path
{
int curop = cube->op;
int n = 0;
while (cube) {
if (cube->op == curop) {
if (++n == 4) return 1;
cube = cube->parent;
} else break;
}
return 0;
}
static int cut_branch(int newval, int cutval)
{
return newval < cutval;
}
static CUBE* search(TABLE *table, CUBE *start, int state, char *oplist, int opnum, int cutval)
{
CUBE *curcube ,*newcube;
int newstate, newvalue, i;
start->parent = NULL;
start->op = -1;
if (cube_check_state(start, 0) >= state) return start;
// init search table
table->open = table->close = 0;
// put original cube into open table
table->cubes[table->open] = *start;
table->open++;
while (table->close < table->open) {
// check memory usage
if (table->open + 6 >= table->size - 1) { printf("all table memory have been used !\n"); break; }
// dequeue a cube from open table
curcube = &(table->cubes[table->close++]);
// extend cubes check state and put new cubes into open table
for (i=0; i<opnum; i++) {
newcube = &(table->cubes[table->open]);
memcpy (newcube, curcube, sizeof(CUBE));
cube_op(newcube, oplist[i]);
newcube->op = oplist[i];
newcube->parent = curcube;
newstate = cube_check_state(newcube, 0 );
newvalue = cube_check_state(newcube, state);
if (newstate >= state) return newcube; // found
if (cut_branch(newvalue, cutval) || is_4same_ops(newcube)) continue; // cut branch
table->open++;
}
}
return NULL;
}
static void print_solve_oplist(CUBE *cube)
{
static char* optab[] = {
"F" , "B" , "U" , "D" , "L" , "R" ,
"F2", "B2", "U2", "D2", "L2", "R2",
"F'", "B'", "U'", "D'", "L'", "R'",
};
char *oplist[256];
int last = -1, times = 0, i = 0, n = 0;
while (cube) {
if (cube->op >= 0) {
if (last != cube->op) {
if (last != -1) {
oplist[i++] = optab[last + times * 6];
}
last = cube->op;
times= 0;
} else {
times++;
}
} else {
if (last != -1) oplist[i++] = optab[last + times * 6];
}
cube = cube->parent;
}
printf("\noperation list:\n");
while (--i >= 0) {
printf("%s%s%s", oplist[i], i == 0 ? "" : " -> ", ++n % 12 == 0 ? "\n" : "");
}
printf("\n");
}
static void cube_solve(CUBE *c)
{
TABLE t;
if (search_table_create(&t, 1024*1024*16) != 0) {
printf("failed to create cube search table !\n");
return;
}
if (1) {
static char oplisttab[][6] = {
{ CUBE_OP_F, CUBE_OP_U, CUBE_OP_D, CUBE_OP_L, CUBE_OP_R },
{ CUBE_OP_U, CUBE_OP_D, CUBE_OP_L, CUBE_OP_R, CUBE_OP_B },
{ CUBE_OP_B, CUBE_OP_R, CUBE_OP_U },
};
static int stepparams[][4] = {
{ 2 , 0, 5, 0 }, //+ fcross0
{ 4 , 0, 5, 2 }, //- fcross0
{ 8 , 0, 5, 2 }, //+-fcross1
{ 9 , 0, 5, 3 }, //+ fcorners
{ 10, 0, 5, 4 }, //..
{ 11, 0, 5, 5 }, //..
{ 12, 0, 5, 5 }, //- fcorners
{ 13, 1, 5, 6 }, //+ medges
{ 14, 1, 5, 6 }, //..
{ 15, 1, 5, 7 }, //..
{ 16, 1, 5, 8 }, //- medges
{ 18, 1, 5, 10}, //+ bcross
{ 20, 1, 5, 11}, //- bcross
{ 21, 1, 5, 11}, //+ bsurface
{ 24, 1, 5, 11}, //- bsurface
{ 26, 2, 3, 12}, //+ bcorners
{ 28, 2, 3, 12}, //- bcorners
{ 32, 2, 2, 12}, //+-bedges
{ 0 , 0, 0, 0 },
};
CUBE *start = c;
CUBE *find = NULL;
int i;
for (i=0; stepparams[i][0]; i++) {
find = search(&t, start, stepparams[i][0], oplisttab[stepparams[i][1]], stepparams[i][2], stepparams[i][3]);
if (find) {
if (find != start) {
start = find;
*c =*find;
print_solve_oplist(find);
if (stepparams[i][0] != 32) cube_render(c);
}
} else {
printf("can't solve !\n");
goto done;
}
}
printf("\ncube solved !\n");
}
done:
search_table_destroy(&t);
}
static void cube_input(CUBE *c)
{
char str[256];
printf("please input F surface of cube:\n");
(void)scanf(" %c %c %c %c %c %c %c %c %c",
&(c->f[0][0]), &(c->f[0][1]), &(c->f[0][2]),
&(c->f[1][0]), &(c->f[1][1]), &(c->f[1][2]),
&(c->f[2][0]), &(c->f[2][1]), &(c->f[2][2]));
fgets(str, sizeof(str), stdin);
printf("please input U surface of cube:\n");
(void)scanf(" %c %c %c %c %c %c %c %c %c",
&(c->u[0][0]), &(c->u[0][1]), &(c->u[0][2]),
&(c->u[1][0]), &(c->u[1][1]), &(c->u[1][2]),
&(c->u[2][0]), &(c->u[2][1]), &(c->u[2][2]));
fgets(str, sizeof(str), stdin);
printf("please input D surface of cube:\n");
(void)scanf(" %c %c %c %c %c %c %c %c %c",
&(c->d[0][0]), &(c->d[0][1]), &(c->d[0][2]),
&(c->d[1][0]), &(c->d[1][1]), &(c->d[1][2]),
&(c->d[2][0]), &(c->d[2][1]), &(c->d[2][2]));
fgets(str, sizeof(str), stdin);
printf("please input L surface of cube:\n");
(void)scanf(" %c %c %c %c %c %c %c %c %c",
&(c->l[0][0]), &(c->l[0][1]), &(c->l[0][2]),
&(c->l[1][0]), &(c->l[1][1]), &(c->l[1][2]),
&(c->l[2][0]), &(c->l[2][1]), &(c->l[2][2]));
fgets(str, sizeof(str), stdin);
printf("please input R surface of cube:\n");
(void)scanf(" %c %c %c %c %c %c %c %c %c",
&(c->r[0][0]), &(c->r[0][1]), &(c->r[0][2]),
&(c->r[1][0]), &(c->r[1][1]), &(c->r[1][2]),
&(c->r[2][0]), &(c->r[2][1]), &(c->r[2][2]));
fgets(str, sizeof(str), stdin);
printf("please input B surface of cube:\n");
(void)scanf(" %c %c %c %c %c %c %c %c %c",
&(c->b[0][0]), &(c->b[0][1]), &(c->b[0][2]),
&(c->b[1][0]), &(c->b[1][1]), &(c->b[1][2]),
&(c->b[2][0]), &(c->b[2][1]), &(c->b[2][2]));
fgets(str, sizeof(str), stdin);
}
static void show_help(void)
{
printf(
"\n"
"available commands:\n\n"
"f f2 f' b b2 b' u u2 u' d d2 d' l l2 l' r r2 r'\n"
"init rand input solve help exit\n\n"
"note: all command is case sensitive.\n"
);
}
int main(void)
{
char cmd[128];
char str[256];
CUBE c;
// init cube
cube_init(&c);
show_help();
while (1) {
cube_render(&c);
printf("command: ");
*cmd = 0; (void)scanf("%s", cmd);
if (strcmp(cmd, "f") == 0) {
cube_f(&c);
} else if (strcmp(cmd, "f2") == 0) {
cube_f(&c); cube_f(&c);
} else if (strcmp(cmd, "f'") == 0) {
cube_f(&c); cube_f(&c); cube_f(&c);
} else if (strcmp(cmd, "b" ) == 0) {
cube_b(&c);
} else if (strcmp(cmd, "b2") == 0) {
cube_b(&c); cube_b(&c);
} else if (strcmp(cmd, "b'") == 0) {
cube_b(&c); cube_b(&c); cube_b(&c);
} else if (strcmp(cmd, "u" ) == 0) {
cube_u(&c);
} else if (strcmp(cmd, "u2") == 0) {
cube_u(&c); cube_u(&c);
} else if (strcmp(cmd, "u'") == 0) {
cube_u(&c); cube_u(&c); cube_u(&c);
} else if (strcmp(cmd, "d" ) == 0) {
cube_d(&c);
} else if (strcmp(cmd, "d2") == 0) {
cube_d(&c); cube_d(&c);
} else if (strcmp(cmd, "d'") == 0) {
cube_d(&c); cube_d(&c); cube_d(&c);
} else if (strcmp(cmd, "l" ) == 0) {
cube_l(&c);
} else if (strcmp(cmd, "l2") == 0) {
cube_l(&c); cube_l(&c);
} else if (strcmp(cmd, "l'") == 0) {
cube_l(&c); cube_l(&c); cube_l(&c);
} else if (strcmp(cmd, "r" ) == 0) {
cube_r(&c);
} else if (strcmp(cmd, "r2") == 0) {
cube_r(&c); cube_r(&c);
} else if (strcmp(cmd, "r'") == 0) {
cube_r(&c); cube_r(&c); cube_r(&c);
} else if (strcmp(cmd, "init") == 0) {
cube_init(&c);
} else if (strcmp(cmd, "rand") == 0) {
*str = 0; fgets(str, sizeof(str), stdin);
cube_rand(&c, atoi(str) == 0 ? 100 : atoi(str));
} else if (strcmp(cmd, "input") == 0) {
cube_input(&c);
} else if (strcmp(cmd, "solve") == 0) {
cube_solve(&c);
} else if (strcmp(cmd, "help") == 0) {
show_help();
} else if (strcmp(cmd, "exit") == 0 || strcmp(cmd, "quit") == 0) {
break;
} else {
printf("unsupported command !\n");
}
printf("\n");
}
return 0;
}