admin管理员组文章数量:1336732
My am on the Last part of this game and stuck if I place the ships my self they fit of course but I am using Math.floor(Math.Random() * 10) to get the x,y coords. The ships gets placed on the edges and go offbound often or through a weird undefined error. I have tried returning the function to itself to auto generate a different x,y and changing the path of array[y+i][x] to array[y-i][x] and so on with no good effect so far. I also added a sample of what calls the code.
My github is If the randomDir() placeships are uncommmented and the ones with numbers get commentted out is the reflection
The 3 cases are a undefined that breaks the function or code that still goes above 9 or it works perfect
Here is the code:
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
}
```
My am on the Last part of this game and stuck if I place the ships my self they fit of course but I am using Math.floor(Math.Random() * 10) to get the x,y coords. The ships gets placed on the edges and go offbound often or through a weird undefined error. I have tried returning the function to itself to auto generate a different x,y and changing the path of array[y+i][x] to array[y-i][x] and so on with no good effect so far. I also added a sample of what calls the code.
My github is https://github/jsdev4web/battleship If the randomDir() placeships are uncommmented and the ones with numbers get commentted out is the reflection
The 3 cases are a undefined that breaks the function or code that still goes above 9 or it works perfect
Here is the code:
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
}
```
Share
Improve this question
asked Nov 20, 2024 at 2:09
Jermain SingletonJermain Singleton
151 silver badge4 bronze badges
2
|
1 Answer
Reset to default 0This is the resolution I used. For each case of the board going out of boards I subtracted or added to the x,y. I knew the board goes down or to the right when ships are placed so i accounted for each case.
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
}
本文标签: objectGame Board Boundaries Using Random x y coordinates JavascriptStack Overflow
版权声明:本文标题:object - Game Board Boundaries Using Random x y coordinates Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742386601a2465158.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
x
andy
, sincex
andy
could be-1
andarray[y][x+i]
would throw an error (e.g.array[-1][0]
) – Hao Wu Commented Nov 20, 2024 at 2:18