-3

I'm playing the game Battle Ship, the game board consists of 10 x 10 squares in the form of arrays:

const board = [
      [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,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,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,0,0,0],
      [0.0.0.0,0,0,0,0,0,0]
    ]

I want to fill the board with boats, in the form of numbers 2, which means the ship is alive and have already changed the game for example by -1 to understand that the ship is hit. I was able to fill the game board using random positions during initialization, but the problem is that the ships touch each other, that you can think of that the positions of the ships at the start are not in contact with each other. What would be between them was the distance of at least one empty cage.

Sebastian Lagua
  • 380
  • 1
  • 6
  • 17
  • Please take help from this https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range to get random number between a range. You can pass min index and max index of array to get random index number, you can fill it by random index or if you have to fill all positions loop through all index and initialize values with random number between -1 and 2 using this function. – Pardeep Dhingra Jul 20 '18 at 04:22
  • can you post your try please?, that could be very usefull – mrpepo877 Jul 20 '18 at 04:36
  • How many boats do you want to put on board on start? – Chatar Singh Jul 20 '18 at 05:57
  • @ChatarSIngh One L ship one I ship two dot ship – Sebastian Lagua Jul 20 '18 at 11:25

1 Answers1

0

This is sample code. Maybe this will help you.

const board = [
      [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,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,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,0,0,0],
      [0,0,0,0,0,0,0,0,0,0]
    ]

    xMax=9;
    yMax=9   

    function placeBoat(numberOfBoat, fillWith){
        boatPlaced=0;
        while(boatPlaced < numberOfBoat){
            var randX=Math.floor(Math.random() * 10);
            var randY=Math.floor(Math.random() * 10);
            ok=true;
            if(randX!=0 && randY!=0){
              if(board[randX-1][randY]!=0){
                ok=false;
              }
              if(board[randX-1][randY-1]!=0){
                ok=false;
              }
              if(board[randX][randY-1]!=0){
                ok=false;
              }
            }
            else if(randX!=0){
              if(board[randX-1][randY]!=0){
                ok=false;
              }
            }
            else{
              if(board[randX][randY-1]!=0){
                ok=false;
              }
            }

            if(randX!=xMax && randY!=yMax){
              if(board[randX+1][randY]!=0){
                ok=false;
              }
              if(board[randX+1][randY+1]!=0){
                ok=false;
              }
              if(board[randX][randY+1]!=0){
                ok=false;
              }
            }
            else if(randY!=yMax){
               if(board[randX][randY+1]!=0){
                ok=false;
              }
            }
            else{
               if(board[randX][randY+1]!=0){
                ok=false;
              }
            }
            if(randX!=0 && randY!=yMax){
              if(board[randX-1][randY+1]!=0){
                ok=false;
              }
            }
            if(randX!=xMax && randY!=0){
              if(board[randX+1][randY-1]!=0){
                ok=false;
              }
            }
            if(board[randX][randY]!=0){
              ok=false;
            }


            if(ok){
              board[randX][randY]=fillWith;
              boatPlaced++;
            }
        }
    }  
    placeBoat(1,'L');
    placeBoat(1,'I');
    placeBoat(2,'.');
Chatar Singh
  • 943
  • 9
  • 13