【问题标题】:Python Validation Check Messing Up My LoopPython 验证检查搞砸了我的循环
【发布时间】:2017-08-11 20:58:45
【问题描述】:

我正在创建一个 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

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

EDIT --根据建议将代码更改为此,但未收到任何验证错误。

 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)

【问题讨论】:

    标签: python


    【解决方案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
    

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

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

    最终答案,基于 cmets

    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)
    

    【讨论】:

    • 谢谢——这完美地解决了打印错误网格的问题——但它继续到新船而不是再次要求玩家重新输入船坐标--知道如何解决这个问题吗?
    • @JohnRogerson 添加到答案。看看这是否有效 - 关键是不断询问,直到提供一组好的坐标。要做到这一点,与其事后检查提供的坐标是否好,不如问他们好像不是,一旦他们好就打破。 AKA - 假设它们会变坏,然后使用 while 循环直到它们变好。
    • 嗯,这很有意义——我尝试了代码——我编辑了 OP 以显示更新的代码——(注意我必须添加一个循环才能创建那个“坐标”变量...但不幸的是,它现在无法识别重叠的船只..UGH。
    • 实际上我在 grid1.play_one_board 中有一个错误——它实际上是在发现错误但仍在打印错误船并继续下一艘船...
    • 啊,好吧——我现在看到了……而且似乎工作得很好。谢谢你一直陪着我!非常感谢。
    【解决方案2】:

    当您执行 continue 时,它只是从“for coord 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      相关资源
      最近更新 更多