Written some cool source code? Upload it to Programmer's Heaven.

View \SUDOKU.CPP

A simple SUDOKU creation v1.0

Submitted By: ParvezMI
Rating: (Not rated) (Rate It)


# include <stdlib.h>
# include <stdio.h>
# include <conio.h>
 int A[9][9],a[3][3];
void StepDown(int x)
{
 int i,j,k,I,J,K;
 int b[3][3];
 for(I=x,i=0;I<3;I++,i++)for(J=0,j=0;J<3;J++,j++) b[I][J]=a[i][j];
 for(I=0;I<x;I++,i++) for(J=0,j=0;J<3;J++,j++) b[I][J]=a[i][j];
 for(i=0;i<3;i++) for(j=0;j<3;j++) a[i][j]=b[i][j];
}
void main()
{
 int i,j,k,I,J,K,M,N,P[9],r1;
 randomize();
 clrscr();
 for(i=0,k=1;i<9;k++,i++) P[i]=k;
 /*Randomize the values of array a 3 x 3 [LATER]*/
for(j=0;j<20;j++)
 for(i=0;i<9;i++)
 {
    r1=rand()%9;
    P[i]=(P[i]+P[r1])-(P[r1]=P[i]);
 }

        for(M=0,N=0;M<3;M++,N+=3)
        {
          for(i=0,I=0;i<3;i++) for(j=0;j<3;j++,I++) a[i][j]=P[I];
          StepDown(M);
          for(i=0;i<3;i++) for(j=0;j<3;j++) A[i][j+N]=a[i][j];
        }
        for(M=0,N=0;M<3;M++,N+=3)
        {
          for(i=0,I=-1;i<3;i++) for(j=0;j<3;j++,I++) a[i][j]=P[I];
          a[0][0]=P[8];
          StepDown(M);
          for(i=0;i<3;i++) for(j=0;j<3;j++) A[i+3][j+N]=a[i][j];
        }

        for(M=0,N=0;M<3;M++,N+=3)
        {
          for(i=0,I=-2;i<3;i++) for(j=0;j<3;j++,I++) a[i][j]=P[I];
          a[0][0]=P[7];a[0][1]=P[8];
          StepDown(M);
          for(i=0;i<3;i++) for(j=0;j<3;j++) A[i+6][j+N]=a[i][j];
        }
 int cl=2,clr=cl;
 for(i=0;i<9;i++)
 {
  for(clr=cl,j=0;j<9;j++)
  {
   textcolor(clr); cprintf("%4d",A[i][j]);
    if((j+1)%3==0) {clr++; printf("  "); }
  }
    if((i+1)%3==0) {cl+=9; printf("\n\n");}
    printf("\n\n");
 }
 getch();
}

corner
© 1996-2008. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin.
bootstrapLabs Logo A bootstrapLabs project.