You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For each cell, check if sudoku numbers have already been used in the pools related to this cell?
We check the horizontal pool, one vertical pool and one square 3x3 pool.
If used, then remove this value from our pool of sudoku numbers.
If there are no numbers left in the pool of sudoku numbers, then we need to erase the entire line, clear the related pools and recreate the line.
If attempts to rearrange the numbers do not help (usually this is the 6th line), then we are trying to recreate the entire table.
If there are numbers in the pool of sudoku numbers, then we take a random number from these numbers and put it in the table.
We also add the selected used number to three pools: a horizontal pool, one vertical pool and one square pool.
functionSudoku(){functionGenerator(){constnumbers=[1,2,3,4,5,6,7,8,9]// 1)lettab=[],yl=[],sq=[]for(leti=0;i<numbers.length;i++){tab[i]=[]letxl=newMap()// 2), 3)letrebuild_iterator=0if(!sq[Math.floor(i/3)])sq[Math.floor(i/3)]=[]for(letj=0;j<numbers.length;j++){if(!yl[j])yl[j]=newMap()// 2), 4)if(!sq[Math.floor(i/3)][Math.floor(j/3)])sq[Math.floor(i/3)][Math.floor(j/3)]=newMap()// 2), 5)letsud_pull=[...numbers]// copy one dimention array, let sud_pull = digits - is a linkfor(letk=sud_pull.length-1;k>-1;k--){// 6)if(xl.has(sud_pull[k])){sud_pull.splice(k,1)}elseif(yl[j].has(sud_pull[k])){sud_pull.splice(k,1)}elseif(sq[Math.floor(i/3)][Math.floor(j/3)].has(sud_pull[k])){sud_pull.splice(k,1)}}if(sud_pull.length==0){// 7) if our string is wrong, clean values and start xl from beginif(rebuild_iterator++==30)// 8)return[]j--// current j is not set yet so we move counter to previouswhile(j>-1){yl[j].delete(tab[i][j])sq[Math.floor(i/3)][Math.floor(j/3)].delete(tab[i][j])deletetab[i][j]j--}xl.clear()continue}else{letdigit=sud_pull[Math.floor(Math.random()*(sud_pull.length))]// 9)tab[i][j]=digit// 9)xl.set(digit,1)// 10)yl[j].set(digit,1)// 10)sq[Math.floor(i/3)][Math.floor(j/3)].set(digit,1)// 10)}}}returntab}lettab=[]while(!tab.length){tab=Generator()}returntab}consttab=Sudoku()for(letentitesoftab){console.log(String(entites))}