C语言编写的数独猜谜游戏

作者在 2011-05-31 23:53:35 发布以下内容
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define ESC   0x011b
#define DEL   0x5300
#define UP    0x4800
#define DOWN  0x5000
#define LEFT  0x4b00
#define RIGHT 0x4d00
 
int f_Open()
{
    int n;
    textcolor(YELLOW);
    gotoxy(2,2);
    cprintf("The Rules Of The Game:\n");
    gotoxy(6,3);
    cprintf("Make every line, every column and every little 3x3 matrix");
    gotoxy(6,4);
    cprintf("all fill in one to nine of the number nine differ.");
    textcolor(LIGHTGREEN);
    gotoxy(16,7);
    cprintf("*************************************\n");
    gotoxy(16,8);
    cprintf("*                                   *\n");
    gotoxy(16,9);
    cprintf("*         S U D O K U  v1.0         *\n");
    gotoxy(16,10);
    cprintf("*                                   *\n");
    gotoxy(16,11);
    cprintf("*************************************\n");
    textcolor(LIGHTCYAN);
    gotoxy(40,12);
    cprintf("Powered by TYY");
    textcolor(LIGHTRED);
    gotoxy(25,14);
    cprintf("\nDifficulty:1.EASY");
    gotoxy(25,15);
    cprintf("\n           2.NORMAL");
    gotoxy(25,16);
    cprintf("\n           3.HARD");
    textcolor(YELLOW);
    gotoxy(21,22);
    cprintf("2011-2012@Copytight Co.Ltd.");
    textcolor(CYAN);
    gotoxy(25,17);
    cprintf("\nMake your choice:");
    scanf("%d",&n);
    if(n==1)
        n=5;
        else
            if(n==2)
                n=7;
                else
                    n=9;
    return n;
}
 
void f_Close(int r)
{
    if(r==1)
    {
        clrscr();
        textcolor(LIGHTCYAN);
        gotoxy(16,7);
        cprintf("*************************************\n");
        gotoxy(16,8);
        cprintf("*                                   *\n");
        gotoxy(16,9);
        cprintf("*         G A M E   O V E R         *\n");
        gotoxy(16,10);
        cprintf("*              You Win              *\n");
        gotoxy(16,11);
        cprintf("*       Press any key to exit       *\n");
        gotoxy(16,12);
        cprintf("*                                   *\n");
        gotoxy(16,13);
        cprintf("*************************************\n");
    }
    else
    {
        clrscr();
        textcolor(LIGHTCYAN);
        gotoxy(16,7);
        cprintf("*************************************\n");
        gotoxy(16,8);
        cprintf("*                                   *\n");
        gotoxy(16,9);
        cprintf("*         G A M E   O V E R         *\n");
        gotoxy(16,10);
        cprintf("*             You Lose              *\n");
        gotoxy(16,11);
        cprintf("*       Press any key to exit       *\n");
        gotoxy(16,12);
        cprintf("*                                   *\n");
        gotoxy(16,13);
        cprintf("*************************************\n");
    }
}
   

void f_Dig(int mark[9][9],int level)
{
    int a[9];
    int i,j,k,n,m,t,p,q;
    for(i=0;i<9;i++)
        for(j=0;j<9;j++)
            mark[i][j]=0;
    srand((unsigned)time(NULL));
    for(i=0;i<9;i++)
    {
        n=rand()%level+1;
        for(k=0;k<9;k++)
            a[k]=k+1;
        for(j=0;j<n;j++)
        {
            m=rand()%(9-j);
            t=a[m];
            mark[i][t]=1;
            a[m]=0;
            for(p=0;p<8;p++)
            {
                for(q=0;q<8-p;q++)
                {
                    if(a[q]<a[q+1])
                    {
                        t=a[q];
                        a[q]=a[q+1];
                        a[q+1]=t;
                    }
                }
            }
        }
    }
}
 

