gpt4 book ai didi

javascript - 扫雷炸弹威胁

转载 作者:行者123 更新时间:2023-12-02 03:01:35 25 4
gpt4 key购买 nike

我正在尝试用 JavaScript 构建一个扫雷游戏,但我一直坚持添加垂直炸弹威胁。

这是我的代码:

const generateBoardForPlay = function () {

const gameBoard = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]

function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

const generateBombs = function () {
return [
[getRandomInt(9), getRandomInt(9)],
[getRandomInt(9), getRandomInt(9)],
[getRandomInt(9), getRandomInt(9)],
[getRandomInt(9), getRandomInt(9)],
[getRandomInt(9), getRandomInt(9)],
[getRandomInt(9), getRandomInt(9)],
[getRandomInt(9), getRandomInt(9)],
[getRandomInt(9), getRandomInt(9)],
[getRandomInt(9), getRandomInt(9)],
[getRandomInt(9), getRandomInt(9)]
]
}

const bombArr = generateBombs()
const addBombsToBoard = function (gameBoard) {
for (let x of bombArr) {
gameBoard[x[0]][x[1]] = "99"
}

return gameBoard
}

const board = addBombsToBoard(gameBoard)

// return board;
const addWarnings = function (array) {
for (let x in array) {
if (array[x] === '99' && x > 0 && array[x - 1] !== "99") {

array[x - 1] += 1
}
}
for (let i = array.length; i > 0; i--) {
if (array[i] === '99' && i < 9 && array[i + 1] !== "99") {
array[i + 1] += 1
}
}
return array
}

addWarnings(board[0])
addWarnings(board[1])
addWarnings(board[2])
addWarnings(board[3])
addWarnings(board[4])
addWarnings(board[5])
addWarnings(board[6])
addWarnings(board[7])
addWarnings(board[8])
addWarnings(board[9])

const addVerticalWarning = function (board) {
// THIS IS WHERE I'M STUCK
}


return board;
}

这是输出

[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, '99', '99', '99', 1, 0, 0],
[0, 0, 1, '99', 1, 0, 0, 0, 0, 0],
[0, 1, '99', 1, 0, 0, 0, 1, '99', 1],
[0, 1, '99', 1, 0, 1, '99', 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, '99', 1, 0, 0, 0, 0, 0],
[1, '99', 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]

我收到了水平炸弹威胁,但两个循环的复杂性阻止了我弄清楚水平炸弹威胁。我这样做是为了一项作业,所以我不想只是从其他地方复制并粘贴它。如果有一种方法可以完成代码,那就太棒了,如果没有,我想为我指明了正确的方向。

最佳答案

您的游戏板由嵌套在父数组中的 10 个并行数组组成。
这应该可以帮助您了解如何使用并行嵌套数组来访问彼此对应的值:

// Defines the gameBoard
let gameBoard = makeGameBoard();

// Loops through rows of the gameBoard
for (let rowIndex = 0; rowIndex < gameBoard.length; rowIndex++) {
// Defines the current row and its neighbors
let currentRow, previousRow, nextRow;
currentRow = gameBoard[rowIndex];
if(rowIndex > 0){ previousRow = gameBoard[rowIndex - 1]; }
if(rowIndex < gameBoard.length - 1){ nextRow = gameBoard[rowIndex + 1]; }

// Loops through the current row
for(let colIndex = 0; colIndex < currentRow.length; colIndex++){

// Logs what is in this column for this row and neighbors...
console.log(`row ${rowIndex}, col ${colIndex}: ${gameBoard[rowIndex][colIndex]}`);

if (previousRow){ console.log(`in square above: ${previousRow[colIndex]}`); }
else{ console.log("no square above first row"); }

if(nextRow) {console.log(`in square below: ${nextRow[colIndex]}`); }
else{console.log("no square below last row"); }

console.log("");
}
}

function makeGameBoard() {
return [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
];
}

关于javascript - 扫雷炸弹威胁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59851350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com