JavaバージョンをJavaScriptバージョンに変更しました。悲しいかな、私はどのように自由ですか。
コードコピーは次のとおりです。
var sudoku = {
init:function(str){
this.blank = [];
this.fixed = [];
this.cell = [];
this.trials = [];
for(i = 0; i <81; i ++){
var chr = str.charcodeat(i);
if(chr == 48){
this.cell [i] = 511;
this.blank.push(i);
} それ以外 {
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){
board = 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( "]");
}
返品ボード。
}、
チェック:function(){
var checkpoint = [0、12、24、28、40、52、56、68、80];
(チェックポイントのvar i){
var r、b、c;
r = b = c = this.cell [checkpoint [i]];
for(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){
falseを返します。
}
}
trueを返します。
}、
BitCount:function(i){
var n = 0;
for(var j = 0; j <9; j ++){
if((i >> j&1)== 1)
n ++;
}
n;
}、
numberoftrailingzeros:function(i){
var n = 0;
for(var j = 0; j <9; j ++){
if((i >> j&1)== 0)
n ++;
それ以外{
壊す;
}
}
n;
}、
updateCandidates:function(){
for(var i in this.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;
//!知らせ
if(this.cell [this.getx(this.fixed [i])[j]] == 0){
//console.log("error-0候補者: "+x [this.fixed [i]] [j]);
falseを返します。
}
}
}
trueを返します。
}、
SeekUniqueCandidate:function(){
for(this.blankのvar bidx){
var row = 0、col = 0、box = 0;
for(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]]&〜ow)== 1){
this.cell [this.blank [bidx]]&=〜row;
続く;
}
if(this.bitcount(this.cell [this.blank [bidx]]&〜col)== 1){
this.cell [this.blank [bidx]]&= 〜col;
続く;
}
if(this.bitcount(this.cell [this.blank [bidx]]&〜box)== 1){
this.cell [this.blank [bidx]]&= 〜box;
}
}
}、
SeekFillidable:function(){
this.fixed = [];
var _del = [];
for(this.blankのvar i){
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]]);
//this.blank.splice(i、1); //ループで削除するとバグが発生します
_del.push(i);
}
}
while(_del.length> 0){
this.blank.splice(_del.pop()、1);
}
}、
SEEKMUTEXCELL:function(){
var two = [];
for(this.blankのvar n){
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 [2 [i]];
if(parseint(2 [i] / 9)== parseint(2 [j] / 9)){
for(n = 0; n <8; n ++){
this.cell [this.getX(2 [i])[n]]&= opt;
}
}
if((2 [i] -2 [j])%9 == 0){
for(n = 8; n <16; n ++){
this.cell [this.getX(2 [i])[n]]&= opt;
}
}
if((parseint(2 [i] / 27) * 3 + parseint(2 [i]%9 /3))==(parseint(2 [j] / 27) * 3 + parseint(2 [j]%9/3))){
for(n = 16; n <24; n ++){
this.cell [this.getX(2 [i])[n]]&= opt;
}
}
this.cell [two [j]] = 〜opt;
}
}
}
}、
basicsolve:function(){
する {
if(!this.updateCandidates(this.fixed)){
this.backforward();
}
this.seekuniqueCandidate();
this.seekmutexcell();
this.seekfillable();
} while(this.fixed.length!= 0);
this.blank.length == 0を返します。
}、
SetTrialCell:function(){
for(this.blankのvar i){
if(this.bitcount(this.cell [this.blank [i]])== 2){
var trialValue = 1 << this.numberoftrailingzeros(this.cell [this.blank [i]]);
var waitevalue = this.cell [this.blank [i]] ^ trialValue;
//console.log( "try:[" + this.blank [i] + "] - >" +(this.numberoftrailingzeros(trialValue) + 1) + "#" +(this.numberoftrailingzeros(waitevalue) + 1));
this.cell [this.blank [i]] = trialValue;
this.trials.push(this.createtrialpoint(this.blank [i]、waitevalue、this.cell));
trueを返します。
}
}
falseを返します。
}、
backforward:function(){
if(this.trials.length == 0){
console.log( "たぶん解決策!");
戻る;
}
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));
}、
リセット:function(data){
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);
} それ以外 {
this.fixed.push(i);
}
}
}、
trialSolve:function(){
while(this.blank.length!= 0){
if(this.settrialcell()){
this.basicsolve();
} それ以外 {
if(this.trials.length == 0){
//console.log("can not go backforward!多分解決策はありません! ");
壊す;
} それ以外 {
this.backforward();
this.basicsolve();
}
}
}
}、
再生: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!]");
} それ以外 {
console.log( "[" +(end -start) + "ms、それを解くことはできませんか?");
}
// this.showboard();を返します。
}、
getX:function(idx){
var neighbors = 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)継続;
Neighbors [i ++] = r*9+n;
}
for(var n = 0; n <9; n ++){
if(n == r)継続;
Neighbors [i ++] = c+n*9;
}
for(var n = 0; n <9; n ++){
var t = xs+box [n];
if(t == idx)継続;
Neighbors [i ++] = t;
}
隣人を返します。
}、
CreateTrialPoint:function(idx、val、board){
var tp = {};
tp.idx = idx;
tp.val = val;
tp.data = board.concat();
tpを返します。
}
};
//sudoku.init("00000000000000000008300600100000008009300000000020700000000000000000000000000000000017090000060 ");
//sudoku.init("530070000600195000098000060800060003400803001700020006060000280000419005000080079 ");
sudoku.init( "800000000000036000000007009020005000700000045700000100030001000068008500010090000400");
sudoku.play();
上記は、JavaScriptを使用してSudokuソリューションを実装することに関するすべてのコードです。あなたがそれを気に入っていただければ幸いです。