-Hi. In this second video of the redo of week 3,
we are going to look at how to implement the DieHandle we were talking about.
Basically, we would like the handle API to be "DieHandle new".
"DieHandle new".
We would like to add some die to it, so "addDie:",
for example, one with six faces.
And another one with...
ten faces, for example.
"10" and "yourself".
Cool.
Basically, what this piece of code does
is create an instance of DieHandle class.
This instance will receive the message "addDie",
so I would like to add a Die to a handle
with six faces, for example.
This symbol right there will say
that the next message will be sent to the same receiver as the previous one.
Basically, this message there will be sent to the same receiver
as this message there,
that is the instance of DieHandle.
We will send "addDie" twice to the instance of a handle,
once with a Die with six faces
and another time with a Die with ten faces.
"Yourself" will output the instance.
Let's write this.
We will go to DieTest and...
create "DieHandleTest".
Save it.
The first test method we would like to make
is "testCreationAndAdding".
Basically, we will use that,
but we will need a temporary variable,
so "handle".
And right there, "handle ="...
I will tab everything. Dot.
And then, we will test the number of dices in the handle.
So, "handle diceNumber", for example,
"DiceNumber equals: 2".
We have not defined anything
but do not worry, we will do it right away. Cmd+S.
OK.
And then, Define new class, because we have not defined the handle.
And we would like to add the dice instance variable.
This is a collection that will hold all the dice we have added to the handle.
OK.
We have some red messages, but that is fine.
We will write another test, "CreationWithTheSameDice".
So, what we will do there is...
I will write this on the same line, remove that.
We will add twice the same dice.
So, dot.
We will check that the first time, it equals 1
and the second time, it equals 2.
We do not need to reinstantiate it.
So, "handle addDie",
we will add another die with six faces.
This time, we would like to have "diceNumber equals: 2".
OK, cool.
If we execute the test right away,
we can see that the Debugger opens
and nothing is implemented, so that is normal.
We will go over to the Class and go to Inst. side method.
First, we will write an "initialize" method.
So, "initialize",
call "super initialize".
And then, give to the dice instance variable
the value of a new "OrderedCollection".
Now, we need to add a die. So, "addDie: aDie".
What we will do is simply call "add" on the dice collection,
so "add: aDie".
Save this.
We will need a getter of our dice and instance variables,
so "dice".
Save this.
And finally, "diceNumber".
"DiceNumber" that will return for size of the dice collection.
Save.
We have got our four methods.
If we run our tests again, they are now green.
It is a great time to save, so go to Tools,
Iceberg,
then we can go over to there.
"Uncommitted changes".
I will just repair this.
"Checkout branch
discarding image changes".
OK.
So, we will add everything. OK, Checkout.
OK, Commit.
So, normally you should just have to commit everything.
This is because I am messing around with my branches, but it should be fine.
Now, it is up-to-date.
And we can go over to there
and look at the output "Die withFaces" gives us.
If I press Cmd+P or print, I can right-click and go to Print it.
We can go to "aDie"
and if we go to this one, it will output "aDie" as well.
While it is kind of useful,
we can make it better by, for example, displaying the number of faces.
In order to do so,
we will go to the die Class
and we will write a method called "printOn".
"PrintOn: aStream".
This is a method that is already defined but we will extend it,
so "super printOn: aStream"
because we would like to keep the "aDie" thing for now, so "aDie".
We would like to add the number of faces,
so "aStream nextPutAll:".
We will add an open parenthesis.
Then, in order to concatenate strings in Pharo,
you have to use the comma, so comma, "faces printString".
this will convert the number of faces to a string, comma, closing parenthesis.
Now, if I save this, go over to there and press Cmd+P,
we can see that we now have a number of faces between parentheses,
so "Die withFaces: 6", "Die withFaces: 10". That is great.
What we need to do now
is to make a "roll" method for the DieHandle.
Basically, how will we do this?
We will do "roll",
we need to keep the result as a temporary variable,
initialize it to 0,
and then, we need to iterate all the dice collection we have
as an instance variable,
so "dice"
and what we will do is call the method "do"
and then, brackets with double dash "each".
This will iterate each member of the dice collection
and we will add the result of a roll
so "res + each roll".
"Do" will iterate all the dice collection
and for each of the dice within the dice collection,
we will roll it and add its results to the global result "res".
So, let's write "res". Save this.
For testing purposes, we will need a "maxValue".
What is the "maxValue"?
For example, for our handle there, it would be 6 plus 10, so 16.
This is exactly what we will do right there.
Instead of adding the roll, we will add in the number of faces.
Basically, this means this one rolls the highest value possible, so 6,
and this one as well, so 10.
And this will return the "res".
Save this. Now, we will go over to the test.
First, we will test
the "testMaxValue".
So, I will just borrow some of this.
I will borrow everything. Let's go.
So, I am there.
"TestMaxValue", "handle", we will create our handle,
"self assert maxValue equals: 16".
Save this test, run it.
Everything is green, that is great.
Now, the last one is "testRoll".
And we will keep the same handle,
but this time, we will do the same thing
that we did for the "roll" method for the die.
We will do "10 timesRepeat"
and then block with the assertion.
Block, "self assert",
but this will not be...
We would like to verify
that "handle roll is between"...
I will put the parentheses first, parenthesis there, parenthesis there,
remove this.
"Between"...
If each die rolls 1, basically, this would mean 1, 2,
so the number of dice,
"between handle diceNumber and handle maxValue".
That is why we needed the "maxValue" function.
That is great.
Let's save it, run it, you can see it is green,
so everything is looking fine.
We can save our repository once again,
so Iceberg, "Uncommitted changes".
This is what you are supposed to see on the first time we save,
so double-click on it.
Now, Commit.
And you can write, for example...
What did we add?
We added "maxValue", "roll",
so "Roll method for DieHandle
and better print for Die" and commit.
Cool.
This is the end of part 2.
In part 3, we are going to look
at how we can use the behavior we wanted with, for example, "1 D4 + 2 D6",
because right now, we have the back-end simulation of what I have written there,
but we have not...
This message will not be understood by the system for now.
We will see that in the next part.