#define D 3 #define SIDES 6 #define TEST 1 #define TOP 0 #define DOWN 1 #define FRONT 2 #define BACK 3 #define LEFT 4 #define RIGHT 5 #define NULL 0 /* unirubik backported to Unix v5 august 2014 M Longridge to compile: cc unirubik.c gets.c */ int q[SIDES][D][D], q1[SIDES][D][D]; int d; int s1, s2, s3, s4; int stime[2]; int c; help(); main() { int x,y; char k; d=D; printf ("\nunirubik 1.0 for unix v5\n"); printf ("\nDeveloped and ported to unix v5 by M Longridge August 2014\n\n"); printf ("\n"); help(); initial_sides (); refresh_image (); display_sides (); while (( k = getchar()) !='q') { switch (k) { case '?': help(); break; case 'u': top(); break; case 'd': down(); break; case 'r': right(); break; case 'f': front(); break; case 'b': back(); break; case '*': initial_sides (); refresh_image(); break; case 'l': left(); break; case 'm': mix(); break; case '[': load(); display_sides(); break; case ']': save(); display_sides(); break; case '\n': display_sides(); break; } } } help() { printf ("u up d down m mix\n"); printf ("f front b back * reset\n"); printf ("l left r right ? help\n"); printf ("[ load ] save\n"); printf ("q quit\n\n"); } initial_sides() { int x,y,z; for (z=0; z<=SIDES-1; z++) for (x=0; x<=d-1; x++) for (y=0; y<=d-1; y++) q[z][x][y] = z; } refresh_image() { int x,y,z; for (z=0; z<=SIDES-1; z++) for (x=0; x<=d-1; x++) for (y=0; y<=d-1; y++) q1[z][x][y] = q[z][x][y]; } display_sides() { int x,y; printf (" TOP\n"); for (x=0; x<=d-1; x++) { printf (" "); for (y=0; y<=2; y++) { printf ("%3d", q[TOP][x][y]); } printf ("\n"); } printf (" LEFT FRONT RIGHT BACK\n"); for (x=0; x<=d-1; x++) { for (y=0; y<=d-1; y++) { printf ("%3d", q[LEFT][x][y]); } for (y=0; y<=d-1; y++) { printf ("%3d", q[FRONT][x][y]); } for (y=0; y<=d-1; y++) { printf ("%3d", q[RIGHT][x][y]); } printf (" "); for (y=0; y<=d-1; y++) { printf ("%3d", q[BACK][x][y]); } printf ("\n"); } printf (" DOWN\n"); for (x=0; x<=d-1; x++) { printf (" "); for (y=0; y<=d-1; y++) { printf ("%3d", q[DOWN][x][y]); } printf ("\n"); } printf ("\n"); } top() { int y,z; z = TOP; rotate_face(z); for (y=0; y<=d-1; y++) { q[LEFT][0][y] = q1[FRONT][0][y]; q[BACK][0][y] = q1[LEFT][0][y]; q[RIGHT][0][y]= q1[BACK][0][y]; q[FRONT][0][y]= q1[RIGHT][0][y]; } refresh_image(); } rotate_face (z) int z; { int x,y; for (x=0; x<=d-1; x++) for (y=0; y<=d-1; y++) { s1 = y; s2 = x; s3 = 2-x; s4 = y; q[z][s1][s2] = q1[z][s3][s4]; } } left () { int x,z; z = LEFT; rotate_face (z); for (x=0; x<=d-1; x++) { z = 2-x; q[TOP][x][0] = q1[BACK][z][d-1]; q[FRONT][x][0] = q1[TOP][x][0]; q[DOWN][x][0] = q1[FRONT][x][0]; q[BACK][x][d-1] =q1[DOWN][z][0]; } refresh_image(); } down() { int y,z; z = DOWN; rotate_face (z); for (y=0; y<=d-1; y++) { q[LEFT][d-1][y] = q1[BACK][d-1][y]; q[BACK][d-1][y] = q1[RIGHT][d-1][y]; q[RIGHT][d-1][y]= q1[FRONT][d-1][y]; q[FRONT][d-1][y]= q1[LEFT][d-1][y]; } refresh_image(); } right () { int x,z; z = RIGHT; rotate_face (z); for (x=0; x<=d-1; x++) { z= 2-x; q[TOP][x][d-1] = q1[FRONT][x][d-1]; q[FRONT][x][d-1] = q1[DOWN][x][d-1]; q[DOWN][x][d-1] = q1[BACK][z][0]; q[BACK][x][0] = q1[TOP][z][2]; } refresh_image(); } front() { int x,z; z = FRONT; rotate_face (z); for (x=0; x<=d-1; x++) { z = 2-x; q[LEFT][z][d-1] = q1[DOWN][0][z]; q[TOP][d-1][x] = q1[LEFT][z][d-1]; q[RIGHT][x][0] = q1[TOP][d-1][x]; q[DOWN][0][z] = q1[RIGHT][x][0]; } refresh_image(); } back() { int x,z; z = BACK; rotate_face (z); for (x=0; x<=d-1; x++) { z = 2-x; q[LEFT][x][0] = q1[TOP][0][z]; q[TOP][0][x] = q1[RIGHT][x][2]; q[RIGHT][x][2]= q1[DOWN][d-1][z]; q[DOWN][d-1][x]=q1[LEFT][x][0]; } refresh_image(); } mix() { int x; time(stime); srand(stime[1]); for (x=0; x<=20; x++) { /* Choose a random number */ c = rand() % SIDES; /* printf ("%d ", c); */ switch (c) { case 0: top(); break; case 1: down(); break; case 2: left(); break; case 3: right(); break; case 4: front(); break; case 5: back(); break; } } } strcat (s, t) char s[], t[]; { int i,j; i=j=0; while (s[i] !='\0') i++; while ((s[i++] = t[j++]) !='\0') ; } save_file (output) int output; { int x,y,z; for (z=0; z<=SIDES-1; z++) for (x=0; x<=d-1; x++) for (y=0; y<=d-1; y++) write (output, &q[z][x][y], 1); refresh_image(); } save() { int fp_out; char fn_out[30]; printf ("Enter name of output file: "); /* eat newline */ getchar(); gets (fn_out); strcat (fn_out,".q"); fp_out = creat (fn_out, 0644); fp_out = open (fn_out, 1); if (fp_out < 0) { printf ("\nCan't write file\n"); return; } save_file (fp_out); printf ("\nOutput in file: %s\n", fn_out); close (fp_out); } load() { int fp_in; char fn_in[30]; printf ("Enter name of input file: "); /* eat newline */ getchar(); gets (fn_in); strcat (fn_in, ".q"); fp_in = open (fn_in, 0); if (fp_in < 0) { printf ("No input file %s\n", fn_in); goto out; } load_file (fp_in); printf ("\nInput in file: %s\n", fn_in); close (fp_in); out:; } load_file (fp_in) int fp_in; { int x,y,z; for (z=0; z<=SIDES-1; z++) for (x=0; x<=d-1; x++) for (y=0; y<=d-1; y++) read (fp_in, &q[z][x][y], 1); }