Code

One Turn at a Time

Real games split their work into three jobs that repeat:

  1. Read a command.
  2. Update the state.
  3. Draw the picture.

We keep all the state in one dict, and give each job its own function:

state = {"world": [".C.", "#.."],
         "pos": (0, 0), "backpack": []}

for command in commands:
    message = step(state, command)  # update
    print(message)
    for line in draw(state):        # draw
        print(line)

step(state, command) changes the state and returns a message. It never prints. Because the dict passed in is the same dict, state["pos"] = new sticks after step returns.

Why keep them apart? You can test step without a screen, and later swap the text drawing for pixel art without touching the game rules.

Assignment

Complete step(state, command). First clean the command with .strip().lower(). Then:

  1. A direction in MOVES: if can_enter the new square, move there and return f"Pip walks {cmd}.". If not, return "Something blocks the way."
  2. "take": if the tile under Pip is in ITEMS, append its name, set that tile to "." and return f"Pip picks up the {name}." Otherwise return "Nothing to take here."
  3. Anything else: return f"Pip doesn't understand '{cmd}'."