English 中文(简体)
防止船舶在作战中重叠(贾瓦文)
原标题:Prevent ships from overlapping in Battleship (JavaScript)

我正在努力防止船舶在我正在从事的这一作战项目中出现重叠,但不能说明如何防止船舶在两阵列中重叠。 我尝试使用包括(a)在内的方法,看看船舶价值是否已经存在但并未发挥作用。 不能真正确定如何解决这一问题。

const Gameboard = () => {
  const rows = 10
  const columns = 10
  const board = []
  const missedCoord = []

  console.log(board)
  const getBoard = () => [...board]

  for (let i = 0; i < rows; i += 1) {
    board[i] = []
    for (let j = 0; j < columns; j += 1) {
      board[i][j] = null
    }
  }

  // Need to reduce array amount to a single value
  const cellCount = getBoard().reduce((row, col) => row + col.length, 0)

  const placeShipsHorizontal = (x, y, ship, direction) => {
    const shipsLength = ship.getLength()
    const currentBoard = getBoard()
    if (direction === "horizontal") {
      if (y + shipsLength > columns) {
        console.log("Cannot place ship horizontally, out of bounds.");
        return false;
      }
      // Places ship horizontally
      for (let i = 0; i < shipsLength; i += 1) {
        if (currentBoard.includes(ship)) {
          return false
        }
        board[x][y + i] = ship.name
      }
    }
    return true
  }
  const placeShipsVertical = (x, y, ship, direction) => {
    const shipsLength = ship.getLength()
    const currentBoard = getBoard()
    if (direction === "vertical") {
      if (x + shipsLength > rows) {
        if (board[x][y].includes(null))
          console.log("Cannot place ship vertically, out of bounds.")
        return false
      }
    }
    // Places ship vertically
    for (let i = 0; i < shipsLength; i += 1) {
      if (currentBoard.includes(ship)) {
        return false
      }
      board[x + i][y] = ship.name;
    }
    return true
  }

  function receiveAttack(x, y, ship) {
    // If board has coordinate with ship
    if (board[x][y]) {
      // Send hit to ship
      ship.hit()
      // Mark X where ship has been hit
      board[x][y] = "X"
      console.log(`${ship.name} has been hit!`)
      if (ship.isSunk()) {
        console.log(`${ship.name} has sunk!`)
      }
      return true
    }
    // Push missed coordinates to array
    missedCoord.push(x, y)
    console.log(`Attack missed at coordinates: [${x},${y}]`)
    return false
  }
  return {
    placeShipsVertical,
    placeShipsHorizontal,
    getBoard,
    cellCount,
    receiveAttack
  }
}


const gameboard = Gameboard()

gameboard.placeShipsHorizontal(2, 4, testShip, "horizontal")
gameboard.placeShipsVertical(2, 4, testShip2, "vertical")

const createShip = (shipLength, name) => {
  let hits = 0
  let sunk = false

  function getLength() {
    return this.shipLength
  }

  function hit() {
    hits += 1
    return hits
  }

  function isSunk() {
    if (hits >= shipLength) {
      sunk = true
      return sunk
    }
    sunk = false
    return sunk
  }
  return {
    getLength,
    hit,
    isSunk,
    shipLength,
    name,
    hits
  }
}

const testShip = createShip(3, "Boat")
const testShip2 = createShip(5, "Boat 2")

[null, null, null, null, null, null, null, null, null, null]
[null, null, null, null, null, null, null, null, null, null]
[null, null, null, null,  Boat 2 ,  Boat ,  Boat , null, null, null]
[null, null, null, null,  Boat 2 , null, null, null, null, null]
[null, null, null, null,  Boat 2 , null, null, null, null, null]
[null, null, null, null,  Boat 2 , null, null, null, null, null]
[null, null, null, null,  Boat 2 , null, null, null, null, null]
[null, null, null, null, null, null, null, null, null, null]
[null, null, null, null, null, null, null, null, null, null]
[null, null, null, null, null, null, null, null, null, null]
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签