我在这个游戏的最后一部分,如果我自己放置船只,它们当然适合,但我正在使用 Math.floor(Math.Random() * 10) 来获取 x,y 坐标。船只被放置在边缘并经常偏离航线或出现奇怪的未定义错误。我尝试将函数返回给自身以自动生成不同的 x,y 并将 array[y+i][x] 的路径更改为 array[y-i][x] 等等,到目前为止没有良好的效果。我还添加了一个调用代码的示例。
我的github是https://github.com/jsdev4web/battleship 如果 randomDir() 位置船未注释,并且带有数字的位置船被注释掉,则为反射
这 3 种情况是未定义的,它破坏了仍然高于 9 的函数或代码,或者它工作完美
这是代码:
let [kX, kY, lX, lY, mX, mY, nX, nY, oX, oY] = Array.from({ length: 10 }, () => Math.floor(Math.random() * 10))
let [pX, pY, qX, qY, rX, rY, sX, sY, tX, tY] = Array.from({ length: 10 }, () => Math.floor(Math.random() * 10))
//cpu.placeShip(kX,kY,chaser,"vertical")
//cpu.placeShip(lX,lY,seeker,"vertical")
//cpu.placeShip(mX,mY,longride,"horizontal")
//cpu.placeShip(nX,nY,underwater,"horizontal")
//cpu.placeShip(oX,oY,toofast,"vertical")
placeShip(x, y, ship, direction){
//what i searched -- place a vertical object on a 2D array javascript
let array = this.board
//console.log(array)
let length = array.length
//console.log(length)
if(x < 0 || x >= length || y < 0 || y >= length){
return "Out of bounds"
}
let len = ship.length
//Here i push a obj list of ships each player
this.playerShips.push(ship)
x = x-1
y = y-1
//add a if statement to stop off the board
if(direction === "horizontal"){
for(let i = 0; i < len; i++){
array[y][x+i] = "S";
this.queue.push(`${y}-${x+i}`)
ship.coords.push(`${y}-${x+i}`)
}
}else if (direction === "vertical"){
for(let i = 0; i < len; i++){
array[y+i][x] = "S";
this.queue.push(`${y+i}-${x}`)
ship.coords.push(`${y+i}-${x}`)
}
}
return array
}placeShip(x, y, ship, direction){
//what i searched -- place a vertical object on a 2D array javascript
let array = this.board
//console.log(array)
let length = array.length
//console.log(length)
if(x < 0 || x >= length || y < 0 || y >= length){
return "Out of bounds"
}
let len = ship.length
//Here i push a obj list of ships each player
this.playerShips.push(ship)
x = x-1
y = y-1
//add a if statement to stop off the board
if(direction === "horizontal"){
for(let i = 0; i < len; i++){
array[y][x+i] = "S";
this.queue.push(`${y}-${x+i}`)
ship.coords.push(`${y}-${x+i}`)
}
}else if (direction === "vertical"){
for(let i = 0; i < len; i++){
array[y+i][x] = "S";
this.queue.push(`${y+i}-${x}`)
ship.coords.push(`${y+i}-${x}`)
}
}
return array
}
```
这是我使用的分辨率。对于板子走出板子的每种情况,我都会减去或添加 x,y。我知道当船只放置时,木板会向下或向右,所以我考虑了每种情况。
placeShip(x, y, ship, direction){
//what i searched -- place a vertical object on a 2D array javascript
let array = this.board
//console.log(array)
let length = array.length
//console.log(length)
if(x < 0 || x >= length || y < 0 || y >= length){
console.log(x,y)
return "Out of bounds"
}
let len = ship.length
//Here i push a obj list of ships each player
this.playerShips.push(ship)
x = x-1
y = y-1
if (x === -1 || y === -1){
x = x+1
y = y+1
}
//This code accounts for if I go to far to the right
if (direction === "horizontal" && ship.length === 5 && x === 9){
x = x - 4
} else if (direction === "horizontal" && ship.length === 5 && x === 8){
x = x - 3
} else if (direction === "horizontal" && ship.length === 5 && x === 7){
x = x - 2
} else if (direction === "horizontal" && ship.length === 5 && x === 6){
x = x - 1
}
if (direction === "horizontal" && ship.length === 4 && x === 9){
x = x - 3
} else if (direction === "horizontal" && ship.length === 4 && x === 8){
x = x - 2
} else if (direction === "horizontal" && ship.length === 4 && x === 7){
x = x - 1
}
if (direction === "horizontal" && ship.length === 3 && x === 9){
x = x - 2
} else if (direction === "horizontal" && ship.length === 3 && x === 8){
x = x - 1
}
if (direction === "horizontal" && ship.length === 2 && x === 9){
x = x - 1
}
//This code accounts for not going to far down
if (direction === "vertical" && ship.length === 5 && y === 9){
y = y - 4
} else if (direction === "vertical" && ship.length === 5 && y === 8){
y = y - 3
} else if (direction === "vertical" && ship.length === 5 && y === 7){
y = y - 2
} else if (direction === "vertical" && ship.length === 5 && y === 6){
y = y - 1
}
if (direction === "vertical" && ship.length === 4 && y === 9){
y = y - 3
} else if (direction === "vertical" && ship.length === 4 && y === 8){
y = y - 2
} else if (direction === "vertical" && ship.length === 4 && y === 7){
y = y - 1
}
if (direction === "vertical" && ship.length === 3 && y === 9){
y = y - 2
} else if (direction === "vertical" && ship.length === 3 && y === 8){
y = y - 1
}
if (direction === "vertical" && ship.length === 2 && y === 9){
y = y - 1
}
//add a if statement to stop off the board
if(direction === "horizontal"){
for(let i = 0; i < len; i++){
array[y][x+i] = "#";
this.queue.push(`${y}-${x+i}`)
ship.coords.push(`${y}-${x+i}`)
}
}else if (direction === "vertical"){
for(let i = 0; i < len; i++){
array[y+i][x] = "#";
this.queue.push(`${y+i}-${x}`)
ship.coords.push(`${y+i}-${x}`)
}
}
return array
}