/* Unirubik is an attempt to make a universal Rubik's Cube simulation which can be compiled with any ANSI C compiler or Unix v7's cc First version made on August 1st, 1988 Compiled on Linux Redhat 8.0 on March 17th, 2003 Compiled on PDP-11 Unix Version 7 on March 12th, 2013 Compiled on Interdata 7/32 Unix v7 on Jan 3rd, 2016 Compiles on: QuickC (Intel X86 DOS) 1988 Lattice C (Amiga) 1988 gcc (Intel X86 Linux) 2003 cc (Unix version 7) 2013 The program appends .q to the end of saved files One can enter a string of cube operators to be processed, e.g. uuffrruuulldbrrrbrrrbrrrdddlluuu gives 'cube in a cube' Singmaster (befuddler) notation used: u = up d = down f = front b = back l = left r = right */ #include #include #define D 3 /* Cube is size '3' */ #define SIDES 6 #define TOP 0 #define DOWN 1 #define FRONT 2 #define BACK 3 #define LEFT 4 #define RIGHT 5 unsigned short c; short d; short s1, s2, s3, s4; char k; /* Define 6 sides of Cube's surface and image */ short q[6][D][D], q1[6][D][D]; help(); initial_sides(); refresh_image(); display_sides(); top(); down(); front(); back(); left(); right(); load(); save(); mix(); int main() { d=D; printf ("\nunirubik 1.0 Rubik's Cube Simulation\n"); printf ("\nDeveloped by Mark Longridge April 1988\n\n"); printf ("\n"); help(); initial_sides (); refresh_image (); display_sides (); while (( k = getchar()) !='q') { switch (k) { case 'u': top(); break; case 'd': down(); break; case 'f': front(); break; case 'b': back(); break; case 'l': left(); break; case 'r': right(); break; case 'm': mix(); break; case '*': initial_sides (); refresh_image(); break; case '[': load(); break; case ']': save(); break; case '?': help(); break; case '\n': display_sides(); break; } } return(1); } initial_sides() { short 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() { short 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() { short 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() { short 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(); } down() { short 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(); } front() { short 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() { short 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(); } left () { short 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(); } right () { short 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(); } rotate_face (z) short z; { short 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]; } } save() { FILE *fopen(), *fp_out; char fn_out[30]; printf ("Enter name of output file: "); scanf ("%s", fn_out); strcat (fn_out,".q"); fp_out = fopen (fn_out, "w"); if (fp_out == NULL) { printf ("\nCan't write file\n\n"); return; } printf ("\nOutput in file: %s\n", fn_out); save_file (fp_out); fclose (fp_out); } save_file (output) FILE *output; { short x,y,z; for (z=0; z<=SIDES-1; z++) for (x=0; x<=d-1; x++) for (y=0; y<=d-1; y++) fputc (q[z][x][y], output); refresh_image(); } load() { FILE *fopen(), *fp_in; char fn_in[30]; printf ("Enter name of input file: "); scanf ("%s", fn_in); strcat (fn_in, ".q"); fp_in = fopen (fn_in, "r"); if (fp_in == NULL) { printf ("No input file %s\n", fn_in); goto out; } load_file (fp_in); printf ("\nInput in file: %s\n", fn_in); fclose (fp_in); out:; } load_file (fp_in) FILE *fp_in; { short 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] = fgetc(fp_in); } mix() { short x; srand (time(NULL)); 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; } } } 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"); } $