JS implementation of Sudoku algorithm

Source: Internet
Author: User

Recently, we are going to develop Sudoku games with small programs, so we have studied the algorithms of the next Sudoku feasibility data. The specific code is as follows:

varChess//the state of all chess piecesvarChessstack = [];//Cache Point//Create a chessboardfunctioncreatchess () {Chess= [];  for(vari = 0; I < 9; i++) {Chess[i]= [];  for(varj = 0; J < 9; J + +) {Chess[i][j]= {num:0,fixed:false, temp:[1,2,3,4,5,6,7,8,9]}; }    }}//Print Boardfunctionprintchess () {varSay = "";  for(vari = 0; I < 9; i++)    {         for(varj = 0; J < 9; J + +) {say+=Chess[i][j].num; } Say+ = ' \ n '; } console.log (say);}//pre-set nine Gongge position numbers that don't affect each otherfunctionSetarea (A, b) {vartemp = [1,2,3,4,5,6,7,8,9];//Each small nine Gongge contains nine numbers     for(vari = A; I < b; i++)    {         for(varj = A; J < b; J + +)        {            varlen = temp.length;//the remaining length of temp            varindex = Getrandom (len);//randomly extract aChess[i][j].num = Temp[index];//Assign ValueChess[i][j].fixed =true;//Assign ValueTemp.splice (index,1);//Delete the assigned value        }    }}//set number to solvefunctionSetpre (r,c,v) {Chess[r][c]= {num:v,fixed:true, temp:[]};}//get a random number subscriptfunctionGetrandom (a) {returnMath.floor (Math.random () *a);}//Initialize the boardfunctionChessinit () { for(vari = 0; I < 9; i++)    {         for(varj = 0; J < 9; ) {            //when the number is determined, the next one is randomly taken.            if(!chess[i][j].fixed) {                varlen = chess[i][j].temp.length;//the remaining length of temp                //when there is an optional number                if(Len > 0)                {                    varindex = Getrandom (len);//randomly extract aChess[i][j].num = Chess[i][j].temp[index];//Assign ValueChess[i][j].temp.splice (index,1);//Delete the assigned valueChessstack.push ([I,j,json.parse (Json.stringify (chess))]);//StorageChess[i][j].fixed =true;//becomes OK                    if(!Checktempnum (I,J)) {                        varcs = Chessstack.pop ();//BACKSPACEi = Cs[0]; J= Cs[1]; Chess= Cs[2];//Remove Board Storage point                    }                }                Else                {                    //when there are no optional numbers.                    varcs = Chessstack.pop ();//BACKSPACEi = Cs[0]; J= Cs[1]; Chess= Cs[2];//Remove Board Storage point                }            }            Else{J++;//into a            }        }    }}//Delete a specific elementfunctionRemovebyvalue (r,c,v) {if(Chess[r][c].num = =v) {return false;//conflict.    }    if(chess[r][c].fixed) {return true;//has been determined, there is no need to delete    }    varLen =chess[r][c].temp.length;  for(vari = 0; i < Len; i++)    {        if(Chess[r][c].temp[i] = =v) {chess[r][c].temp.splice (I,1);//Remove Alternate numbers             Break;//Exit        }    }    returnChess[r][c].temp.length==0?false:true;//there is no certainty and there is no alternative element, then return error}//remove a number from the relevant 20-grid alternative listfunctionChecktempnum (r,c) {vari,j; varnum = Chess[r][c].num;//the number to remove    //Check Column     for(i = 0; i < 9; i++)    {        if(I! = R &&!removebyvalue (I,c,num))return false;//Remove alternative numbers and determine the legitimacy of the numbers    }    //Check Row     for(j = 0; J < 9; J + +)    {        if(J! = C &&!removebyvalue (R,j,num))return false;//Remove alternative numbers and determine the legitimacy of the numbers    }    //Check Small nine    varleft = Math.floor (R/3) *3;//the coordinates of the small ninevartop = Math.floor (C/3) *3;//draws the upper-left coordinate of the small nine for(i = left; i < left+3; i++)    {         for(j = top; J < Top+3; J + +)        {            if(i = = r && J = =c) {Continue;//The same lattice jumps over            }            if(!removebyvalue (I,j,num))return false;//Remove alternative numbers and determine the legitimacy of the numbers        }    }    return true;}//Initializefunctionchess () {creatchess ();//Create a chessboard    //Setarea (0,3);//random set upper left corner nine number    //Setarea (3,6);//randomly set the median nine lattice number    //Setarea (6,9);//randomly set the lower right corner of the nine numberSetpre (0,2,5); Setpre (0,3,3); Setpre (1,0,8); Setpre (1,7,2); Setpre (2,1,7); Setpre (2,4,1); Setpre (2,6,5); Setpre (3,0,4); Setpre (3,5,5); Setpre (3,6,3); Setpre (4,1,1); Setpre (4,4,7); Setpre (4,8,6); Setpre (5,2,3); Setpre (5,3,2); Setpre (5,7,8); Setpre (6,1,6); Setpre (6,3,5); Setpre (6,8,9); Setpre (7,2,4); Setpre (7,7,3); Setpre (8,5,9); Setpre (8,6,7); Chessinit ();//Initialize the boardPrintchess ();//Print Board}chess ();

JS implementation of Sudoku algorithm

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.