Heim > Web-Frontend > js-Tutorial > Hauptteil

Javascript implementiert Sudoku-Lösung_Javascript-Fähigkeiten

WBOY
Freigeben: 2016-05-16 16:09:39
Original
1285 Leute haben es durchsucht

Shengsheng hat die Java-Version, die ich geschrieben habe, in die Javascript-Version geändert. Es ist das erste Mal, dass ich schreibe, es ist sehr unprofessionell, tut mir leid. Oh, wie frei ich bin.

Code kopieren Der Code lautet wie folgt:

var Sudoku = {
    init: Funktion (str) {
        this.blank = [];
        this.fixed = [];
        this.cell = [];
        this.trials=[];
        für (i = 0; i < 81; i ) {
            var chr = str.charCodeAt(i);
            if (chr == 48) {
                this.cell[i] = 511;
                this.blank.push(i);
            } sonst {
                this.cell[i] = 1 << chr - 49;
                this.fixed.push(i);
            }
        }
    },
    showBoard: Funktion () {
        var board = "";
        für (var i = 0; i < 81; i ) {
            if (i % 9 == 0) {
                board = board.concat("n");
            }
            board = board.concat("[");
            für (var j = 0; j < 9; j ) {
                if ((this.cell[i] >> j & 1) == 1) {
                    board = board.concat(String.fromCharCode(j 49));
                }
            }
            board = board.concat("]");
        }
        Rückgabebrett;
    },
    Überprüfen Sie: Funktion () {
        var checkpoint = [0, 12, 24, 28, 40, 52, 56, 68, 80];
        for (var i in checkpoint) {
            var r, b, c;
            r = b = c = this.cell[checkpoint[i]];
            für (j = 0; j < 8; j ) {
                c ^= this.cell[this.getX(checkpoint[i])[j]];
                b ^= this.cell[this.getX(checkpoint[i])[8 j]];
                r ^= this.cell[this.getX(checkpoint[i])[16 j]];
            }
            if ((r & b & c) != 0x1FF) {
                return false;
            }
        }
        return true;
    },
    bitCount: Funktion (i) {
        var n = 0;
        für (var j = 0; j < 9; j ) {
            if ((i >> j & 1) == 1)
                n ;
        }
        return n;
    },
    numberOfTrailingZeros: function(i){
        var n = 0;
        für (var j = 0; j < 9; j ) {
            if ((i >> j & 1) ==0)
                n ;
            sonst{
                Pause;
            }
        }
        Rückkehr n;       
    },
    updateCandidates: Funktion () {
        for (var i in this.fixed) {
            var opt = 0x1FF ^ this.cell[this.fixed[i]];
            für (var j = 0; j < 24; j ) {
                this.cell[this.getX(this.fixed[i])[j]] &= opt;
                //!notice
                if (this.cell[this.getX(this.fixed[i])[j]] == 0) {
                    //console.log("Fehler-0-Kandidat:" x[this.fixed[i]][j]);
                    return false;
                }
            }
        }
        return true;
    },
    seeUniqueCandidate: Funktion () {
        for (var bidx in this.blank) {
            var row = 0, col = 0, box = 0;
            für (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;
                weiter;
            }
            if (this.bitCount(this.cell[this.blank[bidx]] & ~col) == 1) {
                this.cell[this.blank[bidx]] &= ~col;
                weiter;
            }
            if (this.bitCount(this.cell[this.blank[bidx]] & ~box) == 1) {
                this.cell[this.blank[bidx]] &= ~box;
            }
        }
    },
    seeFilledable: Funktion () {
        this.fixed = [];
  var _del=[];
        for (var i in this.blank) {
            if (this.bitCount(this.cell[this.blank[i]]) == 1) {
                this.fixed.push(this.blank[i]);
                //console.log("fixed:" this.blank[i] "=>" this.cell[this.blank[i]]);
                //this.blank.splice(i, 1);//es in der Schleife zu löschen würde einen Fehler verursachen
    _del.push(i);
            }
        }
  while(_del.length>0){
   this.blank.splice(_del.pop(), 1);
  }
    },
    seekMutexCell: Funktion () {
        var zwei = [];
        for (var n in this.blank) {
            if (this.bitCount(this.cell[this.blank[n]]) == 2) {
                two.push(this.blank[n]);
            }
        }
        for (var i = 0; i < two.length; i ) {
            for (var j = i 1; j < two.length; j ) {
                if (this.cell[two[i]] == this.cell[two[j]]) {
                    var opt = ~this.cell[two[i]];
                    if (parseInt(two[i] / 9) ==parseInt(two[j] / 9)) {
                        für (n = 0; n < 8; n ) {
                            this.cell[this.getX(two[i])[n]] &= opt;
                        }
                    }
                    if ((two[i] - two[j]) % 9 == 0) {                       
                        für (n = 8; n < 16; n ) {
                            this.cell[this.getX(two[i])[n]] &= opt;
                        }
                    }
                    if ((parseInt(two[i] / 27) * 3 parseInt(two[i] % 9 / 3)) == (parseInt(two[j] / 27) * 3 parseInt(two[j] % 9 / 3 ))) {
                        für (n = 16; n < 24; n ) {
                            this.cell[this.getX(two[i])[n]] &= opt;
                        }
                    }
                    this.cell[two[j]] = ~opt;
                }
            }
        }
    },
    basicSolve: Funktion () {
        mach {
            if (!this.updateCandidates(this.fixed)) {
                this.backForward();
            }
            this.seekUniqueCandidate();
            this.seekMutexCell();
            this.seekFilledable();
        } while (this.fixed.length != 0);
        return this.blank.length == 0;
    },   
    setTrialCell: function() {
        for (var i in this.blank) {
            if (this.bitCount(this.cell[this.blank[i]]) == 2) {
                var tryValue = 1 << this.numberOfTrailingZeros(this.cell[this.blank[i]]);
                varwaitingValue = this.cell[this.blank[i]] ^trialValue;
                //console.log("try:[" this.blank[i] "]->" (this.numberOfTrailingZeros(trialValue) 1) "#" (this.numberOfTrailingZeros(waitingValue) 1));
                this.cell[this.blank[i]] = tryValue;               
                this.trials.push(this.createTrialPoint(this.blank[i], waitingValue, this.cell));
                return true;
            }
        }
        return false;
    },
    backForward: function() {
        if (this.trials.length==0) {
            console.log("Vielleicht keine Lösung!");
            zurück;
        }
        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));
    },
    zurücksetzen: Funktion(Daten) {
        this.blank=[];
        this.fixed=[];
        this.cell=data.concat();
        für (var i = 0; i < 81; i ) {
            if (this.bitCount(this.cell[i]) != 1) {
                this.blank.push(i);
            } sonst {
                this.fixed.push(i);
            }
        }
    },
    trySolve: function() {
        while (this.blank.length!=0) {
            if (this.setTrialCell()) {
                this.basicSolve();
            } sonst {
                if (this.trials.length==0) {
                    //console.log("Kann nicht zurückgehen! Vielleicht keine Lösung!");
                    Pause;
                } sonst {
                    this.backForward();
                    this.basicSolve();
                }
            }
        }
    },
    spielen: 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("[" (Ende - Start) "ms OK!]");
        } sonst {
            console.log("[" (end - start) "ms, kann es nicht lösen?");
        }
  //gib this.showBoard();
zurück     },
    getX:function(idx){
        var Nachbarn=new Array(24);
        var box=new Array(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 ){
            if(n==c)continue;
            Nachbarn[i ]=r*9 n;
        }
        for(var n=0;n<9;n ){
            if(n==r)continue;
            Nachbarn[i ]=c n*9;
        }
        for(var n=0;n<9;n ){
            var t=xs box[n];
            if(t==idx)continue;
            Nachbarn[i ]=t;
        }
          Rückkehr Nachbarn;
    },
 createTrialPoint:function(idx, val, board) {
        var tp = {};
        tp.idx = idx;
        tp.val = val;
        tp.data = board.concat();
        return tp;
 }
};
//Sudoku.init("000000500000008300600100000080093000000000020700000000058000000000200017090000060");
//Sudoku.init("530070000600195000098000060800060003400803001700020006060000280000419005000080079");
Sudoku.init("800000000003600000070090200050007000000045700000100030001000068008500010090000400");
Sudoku.play();

以上就是关于使用javascript实现数独解法的全部代码了,希望大家能够喜欢.

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!