简体   繁体   English

我不知道:x 未定义

[英]I have no idea: x is undefined

Im making a 4-in-a-row-game, Im nearly done, but I have this annoying error while it's checking for anyone to win:我正在制作一个四合一游戏,我几乎完成了,但是在检查是否有人获胜时出现了这个烦人的错误:

Uncaught TypeError: d[(y + 1)] is undefined

I have no idea what's causing the problem and where it occurs, here's my code:我不知道是什么导致了问题以及它发生在哪里,这是我的代码:

let rows = 10
let cols = 10
let size = 50
let padding = "5px"




let turn = 1
let win = 0

let tileDiv = document.getElementById("tiles")

const data = [];
for (let i = 0; i < rows; i++) {
    data.push((new Array(cols)).fill(0));
}

function createTiles() {
    let a = 0
    for (let y = 0; y < rows; y++) {
        for (let x = 0; x < cols; x++) {
            a = document.createElement("img")
            a.src = "tiles/0.png"
            a.height = size
            a.style = "padding: " + padding
            a.id = String(y) + "_" + String(x)
            tileDiv.appendChild(a)
            tileDiv.lastChild.addEventListener("click", function () { clickedTile(this) })
        }
        a = document.createElement("p")
        tileDiv.appendChild(a)
    }
}

function setTile(x, y, z) {
    let a = data[y]
    a[x] = z
    data[y] = a
    document.getElementById(String(y) + "_" + String(x)).src = "tiles/" + z + ".png"

}

function gravity(x, z) {
    let i = 0
    for (let i = 0; i < rows; i++) {
        if ((data[i])[x] == 0) {
            y = i


        }

    }
    if (data[y][x] == 0) {
        setTile(x, y, z)
    }
    else { alert("Column full, next player") }
}
function checkVerticalWinFor(x, y, d) {
    console.log("checkV-" + x + "-" + y)
    let a = d[y][x]
    let b = d[(y - 1)][x]
    let c = d[(y - 2)][x]
    let d_ = d[(y - 3)][x]
    if (a == b && b == c && c == d_ && d_ == a) {
        win = a
        if (win != 0) {
            setTile(x, y, (win + "_win"))
            setTile(x, y - 1, (win + "_win"))
            setTile(x, y - 2, (win + "_win"))
            setTile(x, y - 3, (win + "_win"))
        }
    }

}

function checkHorizontalWinFor(x, y, d) {
    console.log("checkH-" + x + "-" + y)
    let a = d[y][x]
    let b = d[y][(x + 1)]
    let c = d[y][(x + 2)]
    let d_ = d[y][(x + 3)]
    if (a == b && b == c && c == d_ && d_ == a) {
        win = a
        if (win != 0) {
            setTile(x, y, (win + "_win"))
            setTile(x + 1, y, (win + "_win"))
            setTile(x + 2, y, (win + "_win"))
            setTile(x + 3, y, (win + "_win"))
        }
    }
}

function checkDiagonalWinFor(x, y, d) {
    console.log("checkD-" + x + "-" + y)
    let a = d[y][x]
    let b = d[(y + 1)][(x + 1)]
    let c = d[(y + 2)][(x + 2)]
    let d_ = d[(y + 3)][(x + 3)]
    if (a == b && b == c && c == d_ && d_ == a) {
        win = a
        if (win != 0) {
            setTile(x, y, (win + "_win"))
            setTile(x + 1, y + 1, (win + "_win"))
            setTile(x + 2, y + 2, (win + "_win"))
            setTile(x + 3, y + 3, (win + "_win"))
        }
    }
}

function checkDiagonal_WinFor(x, y) {
    console.log("checkD_-" + x + "-" + y)
    let a = d[y][x]
    let b = d[(y + 1)][(x - 1)]
    let c = d[(y + 2)][(x - 2)]
    let d_ = d[(y + 3)][(x - 3)]
    if (a == b && b == c && c == d_ && d_ == a) {
        win = a
        if (win != 0) {
            setTile(x, y, (win + "_win"))
            setTile(x - 1, y + 1, (win + "_win"))
            setTile(x - 2, y + 2, (win + "_win"))
            setTile(x - 3, y + 3, (win + "_win"))
        }
    }
}

function checkWin() {
    for (let y = 0; y < rows; y++) {
        for (let x = 0; x < cols; x++) {
            if (data[y][x] != 0) {
                checkVerticalWinFor(x, y, data)

                if (win == 0) {
                    checkHorizontalWinFor(x, y, data)

                    if (win == 0) {
                        checkDiagonalWinFor(x, y, data)
                        if (win == 0) { checkDiagonal_WinFor(x, y, data) }
                    }
                }
            }
        }
    }
}

function clickedTile(z) {
    if (win == 0) {
        q = z.id
        c = q.split("_")
        x = c[1]

        gravity(x, turn)

        if (turn == 1) { turn = 2 }
        else if (turn == 2) { turn = 1 }

        checkWin()
    }


}
createTiles()

I know it's a lot, but can someone help?我知道这很多,但有人可以帮忙吗? The error says the problem is at line 98.错误表明问题出在第 98 行。

The filelocation in the setTile-function are just the tiles in image-format: -0.png -1.png -1_win.png -2.png -2_win.png setTile 函数中的文件位置只是图像格式的图块:-0.png -1.png -1_win.png -2.png -2_win.png

Saw 2 things:看到2件事:

  1. In your function checkDiagonal_WinFor declaration, you forgot to include the variable d.在您的 function checkDiagonal_WinFor 声明中,您忘记包含变量 d。 So it will give you an error.所以它会给你一个错误。
  2. before calling checkDiagonal_WinFor, checkDiagonalWinFor, checkHorizontalWinFor and checkVerticalWinFor, check limits在调用 checkDiagonal_WinFor、checkDiagonalWinFor、checkHorizontalWinFor 和 checkVerticalWinFor 之前,检查限制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM