UNKNOWN //************************************** // Name: sudoku // Description:this code generates sudoku puzzles // By: deepak.nandan // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //This code is copyrighted and has limited warranties. //Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.13789/lngWId.3/qx/vb/scripts/ShowCode.htm //for details. //************************************** //********************************************************************** //********************************************************************** //********************************************************************** //************************INCLUDED HEADER FILES************************* //********************************************************************** #include<iostream.h> #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<time.h> #include<dos.h> int a[9][9],rdef[60],cdef[60],row=0,col=0,counter=0,b[9][9]; void solve_user(); int solve(int [][9]); void table(); void display() { int c[9][9]; textcolor(WHITE); cprintf(" WELCOME TO THE WORLD OF SUDOKU"); cout<<"\n\n INSTRUCTIONS\n"; cout<<"1.Use the keys A,D,S,W(capital) to move left,right,down,up respectively\n"; cout<<"W\n"; cout<<" A S D\n"; cout<<"2.Press 'm' to modify a cell"; cout<<"\n You will not be able to modify fixed cells"; cout<<"\n3.Press 'q' to quit the game"; cout<<"\n\nPress any key to continue..";getch();clrscr(); randomize(); int i,j,x,y,m; start: for(i=0;i<9;i++) { for(j=0;j<9;j++) { b[i][j]=a[i][j]; } } m=40+random(15); for(i=0;i<m;i++) { x=random(9); y=random(9); b[x][y]=0; } for(i=0;i<9;i++) { for(j=0;j<9;j++) { c[i][j]=b[i][j]; } } if(solve(c)==0) { goto start; } table();gotoxy(1,1); counter=0; for(i=0,y=4;i<9;i++,y+=2) { for(j=0,x=6;j<9;j++,x+=4) { gotoxy(x,y); if(b[i][j]==0) cout<<" "; else { cout<<b[i][j]; rdef[counter]=i; cdef[counter]=j;counter++; } } } gotoxy(1,24); cout<<"Do you wish to solve the sudoku yourself(y/n):"; char ch; cin>>ch; if(ch=='y'||ch=='Y') solve_user(); else { clrscr(); table(); for(i=0,y=4;i<9;i++,y+=2) { for(j=0,x=6;j<9;j++,x+=4) { gotoxy(x,y); cout<<a[i][j]; } } gotoxy(1,23); cout<<"Press any key to continue.."; getch();return; } return; } int con(int count,int i,int j) { for(int x=0;x<count;x++) if(i==rdef[x] && j==cdef[x]) return(1); return(0); } void table() { clrscr();int j,k,i,x,y; for(i=0,x=6;i<=8;i++,x+=4) { gotoxy(x,1); cout<<i; } for(i=0,y=4;i<=8;i++,y+=2) { gotoxy(1,y); cout<<i; } for(y=3;y<22;y+=2) { for(i=4;i<=40;i++) { gotoxy(i,y); cout<<"-"; } } for(j=4,y=3;y<22;j+=4,y+=2) { for(k=3,i=4;i<=40;i+=2,k++) { gotoxy(j,k); cout<<"|"; } } for(x=16;x<=28;x+=12) { for(y=3;y<22;y++) { gotoxy(x,y);textcolor(GREEN); cprintf("@");textcolor(WHITE); } } for(y=9;y<=15;y+=6) { for(x=4;x<=40;x+=2) { gotoxy(x,y);textcolor(GREEN); cprintf("@");textcolor(WHITE); } } } int cgrid(int i,int j) { int x,y; for(x=(i/3)*3;x<((i/3)*3+3);x++) for(y=(j/3)*3;y<((j/3)*3+3);y++) { if(i!=x&&j!=y) if(a[i][j]==a[x][y]) return(0); } return(1); } int crow(int i, int j) { for(int x=0;x<9;x++) { if(x!=j) if(a[i][j]==a[i][x]) return(0); } return(1); } int ccolumn(int i, int j) { for(int x=0;x<9;x++) { if(x!=i) if(a[i][j]==a[x][j]) return(0); } return(1); } /*FUNCTION TO CHECK CONSISTENCY OF AN ELEMENT*/ int check(int i,int j) { int s; s=cgrid(i,j); if(s==1) s=s*crow(i,j); else return(s); if(s==1) s=s*ccolumn(i,j); else return(s); return(s); } /*FUNCTION TO SOLVE SUDOKU*/ int solve(int a[][9]) { int i=0,j=0,number; mainallot: for(i=row;i<9;i++) { j=col; while(j<9) { number=1; if(con(counter,i,j)==0) { allot: a[i][j]=number; if(check(i,j)==0) { number++; if(number>9) { a[i][j]=0; if(j==0) { row=i-1; col=8; if(row<0) return(0); goto backadjust; } else { row=i; col=j-1; if(row<0 || col<0) return(0); goto backadjust; } } goto allot; } } j++; if(j>8) col=0; } } return(1); backadjust: if(con(counter,row,col)==0) { number=a[row][col]; allot1: number++; a[row][col]=number; if(number>9) { a[row][col]=0; if(col==0) { row--; col=8; if(row<0) return(0); goto backadjust; } else { col--; if(col<0) return(0); goto backadjust; } } if(check(row,col)==0) goto allot1; else { if(col==8) { row++; if(row>8) return(0); col=0; goto mainallot; } else { col++; if(col>8) return(0); goto mainallot; } } } else { if(col==0) { row--; col=8; if(row<0) return(0); goto backadjust; } else { col--; if(col<0) return(0); goto backadjust; } } } /*FUNCTION TO GENERATE SODOKU*/ void generate() { int i,j,count=1,e; char choice; randomize(); generate: for(i=0;i<9;i++) for(j=0;j<9;j++) a[i][j]=0; for(i=0;i<60;i++) { rdef[i]=20; cdef[i]=20; } a[0][0]=1+random(9); rdef[0]=0; cdef[0]=0; for(count=1;count<10;count++) { choose: i=random(9); j=random(9); if(con(counter,i,j)==1) goto choose; rdef[count]=i; cdef[count]=j; allot: a[i][j]=1+random(9); if(a[i][j]==0) goto allot; if(check(i,j)==0) goto allot; } counter=count; e=solve(a); row=0; col=0; for(i=0;i<60;i++) { rdef[i]=20; cdef[i]=20; } if(e==0) goto generate; } void solve_user() { int i,j,k,x,y,r,c,m,n,flag=0;char ch,choices; clrscr(); table(); solve: for(i=0,y=4;i<9;i++,y+=2) { for(j=0,x=6;j<9;j++,x+=4) { gotoxy(x,y); if(b[i][j]==0) cout<<" "; else cout<<b[i][j]; } } for(i=0;i<9;i++) { for(j=0;j<9;j++) { if(b[i][j]==0) { x=(j+2)*4-2;y=2*i+2; break; } } } for(;;) { acceptor: gotoxy(x,y); ch=getch(); if((int)ch>=65) { if(ch=='w'||ch=='W') { y=y-2; } else if(ch=='s'||ch=='S') { y=y+2; } else if(ch=='A'||ch=='a') { x=x-4; } else if(ch=='d'||ch=='D') { x=x+4; } if(x>38) { x=6; } if(x<6) { x=38; } if(y>20) { y=4; } if(y<4) { y=20; } if(ch=='M'||ch=='m') { i=(y/2)-2;j=(x-6)/4; if(con(counter,i,j)==0) { b[i][j]=0; clrscr(); table(); for(m=0,c=4;m<9;m++,c+=2) { for(n=0,r=6;n<9;n++,r+=4) { gotoxy(r,c); if(b[m][n]!=0) cout<<b[m][n]; else cout<<" "; } } gotoxy(x,y); } } if(ch=='q'||ch=='Q') { gotoxy(1,23); cout<<"Do you wish to continue playing the sudoku(y/n):"; cin>>choices; if(choices=='N'||choices=='n') { clrscr();table(); for(i=0,y=4;i<9;i++,y+=2) { for(j=0,x=6;j<9;j++,x+=4) { gotoxy(x,y); cout<<a[i][j]; } } gotoxy(1,23); cout<<"Thanks for visiting.."; gotoxy(1,25); cout<<"Press any key to contine..."; getch(); return; } else { gotoxy(1,23); cout<<" "; } } goto acceptor; } if((int)ch>=49&&(int)ch<=57) { i=(y/2)-2;j=(x-6)/4; if(b[i][j]==0) { b[i][j]=(int)ch-48; cout<<b[i][j]; gotoxy(x,y); } } for(i=0;i<9;i++) { for(j=0;j<9;j++) { if(b[i][j]==0) { goto acceptor; } } } gotoxy(1,23); cout<<"Press 1 to check";gotoxy(x,y); ch=getch(); if((int)ch==49) { for(m=0;m<9;m++) { for(n=0;n<9;n++) { if(a[m][n]!=b[m][n]) { gotoxy(1,24); cout<<"The solution is wrong"; flag=1;getch();gotoxy(x,y);goto acceptor; } } if(flag==1) break; } if(flag==0) { gotoxy(1,23); cout<<"YOU SOLVED IT !!!!.."; } } else { gotoxy(1,23); cout<<""; goto acceptor; } break; } getch(); } void main() { int choice; start: generate(); for(;;) { clrscr(); for(int i=7;i<=55;i++) { gotoxy(i,2); cout<<"-";delay(30); } for(i=7;i<=55;i++) { gotoxy(i,11); cout<<"-";delay(30); } for(i=3;i<=10;i++) { gotoxy(7,i); cout<<"|";delay(30); } for(i=3;i<=10;i++) { gotoxy(55,i); cout<<"|";delay(30); } gotoxy(27,3);textcolor(RED); cprintf("S");delay(25); cprintf("U");delay(25); cprintf("D");delay(25); cprintf("O");delay(25); cprintf("K");delay(25); cprintf("U");delay(25);textcolor(WHITE); delay(500); gotoxy(24,5); cout<<"1.START GAME"; gotoxy(24,6); cout<<"2.CREDITS"; gotoxy(24,7); cout<<"3.EXIT"; gotoxy(18,9); cout<<"Please specify your choice:"; cin>>choice; if(choice==1) break; if(choice==2) { clrscr(); cout<<"This program has been developed as a project"; cout<<"\n\nThe programmers are:"; textcolor(CYAN); gotoxy(15,4); cprintf("1.AKSHAY VENKAT"); gotoxy(15,5); cprintf("2.DEEPAK NANDAN"); gotoxy(15,6); cprintf("3.NIKHIL T C"); textcolor(WHITE); cout<<"\n\nThis program has been developed keeping in mind\n"; cout<<"most of the features of modern day games"; cout<<"\n\n\n"; cout<<"Press any key to continue..."; getch();continue; } if(choice==3) { clrscr(); cout<<"Thanks for visiting"; cout<<"\n\nPress any key to continue..."; getch();exit(0); } } clrscr(); display(); goto start; }