Python 移动函数的问题:处理棋盘外开始的玩家

Trouble with Python move function: handling player starting outside the board

提问人:sKenny1 提问时间:7/8/2023 更新时间:7/8/2023 访问量:22

问:

我正在开发一个 Python 程序,其中有一个移动函数,可以在游戏板上移动玩家棋子。该函数应处理玩家在棋盘外开始的情况,由玩家位置 (-1, -1) 表示。但是,我无法让它正常工作

下面是 move 函数的代码:

import copy

def find_x(board, goal):
    row_index = -1
    col_index = -1

    for row in range(len(board)):
        for col in range(len(board[row])):
            if board[row][col] == goal:
                row_index = row
                col_index = col
                return (row_index, col_index)

    return (row_index, col_index)

def move(orig_board, curr_board, piece, direction):
    print("Entered move function")
    print("Direction:", direction)

    # Print curr_board to verify player piece representation
    print("Current Board:")
    for row in curr_board:
        print(row)

    if direction != "R" and direction != "D":
        print("Invalid direction:", direction)
        return (None, None)  # Invalid direction, return (None, None)

    # Find the player piece
    player_position = find_x(curr_board, piece)
    print("Player position:", player_position)
    if player_position == (-1, -1):
        print("Player piece not found")
        return (None, None)  # Player piece not found, return (None, None)

    curr_row, curr_col = player_position
    print("Current Position:", (curr_row, curr_col))

    if direction == "R":
        new_row = curr_row
        new_col = curr_col + 1

        # Adjust for moving to the next row if at the end of a row
        if new_col >= len(curr_board[curr_row]):
            new_row += 1
            new_col = 0

            # Check for out-of-bounds after adjusting
            if new_row >= len(curr_board):
                print("Out-of-bounds move")
                return (None, None)  # Out-of-bounds move, return (None, None)

    elif direction == "D":
        new_row = curr_row + 1
        new_col = curr_col

        # Check for out-of-bounds
        if new_row >= len(curr_board):
            print("Out-of-bounds move")
            return (None, None)  # Out-of-bounds move, return (None, None)

    print("New Position:", (new_row, new_col))

    # Store the room name before moving
    room_name = curr_board[new_row][new_col]

    # Perform the move
    new_board = copy.deepcopy(curr_board)  # Create a deepcopy of the current board to avoid modifying ORIG_BOARD
    new_board[curr_row][curr_col] = ""  # Update the previous position with an empty room
    new_board[new_row][new_col] = piece  # Move the player piece to the new position

    print("Updated Board:")
    for row in new_board:
        print(row)

    return (room_name, new_board)

# Example usage
orig_board = [['dining', 'living', 'garden'],
              ['lounge', 'game', 'gym'],
              ['spa', 'bedroom', 'office']]
curr_board = [['dining', 'living', 'garden'],
              ['lounge', 'game', 'gym'],
              ['spa', 'bedroom', 'office']]
piece = "X"
direction = "R"
result = move(orig_board, curr_board, piece, direction)
print("Result:", result)

move([['dining', 'living', 'garden'], ['lounge', 'game', 'gym'], ['spa', 'bedroom', 'office']],
     [['dining', 'living', 'garden'], ['lounge', 'game', 'gym'], ['spa', 'bedroom', 'office']],
     "X", "R")

这就是我想要的输出:(“dining”,[[“X”,“living”,“garden”],[“lounge”,“game”,“gym”],[“spa”,“bedroom”,“office”]])

Python 嵌套列表

评论

0赞 Michael Butscher 7/8/2023
用作板外的位置。设置在 的第一行。(0, -1)row_index = 0find_x
0赞 sKenny1 7/8/2023
你是最好的。谢谢

答: 暂无答案