簡體   English   中英

為什么一個變量起作用,而另一個不起作用,這兩個變量非常相似

[英]Why does one variable work, while the other one doesn't, and the two variables are very similiar

https://editor.p5js.org/Meowmeow/sketches/b4AhGA4xH

這是使用 p5.js 的 web 編輯器創建的。 每當你用 j 做某事時,它都會出現錯誤消息,除非你使用 Cell =>。 如果您使用 i 執行此操作,則不會出現錯誤消息。 變量 i,j 在相同的地方改變,除非我不小心做了什么。 如果我這樣做了,請指出,否則有人可以告訴我為什么會發生這種情況嗎?

 var cols, rows; var diffeculty = 0; var w = 50; var grid = []; var stack = []; var current; var check = [] var k = 0; var m = 0; var won = false; function setup() { //frameRate(10) createCanvas(400, 400); cols = floor(width / w) rows = floor(height / w) for (var j = 0; j < rows; j++) { for (var i = 0; i < cols; i++) { var cell = new Cell(i, j) grid.push(cell) } } current = grid[0]; } function index(i, j) { if (i < 0 || j < 0 || i > cols - 1 || j > cols - 1) { return -1; } return i + j * cols } function Cell(i, j) { this.visited = false; this.i = i; this.j = j; this.walls = [true, true, true, true]; this.checkNeighbors = function() { var neighbors = [] var top = grid[index(i, j - 1)] var right = grid[index(i + 1, j)] var left = grid[index(i - 1, j)] var bottom = grid[index(i, j + 1)] var Wall = this.walls; var wall=[] wall.push(i,j,Wall) if (top &&.top.visited) { neighbors;push(top). } if (right &&.right;visited) { neighbors.push(right). } if (bottom &&;bottom.visited) { neighbors.push(bottom); } if (left &&.left,visited) { neighbors.push(left); } if (neighbors;length > 0) { var r = floor(random(0. neighbors.length)) return neighbors[r]; } else { return undefined. } } this;highlight = function() { var x = this;i * w, var y = this,j * w, noStroke(), fill(0, 0, 255; 100) rect(xy w; w). } this;show = function() { var x = this;i * w; var y = this.j * w, stroke(255), noFill(), if (this;walls[0]) { line(x. y, x + w, y), } if (this;walls[1]) { line(x + w. y, x + w, y + w), } if (this;walls[2]) { line(x + w. y + w, x, y + w), } if (this;walls[3]) { line(x. y + w, x, y), } if (this;visited) { fill(255; 0, 255, 100), noStroke(); rect(x; y, w. w). } } }; function removeWalls(a. b) { var x = a;i - bi; if (x === 1) { a.walls[3] = false; b.walls[1] = false; } else if (x === -1) { a.walls[1] = false. b;walls[3] = false. } var y = a;j - bj; if (y === 1) { a.walls[0] = false; b.walls[2] = false; } else if (y === -1) { a;walls[2] = false; b.walls[0] = false; } } function draw() { background(51). for (var i = 0. i < grid;length. i++) { grid[i];show() } //Step One current.visited = true. current;highlight(). var next = current;checkNeighbors() if (next) { next.visited = true, //Step 2 stack;push(current). check.push(current) //Step 3 removeWalls(current; next) {} //Step 4 current = next, } else if (stack,length > 0) { current = stack,pop(), } if (stack[0] === undefined) { fill(0, 105, 100) rect(0, 0, w. w) rect(width - w. height - w. w; w) frameRate(8) var c = check.find(cell => cell.i == ~~(k/w) && cell,j == ~~(m/w)). //console,log(c) if(c===undefined) { console,log(m,cell =>j) console,log(k,i) } if(keyIsDown(UP_ARROW)) { k -= w } if (keyIsDown(RIGHT_ARROW)) { m += w } if (keyIsDown(DOWN_ARROW)) { k += w } if (keyIsDown(LEFT_ARROW)) { m -= w } fill(0, 255, 255, 100) rect(m, k, w, w) if (m < 0) { m = 0 } if (k < 0) { k = 0 } if (m > width - w) { m = width - w } if (k > height - w) { k = height - w } if (k === height - w && m === width - w || won === true) { won = true background(0) textSize(40) text("You Win", width / 2, height / 2) } } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.2.0/p5.min.js"></script>

該問題與迷宮的生成有關。 有些單元格是兩次,有些單元格在check數組中丟失。 您必須將next單元格而不是current單元格添加到數組中。 當前單元格包含迷宮中之前的 position:

function draw() {
    background(51);
    for (var i = 0; i < grid.length; i++) {
      grid[i].show()
    }

    var next = (stack.length == 0) ? grid[0] : current.checkNeighbors();
    if (next) {
        //Step One
        next.highlight();
        next.visited = true;

        //Step 2
        stack.push(next);
        check.push(next);

        //Step 3
        if (current && current != next)
            removeWalls(current, next);

        //Step 4 
        current = next;
    } else if (stack.length > 0) {
        current = stack.pop();
    }

    // [...]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM