1

我正在用 Python 构建 Battleships 游戏。我有一个列表,我正在尝试在 Python 中构建一个验证工具,以捕获超出列表 10x10 范围的用户输入。

这是代码:

from random import randint

player = "User"
board = []
board_size = 10

ships = {"Aircraft Carrier":5,
            "Battleship":4,
            "Submarine":3,
            "Destroyer":3,
            "Patrol Boat":2}

def print_board(player, board): # to print joined board
    print("Here is " + player + "'s board")
    for row in board:
        print(" ".join(row))

def switch_user(player): # to switch users
    if player == "User":
        player = "Computer"
    elif player == "Computer":
        player = "User"
    else:
        print("Error with user switching")

for x in range(0, board_size): # to create a board
    board.append(["O"] * board_size)

print_board(player,board)

def random_row(board): # generate random row
    return randint(0, len(board) - 1)

def random_col(board): # generate random column
    return randint(0, len(board[0]) - 1)

def user_places_ships(board, ships): # user choses to place its ships by providing starting co-ordinate and direction.
    for ship in ships:
        valid = False
        while(not valid):
            user_input_coordinates = input("Please enter row & column number for your " + str(ship) + ", which is " + str(ships[ship]) + "-cells long (row, column).")
            ship_row, ship_col = user_input_coordinates.split(",")
            ship_row = int(ship_row)
            ship_col = int(ship_col)
            user_input_dir = input("Please enter direction for your " + str(ship) + ", which is " + str(ships[ship]) + "-cells long (h for horizontal or v for vertical).")
            valid = validate_coordinates(board, ships[ship], ship_row, ship_col, user_input_dir)
            if not valid:
                print("The ship coordinates either outside of" , board_size, "X" , board_size, "range, overlap with or too close to another ship.")
        place_ship(board, ships[ship], ship_row, ship_col, user_input_dir)
    print("You have finished placing all your ships.")

def validate_coordinates(board, ship_len, row, col, dir): # validates if the co-ordinates entered by a player are within the board and don't overlap with other ships
    if dir == "h" or dir == "H":
        for x in range(ship_len):
            if row-1 > board_size or col-1+x > board_size:
                return False
            elif row-1 < 0 or col-1+x < 0:
                return False
            elif board[row-1][col-1+x] == "S":
                return False
    elif dir == "v" or dir == "V":
        for x in range(ship_len):
            if row-1+x > board_size or col-1 > board_size:
                return False
            elif row-1+x < 0 or col-1 < 0:
                return False
            elif board[row-1+x][col-1] == "S":
                return False
    return True

def place_ship(board, ship_len, row, col, dir): # to actually place ships and mark them as "S"
    if dir == "h" or dir == "H":
        for x in range(ship_len):
            board[row-1][col-1+x] = "S"
    elif dir == "v" or dir == "V":
        for x in range(ship_len):
            board[row-1+x][col-1] = "S"
    else:
        print("Error with direction.")
    print_board(player,board)

user_places_ships(board,ships)

如果用户输入“10,10”作为船舶坐标,输入“h”作为水平方向,则 Python 会生成以下错误消息:

Traceback (most recent call last):   File "C:/Users/Elchin's PC/Downloads/battleships game.py", line 85, in <module>
    user_places_ships(board,ships)   File "C:/Users/Elchin's PC/Downloads/battleships game.py", line 49, in user_places_ships
    valid = validate_coordinates(board, ships[ship], ship_row, ship_col, user_input_dir)   File "C:/Users/Elchin's PC/Downloads/battleships game.py", line 62, in validate_coordinates
    elif board[row-1][col-1+x] == "S": IndexError: list index out of range

我知道错误在这一行:

elif board[row-1][col-1+x] == "S":
    return False

但我不知道如何解决它。你能帮我找出解决方案吗?

4

1 回答 1

1

如果列表的长度为n ,您可以访问从 0 到n-1(包括两者)的索引。

但是,您的if陈述会检查:

if row-1+x > board_size or col-1 > board_size:  # greater than n
    return False
elif row-1+x < 0 or col-1 < 0:                  # less than 0
    return False
elif board[row-1+x][col-1] == "S":
    return False

因此,如果我们到达最后elif一部分,我们可以保证索引是0 < i <= n. 但这些应该是0 < i < n

因此,您应该将第一if条语句更改为:

if row-1+x >= board_size or col-1 >= board_size:  # greater than or equal n
    return False
elif row-1+x < 0 or col-1 < 0:                    # less than 0
    return False
elif board[row-1+x][col-1] == "S":
    return False

您可以通过编写使代码更优雅:

if not (0 < row-1+x < board_size and 0 < col-1 < board_size) or \
        board[row-1+x][col-1] == "S":
    return False
于 2017-06-29T21:09:13.543 回答