void f_Generate(int b[9][9])
{
       int a[9];
       int n,i=0,j=0,p,q,r,t,k=0;
       for(p=0;p<9;p++)
           a[p]=9-p;
       for(p=0;p<9;p++)
           for(q=0;q<9;q++)
               b[p][q]=0;
       srand((unsigned)time(NULL));
       while(b[8][8]==0)
      {
          r=0;
          n=rand()%(9-k);
          b[i][j]=a[n];
          a[n]=0;
          for(p=0;p<8;p++)
            {
                for(q=0;q<8-p;q++)
                {
                    if(a[q]<a[q+1])
                    {
                        t=a[q];
                        a[q]=a[q+1];
                        a[q+1]=t;
                    }
                }
            }
 
        for(p=j;p>0;p--)
        {
         if(b[i][j]==b[i][p-1])
           r++;
        }

        for(p=i;p>0;p--)
        {
          if(b[i][j]==b[p-1][j])
            r++;
        }

        if((i!=0) && (i!=3) && (i!=6))
        {
          if((i==1) || (i==4) || (i==7))
          {
             for(q=(j/3)*3;q<(j/3)*3+3;q++)
             {
               if(b[i][j]==b[i-1][q])
                 r++;
             }
           }
           else
           {
             for(p=(i/3)*3;p<(i/3)*3+2;p++)
             {
               for(q=(j/3)*3;q<(j/3)*3+3;q++)
               {
                 if(b[i][j]==b[p][q])
                   r++;
               }
             }
           }
         }
 
        if(r==0)
        {
            j++;
            for(p=0;p<9;p++)
               a[p]=9-p;
            k=0;
        }
        else
        {
            k++;
            b[8][8]=0;
        }
        if(k==9)
        {
          k=0;
          for(p=0;p<9;p++)
              a[p]=9-p;
          i=0;
          j=0;
         }

        if(j==9)
        {
         i++;
         j=0;
         }
      }
}
 

void f_Match(int b[9][9],int mark[9][9])
{
    int i,j;
    f_Generate(b);
    for(i=0;i<9;i++)
    {
        for(j=0;j<9;j++)
        {
            if(mark[i][j]==1)
                b[i][j]=0;
        }
    }
}
 
