-Hi. In this last part of the redo of week 3,
we are going to look at how we can finally implement this actual language.
So, basically,
what we will have to do is send the message "D4" to the Integer 1,
send "D6" to the Integer 2.
We would expect "2 D6" to output a handle
and finally, we will have to implement the operation between handles.
First, let's go over to the Integer class as we will have to extend it.
Cmd+B,
to open the System Browser for the correct class and package.
What we will do is create a new method called "D20" for example.
What should it do?
It should output a handle so we have to create a handle.
"handle := DieHandle new".
Then, we will have to add...
For example, if we look at "2 D6",
we will have to add a die with 6 faces
for each time the Integer here represents.
What this means is that we will have to do "self timesRepeat".
Then, we will have to add the die, so "addDie:
(Die withFaces: 20)"
because we are using the D20.
Basically, this will be the Integer,
"1 D20", "2 D20" for example, "2 D20",
we will repeat twice the adding of a die with 20 faces
and then it will output the handle.
We will save that.
As you can see,
it has been saved in the Kernel "Numbers" package
so we might not want to put our method there.
What we can do is either right-click,
go to Refactorings and put it in our package,
or we can just check the box that is hidden there, "extension".
We will check this and go to "Dice", click that, press OK.
And now, we can go back there
and see that we have an Integer class that just appeared
and if we click on it, we can look back for our D20 method.
That is great. We will write a test for it.
Basically, "testSimpleHandle".
What we will do is "self assert"
that "2 D20 diceNumber"
equals: 2".
What does "2 D20" represent if we inspect it?
We can see that it is a "dieHandle" with the two "Die" with 20 faces.
This should be correct
because it will output the diceNumber of this handle there.
Let's save it, run it,
and perfect, our test is green.
Now, we would like to have our "D4", "D6", "D10", "D16", "D12", whatever.
We can just go there and copy this while changing this one
or what we can do that might be a bit smarter,
is write a "D: aNumber"
and to replace the "20" there with "aNumber"
because this way, we can simplify this call each time.
For example, let's save it, let's now call "D6".
What do we have to call?
We simply have to call "self D: 6".
This will output the same thing as with "D20"
except with a die with 6 faces.
So, let's save this.
We can even rewrite this one
and call the "D:" method, "D: 20".
We will do a couple more, for example "10",
"6".
Wait, I already did 6.
"4" and "12".
We have most of the dice needed.
Perfect, we have everything
and now we will check that it is correctly working.
What we can do is simply go over there,
inspect this part.
We can see that we have a DieHandle within a die collection,
2 Die with 6 faces.
And if we go over to this one and press 3, inspect it,
we have 3 Die with 4 faces.
That seems correct.
Right now, what we need is the "+" function to work
so we have to go over to the DieHandle
and implement the "+ aDieHandle".
Basically, what will happen is we will create another DieHandle
that will be the result of a combination of both at once
and we will have to combine
the dice collection from the D4 result there
with the dice collection from the handle of D6.
So, we will create the handle.
We will say "self class new"
because right now, we are already in the DieHandle.
And we will go to "self dice do:"
so we will iterate all the dice collection
from the dice handle we are currently in.
So, "do: [ :each | handle addDie: each ]".
This will add all the dice from within the current handle
into the results.
Now, we need to add the one in the argument,
so: "aDieHandle dice do:" and the same thing.
Once we have added both together, we can simply return the handle.
Let's try to test to see if the correct result is being output,
so, "testSumming".
We will say "self assert"
that "3 D4 + 2 D6",
within parentheses,
actually holds 5 dice.
Let's save this.
We can run it now
and I can see that the test is green.
I will just remove this.
If we inspect this,
we can see that we have our 3 dice with 4 faces
and our 2 dice with 6 faces.
That is exactly what we would like to see.
But now, we will see
if the roll function is working, so "roll".
We know that we can have a result between 5, the number of dice,
and the maxValue, that we can call right now, "maxValue",
which is 24.
So, we have correctly implemented a way
to understand this kind of language, "3 D4 + 2D6",
and then just roll the whole handle.
And right before I stop the video,
we are going to save in our Repository for the last time.
So, go over to your Iceberg.
We can see there that we have uncommitted changes
so double-click on that, press Commit.
We have added the method "+" for the DieHandle,
two simple tests and then, the extension of the Integer class
for our "D10", "D12",
or basically any dice since we have this method right there.
We will say
"Extensions of the Integer class for Dies
and operation on DieHandle".
Then, we just commit.
And this is the end of the redo of week 3.