I'm creating a Python 2-Player battleship game and all is almost done, except for a few small issues. In the stage where the player places all the ships on the board--I'm having trouble with validation to check for duplicate ships. Here is my code for loop of ship placement:
while True:
for ship_name, ship_size in Game.SHIP_INFO:
# create ship instance
ship1 = Ship(player1, ship_name, ship_size)
# ask user for starting coordinate for ship in form "A1" and split into x,y variables
x, y = ship1.split_coordinates(ship_name,player1.player)
# ask user for ship's position --horizontal or vertical
direction = ship1.ask_ship_location()
# create all coordinates for ship based on size of ship and location
created_coords = ship1.create_ship_coordinates(x, y, ship_size,direction)
# check to see if ship already on board
for coord in created_coords:
if any(coord in ship for ship in grid1.play_one_board):
print("Sorry you already have a ship in that location")
continue
else:
break
# add coordinates to player's grid
grid1.play_one_board.append(created_coords)
# loop through coords for ship to print out on displayed grid
grid1.print_ship_coordinates(created_coords,direction)
it's this validation part here which i just tried to implement which is causing problems.
for coord in created_coords:
if any(coord in ship for ship in grid1.play_one_board):
print("Sorry you already have a ship in that location")
continue
else:
break
it is correctly identifying if an existing coordinate has allready been placed --BUT it is continuing on to the next two steps in the loop which print the board and then moving on to the next ship placement without asking again for a corrected version of the overlapped ship placement. Just need to figure out best way for the loop to go back to the beginning if there is an error in ship overlap. Any ideas? Thanks.
EDIT --changed code to this per suggestions but not getting any validation errors.
while True:
for ship_name, ship_size in Game.SHIP_INFO:
# create ship instance
ship1 = Ship(player1, ship_name, ship_size)
ship_exists = True
while ship_exists:
# ask user for starting coordinate for ship in form "A1" and split into x,y variables
x, y = ship1.split_coordinates(ship_name,player1.player)
# ask user for ship's position --horizontal or vertical
direction = ship1.ask_ship_location()
# create all coordinates for ship based on size of ship and location
created_coords = ship1.create_ship_coordinates(x, y, ship_size,direction)
# check to see if ship already on board
for coord in created_coords:
ship_exists = any(coord in ship for ship in grid1.play_board)
if ship_exists:
print("sorry")
else:
break
# function to check for overlapped ships
# ship1.check_overlap(created_coords, grid1.play_one_board)
# add coordinates to player's grid
grid1.play_one_board.append(created_coords)
# loop through coords for ship to print out on displayed grid
grid1.print_ship_coordinates(created_coords, direction)