我正试图用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]
]我得到了水平炸弹威胁,但这两个循环的复杂性阻止了我找出水平的威胁。我这样做是为了完成一项任务,所以我不想从其他地方复制和超越它。如果有一种方法来完成代码,这是很棒的,如果没有,我猜我的方向是正确的。
发布于 2020-01-22 01:47:38
游戏板由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],
];
}
发布于 2020-01-22 02:08:03
另一种看待它的方法就是转置数组。这是因为董事会是正方形的:
function warnings() {
let verticalWarnings = [];
let transposedBoard = gameBoard[0].map((col, i) => gameBoard.map(row => row[i]));
transposedBoard.forEach(function(row, i) {
row.forEach(function(col, j) {
if (col === '99') {
verticalWarnings.push([j, i])
}
});
});
return verticalWarnings;
}
console.log(warnings());另外,如果您想使用相同的技术来解决水平问题:用完全相同的方式在gameBoard上迭代,但是推[i, j]而不是j, i
发布于 2020-01-22 02:44:52
我很难解释事情。也许这就是你所追求的逻辑?
class BombThreats{
constructor(width = 10, height = 10){
this.width = width; this.height = height; this.board = [];
for(let i=0,a,w=this.width,h=this.height; i<h; i++){
a = []; this.board.push(a);
for(let n=0; n<w; n++){
a.push(0);
}
}
}
rand(max){
return Math.floor(Math.random()*(max+1));
}
addThreats(count){
for(let i=0,w=this.width,c=w-1,h=this.height,r=h-1,row,beforeRow,afterRow,col,beforeCol,afterCol,l=count; i<l; i++){
row = this.rand(r); beforeRow = row-1; afterRow = row+1;
col = this.rand(c); beforeCol = col-1; afterCol = col+1;
if(this.board[row][col] === 99){
i--; continue;
}
this.board[row][col] = 99;
if(beforeRow > -1 && this.board[beforeRow][col] !== 99)this.board[beforeRow][col] = 1;
if(afterRow < h && this.board[afterRow][col] !== 99)this.board[afterRow][col] = 1;
if(beforeCol > -1 && this.board[row][beforeCol] !== 99)this.board[row][beforeCol] = 1;
if(afterCol < w && this.board[row][afterCol] !== 99)this.board[row][afterCol] = 1;
}
return this;
}
get seeBoard(){
this.board.forEach(a => { console.log(JSON.stringify(a)); });
return this;
}
setSweeper(row, col){
this.sweeperRow = row; this.sweeperCol = col;
if(this.sweeperRow === undefined){
this.sweeperRow = this.rand(this.height-1); this.sweeperCol = this.rand(this.width-1);
}
return this;
}
moveSweeper(direction){
let r = this.sweeperRow, c = this.sweeperCol;
if(direction !== 'back'){
this.prevSweeperRow = r; this.prevSweeperCol = c;
}
switch(direction){
case 'up':
if(r > 0)this.sweeperRow--;
break;
case 'right':
if(c < this.width-1)this.sweeperCol++;
break;
case 'down':
if(r < this.height-1)this.sweeperRow++;
break;
case 'left':
if(c > 0)this.sweeperCol--;
break;
case 'back':
return this.setSweeper(this.prevSweeperRow, this.prevSweeperCol);
}
return this;
}
get sweeperStatus(){
switch(this.board[this.sweeperRow][this.sweeperCol]){
case 0:
return 'safe';
case 1:
return 'warning';
case 99:
return 'bomb';
}
}
}
const bt = new BombThreats;
console.log('sweeperStatus: '+bt.addThreats(20).seeBoard.setSweeper().sweeperStatus);
console.log('current sweeper position: {row: '+bt.sweeperRow+', col: '+bt.sweeperCol+'}');
console.log('after moved up: '+bt.moveSweeper('up').sweeperStatus);
// moveSweeper is cumulative
console.log('after moved right: '+bt.moveSweeper('right').sweeperStatus);
console.log('after moved down: '+bt.moveSweeper('down').sweeperStatus);
console.log('after moved left: '+bt.moveSweeper('left').sweeperStatus);
console.log('after moved back: '+bt.moveSweeper('back').sweeperStatus);
console.log('-------------------------------------------');
// now you can make board any size
const bT = new BombThreats(15, 25);
bT.addThreats(42).seeBoard;
我想最好的解释方法是..。您需要考虑行和列。我在addThreats中命名了变量,这样您就可以很容易地看到该技术。
https://stackoverflow.com/questions/59851350
复制相似问题