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