ฉันเปลี่ยนเวอร์ชัน java ที่ฉันเขียนถึงเวอร์ชัน JavaScript อนิจจาฉันเป็นอิสระได้อย่างไร
การคัดลอกรหัสมีดังนี้:
var sudoku = {
init: function (str) {
this.blank = [];
this.fixed = [];
this.cell = [];
this.trials = [];
สำหรับ (i = 0; i <81; i ++) {
var chr = str.charcodeat (i);
ถ้า (chr == 48) {
this.cell [i] = 511;
this.blank.push (i);
} อื่น {
this.cell [i] = 1 << chr - 49;
this.fixed.push (i);
-
-
-
Showboard: function () {
var board = "";
สำหรับ (var i = 0; i <81; i ++) {
ถ้า (i % 9 == 0) {
board = board.concat ("/n");
-
board = board.concat ("[");
สำหรับ (var j = 0; j <9; j ++) {
if ((this.cell [i] >> j & 1) == 1) {
board = board.concat (String.FromCharcode (J + 49));
-
-
board = board.concat ("]");
-
กระดานกลับ;
-
ตรวจสอบ: ฟังก์ชัน () {
จุดตรวจ var = [0, 12, 24, 28, 40, 52, 56, 68, 80];
สำหรับ (var i ในจุดตรวจ) {
var r, b, c;
r = b = c = this.cell [จุดตรวจสอบ [i]];
สำหรับ (j = 0; j <8; j ++) {
c ^= this.cell [this.getx (จุดตรวจสอบ [i]) [j]];
b ^= this.cell [this.getx (จุดตรวจสอบ [i]) [8 + j]];
r ^= this.cell [this.getx (จุดตรวจสอบ [i]) [16 + j]];
-
if ((r & b & c)! = 0x1ff) {
กลับเท็จ;
-
-
กลับมาจริง;
-
BitCount: ฟังก์ชั่น (i) {
var n = 0;
สำหรับ (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 1)
n ++;
-
กลับ n;
-
NumberOfTrailingZeros: function (i) {
var n = 0;
สำหรับ (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 0)
n ++;
อื่น{
หยุดพัก;
-
-
กลับ n;
-
updateCandidates: function () {
สำหรับ (var i ใน this.fixed) {
var opt = 0x1ff ^ this.cell [this.fixed [i]];
สำหรับ (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]);
กลับเท็จ;
-
-
-
กลับมาจริง;
-
SeekuniqueCandidate: function () {
สำหรับ (var bidx ใน this.blank) {
var row = 0, col = 0, box = 0;
สำหรับ (i = 0; i <8; i ++) {
แถว | = this.cell [this.getx (this.blank [bidx]) [i]];
กล่อง | = 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;
ดำเนินการต่อ;
-
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;
-
-
-
seekfilledable: function () {
this.fixed = [];
var _del = [];
สำหรับ (var i ใน this.blank) {
if (this.bitcount (this.cell [this.blank [i]]) == 1) {
this.fixed.push (this.blank [i]);
//console.log("fixed:"+this.blank [blank inhienthi+"=>"+this.cell [this.blank [blank]]);
//this.blank.splice(i, 1); // เพื่อลบในลูปจะทำให้เกิดข้อผิดพลาด
_del.push (i);
-
-
ในขณะที่ (_del.length> 0) {
this.blank.splice (_del.pop (), 1);
-
-
seekmutexcell: function () {
var two = [];
สำหรับ (var n ใน this.blank) {
if (this.bitcount (this.cell [this.blank [n]]) == 2) {
two.push (this.blank [n]);
-
-
สำหรับ (var i = 0; i <two.length; i ++) {
สำหรับ (var j = i+1; j <two.length; j ++) {
if (this.cell [สอง [i]] == this.cell [สอง [j]]) {
var opt = ~ this.cell [สอง [i]];
if (parseint (สอง [i] / 9) == parseint (สอง [j] / 9)) {
สำหรับ (n = 0; n <8; n ++) {
this.cell [this.getx (สอง [i]) [n]] & = opt;
-
-
if ((สอง [i] - สอง [j]) % 9 == 0) {
สำหรับ (n = 8; n <16; n ++) {
this.cell [this.getx (สอง [i]) [n]] & = opt;
-
-
if ((parseint (สอง [i] / 27) * 3 + parseint (สอง [i] % 9 /3)) == (parseint (สอง [j] / 27) * 3 + parseint (สอง [j] % 9 /3))) {
สำหรับ (n = 16; n <24; n ++) {
this.cell [this.getx (สอง [i]) [n]] & = opt;
-
-
this.cell [สอง [j]] = ~ opt;
-
-
-
-
Basicsolve: function () {
ทำ {
if (! this.updateCandidates (this.fixed)) {
this.backforward ();
-
this.seekuniquecandidate ();
this.seekmutexcell ();
this.seekfilledable ();
} ในขณะที่ (this.fixed.length! = 0);
ส่งคืนสิ่งนี้ blank.length == 0;
-
SetTrialCell: function () {
สำหรับ (var i ใน this.blank) {
if (this.bitcount (this.cell [this.blank [i]]) == 2) {
var trialValue = 1 << this.numberoftrailingzeros (this.cell [this.blank [i]]);
var waitingValue = this.cell [this.blank [i]] ^ trialValue;
//console.log ("ลอง: [" + this.blank [i] + "]->" + (this.numberoftrailingzeros (trialValue) + 1) + "#" + (this.numberoftrailingzeros
this.cell [this.blank [i]] = trialValue;
this.trials.push (this.createTrialPoint (this.blank [i], waitingValue, this.cell));
กลับมาจริง;
-
-
กลับเท็จ;
-
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));
-
รีเซ็ต: ฟังก์ชั่น (ข้อมูล) {
this.blank = [];
this.fixed = [];
this.cell = data.concat ();
สำหรับ (var i = 0; i <81; i ++) {
if (this.bitcount (this.cell [i])! = 1) {
this.blank.push (i);
} อื่น {
this.fixed.push (i);
-
-
-
TrialSolve: function () {
ในขณะที่ (this.blank.length! = 0) {
if (this.setTrialCell ()) {
this.basicsolve ();
} อื่น {
if (this.trials.length == 0) {
//console.log("can ไม่กลับไปข้างหน้า! อาจจะไม่มีทางออก! ");
หยุดพัก;
} อื่น {
this.backforward ();
this.basicsolve ();
-
-
-
-
เล่น: ฟังก์ชั่น () {
console.log (this.showboard ());
var start = date ใหม่ (). getMilliseconds ();
if (! this.basicsolve ()) {
this.trialsolve ();
-
var end = วันที่ใหม่ (). getMilliseconds ();
console.log (this.showboard ());
if (this.check ()) {
console.log ("[" + (end - start) + "ms ok!]");
} อื่น {
console.log ("[" + (end - start) + "ms ไม่สามารถแก้ปัญหาได้?");
-
// return this.showboard ();
-
getx: ฟังก์ชัน (idx) {
var เพื่อนบ้าน = อาร์เรย์ใหม่ (24);
var box = อาร์เรย์ใหม่ (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;
สำหรับ (var n = 0; n <9; n ++) {
ถ้า (n == c) ดำเนินการต่อ;
เพื่อนบ้าน [i ++] = r*9+n;
-
สำหรับ (var n = 0; n <9; n ++) {
ถ้า (n == r) ดำเนินการต่อ;
เพื่อนบ้าน [i ++] = c+n*9;
-
สำหรับ (var n = 0; n <9; n ++) {
var t = xs+box [n];
ถ้า (t == idx) ดำเนินการต่อ;
เพื่อนบ้าน [i ++] = t;
-
คืนเพื่อนบ้าน;
-
CreateTrialPoint: function (idx, val, board) {
var tp = {};
tp.idx = idx;
tp.val = val;
tp.data = board.concat ();
กลับ tp;
-
-
//sudoku.init("00000050000000830060010000000008009300000000207000000000005800000000000200017090000060 ");
//sudoku.init("530070000600195000098000060800060003400803001700020006060000280000419005000080079 ");
Sudoku.init ("800000000360000000070090200050007000000457000001000300030001000068008500010090000400");
sudoku.play ();
ข้างต้นคือรหัสทั้งหมดในการใช้โซลูชัน Sudoku โดยใช้ JavaScript ฉันหวังว่าคุณจะชอบ