1

我正在为一个班级制作这个程序,当它与这个文件一起运行时,网格的角落不会完全填充:

我无法弄清楚我的问题出在哪里。我的另一个问题是计算颜色“室”的数量,计数器不断给出大于正方形总数的数字。这是我遇到问题的两个功能。任何想法,将不胜感激。

def fill(cave, row, col, color):
    """Fill a chamber of the cave with water, starting
    at row, col. Water can spread in all four cardinal
    directions, but cannot penetrate stone.  No effect
    if row or col are not inside the cave.

    Attempting to pour water where there is already WATER or STONE
    has no effect.  Attempting to pour water outside the cavern has
    no effect.  Attempting to pour water in a cell containing AIR 
    not only places colored water in that cell, but also spreads it
    in all directions by causing it to be poured in the cell to the 
    left and right and above and below. 

    Args: 
        cave: A matrix (list of lists) representing the cavern. Each 
            cell in the cave may hold AIR, STONE, or WATER.
        row: Starting row of the grid cell where we pour water
        col: Starting column of the grid cell where we pour water
        color: color of the water we try to pour in.
    """
    if cave[row][col] == AIR : 
        cave[row][col] = WATER
        if row > len(cave) or col > len(cave):
            grid.fill_cell(row, col, color)
            return  
        if row < 0 or col < 0:
            grid.fill_cell(row, col, color)
            return
        grid.fill_cell(row, col, color)
        fill(cave, row+1, col, color)
        fill(cave, row-1, col, color)
        fill(cave, row, col+1, color)
        fill(cave, row, col-1, color)

def main():
    """Reads a cave from a specified configuration file, 
    displays it, and fills each chamber with a different color
    of water.

    Args (from command line):
        Cave description file, example, "cave.txt"

    Usage: python3 cavern.py cave.txt
    """
    desc = sys.argv[1]
    cave = read_cave(desc)
    dump_cave(cave)  ## May be useful for debugging
    display(cave)
    chambers = 0
    for row in range( len(cave)) :
        for col in range( len(cave[0])):
            print("Testing row ", row, "col", col, " Found: ", cave[row][col])
            color = grid.get_cur_color()
            fill(cave, row, col, color)
            if cave[row][col] == STONE:
                chambers += 1           
                grid.get_next_color()
    print(chambers, " chambers in cavern")
    input("Press enter to close display")
4

1 回答 1

0

我不是 100% 确定网格是什么,或者它被初始化为什么,但这看起来确实像一个边界问题:

if row > len(cave) or col > len(cave):
    grid.fill_cell(row, col, color)
    return  
if row < 0 or col < 0:
    grid.fill_cell(row, col, color)
    return

首先,即使超出范围,您也要填充单元格?为什么?即使这不会引发一些错误,显然不是,这不会给你比你的网格更多的正方形吗?

另外,好吧,我可以看到小于 0 的行或列不在网格中,但让我们看看你的 len(cave)...我还要假设 len(cave) 是沿的单元格数网格的一侧,因为洞穴是列表的列表。它会很好地映射。所以如果 len(cave) 是 8,我们就有一个 8 x 8 的网格。但是您使用的是基于 0 的二维数组。所以你的最大单元数应该是 len(cave)-1。

解决这两个问题应该可以解决这种情况。但除此之外,很酷的程序。请稍后添加更新以显示工作网格的外观:)

编辑:还有一件事我看错了。

if row > len(cave) or col > len(cave):

需要是:

if row > len(cave) or col > len(cave[0]):
于 2013-10-26T00:26:18.933 回答