我正在努力防止船舶在我正在从事的这一作战项目中出现重叠,但不能说明如何防止船舶在两阵列中重叠。 我尝试使用包括(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]