Python验证检查弄乱了我的循环

2024-04-20 04:22:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个Python 2人战舰游戏,除了一些小问题之外,所有的工作都差不多完成了。在玩家将所有飞船放在棋盘上的阶段,我在验证是否有重复的飞船时遇到了问题。这是我的飞船放置循环代码:

 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

它正确地标识了现有坐标是否已经准备就绪——但是它继续执行循环中的下两个步骤,即打印电路板,然后继续执行下一个船舶放置,而不需要再次请求重叠船舶放置的更正版本。如果ship overlap中有错误,只需要找出使循环返回到开头的最佳方法。有什么想法吗?谢谢。你知道吗

编辑——根据建议对此代码进行了更改,但没有得到任何验证错误。你知道吗

 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)

Tags: tonameinboardforsizecreatecoords
2条回答

我相信你的问题在于:

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循环后的下两个步骤被执行。我会添加一个check变量,而不是继续:

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)

当您执行continue时,它只是/从“for coords in created \u 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

相关问题 更多 >