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 You should put the out of bounds check after you decrease x and y, since x and y could be -1 and array[y][x+i] would throw an error (e.g. array[-1][0]) – Hao Wu Commented Nov 20, 2024 at 2:18
  • Thanks I just did that and it worked !!! – Jermain Singleton Commented Nov 20, 2024 at 23:09
Add a comment  | 

1 Answer 1

Reset to default 0

This 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