Sudoku Generate

The java program below shows how to generate practically infinite number of Sudoku puzzles. The logic behind the coding is quite simple. The whole program is divided in 2 parts :

1. A generater that  randomly selects 28-32 grids among the 81 grids and fills random digits (1-9) in these grids such that the conditions of Sudoku are not violated.

2. A solver that checks whether the generated puzzle has a unique solution or not. If the puzzle has a unique solution, the solution 9*9 matrix is displayed. However if the solver finds that the puzzle doesn’t have a solution or has more than 1 solution then the generater is called again.
Programming Geeks : SudokuGenerate

Here 0 corresponds to a blank character. The java file given below can then be imported in a GUI to let the users play Sudoku. The output will be as shown here . For now however, the GUI code is not being presented.


import java.util.*;

public class SudokuGenerate {
  static int row,col,nines=0,left=0,gnxt=0,count=0,dispnm=0,nosolve=0,colcount=0,rf,cf,h,smcnt=0,nm,ur,uc,mn;
  static  int a[][]=new int[9][9],b[][]=new int[9][9],posnum[][]=new int[9][9],poss[][][]=new int [9][9][9];
  static int single[]=new int [81];
  static boolean result=false;
  static int newa[][]=new int[9][9];

   static boolean  validate(int num)
	{ int r,c,boxr=0,boxc=0,x,y;
	  for(r=0;r<9;r++)
	    if(num==newa[r][col])          // IF THE PASSED num IS ALREADY PRESENT
	       return false;               // IN THE CURRENT col, RETURN false
	  for(c=0;c<9;c++) 	    if(num==newa[row][c])           // IF THE PASSED num IS ALREADY PRESENT  	      return false;                 // IN THE CURRENT row, RETURN false                  /* boxr AND boxc RESPECTIVELY REPRESENT THE STARTING          row NO. AND col NO. FOR EACH 3*3 MATRIX          */     	  if(row>=0 && row<3) 	    boxr=0; 	  if(row>=3 && row<6) 	    boxr=3;                         // DEPENDING ON THE VALUE OF row, 	  if(row>=6 && row<9)               // INITIALIZE boxr TO 0, 3 OR 6 	    boxr=6; 	  if(col>=0 && col<3) 	    boxc=0; 	  if(col>=3 && col<6)               // DEPENDING ON THE VALUE OF col, 	    boxc=3;                         // INITIALIZE boxc TO 0, 3 OR 6 	  if(col>=6 && col<9)
	    boxc=6;
	  for(x=boxr;x<8)
                poss[upr][uc][uh]=poss[upr][uc][++uh];
              if(uh==8)
                poss[upr][uc][8]=0;
              p=0;num=0;
              for(int htf=0;htf<9;htf++)
              { if(poss[upr][uc][htf]==0)
                   break;
                num+=poss[upr][uc][htf]*Math.pow(10,p++);
              }
              posnum[upr][uc]=num;
             }
           if(poss[upr][uc][uph]==mn && upr==ur)
           { int hp=-1;
             while(poss[upr][uc][++hp]!=0)
                 poss[upr][uc][hp]=0;
             posnum[upr][uc]=0;
           }
           uph++;
         }
       }
       for(upc=0;upc<9;upc++)
       { uph=0;
         while(poss[ur][upc][uph]!=0)
         { if(poss[ur][upc][uph]==mn && upc!=uc)
           { uh=uph;
             while(poss[ur][upc][uh]!=0 && uh<8)
                 poss[ur][upc][uh]=poss[ur][upc][++uh];
              if(uh==8)
                poss[ur][upc][8]=0;
              p=0;num=0;
              for(int htf=0;htf<9;htf++)                                        { if(poss[ur][upc][htf]==0)                     break;                     num+=poss[ur][upc][htf]*Math.pow(10,p++);               }                 posnum[ur][upc]=num;              }              if(poss[ur][upc][uph]==mn && upc==uc)              { int hp=-1;                while(poss[ur][upc][++hp]!=0)                  poss[ur][upc][hp]=0;              posnum[ur][upc]=0;            }            uph++;          }        }          if(ur>=0 && ur<3) 	    boxr=0; 	  if(ur>=3 && ur<6) 	    boxr=3; 	  if(ur>=6 && ur<9) 	    boxr=6; 	  if(uc>=0 && uc<3) 	    boxc=0; 	  if(uc>=3 && uc<6) 	    boxc=3; 	  if(uc>=6 && uc<9)
	    boxc=6;
	  for(x=boxr;x<8)
                     poss[x][y][uh]=poss[x][y][++uh];
                  if(uh==8)
                     poss[x][y][8]=0;
                  p=0;num=0;
                  for(int htf=0;htf<9;htf++)
                  { if(poss[x][y][htf]==0)
                       break;
                    num+=poss[x][y][htf]*Math.pow(10,p++);
                  }
                  posnum[x][y]=num;
                }
                if(poss[x][y][uph]==mn && (x==ur && y==uc))
                { int hp=-1;
                  while(poss[x][y][++hp]!=0)
                    poss[x][y][hp]=0;
                  posnum[x][y]=0;
                }
                uph++;
              }
            }
          }
       }

   /* solve() AND updateCandidates() ARE
      USED IN A SINGLE FUNCTION,update()
      SO THAT AFTER THE FILLING OF EACH
      DIGIT,THE CANDIDATE DIGITS ARE UPDATED
    */ 

   static boolean update()
   {
     result=false;
     while(solve())
     {
       updateCandidates();
     }
     return result;
   }

   /* solveleminaterow() FILLS THE DIGITS
      IN EACH ROW USING CANDIDATE DIGITS,BY
      REDUCING THE NO. OF CANDIDATES
   */

   static void solveleminaterow()
   { int s=0,num,p,samecand=1,posnm=0,rh,mmc,drh;
       for(int mr=0;mr<9;mr++)
       { for(int mc=0;mc<9;mc++)
         { for(mmc=mc+1;mmc<9;mmc++)
           { if(posnum[mr][mc]==0)
                 break;
             if(posnum[mr][mc]==posnum[mr][mmc])                    // IF CANDIDATE DIGITS ARE SAME IN SAME COLUMN,
               { samecand=2;
                 for(int nc=mmc+1;nc<9;nc++)
                  if(posnum[mr][mc]==posnum[mr][nc])
                      samecand++;
                 posnm=posnum[mr][mc];
                 for(int ps=0;ps<2)
                     cci=ci+1;
                 else
                 { cci=0;
                   rri++;
                 }
                 break;
               case 3:
               case 4:
               case 5:
                   if(ci<5)
                       cci=ci+1;
                   else
                   { cci=3;
                     rri++;
                   }
                   break;
               case 6:
               case 7:
               case 8:
                   if(ci<8)
                       cci=ci+1;
                   else
                   { cci=6;
                     rri++;
                   }
           }
           for( ;rri<=limit)
	    {  row=ob1.nextInt(9);
               col=ob2.nextInt(9);
	      if(newa[row][col]==0)
		 free=true;
	      if(free)
              { num=ob3.nextInt(9);
                for(loop=0;loop<10;loop++)                 {num++;                  if(num>9)
                   num=num-9;
                 if(validate(num))
                   break;
                }
                if(loop<10)
                { newa[row][col]=num;
                  a[row][col]=num;
		  count++;
                  free=false;
                }

                 if(loop==10)
                 { count=0;
                   for(newr=0;newr<9;newr++)
                   { for(newc=0;newc<9;newc++)
                       { newa[newr][newc]=0;
                         a[newr][newc]=0;
                       }
                   }
                   free=false;
                 }
          }
       }
   }

    public static void main(String args[]) {
    int x,y;
    generate();
    solveComp();
    try
    {for(int fr=0;fr<9;fr++)
    { for(int fc=0;fc<9;fc++)
      { while(newa[fr][fc]==0)
        { generate();
          solveComp();
          fr=fc=0;
        }
      }
    }
    }
    catch(Exception e)
    { System.out.println("FATAL ERROR !!  KINDLY RERUN THE PROGRAM.");
      System.out.println("SORRY FOR THE INCONVINIENCE CAUSED.");
      System.exit(1);
    }
        for(x=0;x<9;x++)
        { for(y=0;y<9;y++)
          {
            System.out.print(a[x][y]);
            System.out.print("\t ");
          }
          System.out.println("\n\n\n");
        }
     System.out.println("\n\n\n");
     for(x=0;x<9;x++)
        { for(y=0;y<9;y++)
          { System.out.print(newa[x][y]);
            System.out.print("\t ");
          }
          System.out.println("\n\n\n");
        }
    }
}


Related Posts

  • Share/Bookmark
You can leave a response, or trackback from your own site.

Leave a Reply

| Shop Free Cellular Phones at Bestincellphones.com. | Thanks to Best CD Rates, iCellPhoneDeals.com Offers Best Cell Phone Deals. and Incinerador De Grasa