《魔方算法源代码(18页).doc》由会员分享,可在线阅读,更多相关《魔方算法源代码(18页).doc(18页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-魔方算法源代码-第 18 页#include graphics.h#include stdio.hint color69=4,4,4,4,4,4,4,4,4, 2,2,2,2,2,2,2,2,2, 14,14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15,15, 7,7,7,7,7,7,7,7,7, 9,9,9,9,9,9,9,9,9;int column69=195,255,315,345,375,315,255,225,285, 180,180,180,240,300,300,300,240,240, 345,345,345,375,405,
2、405,405,375,375, 470,470,470,530,590,590,590,530,530, 55,55,55,85,115,115,115,85,85, 195,255,315,345,375,315,255,225,285;int row69=135,135,135,105,75,75,75,105,105, 180,240,300,300,300,240,180,180,240, 165,225,285,255,225,165,105,135,185, 90,150,210,210,210,150,90,90,150, 165,225,285,255,225,165,105
3、,135,185, 425,425,425,395,365,365,365,395,395;void boxinit() setcolor(CYAN); moveto(150,150); lineto(330,150); line(150,210,330,210); line(150,270,330,270); line(150,330,330,330); line(180,120,360,120); line(210,90,390,90); line(240,60,420,60); line(360,120,360,300); line(390,90,390,270); line(420,6
4、0,420,240); line(330,210,420,120); line(330,270,420,180); line(330,330,420,240); line(330,150,420,60); line(330,150,330,330); line(270,150,270,330); line(210,150,210,330); line(150,150,150,330); line(150,150,240,60); line(210,150,300,60); line(270,150,360,60); moveto(40,150); lineto(130,60); lineto(
5、130,240); lineto(40,330); lineto(40,150); line(70,120,70,300); line(100,90,100,270); line(40,210,130,120); line(40,270,130,180); moveto(440,60); lineto(620,60); lineto(620,240); lineto(440,240); lineto(440,60); line(440,120,620,120); line(440,180,620,180); line(500,60,500,240); line(560,60,560,240);
6、 moveto(150,440); lineto(330,440); lineto(420,350); lineto(240,350); lineto(150,440); line(210,440,300,350); line(270,440,360,350); line(180,410,360,410); line(210,380,390,380);void temp() delay(1);void upcolor()int i,j; for(i=0;i6;i+) for(j=0;j9;j+) setfillstyle(1,colorij); floodfill(columnij,rowij
7、,CYAN); temp();/该函数实现边面转90度的功能/bt为要转的边面void borderturn(int bt)int x1,x2; x1=colorbt0; x2=colorbt1; colorbt0=colorbt2; colorbt1=colorbt3; colorbt2=colorbt4; colorbt3=colorbt5; colorbt4=colorbt6; colorbt5=colorbt7; colorbt6=x1; colorbt7=x2;/该函数实现中间面转90度的功能/x1,x2,x3,x4为旋转涉及到的面参数,x5-x16为正方形参数说明是哪个正方形参与了
8、旋转/其中x5,x6,x7为x1面的/x8,x9,x10为x2面的/x11,x12,x13为x3面的/x14,x15,x16为x4面的/中间面旋转涉及到四个面的变化void medturn(int x1,int x2,int x3,int x4,int x5,int x6,int x7,int x8, int x9,int x10,int x11,int x12,int x13,int x14,int x15,int x16)int y1,y2,y3; y1=colorx1x5; y2=colorx1x6; y3=colorx1x7; colorx1x5=colorx2x8; colorx1x
9、6=colorx2x9; colorx1x7=colorx2x10; colorx2x8=colorx3x11; colorx2x9=colorx3x12; colorx2x10=colorx3x13; colorx3x11=colorx4x14; colorx3x12=colorx4x15; colorx3x13=colorx4x16; colorx4x14=y1; colorx4x15=y2; colorx4x16=y3;/函数实现UP面顺时针转90*i度的功能void turn1(int i)int k; for(k=0;ki;k+) borderturn(0); medturn(1,2
10、,3,4,0,7,6,0,7,6,6,7,0,6,7,0);/函数实现UP面和DOWN面中间层顺时针转90*i度的功能void turn2(int i)int k; for(k=0;ki;k+) medturn(1,2,3,4,1,8,5,1,8,5,5,8,1,5,8,1);/函数实现DOWN面顺时针转90*i度的功能void turn3(int i)int k; for(k=0;ki;k+) borderturn(5); medturn(1,2,3,4,2,3,4,2,3,4,4,3,2,4,3,2);/函数实现FRONT面顺时针转90*i度的功能void turn4(int i)int
11、k; for(k=0;ki;k+) borderturn(1); medturn(4,5,2,0,0,1,2,0,1,2,2,1,0,2,1,0);/函数实现FRONT面和BACK面的中间层顺时针转90*i度的功能void turn5(int i)int k; for(k=0;ki;k+) medturn(4,5,2,0,7,8,3,7,8,3,3,8,7,3,8,7);/函数实现BACK面顺时针转90*i度的功能void turn6(int i)int k; for(k=0;ki;k+) borderturn(3); medturn(4,5,2,0,6,5,4,6,5,4,4,5,6,4,5
12、,6);/函数实现RIGHT面顺时针转90*i度的功能void turn7(int i)int k; for(k=0;ki;k+) borderturn(2); medturn(1,5,3,0,6,5,4,2,3,4,4,5,6,4,3,2);/函数实现LEFT面和RIGHT面的中间层顺时针转90*i度的功能void turn8(int i)int k; for(k=0;ki;k+) medturn(1,5,3,0,7,8,3,1,8,5,3,8,7,5,8,1);/函数实现LEFT面顺时针转90*i度的功能void turn9(int i)int k; for(k=0;ki;k+) bord
13、erturn(4); medturn(1,5,3,0,0,1,2,0,7,6,2,1,0,6,7,0);/实现i次L-E-L-U-L-E-L-U功能void turna(int i)int k; for(k=0;ki;k+) turn9(1);/L turn2(3);/E turn9(3);/L turn1(1);/U turn9(1);/L turn2(1);/E turn9(3);/L turn1(3);/U/实现i次R-E-R-U-R-E-U功能void turnb(int i)int k; for(k=0;ki;k+) turn7(1);/R turn2(1);/E turn7(3);
14、/R turn1(3);/U turn7(1);/R turn2(3);/E turn7(3);/R turn1(1);/Uvoid turnc(int i)int k; for(k=0;ki;k+) turn7(1); turn9(1); turn1(3); turn9(3); turn1(1); turn7(3); turn1(3); turn9(1); turn1(1); turn9(3);void turnd(int i)int k; for(k=0;ki;k+) turn9(1); turn7(1); turn1(1); turn7(3); turn1(3); turn9(3); t
15、urn1(1); turn7(1); turn1(3); turn7(3); void turne(int i)int k; for(k=0;ki;k+) turn7(3); turn3(1); turn7(1); turn3(3); turn4(1); turn3(3); turn4(3);void turnf(int i)int k; for(k=0;ki;k+) turn4(1); turn2(1); turn9(1); turn2(3); turn4(1);void turnc1(int i)int k; for(k=0;ki;k+) turn4(3); turn2(3); turn7
16、(1); turn2(1); turn4(3);void turnd1(int i)int k; for(k=0;ki;k+) turn8(1); turn9(1); turnd(1); turn8(3); turn9(3);void turnb1(int i)int k; for(k=0;ki;k+) turn7(1); turnd(1); turn7(3);void turnlast()int k,total,k1,total1,i,j,c0,c1,c2,c3,c4,c5; c0=color08; c5=color58; /*对好第一层的十字架 */ total=0; while(tota
17、l4) break; i+; if(i=5) i=0; while(color51!=c5) turn1(1); if(i7) break; i+; if(i=8) i=0; while(color51!=c5) turn2(1); if(i7) break; i+; if(i=8) else total+;turn1(1);turn2(1);turn3(1); else total+;turn1(1);turn2(1);turn3(1); else total+;turn1(1);turn2(1);turn3(1); c0=color08; c1=color18; c2=color28; c
18、3=color38; c4=color48; c5=color58; while(color33!=c3) turn3(1); upcolor(); if(color43=c1) turnf(1); else if(color23=c1) turnc1(1); if(color43!=c4) turn5(2); turn1(2); turn5(2); upcolor(); /*对好第一层的十字架end*/ /*对好第一层*/ total=0; k=0; while(total4) break; i+; while(color12!=c1 | color42!=c4 | color50!=c5)
19、 i=0; while(color12!=c1 | color42!=c4 | color50!=c5) turn9(2); turnc(1); turn9(2); turn1(1); if(i3) break; i+; if(i100) break; k+; upcolor(); /*对好第一层end*/ c0=color08; c1=color18; c2=color28; c3=color38; c4=color48; total=0; /*开始对第二层 */ while(color25!=c2 | color35!=c3 | color31!=c3 | color45!=c4 | co
20、lor41!=c4 | color11!=c1 | color15!=c1 | color21!=c2) /*当看到顶层有第二层的东西就将其放下去*/ for(k=0;k4) break; i+; if(i4) break; i+; if(i4) break; i+; k=0; if(color25!=c2 | color35!=c3) turna(2); turn2(1); turn3(1); k=1; c1=color18; c2=color28; c3=color38; c4=color48; i=0; while(color01!=c2 | color17!=c3) turn2(1);
21、 k+; c1=color18; c2=color28; c3=color38; c4=color48; if(i3) break; i+; if(color01=c2 & color17=c3) turna(1); if(k10) break; total+; i=0; while(color13!=color18) turn2(1); if(i4) break; i+; upcolor(); /*对好第二层end*/ /* 对好第三层的四个角*/ c0=color08; c1=color18; c2=color28; c3=color38; c4=color48; j=0; while(c
22、olor00!=c0 | color10!=c1 | color40!=c4) | (color06!=c0 | color30!=c3 | color46!=c4) | (color02!=c0 | color20!=c2 | color16!=c1) /*将近角对好*/ k=0; while(color02!=c0 | color20!=c2 | color16!=c1) i=0; while(color02!=c0 | color20!=c2 | color16!=c1) turn1(1); if(i4) break; i+; if(i4) turnc(1); if(k16) break
23、; k+; upcolor(); k1=0; while(color00!=c0 | color10!=c1 | color40!=c4 | color06!=c0 | color30!=c3 | color46!=c4) /*将左角对好*/ k=0; while(color00!=c0 | color10!=c1 | color40!=c4) turnd(1); i=0; while(i15) break; k+; upcolor(); /*还是没有对好*/ if(k15)&(color00!=c0 | color10!=c1 | color40!=c4) turn1(1); turnc(1
24、); turn1(3); turnd(1); else /*将远角对好*/ i=0; while(i4) break; k1+; /*还是没有对好*/ if(k14)&(color06!=c0 | color30!=c3 | color46!=c4 | color00!=c0 | color10!=c1 | color40!=c4) turn1(1); turn2(1); turn3(1); c1=color18; c2=color28; c3=color38; c4=color48; if(j16) break; j+; upcolor(); /* 对好第三层的四个角end*/ /*对好第三
25、层的十字*/ k1=0; while(color01!=c0 | color17!=c1 | color03!=c0 | color37!=c3 | color05!=c0 | color27!=c2 | color07!=c0 | color47!=c4) /*找一个已经对好的边*/ k1=0; while(k14)&(color37!=c3 | color05!=c0) k=0; while(k4)&(color37!=c3 | color05!=c0) i=0; while(i4)&(color37!=c3 | color05!=c0) turn1(1); turn2(1); turn3
26、(1); c1=color18; c2=color28; c3=color38; c4=color48; i+; /*没有找到,变换一下角度*/ if(i=4)&(color37!=c3 | color05!=c0) turna(1); turn1(3); turna(1); turn1(1); k+; /*还是没有找到,翻转一下*/ if(k=4)&(color37!=c3 | color05!=c0) turna(1); turn2(3); turnb(1); turn2(1); turna(2); k1+; turn1(1); turn2(1); turn3(1); c1=color18
27、; c2=color28; c3=color38; c4=color48; upcolor(); /*对另外三边*/ k=0; while(k16)&(color01!=c0 | color17!=c1 | color03!=c0 | color27!=c2 | color07!=c0 | color47!=c4) i=0; while(i16) break; k1+; /*将底对齐*/ i=0; while(color13!=c1) turn3(1); if(i4) break; i+; upcolor();main() int driver,mode; int bkcolor69; int
28、 i,j,b; char a; FILE *fp; driver=DETECT; mode=0; initgraph(&driver,&mode,bgi); setbkcolor(1); for(i=0;i6;i+) for(j=0;j9;j+) bkcolorij=colorij; gotoxy(10,15); printf(Are you continue last game?ntpress n another); if(a=getch()!=n) fp=fopen(box.txt,r); b=getc(fp); for(i=0;i6;i+) for(j=0;j9;j+) colorij=
29、b; b=getc(fp); fclose(fp); cleardevice(); boxinit(); upcolor(); while(a=getch()!=q) switch (a) case 0: for(i=0;i6;i+) for(j=0;j9;j+) colorij=bkcolorij; upcolor(); break; case 1: turn1(1); upcolor(); break; case 2: turn2(1); upcolor(); break; case 3: turn3(1); upcolor(); break; case 4: turn4(1); upcolor(); break; case 5: turn5(1); upcolor(); break; case 6: turn6(1); upcolor(); break; case 7: turn7(1); upcolor(); break; case 8: turn8(1); upcolor(); break; case 9: turn9(1); upcolor(); break; case a: turna(1); upcolor(); break; case b: turnb(1); upcolor(); break; case c: turnc(1); upcolor();
限制150内