Python验证检查弄乱我的循环

时间:2017-03-20 14:19:38

标签: python

我正在创建一个Python 2-player战舰游戏,除了一些小问题外,一切都已基本完成。在玩家将所有船只放置在棋盘上的阶段 - 我在检查重复船只时遇到问题。这是我的船舶放置循环代码:

 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)

这是这个验证部分,我刚刚尝试实现,这会导致问题。

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

它正确识别是否已经放置了现有坐标 - 但它继续进行循环中的下两个步骤,打印板然后继续下一个船只放置,而不再要求更正版本重叠的船舶安置。如果船舶重叠出现错误,只需要找出循环回到起点的最佳方法。有任何想法吗?谢谢。

编辑 - 根据建议改变了代码但没有得到任何验证错误。

 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)

2 个答案:

答案 0 :(得分:1)

我相信你的问题在这里:

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

如果在现有位置找到船只,您希望继续询问新坐标。在这种情况下,您的continue实际继续内循环,而不是外循环。

这意味着你的循环会检查所有坐标,并在找到没有现有船只的情况下断开,导致执行for循环后的下两步。我会添加一个检查变量,而不仅仅是继续:

ship_exists = False
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")
        ship_exists = True
        break
if ship_exists:
    continue

这将确保,如果船舶已经存在,则重新执行外部循环的第一步。

=============

最终答案,基于评论

def _are_valid_coordinates(created_coords, play_one_board):
    for ship in play_one_board:
        for coord in created_coords:
            if created_coords in ship:
                return False
    return True


while True:
    for ship_name, ship_size in Game.SHIP_INFO:
        # create ship instance
        ship1 = Ship(player1, ship_name, ship_size)

        valid_coords = False
        # ask user for starting coordinate for ship in form "A1" and split into x,y variables
        while not valid_coords:
            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
            valid_coords = _are_valid_coordinates(created_coords, ship1.play_one_board)
            if not valid_coords:
                print("Sorry you already have a ship in that location")
            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)

答案 1 :(得分:0)

当您执行继续时,它只是/继续来自" forcoord in created_coords "内循环。

要继续外循环,您可以根据标志执行此操作。有点像:

already_had_ship = False
for coord in created_coords:
    if any(coord in ship for ship in grid1.play_one_board):
        already_had_ship = True
        print("Sorry you already have a ship in that location")
        break

if already_had_ship:
    continue