0

I am trying to build a battleship game and using functions. I wish to create and randomise 1 & 0 in my array every time I run the function as seen in the array below

Since it is a battlefield game, is there any way to make the 1s be in a row /column of 4/3/2/1? , to mimic the different sizes of the battleships

 let battelfield = [
                [0,0,0,1,1,1,1,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,0,0,0,0],
                [0,0,0,0,0,0,1,0,0,0],
                [0,0,0,0,0,0,1,0,0,0],
                [1,0,0,0,0,0,1,1,1,1],
                [1,0,0,0,0,0,0,0,0,0],
                [1,0,0,1,0,0,0,0,0,0],
                [1,0,0,1,0,0,0,0,0,0],
                [1,0,0,0,0,0,0,0,0,0]
                ]`
2
  • 1
    What's is the problem exactly? Commented Dec 2, 2022 at 14:00
  • i am trying to built a battleship game using an array by functions , and in the array the values would be 1 or 0 ( the 1 stand for part of the battelship and 0 for empty ) furthermore can the 1s be side by side ? to minic the battleship on the battelfield) Commented Dec 2, 2022 at 14:05

2 Answers 2

1

For a battleship, the way I would do it would be (assuming your grid is already filled with 0s):

For each ship

    1. randomly select a starting position
    1. randomly select a direction (up, down, left, right)
    1. add your ship (by changing however many 1s you need to, based on the size of the ship).

The checks you need to add would be:

  • At step 1, make sure there isn't a boat there already, in which case pick again.
  • At step 2, make sure you're not going to hit the side of your game board, or another ship, in which case try another direction. If all 4 directions have been tried and there isn't enough space for a ship, back to step 1.
Sign up to request clarification or add additional context in comments.

Comments

0

I usually don't give full answers when OP doesn't really show they tried but I liked the challenge.

The idea is to:

  • Set your empty board.
  • Choose a random point in the board where the ship will start
  • Choose direction (H or V)
  • With the random point and direction, make sure there is room for the ship according to the limits of the board
  • Create a list of positions the ship will take
  • Test all positions to make sure they are free
  • Set the positions on the board as filled.

At any given time, if a check is not fulfilled I've put continue; this will stop the current iteration and back to the beginning of the while. That way, the code runs until it finds a spot, and return to leave the loop.

Also, I've made a 1d array instead of 2d because it felt easier for mathing it out and manipulations. Feel free to convert to 2D afterward, or not.

let battlefield = new Array(10*10).fill(0);
placeShip(3);
placeShip(4);
placeShip(4);
placeShip(5);
console.log(battlefield);

function placeShip(length){
  while(true){
      const start = Math.round(Math.random()*99);
      if(battlefield[start]==='1') continue;
      const orientation = Math.random() <=.5?'H':'V';
      // Fill the positions where the ship will be placed.
      const positions = new Array();
      if(orientation === 'H'){
          // First make sure we have room to place it
          if(10-((start+1) % 10) < length)continue;
          for(let p=start;p<start+length;p++){
              // Set for each length the position the ship will take.
              positions.push(p);
          }
      }else if(orientation === 'V'){
          // Same as for H but we divide by 10 because we want rows instead of cells.
          if(10-((start/10) % 10) < length)continue;
          for(let p=start;p<start+length*10;p+=10){
              // Set for each length the position the ship will take.
              positions.push(p);
          }
      }
      // Now let's check to make sure there is not a ship already in one of the positions
      for(let i=0,L=positions.length;i<L;i++){
          if(battlefield[positions[i]]!=="0")continue;
      }
      // Now let's put the ship in place
      for(let i=0,L=positions.length;i<L;i++){
          battlefield[positions[i]] = 1;
      }
      return;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.