Mudei a versão Java que escrevi para uma versão JavaScript. A primeira vez que escrevi, não foi profissional, me perdoo. Infelizmente, como estou livre.
A cópia do código é a seguinte:
var sudoku = {
init: function (str) {
this.blank = [];
this.fixed = [];
this.cell = [];
this.Trials = [];
para (i = 0; i <81; i ++) {
var chr = str.charcodeat (i);
if (chr == 48) {
this.Cell [i] = 511;
this.blank.push (i);
} outro {
this.Cell [i] = 1 << Chr - 49;
this.fixed.push (i);
}
}
},
Showboard: function () {
var board = "";
for (var i = 0; i <81; i ++) {
if (i % 9 == 0) {
placa = board.concat ("/n");
}
Board = Board.Concat ("[");
for (var j = 0; j <9; j ++) {
if ((this.cell [i] >> j & 1) == 1) {
Board = Board.Concat (String.FromCharcode (J + 49));
}
}
Board = Board.Concat ("]");
}
placa de retorno;
},
Verifique: function () {
VAR Ponto de controle = [0, 12, 24, 28, 40, 52, 56, 68, 80];
para (var i no ponto de verificação) {
var r, b, c;
r = b = c = this.cell [ponto de verificação [i]];
for (j = 0; j <8; j ++) {
c ^= this.cell [this.getx (ponto de verificação [i]) [j]];
b ^= this.cell [this.getx (ponto de verificação [i]) [8 + j]];
r ^= this.cell [this.getx (ponto de verificação [i]) [16 + j]];
}
if ((R & b & c)! = 0x1ff) {
retornar falso;
}
}
retornar true;
},
BitCount: function (i) {
var n = 0;
for (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 1)
n ++;
}
retornar n;
},
NumberOftrailingZeros: function (i) {
var n = 0;
for (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 0)
n ++;
outro{
quebrar;
}
}
retornar n;
},
updateCandidates: function () {
para (var i neste.fixed) {
var opt = 0x1ff ^ this.cell [this.fixed [i]];
for (var j = 0; j <24; j ++) {
this.cell [this.getx (this.fixed [i]) [j]] & = opt;
//!Perceber
if (this.cell [this.getx (this.fixed [i]) [j]] == 0) {
//console.log("Error-0 candidato: "+x [this.fixed [i]] [j]);
retornar falso;
}
}
}
retornar true;
},
SeekuniqueCeCandidate: function () {
para (var bidx neste.blank) {
var linha = 0, col = 0, caixa = 0;
for (i = 0; i <8; i ++) {
linha | = this.cell [this.getx (this.blank [bidx]) [i]];
caixa | = this.cell [this.getx (this.blank [bidx]) [8 + i]];
col | = this.cell [this.getx (this.blank [bidx]) [16 + i]];
}
if (this.bitcount (this.cell [this.blank [bidx]] & ~ linha) == 1) {
this.cell [this.blank [bidx]] & = ~ row;
continuar;
}
if (this.bitcount (this.cell [this.blank [bidx]] & ~ col) == 1) {
this.cell [this.blank [bidx]] & = ~ col;
continuar;
}
if (this.bitcount (this.cell [this.blank [bidx]] & ~ caixa) == 1) {
this.cell [this.blank [bidx]] & = ~ caixa;
}
}
},
Seekfillewable: function () {
this.fixed = [];
var _del = [];
para (var i neste.blank) {
if (this.bitcount (this.cell [this.blank [i]]) == 1) {
this.fixed.push (this.blank [i]);
//console.log("fixed:"+this.blanklente ]+"=>"+this.cell[this.blank[i]]);
//this.blank.splice(i, 1); // para excluí -lo no loop causaria bug
_del.push (i);
}
}
while (_del.length> 0) {
this.blank.splice (_del.pop (), 1);
}
},
SeekMutexCell: function () {
var dois = [];
para (var n neste.blank) {
if (this.bitcount (this.cell [this.blank [n]]) == 2) {
dois.push (this.blank [n]);
}
}
for (var i = 0; i <dois.length; i ++) {
for (var j = i+1; j <dois.Length; j ++) {
if (this.cell [dois [i]] == this.cell [dois [j]]) {
var opt = ~ this.cell [dois [i]];
if (parseint (dois [i] / 9) == parseint (dois [j] / 9)) {
para (n = 0; n <8; n ++) {
this.cell [this.getx (dois [i]) [n]] & = opt;
}
}
if ((dois [i] - dois [j]) % 9 == 0) {
para (n = 8; n <16; n ++) {
this.cell [this.getx (dois [i]) [n]] & = opt;
}
}
if ((parseint (dois [i] / 27) * 3 + parseint (dois [i] % 9/3)) == (parseint (dois [j] / 27) * 3 + parseint (dois [j] % 9/3))) {
para (n = 16; n <24; n ++) {
this.cell [this.getx (dois [i]) [n]] & = opt;
}
}
this.Cell [dois [j]] = ~ opt;
}
}
}
},
Basicsolve: function () {
fazer {
if (! this.updateCandidates (this.fixed)) {
this.backforward ();
}
this.seekuniqueCeCandidate ();
this.seekMutexCell ();
this.seekfilleable ();
} while (this.fixed.length! = 0);
return this.blank.length == 0;
},
setTrialCell: function () {
para (var i neste.blank) {
if (this.bitcount (this.cell [this.blank [i]]) == 2) {
var TrialValue = 1 << this.NumberOftrailingZeros (this.cell [this.blank [i]]);
var wageValue = this.cell [this.blank [i]] ^ TrialValue;
//console.log ("Tente: [" + this.blank [i] + "]->" + (this.NumberOftrailingZeros (TrialValue) + 1) + "#" + (this.NumberOftrailingZeros (WaverValue) + 1));
this.cell [this.blank [i]] = TrialValue;
this.Trials.push (this.createtrialpoint (this.blank [i], waitvalue, this.cell));
retornar true;
}
}
retornar falso;
},
Backforward: function () {
if (this.Trials.Length == 0) {
console.log ("Talvez nenhuma solução!");
retornar;
}
var back = this.Trials.pop ();
this.reset (back.data);
this.cell [back.idx] = back.val;
this.fixed.push (back.idx);
//console.log ("back: [" + back.idx + "]->" + (this.NumberOftrailingZeros (back.val) + 1));
},
Redefinir: function (dados) {
this.blank = [];
this.fixed = [];
this.cell = data.concat ();
for (var i = 0; i <81; i ++) {
if (this.bitcount (this.cell [i])! = 1) {
this.blank.push (i);
} outro {
this.fixed.push (i);
}
}
},
TrialSolve: function () {
while (this.blank.length! = 0) {
if (this.ettRialCell ()) {
this.basicsolve ();
} outro {
if (this.Trials.Length == 0) {
//console.log(" Não é de volta! Talvez nenhuma solução! ");
quebrar;
} outro {
this.backforward ();
this.basicsolve ();
}
}
}
},
Play: function () {
console.log (this.Showboard ());
var start = new Date (). getMillisEconds ();
if (! this.basicsolve ()) {
this.TrialSolve ();
}
var end = new Date (). getMillisEconds ();
console.log (this.Showboard ());
if (this.check ()) {
console.log ("[" + (end - start) + "ms ok!]");
} outro {
console.log ("[" + (end - start) + "ms, não pode resolvê -lo?");
}
// return this.Showboard ();
},
getx: function (idx) {
var vizinhos = nova matriz (24);
var caixa = nova matriz (0,1,2,9,10,11,18,19,20);
var r = parseint (idx/9);
var c = idx%9;
var xs = parseint (idx/27)*27+parseint (idx%9/3)*3;
var i = 0;
for (var n = 0; n <9; n ++) {
se (n == c) continuar;
vizinhos [i ++] = r*9+n;
}
for (var n = 0; n <9; n ++) {
se (n == r) continuar;
vizinhos [i ++] = c+n*9;
}
for (var n = 0; n <9; n ++) {
var t = xs+caixa [n];
se (t == idx) continuar;
vizinhos [i ++] = t;
}
retornar vizinhos;
},
CreateTrialPoint: function (idx, val, placa) {
var tp = {};
tp.idx = idx;
tp.val = val;
tp.data = Board.Concat ();
retornar tp;
}
};
//Sudoku.init("00000050000000830060010000000800930000000002070000000000058000000000200017090000060 ");
//Sudoku.init("5300700006001950000980000608000600034008030017000200060600002800000041900500000080079 ");
Sudoku.init ("800000000003600000000700902000500070000004570000010003000100000068008500010090000SOU");
Sudoku.play ();
O exposto acima é todo o código na implementação da solução Sudoku usando JavaScript, espero que gostem.