0

例如,假设我有这个数组数组:

Board = [
[ 0, 0, 0, 1],
[ 1, 0, 0, 1],
[ 0, 1, 0, 0]
[ 0, 0, 1, 0]
]

现在我想检查主对角线冲突,这样没有一个主对角线包含超过一个 1。我已经尝试了各种循环,但似乎无法获得正确的代码。

4

1 回答 1

0

用一个 for 循环完成

function testForCollision (Board) {
    // assuming square array
    var leftToRightFound = false,
        rightToLeftFound = false;
    for (var i = 0, j = Board.length-1; i < Board.length; i++, j--)
        if (Board[i][i] == 1) {
            if (leftToRightFound)
                return false;
            else
                leftToRight = true;
        }
        if (Board[i][j] == 1) {
            if (rightToLeftFound)
                return false;
            else
                rightToLeft = true;
        }
    }
    return true;
}
于 2013-08-15T15:12:45.223 回答