用户名: 密码: 忘记密码? 注册

拼图游戏(c语言)

作者:  时间: 2011-01-04
这个拼图游戏是大一时的c语言课程设计:
说明:
游戏说明:
一、游戏难度:
l--------------------------简单;
m--------------------------一般;
h--------------------------困难;
二、游戏玩法
按“↑”、“↓”、“←”、“→”使方块移动;
当结果为下列情况是获胜:
1、           1  2  3
              4  5  6
              7  8  
2、           1  2  3  4
              5  6  7  8
              9  10 11 12
              13 14 15  
3、           1  2  3  4  5
              6  7  8  9  10
              11 12 13 14 15
              16 17 18 19 20
              21 22 23 24  
三、其他说明
n--------------------------新游戏;
x--------------------------重选游戏难度;
Esc------------------------退出;
本代码在win-tc可运行:
 

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int num1[]={1,2,3,4,5,6,7,8,0};
int num2[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
int num3[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,0};
main()
{ int f1();
    int f2();
    int f3();
    char key=0;
    str();
}
str()
{
    char a;
     printf("low=l\nmedium=m\nhigh=h\ninput:l,m,h\n");
    a=getch();
     switch(a)
    {
    case 108: f1();break;
    case 109: f2();break;
    case 104: f3();break;
    case 27:break;
    }

}

f1()
{
    int pos;
    clrscr();
    randomize();
    newGame1();
    for(;;)
       {
       char key=0;
        key=getch();
        if(key==0) continue;
        pos=GetTheNull1();
        switch(key)
          {
            case 72:
                if(pos<=5) change1(pos,pos+3);
                break;
            case 80:
                if(pos>=3) change1(pos,pos-3);
                break;
            case 77:
                if(pos%3!=0) change1(pos,pos-1);
                break;
            case 75:
                if(pos%3!=2) change1(pos,pos+1);
                break;
            case 110:
                newGame1();break;
            case 120:
                str(); break;
          }
        update1();
        if(isSuccess1())
           {
            gotoxy(26,10);
            printf("\332\304\304\304\304\304\304\304\304\304\304\304\304\267");
            gotoxy(26,11);
            printf("\263 \272");
            gotoxy(26,12);
            printf("\263 Well Done! \272");
            gotoxy(26,13);
            printf("\263 \272");
            gotoxy(26,14);
            printf("\324\315\315\315\315\315\315\315\315\315\315\315\315\274\n");
            getch();
            str();
           }
        if(key==27) break;
       }
    clrscr();
}
 

newGame1()
{
    int i,a,b;
    for(i=0;i<8;i++) num1[i]=i+1;
    num1[8]=0;
    for(i=0;i<300;i++)
   {
        a=random(8);
        b=(a+2)%8;
        change1(a,b);
    }
    update1();
}

update1()
{
    int i,j;
    for(i=0;i<=2;i++)
      {
        gotoxy(24,8+i*3);
        for(j=0;j<=2;j++)
        {
            if (num1[j+i*3]!=0)
                printf("\332\304\304\304\304\267");
            else
                printf(" ");
         }
        gotoxy(24,9+i*3);
        for(j=0;j<=2;j++)
        {
            if (num1[j+i*3]!=0)
                printf("\263 %d \272",num1[j+i*3]);
            else
                printf(" ");
        }
        gotoxy(24,10+i*3);
        for(j=0;j<=2;j++)
       {
            if (num1[j+i*3]!=0)
                printf("\324\315\315\315\315\274");
            else
                printf(" ");
        }
        printf("\n");
      }

}

int isSuccess1()
{
    int i,ret=1;
    for(i=0;i<8;i++)
        ret=ret&&(num1[i]==(i+1));
    return ret;
 }

int GetTheNull1()
 {
    int i;
    for(i=0;i<=8;i++)
     {
        if (num1[i]==0)
            return i;

     }
}

change1(int a,int b)
{
    int temp;
    temp=num1[a];
    num1[a]=num1[b];
    num1[b]=temp;
}
f2()
{
  int pos;
    clrscr();
    randomize();
    newGame2();
    for(;;)
       {
       char key=0;
        key=getch();
        if(key==0) continue;
        pos=GetTheNull2();
        switch(key)
          {
            case 72:
                if(pos<=11) change2(pos,pos+4);
                break;
            case 80:
                if(pos>=4) change2(pos,pos-4);
                break;
            case 77:
                if(pos%4!=0) change2(pos,pos-1);
                break;
            case 75:
                if(pos%4!=3) change2(pos,pos+1);
                break;
            case 110:
                newGame2();break;
            case 120:
                str(); break;
          }
        update2();
        if(isSuccess2())
           {
            gotoxy(29,7);
            printf("\332\304\304\304\304\304\304\304\304\304\304\304\304\267");
            gotoxy(29,8);
            printf("\263 \272");
            gotoxy(29,9);
            printf("\263 Well Done! \272");
            gotoxy(29,10);
            printf("\263 \272");
            gotoxy(29,11);
            printf("\324\315\315\315\315\315\315\315\315\315\315\315\315\274\n");
            getch();
            str();
           }
        if(key==27) break;
       }
    clrscr();
}
 

newGame2()
{
    int i,a,b;
    for(i=0;i<15;i++) num2[i]=i+1;
    num2[15]=0;
    for(i=0;i<300;i++)
   {
        a=random(15);
        b=(a+2)%15;
        change2(a,b);
    }
    update2();
}

update2()
{
    int i,j;
    for(i=0;i<=3;i++)
      {
        gotoxy(24,4+i*4);
        for(j=0;j<=3;j++)
        {
            if (num2[j+i*4]!=0)
                printf("\332\304\304\304\304\267");
            else
                printf(" ");
         }
        gotoxy(24,5+i*4);
        for(j=0;j<=3;j++)
        {
            if (num2[j+i*4]!=0)
                printf("\263 %2d \272",num2[j+i*4]);
            else
                printf(" ");
        }
        gotoxy(24,6+i*4);
        for(j=0;j<=3;j++)
       {
            if (num2[j+i*4]!=0)
                printf("\324\315\315\315\315\274");
            else
                printf(" ");
        }


        printf("\n");
      }

}

int isSuccess2()
{
    int i,ret=1;
    for(i=0;i<15;i++)
        ret=ret&&(num2[i]==(i+1));
    return ret;
 }

int GetTheNull2()
 {
    int i;
    for(i=0;i<=15;i++)
     {
        if (num2[i]==0)
            return i;

     }
}

change2(int a,int b)
{
    int temp;
    temp=num2[a];
    num2[a]=num2[b];
    num2[b]=temp;
}
f3()
{

    int pos;
    clrscr();
    randomize();
    newGame3();
    for(;;)
       {
        char key=0;
        key=getch();
        if(key==0) continue;
        pos=GetTheNull3();
        switch(key)
          {
            case 72:
                if(pos<=19) change3(pos,pos+5);
                break;
            case 80:
                if(pos>=5) change3(pos,pos-5);
                break;
            case 77:
                if(pos%5!=0) change3(pos,pos-1);
                break;
            case 75:
                if(pos%5!=4) change3(pos,pos+1);
                break;
            case 110:
                newGame3();break;
            case 120:
                str(); break;
          }
        update3();
        if(isSuccess3())
           {
            gotoxy(30,6);
            printf("\332\304\304\304\304\304\304\304\304\304\304\304\304\267");
            gotoxy(30,7);
            printf("\263 \272");
            gotoxy(30,8);
            printf("\263 Well Done! \272");
            gotoxy(30,9);
            printf("\263 \272");
            gotoxy(30,10);
            printf("\324\315\315\315\315\315\315\315\315\315\315\315\315\274\n");
            getch();
            str();
           }
        if(key==27) break;
       }
    clrscr();
}
 

newGame3()
{
    int i,a,b;
    for(i=0;i<24;i++) num3[i]=i+1;
    num3[24]=0;
    for(i=0;i<900;i++)
   {
        a=random(24);
        b=(a+2)%24;
        change3(a,b);
    }
    update3();
}

update3()
{
    int i,j;
    for(i=0;i<=4;i++)
      {
        gotoxy(24,1+i*5);
        for(j=0;j<=4;j++)
        {
            if (num3[j+i*5]!=0)
                printf("\332\304\304\304\304\267");
            else
                printf(" ");
         }
        gotoxy(24,2+i*5);
        for(j=0;j<=4;j++)
        {
            if (num3[j+i*5]!=0)
                printf("\263 %2d \272",num3[j+i*5]);
            else
                printf(" ");
        }
        gotoxy(24,3+i*5);
        for(j=0;j<=4;j++)
       {
            if (num3[j+i*5]!=0)
                printf("\324\315\315\315\315\274");
            else
                printf(" ");
        }


        printf("\n");
      }

}

int isSuccess3()
{
    int i,ret=1;
    for(i=0;i<24;i++)
        ret=ret&&(num3[i]==(i+1));
    return ret;
 }

int GetTheNull3()
 {
    int i;
    for(i=0;i<=24;i++)
     {
        if (num3[i]==0)
            return i;

     }
 }

change3(int a,int b)
{
    int temp;
    temp=num3[a];
    num3[a]=num3[b];
    num3[b]=temp;
}