More Movement
repeat.py: 6 points
cmove.py: 6 points
RepeatMove
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!
ContinueMove
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 cmove.py.
Constructor
The constructor for ContinueMove has two parameters:
movesis a required argument that contains a list of all possible directions.periodis 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 forperiodcan 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.
Reminder
Commit and push your changes after you complete the constructor.
Next
The next() method has two purposes: 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.directionattribute to store the value of the current direction. - To update
self.direction, you should pick a new random value fromself.moves. self.directionshould be updated after it has been returnedself.periodtimes.- Use the attribute
self.cycle_countto 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 to 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.directionin a temporary variable. - Decide whether
self.directionneeds to be updated and, if so, update it. - Return the direction value stored in your temporary variable.
Reminder
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):
print(cm.cycle_count,cm.next())
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
- In the file
cmove.py, implement theContinueMoveclass according to the specifications given above. - Write a
mainmethod to test your class. You should make at least two instances of theContinueMoveclass with different sequences, periods, and number of times that you callnext.
Reminder
Again, please remember to commit and push your changes.