More Movement 6 points 6 points


In Part B of the Warmup you worked with the RepeatMove class. This class represents a general purpose approach to solving a problem: given a sequence, develop a class that provides the next element in the sequence. A correct RepeatMove implementation is part of your grade on this lab, so please make sure you’ve completed it before moving on to ContinueMove below!


Let’s now design a class, called ContinueMove, that will allow us to move in a random direction for a given number of steps before picking a new random direction to move in. The ContinueMove class requires a constructor and a next() method. The specifications for these are described below. You should put your implementation in


The constructor for ContinueMove has two parameters:

  • moves is a required argument that contains a list of all possible directions.
  • period is an optional argument that represents the number of times the current direction can be returned before it needs to be updated. (Note that the default value for period can be whatever you like.)

The constructor should initialize four different attributes: self.moves, self.period, self.direction, and self.cycle_count. The first two should be set from their respective argument values. self.direction should be set to a randomly selected value from self.moves. The self.cycle_count should be initialized to 0.


Commit and push your changes after you complete the constructor.


The next() method has two functions: to return the current direction and to periodically choose a new direction. It should always return a given direction exactly self.period times before choosing a new one. (Note however that since we’ll be choosing at random, it’s possible to choose the same direction multiple times in a row).

Additional requirements:

  • Use the self.direction attribute to store the value of the current direction.
  • To update self.direction, you should pick a new random value from self.moves.
  • self.direction should be updated after it has been returned self.period times.
  • Use the attribute self.cycle_count to keep track of how many times the current direction has been returned since the last update.

There are many ways to implement next(), but we advise you to pay careful attention the order in which you perform your operations, since this can impact the outcome when your code is changing the state of your objects. One trick that may help with ordering is to use temporary variables to keep track of values that may change later. For example, here is one way you could use a temporary variable in your implementation of next():

  • Store the current value of self.direction in a temporary variable.
  • Decide whether self.direction needs to be updated and, if so, update it.
  • Return the direction value stored in your temporary variable.


Don’t forget to commit and push your changes before you move on.

Example Output

If all has gone well, the test code

cm = ContinueMove(["a","b","c"], 4)
for i in range(13):

might produce the output below:

0 a
1 a
2 a
3 a
0 b
1 b
2 b
3 b
0 a
1 a
2 a
3 a
0 c

Summary of tasks

  1. In the file, implement the ContinueMove class according to the specifications given above.
  2. Write a main method to test your class. You should make at least two instances of the ContinueMove class with different sequences, periods, and number of times that you call next.


Again, please remember to commit and push your changes.