-Hi.
In the redo of the third week, so right now,
we will look at how we can handle an expression to represent dice.
For example,
imagine that we would like to know the result of an expression
being "2 D20 + 1 D4".
This expression is what you might encounter in a role-playing game
such as "Dungeons and Dragons" for example,
where this would mean
"give me the result of a roll of two dice with 20 faces
plus the result of one dice with four faces."
In order to make everything work,
we will need a class to represent a die, a class to represent dice handle,
and we will look at how we might implement it.
Let's go over to our System Browser.
We will create a die class that is a subclass of Object.
We will give it an instance variable to be the number of faces,
and we will put everything in the "Dice" package.
Let's save this, Cmd+S.
We will filter out all the other packages
and we will write an initialize method.
When you write an initialize method,
it is good practice
to always check for the initialization of the superclasses.
We will write "super initialize".
Then, we will set for the number of faces to be six
so we will have by default a dice with six faces.
Let's save this.
And what we will do is instantly test this behavior.
Go over to your Class,
we are going to write "DieTest",
to be a subclass of "TestCase",
we do not need this anymore, and save everything.
OK, we have our "DieTest", we will write
our "testInitializationIsOk".
So, what do we need to test?
We need to create dice, so "d = Die new"
and we would like to check
that the number of faces of d is actually equal to 6.
"Faces" is in red because we have not defined it
but as we saw last week,
this is fine, the debugger will handle it so let's just save it
and then, run the test.
"Faces" has not been understood, that is fine
because we will just create it right now.
Create in the Class Die
under the "accessing" protocol. OK.
This is exactly what we want, the default behavior of the getter.
Let's press "Proceed".
Our test passed, so we can just click there
and now it is refreshed.
Our test passed.
So, right now, what we can write and what we will need
is a way to roll our dice.
Let's go over there and write a "roll" method.
What we will use
is "faces atRandom".
Basically, we would like to get a result from 1 to the number of faces.
Let's look into the Playground.
If I write 6 atRandom,
double-click, Cmd+P,
you can see that it output a result from 1 to 6.
This is exactly the behavior we would like to use for our dice.
This is our roll function that we will save by pressing Cmd+S.
We will test it right away,
so "testRolling".
And what is good practice for random behavior
is to repeat the test on the random result,
so "timesRepeat:" is the way to go.
This method is applied to a number
and this means it will repeat 10 times the thing we will get.
What we will give to this method is a code block,
so basically, it will be between brackets.
We will write "self assert"
and what we would like to assert is basically
that the roll of a dice is between 1 and 6.
This means it will check for the result of a dice
to be between 1 and 6 with the "assert"
and it will repeat this check 10 times
so let's save it, run it,
and if you are not confident
in the number of times you tested your random behavior,
you might increment it up to 100 for example, save it, run it.
Everything is running, we have a good start,
it is a good time to save our repository.
Let's go over to Iceberg.
Let's go to Add, we will save the project under "Dice".
OK.
Double-click on the project,
our project "Dice".
We will add our Dice package.
Add.
It says that we have uncommitted changes
so let's commit right away.
"Initial commit", what did we do?
We created "Dice initialization
and rolling with tests".
Perfect, press "Commit".
This is our GitHub properties, so "QDucasse"
and your email address.
Cool.
Everything is saved now.
We can just keep it here.
Keep this one as well,
and we will write another test and the associated method.
For example,
one thing we will need if we need a die with 20 faces or with 4 faces
is a way to specify the number of faces,
for example we will write something called:
"CreationIsOk".
And remove this.
I would like to have a method that I can call like this: "withFaces: 20"
and I would like to assert
that my number of faces is equal to 20.
How do I handle this?
You can see it is written in red but as we saw last week,
we will let the debugger handle it so press Cmd+S.
Run it.
The method "withFaces" has not been understood
so let's press "Create".
We will define "withFaces" in the die Class.
"Die Class", under the instance creation protocol.
OK.
This time, it does know how to handle it
so we will write for it:
"d"...
"d" will be our new dice and I would like it
to have the number we provided as faces,
"d faces",
and just give us d.
Cmd+S.
We will proceed.
"Faces:" has not been understood.
This is because we have not defined the setter.
So, let's create it in Die
under the accessing protocol.
OK.
This time, it recognize it
so "faces : = anInteger", that is perfect.
Let's proceed.
It is red,
but if we click on it again, it is green, so that is perfect.
You can see that now we have three tests,
we have the basic functionalities of our Dice running, up and ready,
so it is a good time to save everything on our repository,
so it is there or we can see it there.
Let's just commit
and we will write:
"Class method to specify the number of faces with test."
Cool.
"Commit".
Right now, we have all the basic functionalities of our die
and we will see in the next part how we can handle a dice handle
and how we can handle multiple dice together.