int f_Check(int b[9][9])
{
    int i,j,t,r,k,s=0,x,y;
    int a[9][9];
    k=0;
    r=0;
    x=0;
    y=0;
    for(i=0;i<9;i++)
    {
        for(j=0;j<9;j++)
        {
            if(b[i][j]!=0)
                k++;
        }
    }
    if(k==81)
    {
        for(t=0;t<9;t++)
        {
            for(i=0;i<8;i++)
            {
                for(j=i+1;j<=8;j++)
                {
                    if(b[t][i]==b[t][j])
                        r++;
                }
            }
        }
        for(t=0;t<9;t++)
        {
            for(i=0;i<8;i++)
            {
                for(j=i+1;j<=8;j++)
                {
                    if(b[i][t]==b[j][t])
                        r++;
                }
            }
        }
        for(k=0;k<9;k+=3)
        {
            for(t=0;t<9;t+=3)
            {
                for(i=0;i<3;i++)
                {
                    for(j=0;j<3;j++)
                    {
                        a[x][y]=b[k+i][t+j];
                            y++;
                    }
                }
                x++;
                y=0;
            }
        }

        for(t=0;t<9;t++)
        {
            for(i=0;i<8;i++)
            {
                for(j=i+1;j<=8;j++)
                {
                    if(a[t][i]==a[t][j])
                        r++;
                }
            }
        }
 
        if(r==0)
            s=1;
        else
            s=0;
        return s;
    }
}
 
 
void main()
{
    int key;
    int i,j,k,r=0,level;
    int b[9][9];
    int mark[9][9];
    char map[19][19] =
    {
        {201,205,209,205,209,205,203,205,209,205,209,205,203,205,209,205,209,205,187},
        {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186},
        {199,196,197,196,197,196,215,196,197,196,197,196,215,196,197,196,197,196,182},
        {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186},
        {199,196,197,196,197,196,215,196,197,196,197,196,215,196,197,196,197,196,182},
        {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186},
        {204,205,216,205,216,205,206,205,216,205,216,205,206,205,216,205,216,205,185},
        {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186},
        {199,196,197,196,197,196,215,196,197,196,197,196,215,196,197,196,197,196,182},
        {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186},
        {199,196,197,196,197,196,215,196,197,196,197,196,215,196,197,196,197,196,182},
        {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186},
        {204,205,216,205,216,205,206,205,216,205,216,205,206,205,216,205,216,205,185},
        {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186},
        {199,196,197,196,197,196,215,196,197,196,197,196,215,196,197,196,197,196,182},
        {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186},
        {199,196,197,196,197,196,215,196,197,196,197,196,215,196,197,196,197,196,182},
        {186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186,' ',179,' ',179,' ',186},
        {200,205,207,205,207,205,202,205,207,205,207,205,202,205,207,205,207,205,188}
    };
    struct move_point
    {
        int x;
        int y;
    }man;
    level=f_Open();
    f_Dig(mark,level);
    f_Match(b,mark);
    clrscr();
    for(i=0;i<9;i++)
    {
        for(j=0;j<9;j++)
        {
            if(b[i][j]==0)
                map[i*2+1][j*2+1]=' ';
            else
                map[i*2+1][j*2+1]=b[i][j]+48;
        }
    }
    for (i=0;i<19;i++)
    {
        for (j=0;j<19;j++)
        {
            if(map[i][j]>=49 && map[i][j]<=57)
            {
                textcolor(LIGHTRED);
                cprintf("%c",map[i][j]);
            }
            else
            {
                textcolor(CYAN);
                cprintf("%c",map[i][j]);
            }
        }
        printf("\n");
    }
  man.x=2;
  man.y=2;
  textcolor(YELLOW);
  gotoxy(man.x+25, man.y+5);
  cprintf("Notice:");
  gotoxy(man.x+25, man.y+6);
  cprintf("1.Fill the blank with number 1-9.");
  gotoxy(man.x+25, man.y+7);
  cprintf("2.Use Up,Down,Left & Right Key to move.");
  gotoxy(man.x+25, man.y+8);
  cprintf("3.Use Del Key to delete a number.");
  gotoxy(man.x+25, man.y+9);
  cprintf("4.Press Esc Key to quit game.");
  gotoxy(man.x, man.y);
   while(key!=ESC && r!=1)
{
    while(bioskey(1)==0);
    key=bioskey(0);
   
    switch(key)
    {
     case UP:
        {
          if(man.y-1==1)
             break;
          else
        {
          man.y-=2;
          gotoxy(man.x, man.y);
        }
        } break;
     case DOWN:
        {
          if(man.y+1==19)
             break;
          else
        {
          man.y+=2;
          gotoxy(man.x, man.y);
        }
        } break;
     case LEFT:
         {
          if(man.x-1==1)
             break;
          else
        {
          man.x-=2;
          gotoxy(man.x, man.y);
        }
        } break;
     case RIGHT:
       {
          if(man.x+1==19)
             break;
          else
        {
          man.x+=2;
          gotoxy(man.x, man.y);
        }
        } break;
     case 0x231:
     {
         if(mark[man.y/2-1][man.x/2-1]==1)
         {
             b[man.y/2-1][man.x/2-1]=1;
             textcolor(LIGHTGREEN);
             cprintf("%d\b",b[man.y/2-1][man.x/2-1]);
             r=f_Check(b);
         }
     }
     break;
     case 0x332:
     {
         if(mark[man.y/2-1][man.x/2-1]==1)
         {
             b[man.y/2-1][man.x/2-1]=2;
             textcolor(LIGHTGREEN);
             cprintf("%d\b",b[man.y/2-1][man.x/2-1]);
             r=f_Check(b);
         }
     }
     break;
     case 0x433:
     {
         if(mark[man.y/2-1][man.x/2-1]==1)
         {
             b[man.y/2-1][man.x/2-1]=3;
             textcolor(LIGHTGREEN);
             cprintf("%d\b",b[man.y/2-1][man.x/2-1]);
             r=f_Check(b);
         }
     }
     break;
     case 0x534:
     {
         if(mark[man.y/2-1][man.x/2-1]==1)
         {
             b[man.y/2-1][man.x/2-1]=4;
             textcolor(LIGHTGREEN);
             cprintf("%d\b",b[man.y/2-1][man.x/2-1]);
             r=f_Check(b);
         }
     }
     break;
     case 0x635:
     {
         if(mark[man.y/2-1][man.x/2-1]==1)
         {
             b[man.y/2-1][man.x/2-1]=5;
             textcolor(LIGHTGREEN);
             cprintf("%d\b",b[man.y/2-1][man.x/2-1]);
             r=f_Check(b);
         }
     }
     break;
     case 0x736:
    {
         if(mark[man.y/2-1][man.x/2-1]==1)
         {
             b[man.y/2-1][man.x/2-1]=6;
             textcolor(LIGHTGREEN);
             cprintf("%d\b",b[man.y/2-1][man.x/2-1]);
             r=f_Check(b);
         }
     }
     break;
     case 0x837:
     {
         if(mark[man.y/2-1][man.x/2-1]==1)
         {
             b[man.y/2-1][man.x/2-1]=7;
             textcolor(LIGHTGREEN);
             cprintf("%d\b",b[man.y/2-1][man.x/2-1]);
             r=f_Check(b);
         }
     }
     break;
     case 0x938:
     {
         if(mark[man.y/2-1][man.x/2-1]==1)
         {
             b[man.y/2-1][man.x/2-1]=8;
             textcolor(LIGHTGREEN);
             cprintf("%d\b",b[man.y/2-1][man.x/2-1]);
             r=f_Check(b);
         }
     }
     break;
     case 0xa39:
     {
         if(mark[man.y/2-1][man.x/2-1]==1)
         {
             b[man.y/2-1][man.x/2-1]=9;
             textcolor(LIGHTGREEN);
             cprintf("%d\b",b[man.y/2-1][man.x/2-1]);
             r=f_Check(b);
         }
     }
     break;
     case DEL:
     {
         if(mark[man.y/2-1][man.x/2-1]==1)
         {
             b[man.y/2-1][man.x/2-1]=0;
             printf("%c\b",' ');
         }
     }
     break;
     default:
        break;
    }
}
    f_Close(r);
    getch();
}
 
