汉诺塔c源程序实现及讲解

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

汉诺塔C源程序实现及讲解制作人:贾维华、刘璐汉诺塔•一、汉诺塔游戏简介及其玩法•二、设计思路及其程序设计流程•三、源程序及其讲解•四、游戏程序中应注意的问题一、汉诺塔游戏简介及其玩法•1游戏方法:点击PLAY开始,会看到3个柱子和从大到小编着号码的盘子,而且盘子自上而下半径依次增大。成功把盘子顺序不变的堆到最右边的竹子旁为胜利。•2游戏规则:一次只能移动一个木块,盘子可以累起来,但是必须把半径小的放在半径大的上面。•3操作指南:在挂有圆盘的柱子上方点击向上按钮‘W’或者‘UPDOWN’按键选中圆盘,按左右移动键移动到相对应的柱子上方,按向下键放下圆盘,至此完成一次圆盘移动。二、设计思路及程序设计流程•1设计思路:本设计中将盘子的数目设定为3~9个。设三个柱子A、B、C移动的过程可分解为三个步骤:第一步把A上的n-1个圆盘移到B上;第二步把A上的一个圆盘移到C上;第三步把B上的n-1个圆盘移到C上;其中第一步和第三步是类同的。其实际思想是利用一个递归原理。例如最简单的三阶汉诺塔移动方法:A→C,A→B,C→B,A→C,B→A,B→C,A→C2程序设计流程图:三、源程序及其讲解•1源程序:见wintc•2主程序构架•main()•{•INITIAL();/*界面初始化*/•Start_Logo();/*游戏启动始界面*/•getch();•again:•nStep=0;/*判断条件*/•Logo();/*进入游戏*/•gotoagain;•}3.主要程序分析•#includestdio.h•#includeconio.h•#includestdlib.h•#includedos.h•#includetime.h•#includegraphics.h•#defineSsetcolor•#defineFsetfillstyle•#defineRrectangle•#defineCcircle•#defineBbar•#defineB3Dbar3d•#defineLline•#defineOouttextxy•#defineX200•#defineY20•intnStep=0;•intnRecord[7]={7,15,31,63,127,255,511},nRec;•intnDisk,nDisknum;•intnBaseNum;•floatnDelay;•charchMark[15];•structrod•{•intnDisk[10][5];•intnDiskName[10];•}tree[3];•/********************************************/•ntoa(ints)•{•intb[15];•inti,j;•for(i=0;i15;i++)•chMark[i]='\0';•for(i=0;s=10;i++)•{b[i]=s%10;•s/=10;}•b[i]=s;•for(j=i;j=0;j--)•chMark[i-j]=b[j]+'0';•}•voidINITIAL()/**GAMEINITIAL**/•{•intgd=DETECT,gm=0;•initgraph(&gd,&gm,);•setbkcolor(0);•}•voidStart_Logo()/*启动标志*/•{•F(3,8);•B(200,150,439,259);•S(7);•R(201,151,442,262);•S(WHITE);•R(199,149,440,260);•settextstyle(DEFAULT_FONT,HORIZ_DIR,2);•O(233,180,HANOITOWER);•S(1);•O(232,179,HANOITOWER);••S(WHITE);•settextstyle(DEFAULT_FONT,HORIZ_DIR,2);•O(284,204,V2.0);•S(1);•O(283,203,V2.0);•settextstyle(DEFAULT_FONT,HORIZ_DIR,1);•S(WHITE);•O(205,240,WERTEBYLiuLuANDJiaWeiHua!);•O(204,239,WERTEBYLiuLuANDJiaWeiHua!);•}•voidFLASH_REC()•{•intNUM,x1=8,x2=10;•randomize();•for(NUM=0;NUM33;NUM++)•{•F(1,rand()%16);•B(x1,412,x2,414);•x1=x1+20;•x2=x2+20;•}•}•voidDrawRod()•{•F(1,3);•B(120,152,124,402);/*ROD1*/•B(320,152,324,402);/*ROD2*/•B(520,152,523,402);/*ROD3*/•F(6,LIGHTBLUE);•B(119,151,123,401);•B(319,151,323,401);•B(519,151,522,401);•}•voidDRAW()•{•F(1,8);•B(6,80,633,100);/**BROWNBAR**/•B(6,415,633,479);•F(1,7);/**GAMEBACKGROUND**/•B(7,102,632,410);•S(15);•R(6,0,633,479);•S(15);•settextstyle(DEFAULT_FONT,HORIZ_DIR,4);•O(70,30,HANOITOWERV2.0);•S(9);•O(71,31,HANOITOWERV2.0);•S(7);/**SHOWMESSAGE**/•settextstyle(DEFAULT_FONT,HORIZ_DIR,1);•O(189,445,-MADEBYLiuLuANDJiaWeiHua-);•L(165,419,165,425);•S(12);•O(170,419,MESSAGE:);/**MESSAGEBOX**/•S(7);•O(20,419,PRESSESCTOQUIT);•}•voidShowInfo(intnRn,intnWhetherGetDisk)•{•F(1,8);•B(230,418,620,428);•settextstyle(DEFAULT_FONT,HORIZ_DIR,1);•S(CYAN);•if(nRn==0&&nWhetherGetDisk==0)•O(235,419,YOUNOWATTHEFIRSTROD);•elseif(nRn==1&&nWhetherGetDisk==0)•O(235,419,YOUNOWATTHESECONDROD);•elseif(nRn==2&&nWhetherGetDisk==0)•O(235,419,YOUNOWATTHETHIRDROD);•elseif(nWhetherGetDisk==1)•{S(YELLOW);•switch(nRn)•{case0:O(235,419,YOUGETTHETOPDISKATTHEFIRSTROD);break;•case1:O(235,419,YOUGETTHETOPDISKATTHESECONDROD);break;•case2:O(235,419,YOUGETTHETOPDISKATTHETHIRDROD);break;•default:break;•}•}•elseif(nRn==3&&nWhetherGetDisk==nBaseNum)•{S(WHITE);O(235,419,YOUSHOULDDOYOURBEST);}•elseif(nRn==3&&nWhetherGetDisk==nBaseNum*2)•O(235,419,YOUSEEMSPLAYNOTVERYGOOD...^_^);•elseif(nRn==4&&nWhetherGetDisk==0)•{S(13);O(235,419,O.KYOUHAVEFINISHEDYOURWORK);sleep(1);}•elseO(235,419,YOUHAVEGETDOWNTHEDISK);•}•voidShowRecord()•{•settextstyle(DEFAULT_FONT,HORIZ_DIR,1);•F(1,8);•B(78,82,90,94);•ntoa(nDisk);•S(YELLOW);•O(40,86,DISK:);•S(15);•O(80,86,chMark);•B(348,82,360,94);•ntoa(nRec);•S(YELLOW);•O(290,86,RECORD:);•S(15);•O(350,86,chMark);•B(560,82,610,94);•ntoa(nStep);•S(YELLOW);•O(520,86,STEP:);•S(15);•O(562,86,chMark);•}•voidFillDisk(intx1,inty1,intx2,inty2,intcolor,intcolor1)•{•S(color);•F(1,color);•B3D(x1,y1,x2,y2,5,5);•F(1,color1);•B3D(x1+1,y1+1,x2+1,y2+1,5,5);•}•/*====ENDOFFUNFADEOUT====*/•voidClrScreen(void)•{•intNUM;•F(11,9);•for(NUM=84;NUM476;NUM++)•{B(7,NUM,632,NUM+2);•delay(20);•}•}•intBall(intnRn)•{•F(1,7);•B(20,105,609,130);•F(1,RED);•S(RED);•C(118+(nRn*200),115,10);•floodfill(118+(nRn*200),115,RED);•}•voidQuit()•{•ClrScreen();•settextstyle(DEFAULT_FONT,HORIZ_DIR,2);•S(WHITE);•O(180,210,THANKYOUTOPLAY);•O(180,230,2010.6);•S(RED);•O(181,211,THANKYOUTOPLAY);•O(181,231,2010.6);•O(181,260,ANYKEYTOQUIT);•S(GREEN);•O(180,180,GAMEOVER);•S(15);•O(181,181,GAMEOVER);•getch();•closegraph();•exit(0);•}•intIsWin()•{•inti,nStep_Sub_Rec;•intnDiskNum=0;•for(i=0;inDisk;i++)•{if(tree[2].nDisk[i][0]==1)nDiskNum++;}•nStep_Sub_Rec=nStep-nRec;•if(nDiskNum==nDisk)•{ShowInfo(4,0);/**ITCANBESETASYOURWAY**/•nBaseNum=nRec/9;•if(nStep_Sub_Rec=nBaseNum)/**WIN**/•{ClrScreen();•S(GREEN);settextstyle(DEFAULT_FONT,HORIZ_DIR,3);•O(230,200,YOUWIN);•S(RED);•O(231,201,YOUWIN);}•elseif(nStep_Sub_Rec=nBaseNum*2)•{ClrScreen();•S(RED);•settextstyle(DEFAULT_FONT,HORIZ_DIR,3);•O(220,200,JUSTSOSO);•S(LIGHTBLUE);•O(221,201,JUSTSOSO);}•else•{ClrScreen();•S(LIGHTBLUE);•settextstyle(DEFAULT_FONT,HORIZ_DIR,3);•O(175,200,Y

1 / 21
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功