#include #include #include #define RGBBlack RGB(0,0,0) #define RGBGreen RGB(0,255,0) #define RGBWhite RGB(255,255,255) #define RGBOrange RGB(255,144,0) #define RGBRed RGB(255,0,0) #define RGBBlue RGB(0,0,255) #define RGBYellow RGB(255,255,0) #define SIDES 6 #define D 10 /* Maximum size of cube */ #define SIZE_X 15 #define SIZE_Y 16 #define SEP_X 38 #define SEP_Y 32 #define BITMAP_X 192 #define BITMAP_Y 172 #define WHITE 255 #define BLACK 254 #define TOP 0 #define DOWN 5 #define FRONT 2 #define BACK 4 #define LEFT 1 #define RIGHT 3 short ss[SIDES]; /* Pointers for cube placement */ short tt[SIDES]; void initial_pts(); void initial_sides(); void calc_cube(); char est[_MAX_PATH], ee[15]; typedef int CUBE[SIDES][D][D]; CUBE q; typedef int CUBE[SIDES][D][D]; CUBE q1; typedef int CUBE[SIDES][D][D]; CUBE q2; short col[16]; /* Color index */ short posx[SIDES][D]; short posy[SIDES][D]; short rc; short tempx, tempy, tempcol; int size_x, size_y, sep_x, sep_y; int bitmap_x, bitmap_y; char cur[25]; static char MyName[] = "WinCube"; static char HelloWorld[] = "? for help"; short d = 3; /* Current size of cube */ #define WhiteBrush ColourBrush[0] #define RedBrush ColourBrush[1] #define YellowBrush ColourBrush[2] #define OrangeBrush ColourBrush[3] #define GreenBrush ColourBrush[4] #define BlueBrush ColourBrush[5] HWND MainHwnd; HPEN BlackPen = NULL; HBRUSH ColourBrush[6] = {NULL,NULL,NULL,NULL,NULL,NULL}; LRESULT CALLBACK MainWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); /********************************************************/ void GetFilename (int type) { DWORD n; OPENFILENAME ofn; ZeroMemory(&ofn,n = sizeof(OPENFILENAME)); ofn.lStructSize = n; ofn.hwndOwner = MainHwnd; ofn.lpstrFile = est; ofn.nMaxFile = sizeof(est); ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; if (type == 1) { ofn.lpstrDefExt = "q"; ofn.lpstrFilter = "Cube patterns (*.q)\0*.q\0" "All files (*.*)\0*.*\0"; } else if (type == 2) { ofn.lpstrDefExt = "mov"; ofn.lpstrFilter = "Cube moves (*.mov)\0*.mov\0" "All files (*.*)\0*.*\0"; } GetOpenFileName(&ofn); } void help() { char buffer[256]; sprintf (buffer, "u d f b l r\nArrow keys rotate in space\n%% = Show Symmetry\n\\ = Central Reflect\n| = RL Reflect\n* = Reset"); MessageBox(NULL,buffer,"Help",MB_OK); } int compare_cube (CUBE q, CUBE q1) { short x,y,z; short rc = 0; for (z=0; z<=SIDES-1; z++) for (x=0; x <= d-1; x++) for (y=0; y <= d-1; y++) if (q[z][x][y] != q1[z][x][y]) { rc = 1; break; } return(rc); } void refresh_image_side (short sd, CUBE q, CUBE q1) { short x,y; for (x=0; x <= d-1; x++) for (y=0; y <= d-1; y++) q1[sd][x][y] = q[sd][x][y]; } /* Store q in q1 */ void refresh_image (CUBE q, CUBE q1) { 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]; } void rotate_face(short z, short dir) { short x,y; short s1,s2,s3,s4,s5; for (x=0; x<=d-1; x++) { for (y=0; y<=d-1; y++) { s1=y; s2=x; s3=d-1-x; s4=y; s5=d-1-y; if (dir == 0) { q[z][s1][s2] = q1[z][s3][s4]; } else if (dir == 1) { q[z][s3][s4] = q1[z][s1][s2]; } else if (dir == 2) { q[z][s1][s2] = q1[z][s5][s3]; } } } refresh_image_side (z, q, q1); } void reset_size() { size_x = SIZE_X; size_y = SIZE_Y; sep_x = SEP_X; sep_y = SEP_Y; calc_cube(); if (rc >=1 && rc<= d-2) { /* rc retains it's current value! */ } else { rc=1; } initial_sides (); refresh_image (q, q1); } void rot_col (short c1, short c2, short c3, short c4, short c5, short c6) { 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++) { if (q1[z][x][y] == TOP) q[z][x][y] = c1; else if (q1[z][x][y] == LEFT) q[z][x][y] = c2; else if (q1[z][x][y] == FRONT) q[z][x][y] = c3; else if (q1[z][x][y] == BACK) q[z][x][y] = c4; else if (q1[z][x][y] == DOWN) q[z][x][y] = c5; else if (q1[z][x][y] == RIGHT) q[z][x][y] = c6; } } } refresh_image (q, q1); } void reflect_c (CUBE q, CUBE q1) { /* The infamous Central Reflection */ /* i.e. U1 = D3, U2 = D2, U3 = D1 L1 = R3, L2 = R2, L3 = R1 F1 = B3, F2 = B2, F3 = B1 */ short x,y; short a, b; for (x=0; x<=d-1; x++) { for (y=0; y<=d-1; y++) { a = d-1-x; b = d-1-y; q[1][x][y] = q1[3][a][y]; q[3][x][y] = q1[1][a][y]; q[2][x][y] = q1[4][a][y]; q[4][x][y] = q1[2][a][y]; q[0][x][y] = q1[5][x][b]; q[5][x][y] = q1[0][x][b]; } } refresh_image (q, q1); rot_col (DOWN, RIGHT, BACK, FRONT, TOP, LEFT); } void reflect_rl (CUBE q, CUBE q1) { /* Reflection in the RL plane */ /* i.e. U1 = U3, D1 = D3, L1 = R3, L2 = R2, L3 = R1 F1 = F3 B1 = B3 */ short x,y,z; short a, b; for (z=0; z<= SIDES-1; z++) { for (x=0; x<=d-1; x++) { for (y=0; y<=d-1; y++) { b = d-1-y; if (z == 1 || z == 3) { q[1][x][y] = q1[3][x][b]; q[3][x][y] = q1[1][x][b]; } else { q[z][x][y] = q1[z][x][b]; } } } } refresh_image (q, q1); rot_col (TOP, RIGHT, FRONT, BACK, DOWN, LEFT); } void top (CUBE q, CUBE q1) { char buffer[10]; short y,z; short dir=0; z= TOP; rotate_face(z, dir); 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]; } for (y=0 ; y <=d-1; y++) { q1[LEFT][0][y] = q[LEFT][0][y]; q1[BACK][0][y] = q[BACK][0][y]; q1[RIGHT][0][y] = q[RIGHT][0][y]; q1[FRONT][0][y] = q[FRONT][0][y]; } } void top2 (CUBE q, CUBE q1) { short x,y,z; short dir=2; z= TOP; rotate_face(z, dir); for (y=0 ; y <=d-1; y++) { q[LEFT][0][y] = q1[RIGHT][0][y]; q[BACK][0][y] = q1[FRONT][0][y]; q[RIGHT][0][y] = q1[LEFT][0][y]; q[FRONT][0][y] = q1[BACK][0][y]; } for (y=0 ; y <=d-1; y++) { q1[LEFT][0][y] = q[LEFT][0][y]; q1[BACK][0][y] = q[BACK][0][y]; q1[RIGHT][0][y] = q[RIGHT][0][y]; q1[FRONT][0][y] = q[FRONT][0][y]; } refresh_image_side (z, q, q1); } void front (CUBE q, CUBE q1) { short x,y,z; short dir=0; z= FRONT; rotate_face(z, dir); for (x=0 ; x <=d-1; x ++) { z= d-1-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]; } for (x=0 ; x <=d-1; x ++) { z= d-1-x; q1[LEFT][z][d-1] = q[LEFT][z][d-1]; q1[TOP][d-1][x] = q[TOP][d-1][x]; q1[RIGHT][x][0] = q[RIGHT][x][0]; q1[DOWN][0][z] = q[DOWN][0][z]; } } void front2 (CUBE q, CUBE q1) { short x,y,z; short dir=2; z= FRONT; rotate_face(z, dir); for (x=0 ; x <=d-1; x ++) { z= d-1-x; q[LEFT][z][d-1] = q1[RIGHT][x][0]; q[TOP][d-1][x] = q1[DOWN][0][z]; q[RIGHT][x][0]= q1[LEFT][z][d-1]; q[DOWN][0][z] = q1[TOP][d-1][x]; } for (x=0 ; x <=d-1; x ++) { z= d-1-x; q1[LEFT][z][d-1] = q[LEFT][z][d-1]; q1[TOP][d-1][x] = q[TOP][d-1][x]; q1[RIGHT][x][0]= q[RIGHT][x][0]; q1[DOWN][0][z] = q[DOWN][0][z]; } refresh_image_side (z, q, q1); } void back (CUBE q, CUBE q1) { short x,y,z; short dir=0; z= BACK; rotate_face(z, dir); for (x=0 ; x <=d-1; x++) { z= d-1-x; q[LEFT][x][0]= q1[TOP][0][z]; q[TOP][0][x]= q1[RIGHT][x][d-1]; q[RIGHT][x][d-1]= q1[DOWN][d-1][z]; q[DOWN][d-1][x]= q1[LEFT][x][0]; } for (x=0 ; x <=d-1; x++) { z= d-1-x; q1[LEFT][x][0]= q[LEFT][x][0]; q1[TOP][0][x]= q[TOP][0][x]; q1[RIGHT][x][d-1]= q[RIGHT][x][d-1]; q1[DOWN][d-1][x]= q[DOWN][d-1][x]; } } void back2 (CUBE q, CUBE q1) { short x,y,z; short dir=2; z= BACK; rotate_face(z, dir); for (x=0 ; x <=d-1; x++) { z= d-1-x; q[LEFT][z][0]= q1[RIGHT][x][d-1]; q[TOP][0][x]= q1[DOWN][d-1][z]; q[RIGHT][x][d-1]= q1[LEFT][z][0]; q[DOWN][d-1][z]= q1[TOP][0][x]; } for (x=0 ; x <=d-1; x++) { z= d-1-x; q1[LEFT][z][0]= q[LEFT][z][0]; q1[TOP][0][x]= q[TOP][0][x]; q1[RIGHT][x][d-1]= q[RIGHT][x][d-1]; q1[DOWN][d-1][z]= q[DOWN][d-1][z]; } refresh_image_side (z, q, q1); } void left (CUBE q, CUBE q1) { short x,y,z; short dir=0; z= LEFT; rotate_face(z, dir); for (x=0 ; x <=d-1; x ++) { z= d-1-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]; } for (x=0 ; x <=d-1; x ++) { z= d-1-x; q1[TOP][x][0]= q[TOP][x][0]; q1[FRONT][x][0]= q[FRONT][x][0]; q1[DOWN][x][0]= q[DOWN][x][0]; q1[BACK][x][d-1]= q[BACK][x][d-1]; } refresh_image_side (z, q, q1); } void left2 (CUBE q, CUBE q1) { short x,y,z; short dir=2; z= LEFT; rotate_face(z, dir); for (x=0 ; x <=d-1; x ++) { z= d-1-x; q[TOP][x][0]= q1[DOWN][x][0]; q[FRONT][x][0]= q1[BACK][z][d-1]; q[DOWN][x][0]= q1[TOP][x][0]; q[BACK][z][d-1]= q1[FRONT][x][0]; } for (x=0 ; x <=d-1; x ++) { z= d-1-x; q1[TOP][x][0]= q[TOP][x][0]; q1[FRONT][x][0]= q[FRONT][x][0]; q1[DOWN][x][0]= q[DOWN][x][0]; q1[BACK][z][d-1]= q[BACK][z][d-1]; } refresh_image_side (z, q, q1); } void right (CUBE q, CUBE q1) { short x,y,z; short dir=0; z= RIGHT; rotate_face(z, dir); for (x=0 ; x <=d-1; x ++) { z= d-1-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][d-1]; } for (x=0 ; x <=d-1; x ++) { q1[TOP][x][d-1] = 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][x][0] = q[BACK][x][0]; } refresh_image_side (z, q, q1); } void right2 (CUBE q, CUBE q1) { short x,y,z; short dir=2; z= RIGHT; rotate_face(z, dir); for (x=0 ; x <=d-1; x ++) { z= d-1-x; q[TOP][x][d-1]= q1[DOWN][x][d-1]; q[FRONT][x][d-1]= q1[BACK][z][0]; q[DOWN][x][d-1]= q1[TOP][x][d-1]; q[BACK][z][0]= q1[FRONT][x][d-1]; } for (x=0 ; x <=d-1; x ++) { z= d-1-x; q1[TOP][x][d-1]= 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][z][0]; } refresh_image_side (z, q, q1); } void down (CUBE q, CUBE q1) { short x,y,z; short dir=0; z = DOWN; rotate_face(z, dir); 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]; } for (y=0 ; y <=d-1; y ++) { q1[LEFT][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]; } } void down2 (CUBE q, CUBE q1) { short x,y,z; short dir=2; z = DOWN; rotate_face(z, dir); for (y=0 ; y <=d-1; y ++) { q[LEFT][d-1][y]= q1[RIGHT][d-1][y]; q[BACK][d-1][y]= q1[FRONT][d-1][y]; q[RIGHT][d-1][y]= q1[LEFT][d-1][y]; q[FRONT][d-1][y]= q1[BACK][d-1][y]; } for (y=0 ; y <=d-1; y ++) { q1[LEFT][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]; } refresh_image_side (z, q, q1); } void cube_left (CUBE q, CUBE q1) { short x,y,z; short dir=0; for (x=0; x<=d-1; x++) { for (y=0; y<=d-1; y++) { q[LEFT][x][y] = q1[FRONT][x][y]; q[FRONT][x][y] = q1[RIGHT][x][y]; q[RIGHT][x][y] = q1[BACK][x][y]; q[BACK][x][y] = q1[LEFT][x][y]; } } refresh_image (q, q1); z = TOP; rotate_face(z, dir); z = DOWN; dir =1; rotate_face(z, dir); } void cube_right (CUBE q, CUBE q1) { short x,y,z; short dir=1; /* Next rotation will be counter-clockwise */ for (x=0; x<=d-1; x++) { for (y=0; y<=d-1; y++) { q[LEFT][x][y] = q1[BACK][x][y]; q[FRONT][x][y] = q1[LEFT][x][y]; q[RIGHT][x][y] = q1[FRONT][x][y]; q[BACK][x][y] = q1[RIGHT][x][y]; } } refresh_image (q, q1); z = TOP; rotate_face(z, dir); z = DOWN; dir =0; /* Set direction back to clockwise */ rotate_face(z, dir); } void cube_up (CUBE q, CUBE q1) { short x,y,z,w; short dir=1; for (x=0; x<=d-1; x++) { z = d-1-x; for (y=0; y<=d-1; y++) { w = d-1-y; q[TOP][x][y] = q1[FRONT][x][y]; q[FRONT][x][y] = q1[DOWN][x][y]; q[DOWN][x][y] = q1[BACK][z][w]; q[BACK][z][w] = q1[TOP][x][y]; } } refresh_image (q, q1); z = LEFT; rotate_face(z, dir); z = RIGHT; dir =0; rotate_face(z, dir); } void cube_down (CUBE q, CUBE q1) { short x,y,z,w; short dir=0; for (x=0; x<=d-1; x++) { z= d-1-x; for (y=0; y<=d-1; y++) { w = d-1-y; q[TOP][x][y] = q1[BACK][z][w]; q[FRONT][x][y] = q1[TOP][x][y]; q[DOWN][x][y] = q1[FRONT][x][y]; q[BACK][z][w] = q1[DOWN][x][y]; } } refresh_image (q, q1); z = LEFT; rotate_face(z, dir); z = RIGHT; dir =1; rotate_face(z, dir); } void slice_vert(CUBE q, CUBE q1, short sl) { short x,y,z; if (d < 3) return; sl = 1; y= d-1-sl; for (x=0; x<=d-1; x++) { z= d-1-x; q[FRONT][x][sl] = q1[TOP][x][sl]; q[TOP][x][sl] = q1[BACK][z][y]; q[BACK][z][y] = q1[DOWN][x][sl]; q[DOWN][x][sl] = q1[FRONT][x][sl]; } refresh_image (q, q1); } void slice_hort(CUBE q, CUBE q1, short sl) { short x,z; if (d < 3) return; sl = 1; for (x=0; x<=d-1; x++) { q[FRONT][sl][x] = q1[LEFT][sl][x]; q[LEFT][sl][x] = q1[BACK][sl][x]; q[BACK][sl][x] = q1[RIGHT][sl][x]; q[RIGHT][sl][x] = q1[FRONT][sl][x]; } refresh_image (q, q1); } short cube_solved(CUBE q, CUBE q1) { int x, y, z; int counter =0; for (z=0; z<=SIDES-2; z++) for (x=0; x<=d-1; x++) for (y=0; y<=d-1; y++) { if (q[z][x][y] == q[z][0][0]) counter++; } if (counter == (SIDES-1) * d * d) /* Total number of cells in cube */ return(0); else return(-1); } void mix(CUBE q, CUBE q1, short type) { static short m; short n,x,z,y,c,t2; if (type == 3) { t2 = 1; type = 2; } else t2 = type; loop:; y = 0x7fff / 10; c = rand() / x + 10; for (m=1; m<=c; m++) { x = 0x7fff / (6+d-2); n = rand() / x + 1; if (type !=4) { switch(n) { case 1: for (z=1; z<=t2; z++) top(q, q1); break; case 2: for (z=1; z<=t2; z++) down(q, q1); break; case 3: for (z=1; z<=type; z++) front(q, q1); break; case 4: for (z=1; z<=type; z++) back(q, q1); break; case 5: for (z=1; z<=type; z++) left(q, q1); break; case 6: for (z=1; z<=type; z++) right(q, q1); break; } } if (n > 6 && d > 3) { x = 0x7fff / 2; n = rand() / x +1; if (n == 1) { x = 0x7fff / (d-2); n = rand() / x +1; for (z=1; z<=type; z++) slice_vert(q, q1, n); } else { x = 0x7fff / (d-2); n = rand() / x +1; for (z=1; z<=type; z++) slice_hort(q, q1, n); } } } if (cube_solved(q, q1) != -1) goto loop; } void load_file (FILE *fp_in) { short x,y,z; short old_d; old_d = d; d = fgetc (fp_in); if (old_d != d) { reset_size(); } 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); calc_cube(); rc = 1; refresh_image (q, q1); } int load_text (FILE *fp_in) { int rc; char hl[3], temp[15]; char buffer[256]; char buff2[256]; short x,y,z; strcpy (hl,""); memset (cur, '\0', sizeof(cur)); memset (buff2, '\0', sizeof (buff2)); x=0; z=0; rc = 1; reset_size (); while (rc !=EOF) { rc = fscanf (fp_in, "%s", temp); if (strlen(temp) > 2) { sprintf (buffer, "INVALID mov file containing %s",temp); MessageBox(NULL,buffer,"Error",MB_OK); x = 0; z = 0; rc = EOF; } else { strcpy (hl, temp); } if (rc !=EOF) { strcat (buff2, hl); strcat (buff2, " "); printf ("%s ", hl); if ((hl[0] =='T'|| hl[0] =='U') && (hl[1] == '1' || hl[1] =='\0' || hl[1]=='+')) { top (q, q1); x++; cur[x] = '1'; } else if ((hl[0] =='T'|| hl[0] =='U') && hl[1] == '2') { top (q, q1); top (q, q1); z++; x++; cur[x] ='2'; } else if ((hl[0] =='T'|| hl[0] =='U') && (hl[1] == '3' || hl[1] == '\'' || hl[1]=='-')) { top (q, q1); top (q, q1); top (q, q1); x++; cur[x] = '3'; } if (hl[0] =='D' && (hl[1] == '1' || hl[1] =='\0' || hl[1]=='+')) { down (q, q1); x++; cur[x] = '4'; } else if (hl[0] =='D' && hl[1] == '2') { down (q, q1); down (q, q1); z++; x++; cur[x] = '5'; } else if (hl[0] =='D' && (hl[1] == '3' || hl[1] == '\'' || hl[1]=='-')) { down (q, q1); down (q, q1); down (q, q1); x++; cur[x] ='6'; } if (hl[0] =='L' && (hl[1] == '1' || hl[1] =='\0' || hl[1]=='+')) { left (q, q1); x++; cur[x] ='7'; } else if (hl[0] =='L' && hl[1] == '2') { left (q, q1); left (q, q1); z++; x++; cur[x] ='8'; } else if (hl[0] =='L' && (hl[1] == '3' || hl[1] == '\'' || hl[1]=='-')) { left (q, q1); left (q, q1); left (q, q1); x++; cur[x] ='9'; } if (hl[0] =='F' && (hl[1] == '1' || hl[1] =='\0' || hl[1]=='+')) { front (q, q1); x++; cur[x] ='A'; } else if (hl[0] =='F' && hl[1] == '2') { front (q, q1); front (q, q1); z++; x++; cur[x]='B'; } else if (hl[0] =='F' && (hl[1] == '3' || hl[1] == '\'' || hl[1]=='-')) { front (q, q1); front (q, q1); front (q, q1); x++; cur[x] ='C'; } if (hl[0] =='R' && (hl[1] == '1' || hl[1] =='\0' || hl[1]=='+')) { right (q, q1); x++; cur[x]='D'; } else if (hl[0] =='R' && hl[1] == '2') { right (q, q1); right (q, q1); z++; x++; cur[x]='E'; } else if (hl[0] =='R' && (hl[1] == '3' || hl[1] == '\'' || hl[1]=='-')) { right (q, q1); right (q, q1); right (q, q1); x++; cur[x]='F'; } if (hl[0] =='B' && (hl[1] == '1' || hl[1] =='\0' || hl[1]=='+')) { back (q, q1); x++; cur[x]='G'; } else if (hl[0] =='B' && hl[1] == '2') { back (q, q1); back (q, q1); z++; x++; cur[x]='H'; } else if (hl[0] =='B' && (hl[1] == '3' || hl[1] == '\'' || hl[1]=='-')) { back (q, q1); back (q, q1); back (q, q1); x++; cur[x]='I'; } else if (hl[0] =='d' && (hl[1] == '3' || hl[1] =='\'' || hl[1]=='-')) { slice_hort (q, q1, 2); slice_hort (q, q1, 2); slice_hort (q, q1, 2); x++; } else if (hl[0] =='d' && (hl[1] == '1' || hl[1] =='\0' || hl[1]=='+')) { slice_hort (q, q1, 2); x++; } else if (hl[0] =='u' && (hl[1] == '3' || hl[1] =='\'' || hl[1]=='-')) { slice_hort (q, q1, 1); x++; } else if (hl[0] =='u' && (hl[1] == '1' || hl[1] =='\0' || hl[1]=='+')) { slice_hort (q, q1, 1); slice_hort (q, q1, 1); slice_hort (q, q1, 1); x++; } else if (hl[0] =='u' && hl[1] == '2') { slice_hort (q, q1, 1); slice_hort (q, q1, 1); z++; x++; } else if (hl[0] =='r' && (hl[1] == '3' || hl[1] =='\'' || hl[1]=='-')) { slice_vert (q, q1, 2); x++; } else if (hl[0] =='r' && (hl[1] == '1' || hl[1] =='\0' || hl[1]=='+')) { slice_vert (q, q1, 2); slice_vert (q, q1, 2); slice_vert (q, q1, 2); x++; } else if (hl[0] =='r' && hl[1] == '2') { slice_vert (q, q1, 2); slice_vert (q, q1, 2); z++; x++; } else if (hl[0] =='l' && (hl[1] == '3' || hl[1] =='\'' || hl[1]=='-')) { slice_vert (q, q1, 1); slice_vert (q, q1, 1); slice_vert (q, q1, 1); x++; } else if (hl[0] =='l' && (hl[1] == '1' || hl[1] =='\0' || hl[1]=='+')) { slice_vert (q, q1, 1); x++; } else if (hl[0] =='l' && hl[1] == '2') { slice_vert (q, q1, 1); slice_vert (q, q1, 1); z++; x++; } } } sprintf (buffer, " Number of moves %d q %d f\n%s", x+z, x, buff2); MessageBox(NULL,buffer,"Moves",MB_OK); rc = 0; while (rc != 8 || est[rc]) { if (est[rc] == '.') { break; } est[rc] = est[rc]; rc++; } est[rc] = '\0'; strcat (est, ".q"); /* save(3); */ return(x); } /****************************/ /* Load in Cube Arrangement */ /****************************/ void load(int type) { FILE *fp_in; short hold, xc, order, resetf; char buffer[256]; order = 0; memset (est, '\0', sizeof(est)); GetFilename(type); if (type == 3) { if (ee[0] =='q') { type =1; } if (ee[0] =='m') { type =2; } } fp_in = fopen (est,"r"); /* Check for non-existent filename */ if (fp_in == NULL) { sprintf (buffer, " No input file %s\n",est); MessageBox(NULL,buffer,"Error",MB_OK); goto out; } if (type == 1) { load_file (fp_in); } if (type == 2) { xc = load_text (fp_in); order=1; if (resetf == 1) { while (resetf == 1) { order++; for (hold=1; hold<=xc; hold++) { if ( cur[hold] =='1' ) top(q, q1); else if ( cur[hold] =='2') top2(q, q1); else if ( cur[hold] =='3') { top2(q, q1); top(q, q1); } else if ( cur[hold] =='4') down(q, q1); else if ( cur[hold] =='5') down2(q, q1); else if ( cur[hold] =='6') { down2(q, q1); down(q, q1); } else if ( cur[hold] =='7') left (q, q1); else if ( cur[hold] =='8') left2 (q, q1); else if ( cur[hold] =='9') { left2 (q, q1); left (q, q1); } else if ( cur[hold] =='A') front (q, q1); else if ( cur[hold] =='B') front2 (q, q1); else if ( cur[hold] =='C') { front2 (q, q1); front (q, q1); } else if ( cur[hold] =='D') right (q, q1); else if ( cur[hold] =='E') right2 (q, q1); else if ( cur[hold] =='F') { right2 (q, q1); right (q, q1); } else if ( cur[hold] =='G') back (q, q1); else if ( cur[hold] =='H') back2 (q, q1); else if ( cur[hold] =='I') { back2 (q, q1); back (q, q1); } } if (cube_solved (q, q1) == 0) resetf =0; } } } fclose (fp_in); out:; } void orient (CUBE q, CUBE q1) { int odd_flag, ct, a, b, rc; int count; float x; rc = 0; count = 0; x = (float)d; if ((x/2) == (d/2)) { odd_flag = 0; } else { odd_flag = 1; } if (odd_flag == 1) { /* Find centre point */ ct = d/2; if (q[TOP][ct][ct] == TOP) { /* Do nothing */ } if (q[DOWN][ct][ct] == TOP) { cube_down (q, q1); cube_down (q, q1); } else if (q[LEFT][ct][ct] == TOP) { cube_right (q, q1); cube_up (q, q1); } else if (q[RIGHT][ct][ct] == TOP) { cube_left (q, q1); cube_up (q, q1); } else if (q[FRONT][ct][ct] == TOP) { cube_up (q, q1); } else if (q[BACK][ct][ct] == TOP) { cube_down (q, q1); } if (q[FRONT][ct][ct] == FRONT) { /* Do nothing */ } else if (q[FRONT][ct][ct] == LEFT) { cube_left (q, q1); } else if (q[FRONT][ct][ct] == RIGHT) { cube_right (q, q1); } else if (q[FRONT][ct][ct] == BACK) { cube_left (q, q1); cube_left (q, q1); } } if (odd_flag == 0) { while (rc == 0) { a = 0x7fff / (4); b = rand() / a + 1; count++; if (count == 1000) { rc=1; printf ("Error: Illegal Arrangement"); } switch (b) { case 1: cube_right(q, q1); break; case 2: cube_left (q, q1); break; case 3: cube_up (q, q1); break; case 4: cube_down (q, q1); break; } if (q[FRONT][0][d-1] == FRONT && q[TOP][d-1][d-1] == TOP && q[RIGHT][0][0] == RIGHT) rc = 1; } } } void MyRectangle (HDC hdc, HBRUSH hbrush, short l, short t, short r, short b) { RECT rect; MoveToEx(hdc,l,t,NULL); LineTo(hdc,r,t); LineTo(hdc,r,b); LineTo(hdc,l,b); LineTo(hdc,l,t); rect.left = l+1; rect.top = t+1; rect.right = r; rect.bottom = b; FillRect(hdc,&rect,hbrush); } // MyRectangle void RedrawScreen (HDC hdc) { HPEN OldPen; UINT OldAlign; char buffer[250]; short c, x, y; OldPen = (HPEN)SelectObject(hdc,BlackPen); OldAlign = SetTextAlign(hdc,TA_CENTER); for (c=0; c<=SIDES-1; c++) { for (x=0; x<= d-1; x++) { tempy = posy[c][x] + 8; for (y=0; y<= d-1; y++) { tempx = posx[c][y]; MyRectangle(hdc, ColourBrush[q[c][x][y]], tempx, tempy, tempx+size_x-2, tempy+size_y-2); } } } /* sprintf (buffer, "%d", q[FRONT][0][0]); MessageBox(NULL,buffer,"Info",MB_OK); */ TextOut(hdc,(10+110)/2,170,HelloWorld,strlen(HelloWorld)); SetTextAlign(hdc,OldAlign); SelectObject(hdc,OldPen); } // RedrawScreen int indexer (CUBE q, CUBE q1) { int x, y, z, match, cube_flag, ref_flag, n, w; int total_m, total_c; int rc; char buffer[256]; total_m = 0; total_c = 0; cube_flag = 0; ref_flag = 0; orient(q, q1); refresh_image (q, q2); total_m=0; for (w=1; w<=2; w++) { if (w == 2 && ref_flag == 0) { reflect_c (q, q1); total_c = total_m; ref_flag = 1; } for (x=1; x<=24; x++) { if (x == 1) { /* Do nothing */ } if ((x>=2 && x<=4) || (x>=9 && x<=10) || (x>=14 && x<=16) || (x>=18 && x<=19) || (x==24)) { rot_col (TOP, FRONT, RIGHT, LEFT, DOWN, BACK); cube_right (q, q1); } if (x == 5) { rot_col (TOP, FRONT, RIGHT, LEFT, DOWN, BACK); cube_right (q, q1); rot_col (FRONT, LEFT, DOWN, TOP, BACK, RIGHT); cube_down (q, q1); } if ((x>=6 && x<=7) || (x>=11 && x<=13)) { rot_col (FRONT, LEFT, DOWN, TOP, BACK, RIGHT); cube_down (q, q1); } if (x == 8) { rot_col (FRONT, LEFT, DOWN, TOP, BACK, RIGHT); cube_down (q, q1); rot_col (FRONT, LEFT, DOWN, TOP, BACK, RIGHT); cube_down (q, q1); rot_col (TOP, FRONT, RIGHT, LEFT, DOWN, BACK); cube_right (q, q1); } if (x == 17) { rot_col (FRONT, LEFT, DOWN, TOP, BACK, RIGHT); cube_down (q, q1); rot_col (FRONT, LEFT, DOWN, TOP, BACK, RIGHT); cube_down (q, q1); } if (x == 20) { for (n=1; n<=3; n++) { rot_col (FRONT, LEFT, DOWN, TOP, BACK, RIGHT); cube_down (q, q1); } } if (x>=21 && x<=22) { for (n=1; n<=3; n++) { rot_col (TOP, FRONT, RIGHT, LEFT, DOWN, BACK); cube_right (q, q1); } } if (x == 23) { rot_col (TOP, FRONT, RIGHT, LEFT, DOWN, BACK); cube_right (q, q1); for (n=1; n<=3; n++) { rot_col (FRONT, LEFT, DOWN, TOP, BACK, RIGHT); cube_down (q, q1); } } rc = compare_cube (q, q2); /* If cube IS the same, then total_m++ */ if (rc == 0) { total_m++; } } for (n=1; n<=3; n++) { rot_col (TOP, FRONT, RIGHT, LEFT, DOWN, BACK); cube_right (q, q1); } for (n=1; n<=3; n++) { rot_col (FRONT, LEFT, DOWN, TOP, BACK, RIGHT); cube_down (q, q1); } for (n=1; n<=2; n++) { rot_col (TOP, FRONT, RIGHT, LEFT, DOWN, BACK); cube_right (q, q1); } } if (total_m !=0) { total_m = 48 / total_m; } if (total_c !=0) { total_c = 24 / total_c; } sprintf (buffer, "Total C: %2d Total M: %2d ", total_c, total_m); MessageBox(NULL,buffer,"Symmetry",MB_OK); reflect_c (q, q1); return( total_m); } /********************************************************/ BOOL AppInit (HINSTANCE hInstance, HINSTANCE hPrevInstance, int nCmdShow) { WNDCLASS cls; if ((BlackPen = CreatePen(PS_SOLID,1,RGBBlack)) == NULL) return FALSE; if ((WhiteBrush = CreateSolidBrush(RGBWhite)) == NULL) return FALSE; if ((RedBrush = CreateSolidBrush(RGBRed)) == NULL) return FALSE; if ((YellowBrush = CreateSolidBrush(RGBYellow)) == NULL) return FALSE; if ((OrangeBrush = CreateSolidBrush(RGBOrange)) == NULL) return FALSE; if ((GreenBrush = CreateSolidBrush(RGBGreen)) == NULL) return FALSE; if ((BlueBrush = CreateSolidBrush(RGBBlue)) == NULL) return FALSE; if (!hPrevInstance) { cls.style = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS; cls.lpfnWndProc = MainWndProc; cls.cbClsExtra = 0; cls.cbWndExtra = 0; cls.hInstance = hInstance; cls.hIcon = LoadIcon(NULL,IDI_APPLICATION); cls.hCursor = LoadCursor(NULL,IDC_ARROW); cls.hbrBackground = WhiteBrush; cls.lpszMenuName = NULL; cls.lpszClassName = MyName; if (!RegisterClass(&cls)) return FALSE; } if ((MainHwnd = CreateWindow(MyName,MyName,WS_OVERLAPPEDWINDOW,50,50,600,400,NULL,NULL,hInstance,NULL)) == NULL) return FALSE; ShowWindow(MainHwnd,nCmdShow); return TRUE; } // AppInit /********************************************************/ void AppExit () { DWORD i; for (i = 0; i < 6; i++) { if (ColourBrush[i]) DeleteObject(ColourBrush[i]); ColourBrush[i] = NULL; } if (BlackPen) DeleteObject(BlackPen); BlackPen = NULL; } // AppExit /********************************************************/ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; size_x = SIZE_X; size_y = SIZE_Y; sep_x = SEP_X; sep_y = SEP_Y; initial_pts(); initial_sides (); calc_cube(); refresh_image(q, q1); if (!AppInit(hInstance,hPrevInstance,nCmdShow)) { MessageBox(NULL,"Failure during initialisation!\n\nClick OK to exit.",MyName,MB_OK | MB_ICONERROR); return 0; } for (;;) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { WaitMessage(); } } AppExit(); return msg.wParam; } // WinMain /********************************************************/ LRESULT CALLBACK MainWndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; char buffer[10]; switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CHAR: switch (wParam) { case 'u': top(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case 'd': down(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case 'f': front(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case 'b': back(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case 'l': left(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case 'r': right(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case 'm': mix (q, q1, 2); InvalidateRect(hwnd,NULL,FALSE); return 0; case ',': mix (q, q1, 1); InvalidateRect(hwnd,NULL,FALSE); return 0; case '*': reset_size(); InvalidateRect(hwnd,NULL,FALSE); return 0; case '-': slice_vert(q, q1, 1); InvalidateRect(hwnd,NULL,FALSE); return 0; case '=': slice_hort(q, q1, 1); InvalidateRect(hwnd,NULL,FALSE); return 0; case '\\': reflect_c(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case '|': reflect_rl (q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case '%': indexer(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case '?': help (); InvalidateRect(hwnd,NULL,FALSE); return 0; case '[': load (1); InvalidateRect(hwnd,NULL,FALSE); return 0; case '{': load (2); InvalidateRect(hwnd,NULL,FALSE); return 0; } case WM_KEYDOWN: switch(wParam) { case VK_LEFT: cube_left(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case VK_RIGHT: cube_right(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case VK_UP: cube_up(q, q1); InvalidateRect(hwnd,NULL,FALSE); return 0; case VK_DOWN: cube_down(q ,q1); InvalidateRect(hwnd,NULL,FALSE); return 0; } case WM_PAINT: RedrawScreen(BeginPaint(hwnd,&ps)); EndPaint(hwnd,&ps); return 0; } return DefWindowProc(hwnd,uMsg,wParam,lParam); } // MainWndProc /********************************************************/ void initial_sides () { short x,y,z; /* Black = 254 */ col[0] =255; /* White */ col[1] =224; /* Red */ col[2] =253; /* Yellow for Allegro */ col[3] =240; /* Orange */ col[4] =21; /* Green */ col[5] =3; /* Blue */ 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; } void calc_cube() { short x,y; short s,t; short offset_x, offset_y; int c=0; offset_y=0; offset_x=4; for (s=0; s<=2; s++) { for (t=0; t<=1; t++) { for (x=0; x<= d-1; x++) { posy[c][x] = size_y*x+(tt[c]*(sep_x+(size_y*(d-2))))+offset_y; for (y=0; y<= d-1; y++) { posx[c][y] = size_x*y+(ss[c]*(sep_y+(size_x*(d-2))))+offset_x; } } c++; } } } void initial_pts() { ss[0] =1; tt[0] =0; ss[1] =0; tt[1] =1; ss[2] =1; tt[2] =1; ss[3] =2; tt[3] =1; ss[4] =3; tt[4] =1; ss[5] =1; tt[5] =2; }