Questions about battle ship


#1
def random_row(board):
  return randint(0, len(board) - 1)

def random_col(board):
  return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)

I do not understand these two lines of codes here, we are putting a list as input and output a random number from 0 to 4. My question is why do we put a list as input if we just want a random number. and this is a board with the size 5x5, but the location of the ship set between 0 and 4,why?


#2

Hey man, the board is a constraint, if you didn’t take the length of your board and returned a random number you could get a number outside of your board which is not in the constraints of the game. (Off the board)

The reason the ship set is between 0 and 4 on a 5x5 board is because computers and most programming languages start counting from 0 to be more efficient. If it was a 6x6 board you would go from 0-5.
0 -> 1
1 -> 2
2 -> 3
3 -> 4
4 -> 5


#3

Thanks bro :sunglasses:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.