f | def turtle(coord, direction): | f | def turtle(coord, direction): |
| x, y = coord | | x, y = coord |
n | moves = {0: (1, 0), 1: (0, 1), 2: (-1, 0), 3: (0, -1)} | n | directions = {0: (1, 0), 1: (0, 1), 2: (-1, 0), 3: (0, -1)} |
| while True: | | while True: |
| command = (yield (x, y)) | | command = (yield (x, y)) |
| if command == 'f': | | if command == 'f': |
t | dx, dy = moves[direction] | t | dx, dy = directions[direction] |
| x += dx | | x += dx |
| y += dy | | y += dy |
| elif command == 'l': | | elif command == 'l': |
| direction = (direction + 1) % 4 | | direction = (direction + 1) % 4 |
| elif command == 'r': | | elif command == 'r': |
| direction = (direction - 1) % 4 | | direction = (direction - 1) % 4 |