Randomly generate a Sudoku

Source: Internet
Author: User

Recently learned to write GUI program with Java, feel from doing a small thing to start the best, choose to write some small games to begin, the first is a single game.

Http://en.wikipedia.org/wiki/Sudoku

The first step in writing a Sudoku game is how to generate a primitive Sudoku problem, the simplest way to generate Sudoku is to randomly generate a complete Sudoku and then remove some numbers from it so that you can generate a Sudoku problem. So there is a need to consider how to randomly generate a complete Sudoku, which is considered to be generated using backtracking methods:

    • Insert a 1~9 value at a location in the Sudoku
    • If the value is inserted normally, the recursion is inserted at the next position
    • If no value can be inserted in this position, the value inserted at the previous position is traced back
    • Recursive the above steps until all positions are inserted with the appropriate values
ImportJava.util.Random; Public classMain {Private Static int[] num =New int[81]; Private StaticRandom random =NewRandom (); /*** Randomly generate a Sudoku *@returnreturns a randomly generated Sudoku*/     Public Static int[] Generate () { for(inti = 0; I < 81; i++) {Num[i]= 0; } Solve (0); returnnum; }    /*** Recursive generation of the value of the Sudoku position I *@paramSudoku Position I *@returnwhether position I can fill in values*/    Private Static BooleanSolveinti) {/*returns True if the 81 lattices have been filled*/        if(i = = 81) {            return true; }        /*If position I has already filled in the appropriate value, the value of the next position is generated recursively*/        Else if(num[i]! = 0) {            returnSolve (i + 1)); }        /*If you exactly need to fill in the value of position I*/        Else {            /*storing the values that each location might produce with an array randorder, which is 1~9*/            int[] Randorder =New int[10];  for(intval = 1; Val < 10; val++) {Randorder[val]=Val; }            /*changes the array randorder to a randomly stored 1~9 array*/             for(intval = 1; Val < 10; val++) {                intRand = Random.nextint (10); intTMP =Randorder[rand]; Randorder[rand]=Randorder[val]; Randorder[val]=tmp; }            /*in position I randomly fills in a value and determines whether it is valid*/             for(intval = 1; Val < 10; val++) {                /*if a random number in the 1~9 that is filled in position i is valid*/                if(Islegal (i, Randorder[val])) {/*The random value is put in position I*/Num[i]=Randorder[val]; /*explore whether the next position of I can be filled in correctly, and return true if possible*/                    if(Solve (i + 1)) {                        return true; }                }            }        }        /*If you cannot fill any of the values in the 1~9 at position I, you need to backtrack*/Num[i]= 0; return false; }    /*** In position I fill in the value number is valid, by the column and the Small matrix judgment *@paramfill in position I *@paramfill in the number of position I value *@returnin position I fill in the number value is valid*/    Private Static BooleanIslegal (intIintvalue) {        /*determine if the row is valid*/        if(!Isrowlegal (i, value)) {            return false; }        /*determine if the column is valid*/        if(!Iscollegal (i, value)) {            return false; }        /*determine if the small matrix is valid*/        if(!Issublegal (i, value)) {            return false; }        return true; }    /*** Determine if the value line rule in position I fills in is satisfied *@paramfill in position I *@paramfill in the number of position I value *@returnin position I fills in the number value row rule is valid*/    Private Static BooleanIsrowlegal (intIintvalue) {        introw = I/9;  for(intval = 0; Val < 9; val++) {            if(Value = = Num[row * 9 +Val]) {                return false; }        }        return true; }    /*** Determine if the Value column rule in position I fills in is satisfied *@paramfill in position I *@paramfill in the number of position I value *@returnin position I fills in the number Value column rule is valid*/    Private Static BooleanIscollegal (intIintvalue) {        intCol = i% 9;  for(intval = 0; Val < 9; val++) {            if(Value = = Num[val * 9 +Col]) {                return false; }        }        return true; }    /*** Determine if the value in position I fills in the small matrix rule is satisfied *@paramfill in position I *@paramfill in the number of position I value *@returnin position I fills in the number value small matrix rule is valid*/    Private Static BooleanIssublegal (intIintvalue) {        introw = I/9; intCol = i% 9; intXOFF = ROW/3 * 3; intYoff = COL/3 * 3;  for(intx = 0; x < 3; X + +) {             for(inty = 0; Y < 3; y++) {                if(Value = = num[(xOff + x) * 9 + Yoff +y]) {return false; }            }        }        return true; }     Public Static voidMain (string[] args) {int[] Grid =New int[9] [9]; int[] Sudoku =New int[81];  for(inti = 0; I < 81; i++) {Sudoku[i]= 0; } Sudoku=generate ();  for(inti = 0; I < 9; i++) {             for(intj = 0; J < 9; J + +) {Grid[i][j]= Sudoku[i * 9 +J]; }} System.out.println ("***********************");  for(inti = 0; I < 9; i++) {             for(intj = 0; J < 9; J + +) {System.out.print (Grid[i][j]+ " "); if(j% 3 = = 2) {System.out.print ("| ");            }} System.out.println (); if(i% 3 = = 2) {System.out.println ("***********************"); }        }    }}

Operation Result:

2 3 9 | 7 6 8 | 1 5 4 | 8 4 7 | 1 5 3 | 6 9 2 | 1 5 6 | 2 4 9 | 8 7 3 | 3 7 8 | 5 2 1 | 9 4 6 | 6 1 5 | 4 9 7 | 3 2 8 | 9 2 4 | 3 8 6 | 5 1 7 | 5 8 1 | 6 7 2 | 4 3 9 | 4 9 2 | 8 3 5 | 7 6 1 | 7 6 3 | 9 1 4 | 2 8 5 | 2 4 6 | 7 5 3 | 9 8 1 | 9 1 8 | 6 4 2 | 5 7 3 | 5 7 3 | 9 8 1 | 6 4 2 | 4 5 7 | 3 2 9 | 8 1 6 | 8 3 9 | 5 1 6 | 7 2 4 | 6 2 1 | 8 7 4 | 3 5 9 | 3 8 2 | 1 9 7 | 4 6 5 | 7 6 4 | 2 3 5 | 1 9 8 | 1 9 5 | 4 6 8 | 2 3 7 | ***********************

Randomly generate a Sudoku

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.