• 当前位置:首页>>C语言>>C语言编程实例>>拼图游戏
  • 拼图游戏
  • 运行程序后,会产生9个数字,当人把数字拼成

    1 2 3

    4 5 6

    7 8 9

    你就过关了,很简单的。

    #include<math.h>
    #include<graphics.h>
    #include<stdio.h>
    #include<dos.h>
    #define left 0x4b
    #define right 0x4d
    #define down 0x50
    #define esc 0x01
    #define up 0x48
    #define col 4
    #define row 3
    int a[9]={1,2,3,4,5,6,7,8,9};
    int board[3][3];//面板
    int k,m;//9的坐标
    int get_key(void);
    void Draw9(int x,int y,char s[]);
    void Draw(int x,int y,char s[]);
    main()
    {
    int gdriver=VGA,gmode=VGAHI,errorcode;
    int i,j,key,dec=9,temp,temp1;
    char *s[]={"0","1","2","3","4","5","6","7","8","9"};
    initgraph(&gdriver,&gmode,"d:\\tc");
    errorcode=graphresult();
    if(errorcode!=0)
    {printf("the graph error:%s",grapherrormsg(errorcode));
    getch();
    printf("press any key to halt:");
    exit(1);
    }
    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
    {temp=rand()%dec;dec--;
     board[i][j]=a[temp];
     for(temp1=temp;temp1<dec;temp1++)
    a[temp1]=a[temp1+1];
    }
    setcolor(15);
    rectangle(198,148,351,301);
    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
    {if(board[i][j]!=9)
    Draw((col+j)*50,(row+i)*50,s[board[i][j>);
    else
    {k=i;m=j;
    Draw9((col+j)*50,(row+i)*50,s[board[i][j>);
    }
    }
    while(1)
    {while(bioskey(1)==0);
     key=get_key();
     switch(key) 
     {case up:if(k>0)
      {board[k][m]=board[k-1][m];
             board[k-1][m]=9;
             k--;
      Draw9((col+m)*50,(row+k)*50,s[board[k][m>);
      Draw((col+m)*50,(row+k+1)*50,s[board[k+1][m>);
             if(judge()){printf("you win!!!,press any key to exit");getch();exit(0);   } }
      break;
     case down:if(k<2)
        {
         board[k][m]=board[k+1][m];
                board[k+1][m]=9;
                k++;
         Draw9((col+m)*50,(row+k)*50,s[board[k][m>);
         Draw((col+m)*50,(row+k-1)*50,s[board[k-1][m>);
    if(judge()){printf("you win!!!,press any key to exit");getch();exit(0);}
         }
        break;
     case left:if(m>0)
         {
          board[k][m]=board[k][m-1];
                 board[k][m-1]=9;
                 m--;
                 Draw9((col+m)*50,(row+k)*50,s[board[k][m>);
          Draw((col+m+1)*50,(row+k)*50,s[board[k][m+1>);
         if(judge()){printf("you win!!!,press any key to exit");getch();exit(0);}
                       }
       break;
     case right:if(m<2)
         {board[k][m]=board[k][m+1];
                board[k][m+1]=9;
                m++;
               Draw9((col+m)*50,(row+k)*50,s[board[k][m>);
        Draw((col+m-1)*50,(row+k)*50,s[board[k][m-1>);
                        if(judge()){printf("you win!!!,press any key to exit");getch();exit(0);}
         }
       break;
     case esc:exit(0);
     

    [1] [2] 下一页