title: What is the best way to generate random ships in an array in Python?
body: <p>Recently, I've been working on a battleship game for my CS2 class. The focus of this project is to create a board game using arrays, and I decided to create a battleship. I have most of the code, but I cannot figure out how to get ships to get randomly generated on a 10x10 array without the ships being...</p>
  <p>A. The wrong length
  B. Looping around the array</p>
  <p>Below is the function I currently have.</p>
  <pre><code>def createShips(board):
      shipLen = [5,4,3,3,2]
      shipAvalible = 5
      directionposibilities = [&quot;vertical&quot;, &quot;horizontal&quot;]
      j = 0
     
  
      
      for i in range(shipAvalible):
          boatMade = False
  
          #REGULAR VAR STATMENTS
          direction = random.choice(directionposibilities)   
          col = randint(0,9)
          row = randint(0,9) 
  
          
  
          while boatMade == False:
              
     
              if direction == &quot;vertical&quot;:
                  buildCount = 0
                  if col + int(shipLen[i]) &lt;= 11: 
                      colission = False
                      for i in range(0, int(shipLen[i])):
                          buildCount += 1
                          if board[int(row-i)][int(col)-1] == &quot;X&quot;: 
                              if colission:
                                  pass
                              else:
                                  colission = True
                      if colission:
                          col = randint(0,9)
                          row = randint(0,9)
                      else:
                         
                          for j in range(buildCount):
                              board[int(row-j)][int(col)-1] = &quot;X&quot;
                              
                          boatMade = True
                  else:
                      col = randint(0,9)
                      row = randint(0,9)
              if direction == &quot;horizontal&quot;:
                  if col + int(shipLen[i]) &lt;= 10: 
                      colission = False
                      buildCount = 0
                      for i in range(0, int(shipLen[i])):
                          buildCount += 1
                          if board[int(row)][int(col)+i-1] == &quot;X&quot;:
                              if colission:
                                  pass
                              else:
                                  colission = True
                      if colission:
                          col = randint(0,9)
                          row = randint(0,9)
                      else:
                         
                          for j in range(buildCount):
                              board[int(row)][int(col)+j-1] = &quot;X&quot;
                          boatMade = True
                  else:
                      col = randint(0,9)
                      row = randint(0,9)
          shipAvalible = shipAvalible - 1
      
      return(board)
  
  board = [[&quot;■&quot;] * 10 for x in range(10)]
  
  print(createShips(board))
  </code></pre>
  <p>If you have any idea why this may not work please let me know!</p>
  <p>P.S. I am using another function that prints the array nicely, if you would like that for convenience, it is seen below:</p>
  <pre><code>def showBoard(board):
      print(&quot;   A  B  C  D  E  F  G  H  I  J&quot;)
      print(&quot;  ------------------------------&quot;)
  
      rownumb = 1
      for r in board:
          if rownumb == 10:
              space = &quot;&quot;
          else:
              space = &quot; &quot;
          print(&quot;%d|%s|&quot; % (rownumb, space + &quot;|&quot;.join(r)))
          rownumb += 1
  </code></pre>