-1

I'm fairly new to Python and I have to do this exercise where I need to simulate a cat walking on a few lines. If the cat finds a treat ( * ), he eats it, and the position is replaced with floor ( _ ). If the cat finds a hole ( . ), he will fall to next line and keep walking in the same direction. This is the only way for the cat to move to other line. If the cat reaches the end of the line, he changes direction, and walk the line again eating the treats that he did not eat the first time. If the cat doesn't find another hole and reaches the other end of the line, the loop needs to stop. The other way to stop the loop is if the cat finds a hole in the last line. The output should be the amount of treats the cat found on the way.

I'm having a lot of trouble trying to make the cat stop only if has reached both ends of the line. Please help!

The images have examples of the input (black) and how the cat should walk (red).

examples

platforms = []
n = int(input())

for _ in range(n):
    platforms.append(list(input()))

# Path simulation
treats = 0
row = 0
column = 0
direction = 1  # 1 represents the right direction and -1 represents the left direction

while True:
    if row >= n:
        break

    if column < 0 or column >= len(platforms[row]):
        column -= direction
        direction *= -1  # changes direction at the edges of the rows

        # Check if the cat reached both ends of the line without finding any hole
        if (column == 0 and platforms[row][column] != '.') or (column == len(platforms[row]) and platforms[row][column] != '.'):
            break

    elif platforms[row][column] == '_':
        column += direction

    elif platforms[row][column] == '*':
        treats += 1
        platforms[row][column] = '_'  # replaces the treat with floor ( _ )
        column += direction

    elif platforms[row][column] == '.':
        if row == n:
            break
        if column == 0 or column == len(platforms[row]):
            row += 1  # falls to the lower level in the same column
            column += direction  # moves to the next column in the same row
        else:
            row += 1  # falls to the lower level in the same column
            column += direction  # moves to the next column in the same row

print(treats)
Ake
  • 805
  • 1
  • 3
  • 14
Alegra
  • 1
  • 1

1 Answers1

0

You need a flag to remember that you had already hit the other edge. The following is a trivial implementation of the idea:

# Initialize flag
hit_one_edge = False

while True:

    # ...

    if column < 0 or column >= len(platforms[row]):
        if hit_one_edge:
            print("Hit both edges, nowhere to go. Game over.")
            break
        else:
            # This is the first time we hit an edge, keep going
            hit_one_edge = True

        column -= direction
        direction *= -1  # changes direction at the edges of the rows

    # ...

    elif platforms[row][column] == '.':
        # Reset edge flag as we are on a new platform:
        hit_one_edge = False

        if row == n:
            break

    # ...
Selcuk
  • 57,004
  • 12
  • 102
  • 110