C语言编写的数独猜谜游戏
程序代码:
#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(); }
游戏功能说明:
1.有简单、一般和困难3种难度选择,每次开始游戏时程序随机生成符合难度要求的数独题。
2.游戏中使用方向键控制光标。1-9数字键填入数字。(不支持使用小键盘)
3.使用DEL键消除填写有误的数字,随时使用ESC键可以退出游戏。
4.格子左上角偶尔会出现一个笑脸。经排查未发现等价于printf("%c",2);的语句,求高人指点。
sudoku源码和exe文件.rar
(13.52 KB)
[ 本帖最后由 oriontyy 于 2011-6-1 12:25 编辑 ]