Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
With all of our object-oriented programming knowledge, it's time to tackle a bigger, more comprehensive project.
Creating specialized classes by overriding default arguments in __init__
is a pretty common practice. It's a really easy way to build up a library of classes for other developers to use while keeping each class fairly small and understandable.
Want to see the rules for Yatzy? Wikipedia's got you covered.
[SOUND] You've learned a ton about
object oriented programming in Python.
0:00
You've learned to make your own classes.
0:07
Either from the ground up.
0:09
Or by building on top of
Python's built in classes.
0:09
You've also taken
advantage of magic methods.
0:12
Class methods.
0:14
And properties.
0:14
To make your classes smarter and cleaner.
0:15
You're doing amazing.
0:17
We haven't built anything larger and
more complex together, though.
0:19
You know me.
0:21
I like to build games with
you as a learning tool.
0:22
When I was first planning this course out.
0:24
I wanted to build a terminal
version of Yahtzee.
0:26
With die rolling, and
re-rolling, hand scoring.
0:28
And even human and bot players.
0:30
Yeah, that wasn't as simple
as I thought it would be.
0:34
When I did complete it.
0:36
It was just way too much
to teach in a course.
0:37
So let's scale it back.
0:39
And build something that's less for
one specific game.
0:40
And maybe a bit more universally useful.
0:43
Over the next few videos.
0:45
We'll build a small utility.
0:46
To roll a specific number of dice
with a specific number of sides.
0:47
You could roll two 20-sided dice or
0:50
five six-sided dice or
whatever your game requires.
0:52
While this isn't quite as
exciting as a full game.
0:55
It'll give us a lot more practice
with building smart classes.
0:57
And it'll be a utility we can use for
building other games.
1:00
As we continue working with Python.
1:02
Get a drink or a snack.
1:04
Take a nap if you need it.
1:05
And then let's start.
1:07
By defining exactly what
our code dice should do.
1:07
All right, we want a die that
has a certain number of sides.
1:11
And then a random value between 1 and
the number of sides.
1:15
And we could base this on int.
1:18
But that actually raises
a fair number of issues.
1:20
It's easier and arguably cleaner
to just make our own class.
1:22
We can control the creation of the class.
1:26
And we can make it a little easier
to subclass for other die types.
1:28
Now I've created a directory named yatzy.
1:32
And I've created a file named dice.py.
1:34
You wanna go ahead and do those.
1:37
If they're not in your workspace.
1:38
So then let's import random.
1:40
And lets create our die class.
1:43
So find our init [INAUDIBLE] self.
1:45
I think we want two arguments.
1:49
I think we want a sides.
1:51
And we'll default that to two.
1:53
And let's let them set a value.
1:55
Just in case they want the die
to be four or whatever.
1:59
For testing purposes.
2:02
So, okay, so we take a number of sides and
we take a value.
2:05
And we want to make sure that sides >= 2.
2:08
Otherwise we should raise ValueError(.
2:14
That says, must have at least 2 sides.
2:17
If you can show me a one sided die,
I would love to see it.
2:20
And let's make sure that sides is an int.
2:24
So if not is instance(sides,
int): We're going to raise
2:28
another value error that says
sides must be a whole number.
2:33
Now, arguably, that might ought to be
a type error instead of a value error,.
2:40
But I figured they gave me the wrong
value, so it's a value error.
2:43
So then let's say that self.value = value.
2:48
That was passed in, or
random.randint (1, sides.
2:52
So something between 1 and sides.
2:59
We can't have a number below 1.
3:01
And it can't be above the number
of sides that there are.
3:02
Now, we're not taking into account
if you had a strange die that had
3:06
only even numbers or something like that.
3:10
And we might want to have a check.
3:12
To make sure that value is also an int.
3:14
But I'll leave that up to you.
3:16
Okay, we have enough here to
try out our really simple die.
3:18
So let's come down here.
3:23
And Python from dice import.
3:25
I need to exit.
3:29
Let me get into Yatzy.
3:32
Python from dice import die.
3:34
And then let's say that d is equal to die,
d.value.
3:40
There's 2.
3:46
And let's say d6 is equal to die.
3:48
And sides is equals to 6 and the 6 sides.
3:51
And then we do d6.value
we get 2 there as well.
3:54
All right, so
it rolled a random number for us twice.
3:59
So cool.
4:01
Now, let's go back and let's make
a die class that extends our die.
4:03
But is always a six sided die.
4:09
Right?
Because that's
4:11
a pretty common need, right?
4:12
There are lots of games out
there that need six sided dice.
4:13
The convention is to call a die D.
4:16
And then the number of sides that it has.
4:20
At least in role playing circles.
4:22
So we're gonna call this a d6.
4:24
Ad this is going to extend die.
4:26
But we're gonna override init.
4:28
We're still gonna take value.
4:31
We're not gonna take sides.
4:33
Because we don't care how
many sides they want, right?
4:34
The sides on this is always gonna be six.
4:36
So we're gonna call super and
then the init method.
4:38
We're gonna say size equals six,
and value equals value.
4:42
We'll just pass value on through.
4:45
And that's it for our class.
4:48
Since we set up our Die class
to take a number of sides.
4:50
We can just hardcode that in our D6 class.
4:53
So, let's test this one out.
4:56
So we're gonna import dice and
then we'll say, d6 = dice.D6().
5:03
And then if we do d6.value we get a 3.
5:08
Nice.
5:12
Now we could go further.
5:12
And turn this into a 12 sided or
a 20 sided.
5:13
Or whatever number of sides
the dice in our game require.
5:16
So feel free to do that if you want to.
5:21
You need to sign up for Treehouse in order to download course files.
Sign up