لقد غيرت إصدار Java الذي كتبته إلى إصدار JavaScript. للأسف ، كيف أنا حر.
نسخة الكود كما يلي:
var sudoku = {
init: وظيفة (str) {
this.blank = [] ؛
this.fixed = [] ؛
this.cell = [] ؛
this.trials = [] ؛
لـ (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 = "" ؛
لـ (var i = 0 ؛ i <81 ؛ i ++) {
إذا (i ٪ 9 == 0) {
المجلس = board.concat ("/n") ؛
}
المجلس = board.concat ("[") ؛
لـ (var j = 0 ؛ j <9 ؛ j ++) {
if ((this.cell [i] >> j & 1) == 1) {
board = board.concat (string.fromcharcode (j + 49)) ؛
}
}
المجلس = board.concat ("]") ؛
}
مجلس الإرجاع ؛
} ،
تحقق: وظيفة () {
var checkpoint = [0 ، 12 ، 24 ، 28 ، 40 ، 52 ، 56 ، 68 ، 80] ؛
لـ (var i في نقطة التفتيش) {
var r ، b ، c ؛
r = b = c = this.cell [pointpoint [i]] ؛
لـ (j = 0 ؛ j <8 ؛ j ++) {
c ^= this.cell [this.getx (pointpoint [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) {
العودة كاذبة
}
}
العودة صحيح.
} ،
bitcount: function (i) {
var n = 0 ؛
لـ (var j = 0 ؛ j <9 ؛ j ++) {
إذا ((i >> j & 1) == 1)
n ++ ؛
}
العودة ن ؛
} ،
NumberOfTrailingzeros: وظيفة (i) {
var n = 0 ؛
لـ (var j = 0 ؛ j <9 ؛ j ++) {
إذا ((i >> j & 1) == 0)
n ++ ؛
آخر{
استراحة؛
}
}
العودة ن ؛
} ،
updateCandidates: function () {
لـ (var i in 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 ++) {
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 ؛
يكمل؛
}
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 ؛
}
}
} ،
قابلة للبحث: وظيفة () {
this.fixed = [] ؛
var _del = [] ؛
لـ (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 Budapi/201is +> #"+ this.cell budapthis.blank budapi]]) ؛
//this.blank.splice(i ، 1) ؛ // لحذفها في الحلقة قد تسبب خطأ
_del.push (i) ؛
}
}
بينما (_del.length> 0) {
this.blank.splice (_del.pop () ، 1) ؛
}
} ،
SeekMutexcell: function () {
var اثنين = [] ؛
لـ (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 [two [i]] == this.cell [two [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.seekmutexcell () ؛
هذا. seekfilledable () ؛
} بينما (this.fixed.length! = 0) ؛
إرجاع this.blank.length == 0 ؛
} ،
setTrialCell: function () {
لـ (var i in 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 ("try: [" + this.blank [i] + "]->" + (this.numberOfTrailingzeros (TrialValue) + 1) + "#" + (this.numberOfTrailingzeros (WaitingValue) + 1) ؛
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("cann لا تعود! ربما لا يوجد حل! ") ؛
استراحة؛
} آخر {
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!]") ؛
} آخر {
console.log ("[" + (end - start) + "MS ، لا يمكن حلها؟") ؛
}
// إرجاع this.showboard () ؛
} ،
getx: function (idx) {
var neighbors = new Array (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("00000000000000083001000000000800930000000002070000000005800000000000200017090000060 ") ؛
//sudoku.init("53000000600195009800006080006000340080300170002000606000028000041900000080079 ") ؛
Sudoku.Init ("80000000000360000007009020005000700000045700000100030001000068008500010090000400") ؛
sudoku.play () ؛
ما ورد أعلاه هو كل الكود في تطبيق حل Sudoku باستخدام JavaScript ، وآمل أن يعجبك ذلك.