Pull
Pulling is an important computational thinking pattern with many applications. One object can put another object or, indeed any number of objects. For instance, a locomotive may be pulling a large number of railroad cars. Computationally speaking it make sense to differentiate between the locomotive as an active agent of pulling and all the cars attached to be passively pulled.
Contents
Single Pull (straight)
Example: one car (puller) moving a trailer (pullee) to the left.
- IF car sees trailer to the right THEN make trailer go left
- car moves left
Multi Pull (straight)
Example: locomotive (puller) pulls n railroad cars (pullees) to the left
- IF locomotive sees a railroad car to its right THEN make it go to the left
- reccursion: for a railroad car to go left it will: IF sees railroad car to its right THEN make it go to the left
- move left
Multi Pull (turning but not tight)
When objects can turn things become considerably more complex. A locomotive may pull many railroad cars around on a system of complex train tracks that require the train to be able to go around bends. A popular example would be the game called Centipede in which a centipede is snaking all the way from the top down towards a player. As the centipede finds obstacles it turns. This solution will only work in non-tight situations (not when the snake is winding back to touch itself.
The general solution is complex but a relatively simple solution can be found for cases such as Centipede. The centipede is moving mostly horizontally and occasionally vertically. In the original game the centipede can come back up but for this simple version lets assume it can only move left, right and down. In that case we can have each centipede piece implement the same strategy.
right
For any piece of the centipede to move right it will have to check to its left and above to see if there are other pieces that need to be pulled.
The order of checking is important. The mostly horizontal, occasionally vertical behavior turns into these rules:
Only one piece needs to be pulled.
left
For any piece of the centipede to move left it will have to check to its right and above to see if there are other pieces that need to be pulled.
down
For any piece of the centipede to move down it will have to check to its right, right and finally above to see if there are other pieces that need to be pulled
Multi Pull (tight turning)
This solution will work for all situations. Approach: each piece, starting from the head, is numbered (0, 1, 2, ...). Similar to the Mutli Pull above each body piece looks for its successor. Because of the numbering even tight cases where the snake touches itself can be resolved. Example code below.
AgentCubes Games using this pattern
Games/Simulations Tutorials that use this pattern
- Sokoban
- Centipede