Moving Objects
Learning Objectives
- You can move a block by grabbing, pushing, pulling, and releasing it.
- You can trace the positions of both the robot and its attached block.
- You can organize an object movement with loops and helper functions.
- You can release and reposition the robot to change a block’s movement direction.
The grid robot has a simple gripper. It can attach to one block immediately ahead of it. While attached, moving forward pushes the block and moving backward pulls it. The existing movement methods now affect two objects, so we must trace both positions.
Attaching to a Block
This chapter uses a five-column, four-row board with no interior walls. B marks the block. The robot starts at column 2, row 2, facing east. The goal is to place the block at column 5, row 2.
1 2 3 4 5row 4 . . . . .row 3 . . . . .row 2 . R B . Grow 1 . . . . .Run main.py below. Its program grabs the adjacent block, moves forward twice, and releases it. Use the controls described in Running and Reading the Preview to step through the recording. Before each movement, predict both destination cells.
grab() attaches to the block in the cell ahead. It changes neither position. release() detaches the block, leaving it in its current cell. Both operations change the robot’s holding state.
| After this action | Robot cell | Block cell | Holding |
|---|---|---|---|
| Initial state | (2, 2) | (3, 2) | False |
grab() | (2, 2) | (3, 2) | True |
First forward() | (3, 2) | (4, 2) | True |
Second forward() | (4, 2) | (5, 2) | True |
release() | (4, 2) | (5, 2) | False |
The block reaches the goal while the robot remains one cell behind it. The goal belongs to the block, so moving the robot alone to column 5 would not complete this task. Check the block position and Holding in the preview, not just the robot’s arrow.
Pulling a Block
Consider the same initial positions, with the block’s destination at column 2, row 2. Replace only program in the editor to try a pull:
def program(robot): robot.grab() robot.backward() robot.release()The backward movement puts the robot at column 1, row 2 and the block at column 2, row 2. The robot still faces east. The preview’s original goal marker stays at column 5; this experiment demonstrates pulling rather than completing that board.
The diagram below compares the two allowed movements while attached. The block stays immediately ahead of the robot in both cases.
Pushing and pulling move the robot and its attached block together.
After release(), a later backward() moves only the robot. A later forward() cannot enter the detached block’s cell: the robot must attach again before pushing it. Trace the effect of releasing between movements in the next question.
Robot and Block Positions
0 / 5 points
An open board has six columns and four rows. Coordinates are (column, row), with columns increasing right and rows increasing up. The robot starts at (4, 3), facing west. A block is immediately ahead at (3, 3), initially unattached.
The supplied runner calls:
def program(robot): robot.grab() robot.backward() robot.release() robot.backward()What are the final robot cell, block cell, and attachment state?
Movement Functions and Operation Order
The repeated push can use a movement helper. Replace the earlier functions in the editor with these helpers and caller:
def forward_steps(robot, count): for step in range(count): robot.forward()
def push_steps(robot, count): robot.grab() forward_steps(robot, count) robot.release()
def program(robot): push_steps(robot, 2)This version solves the original board. push_steps coordinates attachment, movement, and release. forward_steps performs the movement calls without deciding whether the robot should be attached. Both helpers operate on the same supplied object.
Releasing before the movement would leave the block behind and make the next forward step invalid. Grabbing after the first forward step would also be too late: that first step would already attempt to enter an occupied cell.
The next helper pulls instead of pushing. Its count parameter controls the repetitions. Even with zero movements, it must perform the promised attachment and release.
Pulling a Block
0 / 25 points
Write pull_steps(robot, count). The robot starts detached, facing a block in the adjacent cell. Grab the block, move backward count times, and release it. Do not turn or return a value.
count is a nonnegative integer, and there is enough room for the requested movements. With zero movements, still grab and release. Keep the supplied preview code unchanged.
Releasing and Repositioning
Turning while attached is rejected. To move a block in a new direction, first release it. The robot can then walk around it, face it from another side, and grab again. The block stays at its release cell while the detached robot moves.
For example, a robot immediately west of a block faces east. To push that block north, it needs to reach the cell immediately south of the block and face north. Plan that detached route before attaching again. The block remains an occupied cell that the robot must avoid.
Arrange a supplied route that changes the side of approach without moving the block.
Releasing and Repositioning
0 / 5 points
A five-column, four-row board has no interior walls. Coordinates are (column, row); rows increase upward. The robot starts at (2, 2), facing east, already attached to the block at (3, 2).
Reposition the robot to (3, 3), facing south and attached again, while leaving the block at (3, 2) throughout.
Arrange the four blocks as calls made by the supplied program(robot) function. Use each block once and keep each block’s calls together. Turns change heading in place; turning while attached is invalid.
Planning a Block Route
A block goal can require movement along both coordinates. Mark the block’s starting cell and goal, then choose an intermediate cell for the change of direction. For each segment, identify where the robot must stand and which way it must face. Leave enough room to move around the released block.
Use a short plan such as “move the block along this row; release; approach from another side; move it along this column; release.” You choose the intermediate position and may use pushing, pulling, or both. Check both objects after each completed segment. Helpers can name repeated movements, while the caller coordinates them.
The model allows one attached block. grab() needs a detached block immediately ahead, and release() needs an attachment. Before a push or pull, the model checks both destination cells. If either movement is blocked, the action raises ValueError and changes neither position. Earlier successful actions remain.
Inspect the last completed state and the reported destination before changing the plan. The gripper does not lift blocks over obstacles.
Apply the planning steps to a new board. The goal is for the released block; the robot may finish in any legal cell and heading.
Placing a Block
0 / 30 points
Write place_block(robot) to leave the released block at column 5, row 4. The robot starts at column 2, row 2, facing east; the block starts at column 3, row 2. There are no interior walls.
1 2 3 4 5 6row 5 . . . . . .row 4 . . . . G .row 3 . . . . . .row 2 . R B . . .row 1 . . . . . .Use the robot’s methods to push or pull along any legal route. The robot may finish in any cell and heading. Do not return a value. Keep the supplied preview code unchanged; you may add helpers.
Check Your Understanding
- How does attachment change a later
backward()call, and how does releasing change the next movement? - Before pushing a block north, where must the robot stand relative to it, and which way must it face?
- Why must a route plan include the robot’s position after releasing, rather than only the block’s goal?
- When an attached movement is rejected, which positions remain unchanged, and which earlier completed actions remain?