Yatzy Scoring7:42 with Kenneth Love
Now that we have a handful of dice, we should build the logic for scoring them while we play the game.
We could abstract our scoresheet further so we could reuse it in other games. We'd probably do this by creating a way to plug scoring categories into the class, probably through mixins (small classes, like our
Agile class, that don't have a lot of value outside of being used with another class).
If we were to build the entire game, we'd want some sort of
Player class, too, that would have a scoresheet associated with it. Feel free to try and build this part on your own!
All right we've got a lot to do in this one. 0:00 So let's get to it. 0:02 First off, let's add a utility to our hand class 0:04 that will give us all of the dice that have a certain value. 0:07 I think this will be handy because nobody can go, okay give me all the four's or 0:12 give me all the two's or whatever. 0:15 And I'm adding this to the hand class, 0:16 because it seems useful even outside of playing Yahtzee. 0:18 So I'm gonna call this def _by_value. 0:23 And notice single underscore at the beginning. 0:27 So this is a thing you probably shouldn't use if you're just playing with 0:29 a hand instance. 0:32 This is the thing you would use in subclasses of hand or 0:32 inside of hand itself. 0:35 So we're gonna say dice = an empty list for 0:36 die in self: because we're a list we can loop through ourself. 0:39 I would say if int(die) == value: then dice.append, 0:43 wait we don't need it at the end we can just say if die === value. 0:48 Dice.append(die) and then we're going to return dice, at the end. 0:54 So, this method will let us get all the 5's or the 4's or whatever. 1:00 Now, let's use that to add some properties in our Yahtzee hand, 1:05 that will give us all of the dice of a certain value. 1:09 Now, this is more for convenience than anything else. 1:12 But, you know what? 1:15 We can be as convenient as we want to ourselves. 1:15 So, property def ones(self): return self._by_value(1). 1:19 I'm gonna copy this cuz I need to make six copies of that. 1:27 Two, three, four, five, six. 1:31 All right, so this one will be twos. 1:36 This one will be threes, This one would be fours. 1:42 This one would be fives. 1:53 And if you guessed that the last one would be sixes. 1:57 You were right. 2:01 And let's go ahead and add a return in there between each of those, 2:05 because I don't like to have methods touching. 2:08 Okay, so now this is useful because in Yahtzee, 2:12 you can score all of the dice of a certain number, like scoring all your ones. 2:14 Let's make another property that gives us a dict where the keys are the die values 2:19 and the values are how many of that die exist in the hand. 2:23 So let's come down here proper key def sets. 2:28 And self and return a dict. 2:34 And the key one, we're going to have the len self dot ones. 2:39 For the key two, self dot twos. 2:45 Three, self dot threes. 2:49 Four, self dot fours. 2:52 Five self.fives. 2:55 And six self.sixes. 2:59 This will be a very handy property when we need to get all of our ones or twos or 3:07 whatever. 3:12 So let's go ahead and do that. 3:13 Let's go ahead and add in that little bit of scoring. 3:16 Now I'm gonna do this in a new class called YatzyScoresheet. 3:18 So I'm actually gonna do this in a new file. 3:22 And I'm gonna call it scoresheets, 3:24 and YatzyScoresheet. 3:29 Nothing new here, right? 3:32 This is just a score sheet. 3:33 The idea here is that you could abstract some of this out into a regular score 3:35 sheet that you can play for multiple games. 3:39 Or you could abstract this into, you could extend this with other versions of 3:41 Yahtzee that you wanted to do scores on. 3:46 But this is Yahtzee score sheet. 3:49 So we have out YatzyScoresheet class. 3:52 And now let's add a method that gives us the score of all the dice 3:54 of a certain number. 3:58 For any number, the score for that number is the sum of all the dice of that number. 3:59 So if I have three fives, I score 15. 4:03 So, we'll say score_ones this will take self and 4:05 we'll take a hand that get passed in. 4:09 And we'll return the sum of hand.ones. 4:12 Okay now, at this point you'd probably expect me to replicate this for 4:17 all the others, twos, threes, fours, and so on. 4:20 I'm gonna leave that to you though. 4:23 So go right ahead and do that. 4:24 Now, let's try a slightly harder scoring. 4:26 Now, we can score things like pairs, three of a kind, four of a kind, and Yahtzee, 4:29 which is five of a kind. 4:33 So first, let's set up a method that will score a group of dice by the number of 4:35 items that are in a set. 4:39 So, let's say _score_set, okay. 4:40 And we're gonna take the hand, and then we're gonna take the set_size. 4:44 So, how many things do we want to find? 4:47 Two, three, eight, how many? 4:49 So scores is equal to that. 4:52 And so for worth and count in self.sets.items, 4:54 and that actually should be 5:01 hand.sets.items because our hand has sets not our score sheet. 5:03 If the count is equal to the set_size, 5:11 then scroes.append(worth*set_size). 5:16 And then we want to return a sum(scores). 5:22 Now, again we need the sum of the dice and the right sized group. 5:27 But what if we want to score pairs and we have two pairs? 5:31 We should return just the score of the highest valued pair, right? 5:34 I want to score my two fives for ten, not my two ones for two. 5:38 So I'm gonna change this a little bit, I'm gonna but 0 in here so 5:43 that we have a placeholder. 5:47 And then, 5:49 instead of returning the sum of the scores I'm gonna return the max of the scores. 5:51 So whatever's the maximum value in here. 5:55 Now I can make a method to score say a single pair. 5:58 So let's do score_one_pair and we're gonna take a hand. 6:02 And we're gonna return self._score_set and 6:08 we want a score out of this hand two items. 6:12 Okay so now, let's test this out by making a hand that has what we want in it. 6:16 So let's hop into python and let's say from hands import YatzyHand 6:24 from dice import the six, because we need to make some dice. 6:30 And from scoresheets import Yahtzee scoresheet. 6:36 Okay, so let's make a few things here. 6:42 So we'll say hand is equal to Yahtzee hand. 6:43 And then let's make a die that has a three. 6:48 So D6, and we'll say value is 3. 6:54 And four is a value of 4. 6:58 And one is a value of 1. 7:03 And then let's replace everything that's in our hand with one, three, three, four. 7:09 And now let's make a Yahtzee score sheet and we'll just make an instance. 7:18 And we'll say score one pair for a hand. 7:23 And we get 8, because our 2 fours are 8. 7:27 That's pretty awesome! 7:30 Some scoring is gonna be trickier than others, though. 7:31 I've provided some tests and instructions on how to run them in the downloads. 7:33 So go ahead and 7:38 try to finish out the rest of the scoring functions on your own, after the course. 7:38
You need to sign up for Treehouse in order to download course files.Sign up