我正在为一个班级制作这个程序,当它与这个文件一起运行时,网格的角落不会完全填充:
我无法弄清楚我的问题出在哪里。我的另一个问题是计算颜色“室”的数量,计数器不断给出大于正方形总数的数字。这是我遇到问题的两个功能。任何想法,将不胜感激。
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")