19/19 multiple ships


#1

Hi! I'm trying to create my own battleship but i don't know where to start. I started using the codes from previous excersises and try to add more functions in it.

so I made multiple ships but how do you make them in different sizes and how do you make sure that the ships won't stack on each other?
This is my code so far.

board = []


for x in range(8):
    board.append(["O"] * 8)

def print_board(board):
    for row in board:
        print " ".join(row)

print "Let's play Battleship!"
print_board(board)

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

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

def random_row_two(board):
    return randint(0, len(board) - 2)

def random_col_two(board):
    return randint(0, len(board[0]) - 2)

ship_row_one = random_row_one(board)
ship_col_one = random_col_one(board)

ship_row_two = random_row_two(board)
ship_col_two = random_col_two(board)

#2

Are you sure this work? I don't think there is much difference to get random number between len()-1 and len()-2 though....
I code it like this, but I am not sure if it work either :pensive: ...
def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row=[]
ship_col=[]
n=4
for i in range(n):
ship_row.append(random_row(board))
ship_col.append(random_col(board))
for j in range(i):
if (ship_col[i]==ship_col[j]and ship_row[i]==ship_row[j]):
ship_col.remove(ship_col[i])
ship_row.remove(ship_row[i])
n=n+1