I take intro comp sci, and the current project is recursive. The program reads a text file with the number of rows and the number of columns - and I The program creates a 2D list which is the grid used for the program. Then the user is prompted to indicate a point in the grid (row, column), and if the user's point is - , the point changes to @ , and then I have to recursively fill the first available space with @ , checking the space at the top, left, right, bottom and filling in the first available space. My recursive function:
def fillWithAt(grid, the_row, the_col): if grid[the_row][the_col] == '-': grid[the_row][the_col] = '@' printFile(grid) if the_row != 0: if grid[the_row - 1][the_col] == '-': fillWithAt(grid, the_row - 1, the_col) if the_col != 0: if grid[the_row][the_col - 1] == '-': fillWithAt(grid, the_row, the_col - 1) if the_col != (len(grid[the_row]) - 1): if grid[the_row][the_col + 1] == '-': fillWithAt(grid, the_row, the_col + 1) if the_row != (len(grid) - 1): if grid[the_row + 1][the_col] == '-': fillWithAt(grid, the_row + 1, the_col) else: printFile(grid)
grid is a 2D list, the_row is the user row, and the_col is the user column. The program works almost perfectly, but it breaks when it falls into the last two columns. I can’t figure out how to copy the output to this because every time I try, it is blank and doesn't mean anything, so I’ll try to explain.
The program runs fine until I hit the last two lines. The program finds an empty space on the right and for some reason takes the following two spaces on the right, and not just one space. After that, the program incorrectly reads spaces around the current point. At the end of the day, the program still performs the task, but I am curious why the error occurs.
I am not looking for any answers, but advice. If anyone can understand why this could happen, I will be grateful for any information. I also like feedback / constructive criticism.
EDIT: Here are a few steps in a recursive process. The current space first checks it over the neighbor for '-'. If a dash is found, then the spaces are occupied, if the left neighbor is not marked, then the right one, and finally lower.
- - - - - II - - - - - - - - - - - - I - - II - - - - - - - - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - - @ - - - - II - - - - - - - - - - - - I - - II - - - - - - - - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - - @ @ - - - II - - - - - - - - - - - - I - - II - - - - - - - - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - - @ @ @ - - II - - - - - - - - - - - - I - - II - - - - - - - - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - - @ @ @ @ - II - - - - - - - - - - - - I - - II - - - - - - - - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - - @ @ @ @ @ II - - - - - - - - - - - - I - - II - - - - - - - - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - - @ @ @ @ @ II - - - - - - - - - - - @ I - - II - - - - - - - - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - - @ @ @ @ @ II - - - - - - - - - - @ @ I - - II - - - - - - - - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - - @ @ @ @ @ II - - - - - - - - - @ @ @ I - - II - - - - - - - - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - - @ @ @ @ @ II - - - - - - - - @ @ @ @ I - - II - - - - - - - - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - - @ @ @ @ @ II - - - - - - - - @ @ @ @ I - - II - - - - - - @ - - - I - - - - IIII - - - - - I - - - - - - - - III - - I - - - - - - - - - - - - - - - I - - - - - - - - III - - - I - - III - - - I - - - - - I - - I - - I - I - - - - - - IIII - - - I - - - - - - - - - - - - - - - - - - -
The program works as expected up to this point:
@ @ @ @ @ II - - - - - - - - @ @ @ @ I - - II - - - - - - @ @ @ @ I - - - - IIII - - @ @ @ I - - - - - - - - III @ @ I - - - - - - - - - - - - @ @ @ I - - - - - - - - III @ @ @ I - - III - - - I - - @ @ @ I - - I @ @ I - I @ - - @ @ @ IIII @ @ @ I @ @ - - @ @ @ @ @ @ @ @ @ @ @ @ - - - @ @ @ @ @ II - - - - - - - - @ @ @ @ I - - II - - - - - - @ @ @ @ I - - - - IIII - - @ @ @ I - - - - - - - - III @ @ I - - - - - - - - - - - - @ @ @ I - - - - - - - - III @ @ @ I - - III - - - I - - @ @ @ I - - I @ @ I - I @ @ @ @ @ @ IIII @ @ @ I @ @ - - @ @ @ @ @ @ @ @ @ @ @ @ - - - @ @ @ @ @ II - - - - - - - - @ @ @ @ I - - II - - - - - - @ @ @ @ I - - - - IIII - - @ @ @ I - - - - - - - - III @ @ I - - - - - - - - - - - - @ @ @ I - - - - - - - - III @ @ @ I - - III - - - I @ @ @ @ @ I - - I @ @ I - I @ @ @ @ @ @ IIII @ @ @ I @ @ - - @ @ @ @ @ @ @ @ @ @ @ @ - - -
For some reason, it occupies two spaces on the right, not one, and then on the next move it occupies both spaces over two previously occupied spaces.
My grid code is:
def get2DList(fo): full_list = [] for line in fo: full_list.append(list(line.strip())) fo.close() return full_list
fo is a file
EDIT: I solved the problem thanks to @Blckknght giving me an idea that my printFile () function was the reason. Now it works great. Thanks to everyone who helped!