TD 1 (en)¶
One Dice¶
We want to use a Dice. We should be able to:
- create a Dice with no parameter,
- create a Dice with the initial position
- get and set the position of the Dice (methods
get_position
andset_position
)- roll a Dice
- get the maximum number
NUMBER_FACES
of faces (same for all the dices, constant)- print it as a string.
Propose an UML modeling of the class Dice
, and write some tests in order to prove the model.
draw all the namespaces and point the name recognition for the following code:
1 2 3 4 | dice = Dice()
dice.roll()
print(dice.get_position())
print(dice.NUMBER_FACES)
|
Many Dices¶
In order to play some dice’s game, we want to model a kind of Green carpet.
- The class named
GreenCarpet
should have: - 5 dices as attributes.
- a roll method which rolls the 5 dices.
- a way to get the positions of each dices.
Propose a UML model and the associated python code for this class.
Trees and Dices¶
As we roll one ore several dices from our GreenCarpet
, we want to immediately take one with the higher position. To do that efficiently, we will use a tree structure, the heap.
Heap structure¶
https://en.wikipedia.org/wiki/Heap_(data_structure)
In a word, a heap is a tree for which a node value is larger or equal than both the node value of its left and right sons.
Draw a heap with 5 nodes and values 1, 3, 3, 6 and 4.
Propose a class for a heap node and for a heap:
- for a given heap one should be able to get access to the root node of the heap,
- for a given node one should be able to access to:
- it’s father (or None if it’s the root)
- it’s left and right sons (or None)
- it’s value.
According to this model, propose an algorithm to maintain a given heap if we modify the value of one of it’s node.
Dices link¶
Propose a way in order to link the heap structure to the dices.
- Setup a use-case in order to tests this architecture:
- five dice. all with initial position 1.
- roll a 3 with the 4th dice in the heap