Giving a Hand6:07 with Kenneth Love
Now that we can roll dice and compare them, it's time to start on the Yatzy game play.
Are you confused by how we're using the
die_class argument? Functions and classes are first-class citizens in Python which means we can pass them around and use them as values just like we would any other variable. That means we can point a variable or parameter at a class or function and then call that variable just like we would the original class. Here's another example:
class Car: pass class Van: pass class Motorcycle: pass def vehicle_factory(cls, count): for _ in range(count): yield cls()
Now we could make any number of
Motorcycle instances that we want (and, notice, it's a generator function since we're using
yield; if our vehicle manufacturing required a lot of memory, we could still be polite to other processes and not tie up all of the RAM with our factory). We'd do this with code similar to
cars = vehicle_factory(Car, 50) vans = vehicle_factory(Van, 10) motorcycles = vehicle_factory(Motorcycle, 100)
Each time the
for loop executes, it finds the class that was passed in and creates an instance of it. This ability of Python makes for really flexible code.
All right, let's talk about this handy class. 0:00 A hand will have a certain number of dice in it I think, as a restriction, 0:02 we'll make it so that all of the dice have to be the same kind of die. 0:05 Sounds like something we should use a list for. 0:09 I think we might have to get a bit clever though so, 0:11 we can use the same hand class to roll different sets of dice. 0:13 Let's get to work spaces and see what we can come up with. 0:17 Okay, so lets start by adding a new file, which we'll call hands.py. 0:21 And inside of here, we're gonna make our list subclass. 0:26 So we'll call this class Hand, let's going to extend list. 0:30 And then in the init, we want to take self, we wanna take the size of the hand, 0:35 the number of dice that should be in there, and then whatever args, and 0:40 kwargs come in from people using this, if they try to use it as a list. 0:44 And again, we wanna do super.init. 0:49 And we just don't care what's there. 0:51 So that's our basic class. 0:54 Now, I wanna add an argument that will hold 0:55 the class that all of the dice will be. 0:58 We can pass around classes just like we do variables or functions, in this way future 1:02 developers can give our constructor a class for the dice that they want to roll. 1:07 So let's add a new argument right here which is die-class, 1:11 we're gonna set that equal to none. 1:14 And let's say, if not die_class, 1:17 they don't give us a die_class for whatever reason, 1:21 then we're gonna raise ValueError, you must provide a die class. 1:26 All right, and then we'll just do our super like that. 1:33 Okay, so we have our size, which is the number of dice to roll, and 1:36 we have the class that needs to be rolled. 1:39 Let's use these two to append dice to our list. 1:41 So we'll say for whatever in range(size): self.append(die_class()) and 1:45 then we'll just call that function, or that class rather. 1:53 Okay, so that should create how many dice we need. 1:58 Let's try this out really quick. 2:01 So, let us open up python. 2:04 And let's import dice, and let us import hands. 2:08 Okay, and then, let's say that hand = hands.Hand size=5, 2:12 and die_class=dice.D6. 2:19 Now, notice that we're not calling the class, we're simply passing the class. 2:24 And if we look at hand, then we've got objects. 2:30 Let's do len(hand) and make sure there's 5 there, there are. 2:33 And if we were to do hand.value, the first one there is a 4. 2:37 That's awesome, it's a big step too. 2:43 Take a minute and make sure that you understand what is going on here. 2:45 We're using the class that's being passed in, in this case D6, 2:48 and then we're instantiating some number of that class. 2:51 So here in our loop, let's look back at this loop. 2:55 There we go, right there. 2:59 Here in that loop, we're creating five D6 instances. 3:00 That's a really cool way to handle this pluggable class requirement. 3:05 All right, let's do one more thing in here. 3:09 Let me get out of Python, clear that out. 3:12 Okay, let's do one more thing in here. 3:15 I think it would be best if the dice that we have on our hand were always sorted, so 3:17 we'd have all the ones, and then the twos, and then the threes and so on. 3:21 Now we can do is what the sort method since our class is base on a list. 3:26 And since our die classes had the ability to compare themselves to each other, 3:29 they already know how to sort themselves. 3:33 That extra work has already paid off, right? 3:36 So let's just do self.sort at the end of the init. 3:39 Okay, so that we could test this out but I don't think right now we need to. 3:42 Let's not worry about that. 3:45 Before we move on though, let's go ahead and create our Yatzy hands. 3:46 So, let's make a new class down here that we're gonna call YatzyHand, 3:51 and it's going to extend Hand. 3:56 And we're gonna do a definite again, and we'll take whatever args and 4:00 kwargs come in. 4:04 We don't really care about them though, right? 4:05 We're gonna call super.init and the size is always gonna be 5. 4:07 And the die_class is always gonna be D6. 4:12 And then we'll pass whatever other args and kwargs come in, so 4:15 that would be things like, I don't know, that' be nothing. 4:19 There's nothing we need there. 4:22 So let's go up here, and 4:24 let's say from dice import D6, that way we have that class available. 4:25 Now, just like with our D6 class, 4:32 we'll just auto set the things that we need in the new class. 4:35 Now we should be able to substantiate this hand and not have set the dice class, 4:38 or anything else. 4:43 So,let's actually go over here, let's change one thing in our dice real quick. 4:44 So let's add one new method here, let's change repr and 4:49 let's return self.value or we just turn this as string, 4:55 so that way we know what are value is, okay? 5:01 So now, let's go down here and create a Yatzy hand and see how this works. 5:07 So from hands import YatzyHand. 5:14 And I'll say yh = YatzyHand. 5:17 And I shouldn't have to talk about anything there. 5:21 And I should be able to look up yh and I've got 2, 2, 4, 5, 5. 5:24 So they're sorted. 5:27 And they are five dice and the highest number in any of them is five so, 5:29 it seems like they're not going over six so, that's awesome. 5:33 Now we have a hand that we can use for playing Yatzy. 5:37 >> What's up with that word spelling of Yatzy? 5:41 It's actually a perfect thing for the scores. 5:43 It's Scandinavian open source game. 5:45 No copyright infringement and we can support open source 5:48 even on our choice game to replicate, but Yatzy isn't just about rolling dice. 5:50 We have to be able to score them, too. 5:54 I don't know that we can cover all of the categories that Yatzy requires, but 5:56 we can do some of them and 5:59 you'll be well prepared to do the rest of them on your own after the course. 6:00 As always, get yourself ready for the next video, and then meet me in Workspaces. 6:04
You need to sign up for Treehouse in order to download course files.Sign up