Делаю эту программу для класса и, когда он работает с этим файлом угол сетки не будет полностью заполнить:Подсчет и ошибки Заполнение на сетке Python
Я могу» t выяснить, где моя проблема. Моя другая проблема заключается в подсчете количества «камер» цвета, счетчик продолжает давать числа, большие, чем общее количество квадратов. Вот две функции, с которыми у меня возникают проблемы. Любые идеи были бы хорошы.
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")
Спасибо за попытку помочь! Честно говоря, я был просто расстроен и добавил его в расстройство, и он заполнил желтые, синие и красные квадраты, которые касаются стороны окна. Таким образом, все самые правые кубы остаются пустыми без заполнения теста максимальной привязки. Изменение луны (пещеры) на len (пещера) - 1 также смещает кубы и делает цвета случайными, когда красный и желтый снова находятся на самой правой стороне. – user2921804
Добавлено еще одно предложение. Вы заметили, что ваша сетка не квадратная. – sdanzig