J'ai changé la version Java que j'ai écrite dans une version JavaScript. La première fois que je l'ai écrit, c'était très peu professionnel, je me pardonne. Hélas, comment je suis libre.
La copie de code est la suivante:
var sudoku = {
init: fonction (str) {
this.blank = [];
this.fixed = [];
this.cell = [];
this.trials = [];
pour (i = 0; i <81; i ++) {
var chr = str.charcodeat (i);
if (chr == 48) {
this.cell [i] = 511;
this.blank.push (i);
} autre {
this.cell [i] = 1 << ch - 49;
this.fixed.push (i);
}
}
},
showboard: function () {
var board = "";
pour (var i = 0; i <81; i ++) {
if (i% 9 == 0) {
Board = Board.Concat ("/ n");
}
Board = Board.Concat ("[");
pour (var j = 0; j <9; j ++) {
if ((this.cell [i] >> j & 1) == 1) {
Board = Board.Concat (String.FromCharcode (J + 49));
}
}
Board = Board.Concat ("]");
}
tableau de retour;
},
vérifier: function () {
Var Checkpoint = [0, 12, 24, 28, 40, 52, 56, 68, 80];
pour (var i dans le point de contrôle) {
var r, b, c;
r = b = c = this.cell [Checkpoint [i]];
pour (j = 0; j <8; j ++) {
c ^ = this.cell [this.getx (point de contrôle [i]) [j]];
b ^ = this.cell [this.getx (point de contrôle [i]) [8 + j]];
r ^ = this.cell [this.getx (point de contrôle [i]) [16 + j]];
}
if ((r & b & c)! = 0x1ff) {
retourne false;
}
}
Retour Vrai;
},
BitCount: fonction (i) {
var n = 0;
pour (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 1)
n ++;
}
retour n;
},
NumberOftrailingzeros: fonction (i) {
var n = 0;
pour (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 0)
n ++;
autre{
casser;
}
}
retour n;
},
UpdateCandidates: function () {
pour (var i dans ce.fixed) {
var opt = 0x1ff ^ this.cell [this.fixed [i]];
pour (var j = 0; j <24; j ++) {
this.cell [this.getx (this.fixed [i]) [j]] & = opt;
//!Avis
if (this.cell [this.getx (this.fixed [i]) [j]] == 0) {
//console.log("Error-0 candidat: "+ x [this.fixed [i]] [j]);
retourne false;
}
}
}
Retour Vrai;
},
Seekuniquecanated: function () {
pour (var bidx dans ce.blank) {
var row = 0, col = 0, box = 0;
pour (i = 0; i <8; i ++) {
row | = this.cell [this.getx (this.blank [bidx]) [i]];
Box | = 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]] & ~ row) == 1) {
this.cell [this.blank [bidx]] & = ~ row;
continuer;
}
if (this.bitCount (this.cell [this.blank [bidx]] & ~ col) == 1) {
this.cell [this.blank [bidx]] & = ~ col;
continuer;
}
if (this.bitCount (this.cell [this.blank [bidx]] & ~ box) == 1) {
this.cell [this.blank [bidx]] & = ~ box;
}
}
},
SECHERDABLEABLE: FONCTION () {
this.fixed = [];
var _del = [];
pour (var i dans ce.blank) {
if (this.bitCount (this.cell [this.blank [i]]) == 1) {
this.fixed.push (this.blank [i]);
//console.log("fixed:"+ ce.blank fichier++"=>"+ ceCell ours de ce.blanot]]);
//this.blank.splice(i, 1); // le supprimer dans la boucle provoquerait un bug
_del.push (i);
}
}
while (_del.length> 0) {
this.blank.splice (_del.pop (), 1);
}
},
SeekMutexCell: function () {
var deux = [];
pour (var n dans ce.blank) {
if (this.bitCount (this.cell [this.blank [n]]) == 2) {
deux.push (this.blank [n]);
}
}
pour (var i = 0; i <deux.length; i ++) {
pour (var j = i + 1; j <deux.length; j ++) {
if (this.cell [deux [i]] == this.cell [deux [j]]) {
var opt = ~ this.cell [deux [i]];
if (parseInt (deux [i] / 9) == parseInt (deux [j] / 9)) {
pour (n = 0; n <8; n ++) {
this.cell [this.getx (deux [i]) [n]] & = opt;
}
}
if ((deux [i] - deux [j])% 9 == 0) {
pour (n = 8; n <16; n ++) {
this.cell [this.getx (deux [i]) [n]] & = opt;
}
}
if ((parseInt (deux [i] / 27) * 3 + parseInt (deux [i]% 9/3)) == (parseInt (deux [j] / 27) * 3 + parseInt (deux [j]% 9/3))) {
pour (n = 16; n <24; n ++) {
this.cell [this.getx (deux [i]) [n]] & = opt;
}
}
this.cell [deux [j]] = ~ opt;
}
}
}
},
Basicsolve: function () {
faire {
if (! this.updatecanDatedates (this.fixed)) {
this.backforward ();
}
this.seekuniquecanatedate ();
this.seekmutexcell ();
this.seekfilable ();
} while (this.fixed.length! = 0);
retourne this.blank.length == 0;
},
setTrialCell: function () {
pour (var i dans ce.blank) {
if (this.bitCount (this.cell [this.blank [i]]) == 2) {
var triatvalue = 1 << this.numberoftrailingzeros (this.cell [this.blank [i]]);
var WaitingValue = this.cell [this.blank [i]] ^ TrialValue;
//console.log ("essai: [" + this.blank [i] + "] ->" + (this.numberoftrailingzeros (TrialValue) + 1) + "#" + (this.numberoftrailingzeros (WaitingValue) + 1));
this.cell [this.blank [i]] = essaisvalue;
this.trials.push (this.CreateTrialPoint (this.blank [i], waitingvalue, this.cell));
Retour Vrai;
}
}
retourne false;
},
Backforward: function () {
if (this.trials.length == 0) {
console.log ("Peut-être pas de solution!");
retour;
}
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));
},
réinitialiser: fonction (data) {
this.blank = [];
this.fixed = [];
this.cell = data.concat ();
pour (var i = 0; i <81; i ++) {
if (this.bitCount (this.cell [i])! = 1) {
this.blank.push (i);
} autre {
this.fixed.push (i);
}
}
},
TrialSolve: function () {
while (this.blank.length! = 0) {
if (this.setTrialCell ()) {
this.basicsolve ();
} autre {
if (this.trials.length == 0) {
//console.log(" ne pas aller rétro! Peut-être pas de solution! ");
casser;
} autre {
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!]");
} autre {
console.log ("[" + (end - start) + "MS, ne peut pas le résoudre?");
}
// retourne this.showboard ();
},
getx: function (idx) {
var voisins = nouveau tableau (24);
Var Box = Nouveau tableau (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;
pour (var n = 0; n <9; n ++) {
si (n == c) continuer;
voisins [i ++] = r * 9 + n;
}
pour (var n = 0; n <9; n ++) {
if (n == r) continuer;
voisins [i ++] = c + n * 9;
}
pour (var n = 0; n <9; n ++) {
var t = xs + box [n];
if (t == idx) continuer;
voisins [i ++] = t;
}
retour des voisins;
},
CreateTrialPoint: fonction (idx, val, cartone) {
var tp = {};
tp.idx = idx;
tp.val = val;
tp.data = board.concat ();
retourner tp;
}
};
//Sudoku.init("0000000000000008300600100000008009300000000020700000000058000000000200017090000060 ");
//Sudoku.init("5300700006001950000980000608000600034008030017000200060600002800004190000000080079 ");
Sudoku.init ("80000000000003600000070090200050007000000457000001000300010000680085000100900400");
Sudoku.play ();
Ce qui précède est tout le code sur l'implémentation de la solution Sudoku à l'aide de JavaScript, j'espère que vous l'aimez.