In this game, the aim is to get to the other side of the crevice by means of a turbo elevator. This consists of a number of horizontal platforms that were placed one after the other. The platforms vertically move up and down, and operate independently. You can only jump to the next platform, if it is situated on the same height as the platform you are on at the moment.
An initial level and an initial direction (going up, going down or standing still) is set for every platform at the beginning of the game. Furthermore, a lowest and highest level is set for every platform, these can not be changed afterwards. With every next time step, the platform moves one level up or down, or it stands still and stays in the same position. If the platform has reached the lowest (resp. highest) level, it will move back up (resp. down) from the next time step. A platform for which the lowest level is equal to the highest level, or for which was indicated that it is immobile, stays in the same position with every time step. Write a class Platform that can be used to model such platforms. For this, an object of the class Platform must contain the following methods:
An initializing method __init__ with which the configuration of a platform can be set. To this method 4 whole numbers must be given as an argument: i) initial level, ii) initial direction, iii) lowest level and iv) highest level. The last two arguments are optional, and are equated with the initial level if it is not given. The values of the four arguments must respectively be appointed to the attributes position, direction, lowest and highest of the newly made object of the class Platform. For the configuration of a platform, The lowest level may not be higher than the highest level, and the initial level must be situated in between the lowest and highest level (boundaries included). Look at the example below to see how the initializing method must react if these conditions are not met. The direction in which the platform initially moves, is represented by the values -1 (down), 0 (standing still) or 1 (up). See to it that these values can be symbolically represented by the variables Platform.DOWN, Platform.STILL and Platform.UP.
A method next to simulate that the elevator moves one level up or down or stays still within the period of one time step. This method must adjust the value of the attribute position according to the description from the introduction.
>>> platform = Platform(0, Platform.OP, -1, 1)
>>> platform.position
0
>>> for steps in range(5):
... platform.next()
... print(platform.position)
1
0
-1
0
1
>>> platform = Platform(1, Platform.DOWN, 1, 1)
>>> for steps in range(3):
... print(platform.position)
... platform.next()
1
1
1
>>> platform = Platform(3, Platform.STILL)
>>> for steps in range(3):
... print(platform.position)
... platform.next()
3
3
3
>>> platform = Platform(-3, Platform.UP, -1, 1)
Traceback (most recent call last):
AssertionError: invalid configuration
Now, also write a class TurboElevator, of which every object represents a turbo elevator that is built from a number of platforms. The first and last platform represent the starting point and the other side of the crevice, but do not have to be on the same level, and also do not have to stand still. To make an object of the class TurboElevator, no arguments must be given. In the beginning the turbo elevator does not have any platforms yet, but an extra platform can be added at any time. Decide for yourself if it is necessary to provide an __init__ method for the class TurboElevator, and how it should be implemented. Objects of the class TurboElevator must however contain the following methods:
A method add with which a new platform (an object of the class Platform) can be added to the turbo elevator.
A method timesteps that prints after how many time steps the last platform of the turbo elevator was reached. When calling this method, the time is set to zero, and one always starts from the first platform with every time step, first all platforms of the turbo elevator either move a position up or down, or they stand still, conform the method next of the class Platform. If, after that, the platform on which we are situated is on the same level as the next platform, we go to the next platform. The method timesteps must, according to the procedure above, also gradually adjust the condition of the platforms of the turbo elevator. Not that, because of this, it is possible that with every next call of the method time steps, a different value is printed, even though nothing has changed in the configuration of the turbo elevator. It is also possible that with a certain configuration of a turbo elevator, it is impossible to reach the other side. Therefore, the method timesteps must print the value None, if the last platform is not reached after 1000 time steps.
>>> turboElevator = TurboElevator()
>>> turboElevator.add(Platform(0, Platform.STILL))
>>> turboElevator.add(Platform(2, Platform.DOWN, -2, 2))
>>> turboElevator.add(Platform(0, Platform.UP, 0, 4))
>>> turboElevator.add(Platform(0, Platform.DOWN, -4, 0))
>>> turboElevator.add(Platform(0, Platform.DOWN, -2, 4))
>>> turboElevator.add(Platform(0, Platform.STILL))
>>> turboElevator.timesteps()
16
>>> turbo elevator.add(Platform(4, Platform.OP, 2, 8))
>>> print(turboElevator.timesteps())
None