Adding multiple ships


I’m currently trying to expand the game to add multiple ships in the game, I tried looking up different ways to compare lists but wasn’t able to get the program to work properly. Here’s what I tried so far, create a list for user guess and list for random ships. If user guess matches the random ship - it will be removed from the list/display you won message.

With my code, it only works if there is one item in the list, making the 2 lists identical. If there are 2 or more items in the list, it doesn’t. What would be the best way to compare the items in the lists rather than the lists themselves?

ships = []
ships_guess = []

number_of_ships = int(input("How many ships would you like to place: "))
for placement in range(number_of_ships):
ships.append([random_row(board), random_col(board)])
if ships == ships:
range = + 1

def user_guess():
guess_row = int(input("Select Row: "))
guess_col = int(input("Select Column: "))
rowcol = [guess_row, guess_col]


Will need to track down the topic but this has come up before. The way I handled making sure the ships don’t overlap was to use a serialization of the board as a random range. The odds of generating the same integer twice in sequence is very remote so just generate as many unique numbers as you need then convert them to cartesian coordinates. I seem to remember writing the coords as tuples so they cannot be manipulated.

The player guess can be written as a tuple, also, then compared directly as opposed to comparing each axis separately.

int(24/5) => 4  =>  ROW
24 % 5    => 4  =>  COL

Update: Found the topic…

Multiple Ships Troubleshooting


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