程序源码 | 阅读 1970 次
文章评论,共17条
烟雾中的迷茫
2011-06-01 07:19
1
佩服 啊 呵呵
行云流雨
2011-06-01 15:22
2
为什么我运行了会有错啊,能解释一下吗?不过还是挺佩服你的
麦穗。夏
2011-06-02 22:20
3
可以用C++编一个吗?
kemoo
2011-06-04 11:13
4
厉害
he253128804
2011-06-07 14:10
5
强力学习!
lovejj23
2011-06-09 16:01
6
请问你用的什么编译环境?
liulei1234
2011-06-09 23:49
7
我用VC6.0运行有错啊,求解释
gao812qiang
2011-06-10 13:50
8
为啥在VC6.0晕运行有错误啊???
陈潍溢
2011-06-10 22:05
9
有错误,错误提示:warning C4305: 'initializing' : truncation from 'const int ' to 'char '<br />
求解答,谢谢!
kemoo
2011-06-11 15:24
10
厉害
xu1990912
2011-06-12 10:30
11
初学者,还没这么深入,才学到指针
zhailiubo
2011-06-12 12:47
12
这是你自己编的?真厉害<img src="image/face/15.gif" class="face">
ZFSCN
2011-06-13 23:22
13
写的很好,佩服!!
bupapanlove
2011-06-21 15:54
14
厉害
wanxl688517
2011-06-27 19:35
15
不错 加油<img src="image/face/22.gif" class="face">
liu472
2011-06-29 11:44
16
厉害~~&nbsp;&nbsp;我挺喜欢数独的,
liu472
2011-06-29 11:44
17
厉害~~&nbsp;&nbsp;我挺喜欢数独的,
游客请输入验证码
浏览1970次
文章归档