| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 652 人关注过本帖
标题:汉诺塔12种玩法带动态显示
只看楼主 加入收藏
tangtang55
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-9-2
收藏
 问题点数:0 回复次数:1 
汉诺塔12种玩法带动态显示
第一次上编程论坛,看论坛很久没发表文章了,发一个自己编的程序,相信这个游戏很多人玩过,有兴趣的可以看看,需要交流的可以发信息到邮箱:1355620288@
程序代码和可执行EXE程序如下:
汉诺塔12种玩法带动态显示(11-2013.1.15).zip (43.96 KB)

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int jg[2187]={0},m=-1,z=0,z2;
void hannuota1(int n,char i,char j,char k);
void hannuota2(int n,char i,char j,char k);
void hannuota3(int n,char i,char j,char k);
void hannuota4(int n,char i,char j,char k);
void hannuota5(int n,char i,char j,char k);
void hannuota6(int n,char i,char j,char k);
void hannuota7(int n,char i,char j,char k);
void hannuota8(int n,char i,char j,char k);
void hannuota9(int n,char i,char j,char k);
void hannuota10(int n,char i,char j,char k);
void hannuota11(int n,char i,char j,char k);
void jieguo(char n,char m)
{
    int a;
    a=(n+m)*(n-m);
    switch(a)
    {
    case -131: jg[z++]=1; break;
    case  131: jg[z++]=2; break;
    case -264: jg[z++]=3; break;
    case  264: jg[z++]=4; break;
    case -133: jg[z++]=5; break;
    case  133: jg[z++]=6; break;
    }
}
//*******************************************
void hannuota0(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
    }
    else
    {
        hannuota0(n-1,i,k,j);
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
        hannuota0(n-1,j,i,k);
    }
}
//*******************************************
void hannuota1(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        {
        jieguo(i,j);
        jieguo(j,k);
        }
        printf(" (%c,%c)   (%c,%c)  ",i,j,j,k);
    }
    else
    {
        hannuota1(n-1,i,j,k);
        if(z2<8)
        jieguo(i,j);
        printf(" (%c,%c)  ",i,j);
        hannuota2(n-1,k,j,i);
        if(z2<8)
        jieguo(j,k);
        printf(" (%c,%c)  ",j,k);
        hannuota1(n-1,i,j,k);
    }
}
//*******************************************
void hannuota2(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
    }
    else
    {
        hannuota3(n-1,i,k,j);
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
        hannuota4(n-1,j,i,k);
    }
}
//*******************************************
void hannuota3(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
    }
    else
    {
        hannuota2(n-1,i,k,j);
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
        hannuota6(n-1,j,i,k);
    }
}
//*******************************************
void hannuota4(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
    }
    else
    {
        hannuota5(n-1,i,k,j);
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
        hannuota2(n-1,j,i,k);
    }
}
//*******************************************
void hannuota5(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
    }
    else
    {
        hannuota4(n-1,i,k,j);
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
        hannuota1(n-1,j,i,k);
    }
}
//*******************************************
void hannuota6(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
    }
    else
    {
        hannuota1(n-1,i,k,j);
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
        hannuota3(n-1,j,i,k);
    }
}
//*******************************************
void hannuota7(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
    }
    else
    {
        hannuota8(n-1,i,k,j);
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
        hannuota7(n-1,j,i,k);
    }
}
//*******************************************
void hannuota8(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        {
        jieguo(i,j);
        jieguo(j,k);
        }
        printf(" (%c,%c)   (%c,%c)  ",i,j,j,k);
    }
    else
    {
        hannuota8(n-1,i,j,k);
        if(z2<8)
        jieguo(i,j);
        printf(" (%c,%c)  ",i,j);
        hannuota8(n-1,k,j,i);
        if(z2<8)
        jieguo(j,k);
        printf(" (%c,%c)  ",j,k);
        hannuota8(n-1,i,j,k);
    }
}
//*******************************************
void hannuota9(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
    }
    else
    {
        hannuota9(n-1,i,k,j);
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
        hannuota8(n-1,j,i,k);
    }
}
//*******************************************
void hannuota10(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
    }
    else
    {
        hannuota11(n-1,i,k,j);
        if(z2<8)
        jieguo(i,k);
        printf(" (%c,%c)  ",i,k);
        hannuota11(n-1,j,i,k);
    }
}
//*******************************************
void hannuota11(int n,char i,char j,char k)
{
    if(n==1)
    {
        if(z2<8)
        {
        jieguo(i,j);
        jieguo(j,k);
        }
        printf(" (%c,%c)   (%c,%c)  ",i,j,j,k);
    }
    else
    {
        hannuota11(n-1,i,j,k);
        if(z2<8)
        jieguo(i,j);
        printf(" (%c,%c)  ",i,j);
        hannuota10(n-1,k,j,i);
        if(z2<8)
        jieguo(j,k);
        printf(" (%c,%c)  ",j,k);
        hannuota11(n-1,i,j,k);
    }
}
//*******************************************
void print(int s)
{
switch(m)
    {
    case 0: printf("无禁步,第%04d步,按任意键继续。\n",s+1); break;
    case 1: printf("禁止从A移到C,第%04d步,按任意键继续。\n",s+1); break;
    case 2: printf("禁止从C移到A,第%04d步,按任意键继续。\n",s+1); break;
    case 3: printf("禁止从B移到A,第%04d步,按任意键继续。\n",s+1); break;
    case 4: printf("禁止从C移到B,第%04d步,按任意键继续。\n",s+1); break;
    case 5: printf("禁止从B移到C,第%04d步,按任意键继续。\n",s+1); break;
    case 6: printf("禁止从A移到B,第%04d步,按任意键继续。\n",s+1); break;
    case 7: printf("禁止从A移到B和B移到A,第%04d步,按任意键继续。\n",s+1); break;
    case 8: printf("禁止从A移到C和C移到A,第%04d步,按任意键继续。\n",s+1); break;
    case 9: printf("禁止从B移到C和C移到B,第%04d步,按任意键继续。\n",s+1); break;
    case 10: printf("禁止从A移到B和B移到C,第%04d步,按任意键继续。\n",s+1); break;
    case 11: printf("禁止从C移到B和B移到A,第%04d步,按任意键继续。\n",s+1); break;
    }
}
//*******************************************
void xianshi(int n)
{
    int a[8]={0},b[8]={0},c[8]={0};
    int i=7-z2,j=7,k=7,l,s,t;
    char s1[23]="            0         ",s2[23]="           000        ",s3[23]="          00000       ";
    char s4[23]="         0000000      ",s5[23]="        000000000     ",s6[23]="       00000000000    ";
    char s7[23]="      0000000000000   ",s8[23]="     000000000000000  ",s9[23]="                      ";
    for(l=7;l>i;l--)
        a[l]=n--;
    for(s=0;s<z;s++)
    {
    if(s) getch();
    system("cls");
    print(s);
    switch(jg[s])
    {
    case 1: i++,t=a[i],a[i]=0; break;    //A到B;
    case 2: j++,t=b[j],b[j]=0; break;    //B到A;
    case 3: i++,t=a[i],a[i]=0; break;    //A到C;
    case 4: k++,t=c[k],c[k]=0; break;    //C到A;
    case 5: j++,t=b[j],b[j]=0; break;    //B到C;
    case 6: k++,t=c[k],c[k]=0; break;    //C到B;
    }
    switch(jg[s])
    {
    case 1: switch(t)
            {
                case 1: printf("\n%s\n",s2); break;
                case 2: printf("\n%s\n",s3); break;
                case 3: printf("\n%s\n",s4); break;
                case 4: printf("\n%s\n",s5); break;
                case 5: printf("\n%s\n",s6); break;
                case 6: printf("\n%s\n",s7); break;
                case 7: printf("\n%s\n",s8); break;
            } break;
    case 2: switch(t)
            {
                case 1: printf("\n%s%s\n",s9,s2); break;
                case 2: printf("\n%s%s\n",s9,s3); break;
                case 3: printf("\n%s%s\n",s9,s4); break;
                case 4: printf("\n%s%s\n",s9,s5); break;
                case 5: printf("\n%s%s\n",s9,s6); break;
                case 6: printf("\n%s%s\n",s9,s7); break;
                case 7: printf("\n%s%s\n",s9,s8); break;
            } break;
    case 3: switch(t)
            {
                case 1: printf("\n%s\n",s2); break;
                case 2: printf("\n%s\n",s3); break;
                case 3: printf("\n%s\n",s4); break;
                case 4: printf("\n%s\n",s5); break;
                case 5: printf("\n%s\n",s6); break;
                case 6: printf("\n%s\n",s7); break;
                case 7: printf("\n%s\n",s8); break;
            } break;
    case 4: switch(t)
            {
                case 1: printf("\n%s%s%s\n",s9,s9,s2); break;
                case 2: printf("\n%s%s%s\n",s9,s9,s3); break;
                case 3: printf("\n%s%s%s\n",s9,s9,s4); break;
                case 4: printf("\n%s%s%s\n",s9,s9,s5); break;
                case 5: printf("\n%s%s%s\n",s9,s9,s6); break;
                case 6: printf("\n%s%s%s\n",s9,s9,s7); break;
                case 7: printf("\n%s%s%s\n",s9,s9,s8); break;
            } break;
    case 5: switch(t)
            {
                case 1: printf("\n%s%s\n",s9,s2); break;
                case 2: printf("\n%s%s\n",s9,s3); break;
                case 3: printf("\n%s%s\n",s9,s4); break;
                case 4: printf("\n%s%s\n",s9,s5); break;
                case 5: printf("\n%s%s\n",s9,s6); break;
                case 6: printf("\n%s%s\n",s9,s7); break;
                case 7: printf("\n%s%s\n",s9,s8); break;
            } break;
    case 6: switch(t)
            {
                case 1: printf("\n%s%s%s\n",s9,s9,s2); break;
                case 2: printf("\n%s%s%s\n",s9,s9,s3); break;
                case 3: printf("\n%s%s%s\n",s9,s9,s4); break;
                case 4: printf("\n%s%s%s\n",s9,s9,s5); break;
                case 5: printf("\n%s%s%s\n",s9,s9,s6); break;
                case 6: printf("\n%s%s%s\n",s9,s9,s7); break;
                case 7: printf("\n%s%s%s\n",s9,s9,s8); break;
            } break;
    }

    printf("\n%13d%22d%22d\n",0,0,0);
    printf("%13d%22d%22d\n",0,0,0);
    for(l=1;l<8;l++)
    {
        switch(a[l])
    {
    case 0: printf("%s",s1); break;
    case 1: printf("%s",s2); break;
    case 2: printf("%s",s3); break;
    case 3: printf("%s",s4); break;
    case 4: printf("%s",s5); break;
    case 5: printf("%s",s6); break;
    case 6: printf("%s",s7); break;
    case 7: printf("%s",s8); break;
    }
        switch(b[l])
    {
    case 0: printf("%s",s1); break;
    case 1: printf("%s",s2); break;
    case 2: printf("%s",s3); break;
    case 3: printf("%s",s4); break;
    case 4: printf("%s",s5); break;
    case 5: printf("%s",s6); break;
    case 6: printf("%s",s7); break;
    case 7: printf("%s",s8); break;
    }
        switch(c[l])
    {
    case 0: printf("%s",s1); break;
    case 1: printf("%s",s2); break;
    case 2: printf("%s",s3); break;
    case 3: printf("%s",s4); break;
    case 4: printf("%s",s5); break;
    case 5: printf("%s",s6); break;
    case 6: printf("%s",s7); break;
    case 7: printf("%s",s8); break;
    }
        printf("\n");
    }
    printf("   %019d   %019d   %019d\n",0,0,0);
    printf("\n%13s%22s%22s\n","A","B","C");

        getch();
    system("cls");
    print(s);
    switch(jg[s])
    {
    case 1: b[j]=t,j--; break;    //A到B;
    case 2: a[i]=t,b[j]=0,i--; break;    //B到A;
    case 3: c[k]=t,a[i]=0,k--; break;    //A到C;
    case 4: a[i]=t,c[k]=0,i--; break;    //C到A;
    case 5: c[k]=t,b[j]=0,k--; break;    //B到C;
    case 6: b[j]=t,c[k]=0,j--; break;    //C到B;
    }
    printf("\n\n\n%13d%22d%22d\n",0,0,0);
    printf("%13d%22d%22d\n",0,0,0);
    for(l=1;l<8;l++)
    {
        switch(a[l])
    {
    case 0: printf("%s",s1); break;
    case 1: printf("%s",s2); break;
    case 2: printf("%s",s3); break;
    case 3: printf("%s",s4); break;
    case 4: printf("%s",s5); break;
    case 5: printf("%s",s6); break;
    case 6: printf("%s",s7); break;
    case 7: printf("%s",s8); break;
    }
        switch(b[l])
    {
    case 0: printf("%s",s1); break;
    case 1: printf("%s",s2); break;
    case 2: printf("%s",s3); break;
    case 3: printf("%s",s4); break;
    case 4: printf("%s",s5); break;
    case 5: printf("%s",s6); break;
    case 6: printf("%s",s7); break;
    case 7: printf("%s",s8); break;
    }
        switch(c[l])
    {
    case 0: printf("%s",s1); break;
    case 1: printf("%s",s2); break;
    case 2: printf("%s",s3); break;
    case 3: printf("%s",s4); break;
    case 4: printf("%s",s5); break;
    case 5: printf("%s",s6); break;
    case 6: printf("%s",s7); break;
    case 7: printf("%s",s8); break;
    }
        printf("\n");
    }
    printf("   %019d   %019d   %019d\n",0,0,0);
    printf("\n%13s%22s%22s\n","A","B","C");
    }
}
//*******************************************
void main()
{
    int n=0,s;
    char i='A',j='B',k='C';
        for(;n<1;)
        {
            printf("请输入游戏级别,级别大于7时不进行动态显示:");
            scanf("%d",&n);
            z2=n;
            if(n<1)
                printf("输入错误\n");
        }
    printf("\n不禁步请输入0;\n");
    printf("禁止从A移到C请输入1;\n");
    printf("禁止从C移到A请输入2;\n");
    printf("禁止从B移到A请输入3;\n");
    printf("禁止从C移到B请输入4;\n");
    printf("禁止从B移到C请输入5;\n");
    printf("禁止从A移到B请输入6;\n");
    printf("禁止从A移到B和B移到A请输入7;\n");
    printf("禁止从A移到C和C移到A请输入8;\n");
    printf("禁止从B移到C和C移到B请输入9;\n");
    printf("禁止从A移到B和B移到C请输入10;\n");
    printf("禁止从C移到B和B移到A请输入11;\n");
    for(;m<0||m>11;)
    {
        printf("请输入禁走类型:");
        scanf("%d",&m);
        if(m<0||m>11)
            printf("输入错误\n");
    }
    printf("游戏移动步骤如下:\n");
    switch(m)
    {
    case 0: hannuota0(n,i,j,k); break;
    case 1: hannuota1(n,i,j,k); break;
    case 2: hannuota2(n,i,j,k); break;
    case 3: hannuota3(n,i,j,k); break;
    case 4: hannuota4(n,i,j,k); break;
    case 5: hannuota5(n,i,j,k); break;
    case 6: hannuota6(n,i,j,k); break;
    case 7: hannuota7(n,i,j,k); break;
    case 8: hannuota8(n,i,j,k); break;
    case 9: hannuota9(n,i,j,k); break;
    case 10: hannuota10(n,i,j,k); break;
    case 11: hannuota11(n,i,j,k); break;
    }
    if(n<8)
    {
    printf("\n动态显示请输入1,退出请输入0:");
        scanf("%d",&s);
        if(s==1)
            xianshi(n);
    }
    printf("\nPress any key to continue\n");
    getch();
}
搜索更多相关主题的帖子: 编程论坛 include 文章 动态 
2013-09-02 19:03
tangtang55
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-9-2
收藏
得分:0 
haha

[ 本帖最后由 tangtang55 于 2013-9-2 21:10 编辑 ]
2013-09-02 19:14
快速回复:汉诺塔12种玩法带动态显示
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033396 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved