Home > Software design >  changing values in array
changing values in array

Time:12-03

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]
                ]`

CodePudding user response:

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.

CodePudding user response:

To create a random arrangement of 1s and 0s in your array, you can use JavaScript's Math.random() function. This function returns a random number between 0 and 1. You can use this to randomly assign either a 1 or a 0 to each element in the array.

Here's an example of how you might do this:

let battlefield = []

// Create a 10x10 grid of zeros
for (let i = 0; i < 10; i  ) {
  let row = []
  for (let j = 0; j < 10; j  ) {
    row.push(0)
  }
  battlefield.push(row)
}

// Randomly assign either a 1 or a 0 to each element in the array
for (let i = 0; i < 10; i  ) {
  for (let j = 0; j < 10; j  ) {
    battlefield[i][j] = Math.random() < 0.5 ? 1 : 0
  }
}

To ensure that your battleships are arranged in a row or column, you can use a similar approach, but instead of assigning a random value to each element in the array, you can loop through the array and manually place the battleships in the desired locations.

Here's an example of how you might do this:

let battlefield = []

// Create a 10x10 grid of zeros
for (let i = 0; i < 10; i  ) {
  let row = []
  for (let j = 0; j < 10; j  ) {
    row.push(0)
  }
  battlefield.push(row)
}

// Place a 4-unit battleship in the top-left corner
for (let i = 0; i < 4; i  ) {
  battlefield[0][i] = 1
}

// Place a 3-unit battleship in the middle of the grid
for (let i = 0; i < 3; i  ) {
  battlefield[5][5 i] = 1
}

// Place a 2-unit battleship in the bottom-right corner
for (let i = 0; i < 2; i  ) {
  battlefield[9][8 i] = 1
}

// Place a 1-unit battleship in the middle of the grid
battlefield[5][5] = 1

CodePudding user response:

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;
  }
}

  • Related