Бомбы MineSweeper - PullRequest
       5

Бомбы MineSweeper

2 голосов
/ 22 января 2020

Я пытаюсь создать игру тральщика с 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]
]

Я получаю угрозы горизонтальной бомбы, но сложность двух петель мешает мне понять из горизонтальных. Я делаю это для задания, поэтому я не хочу просто копировать и вставлять его откуда-то еще. Если есть способ завершить код, это здорово, если нет, я думаю, укажи мне правильное направление.

Ответы [ 3 ]

2 голосов
/ 22 января 2020

Ваша игровая доска состоит из 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],
  ];
}
1 голос
/ 22 января 2020

Мне трудно объяснять вещи. Возможно, это логика c, которую вы ищете?

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, чтобы вы могли легко увидеть технику.

1 голос
/ 22 января 2020

Другой способ взглянуть на это - просто перенести массив. Это работает, потому что доска является квадратом:

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 точно так же, но pu sh [i, j] вместо [j, i]

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...