未能在两者之间移动元素?

我一直在尝试使用 Blacktracking 算法解决数独问题,一切都很好,调用了 canvar,我可以看到数字但事情是数字没有移动,即逻辑不执行 current.i === 0;是我得到错误的地方!即使我已经为 num 声明了一个单独的变量,问题也没有得到解决。只有当我删除 .numcurrent == 0而不是它没有显示任何错误但数字仍然没有移动时

http://img.mukewang.com/628f3a36000101fc19111071.jpg

var cell = [];

var stack = [];

var sudoku =  [2,3,0,9,4,0,6,7,0,

               8,0,0,3,2,5,9,1,4,

               9,0,0,7,6,0,3,2,0,

               1,0,0,0,0,0,7,9,2,

               5,0,3,2,1,0,4,8,6,

               4,0,0,6,8,0,5,3,1,

               7,0,0,1,0,0,0,0,9,

               6,5,9,8,7,2,1,4,3,

               3,0,0,0,9,0,0,0,7];

var current;

var number = 1;


function setup(){

    createCanvas(450,450);

    var a=0;

    var b=0;

    for(var i=0;i<81;i++){

        if(a%9==0 && i!=0){

            b = b+50;

            a = 0;

        }

        each[i] = new each(a,b,i,sudoku[i]);

        a = a+50;

    }

    current = cell[0];

}


function draw(){

    background(10);

    for(var i=0;i<81;i++){

        each[i].show();

    }


    if(current.num === 0){ //the error is typeerror can't read the property of num 


        if(! sameColumn(current.i,number) && ! sameRow(current.i,number) && ! sameSquare(current.i,number) && number<(10)){

            current.num = number;

            stack.push(current);

            number = 0;

            current.each[current.i+1];

        }

        else {

            if(number > 8){

            current.num = 0;

            current = stack.pop();

            number = current.num;

            current.num = 0;

        }

        } 

    }

    else{

        current = each[current+1];

        number = 0;

    }

    number++;

}


function each(a,b,i,num){

        this.a = a;

        this.b = b;

        this.i = i;

        this.num = num;

        this.show = function(){

            noFill();

            stroke(255);

            rect(this.a,this.b,50,50);

            textSize(32);

            text(this.num,a+12,b+40);

        }

    }


慕标5832272
浏览 98回答 1
1回答

三国纷争

错误非常简单。current = cell[0];变得未定义,因为您定义cell为一个空数组并且之后没有对其进行操作。从我到目前为止观察到的情况来看,您的代码的许多部分在逻辑上都不起作用,例如,same Column(current.i,number) && ! sameRow(current.i,number) && ! sameSquare(current.i,number)除非你有一个包含这些函数的单独的 js 文件,否则它被执行肯定会抛出一个错误(不是因为执行没有到达该行)。另一个是current = cell[current+1];如果current变量是用来存储单元格对象的,加1是没有意义的,反之亦然。现在我相信这就是设置功能的样子:function setup(){&nbsp; &nbsp; createCanvas(450,450);&nbsp; &nbsp; var a=0;&nbsp; &nbsp; var b=0;&nbsp; &nbsp; for(var i=0;i<81;i++){&nbsp; &nbsp; &nbsp; &nbsp; if(a%9==0 && i!=0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b = b+50;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; cell[i] = new Cell(a,b,i,sudoku[i]); //changed each[i] to cell[i], also renamed the 'each' class&nbsp; &nbsp; &nbsp; &nbsp; a = a+50;&nbsp; &nbsp; }&nbsp; &nbsp; current = cell[0];}如果可能的话,请编辑更多关于您的代码究竟做了什么的信息。干杯:)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript