Yatzy Scoring

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!

0:00

All right we've got
a lot to do in this one.

0:02

So let's get to it.

0:04

First off,
let's add a utility to our hand class

0:07

that will give us all of the dice
that have a certain value.

0:12

I think this will be handy because nobody
can go, okay give me all the four's or

0:15

give me all the two's or whatever.

0:16

And I'm adding this to the hand class,

0:18

because it seems useful even
outside of playing Yahtzee.

0:23

So I'm gonna call this def _by_value.

0:27

And notice single underscore
at the beginning.

0:29

So this is a thing you probably shouldn't
use if you're just playing with

0:32

a hand instance.

0:32

This is the thing you would
use in subclasses of hand or

0:35

inside of hand itself.

0:36

So we're gonna say dice
= an empty list for

0:39

die in self: because we're a list
we can loop through ourself.

0:43

I would say if int(die) ==
value: then dice.append,

0:48

wait we don't need it at the end
we can just say if die === value.

0:54

Dice.append(die) and then we're
going to return dice, at the end.

1:00

So, this method will let us get all
the 5's or the 4's or whatever.

1:05

Now, let's use that to add some
properties in our Yahtzee hand,

1:09

that will give us all of
the dice of a certain value.

1:12

Now, this is more for
convenience than anything else.

1:15

But, you know what?

1:15

We can be as convenient
as we want to ourselves.

1:19

So, property def ones(self):
return self._by_value(1).

1:27

I'm gonna copy this cuz I need
to make six copies of that.

1:31

Two, three, four, five, six.

1:36

All right, so this one will be twos.

1:42

This one will be threes,
This one would be fours.

1:53

This one would be fives.

1:57

And if you guessed that
the last one would be sixes.

2:01

You were right.

2:05

And let's go ahead and add a return
in there between each of those,

2:08

because I don't like to
have methods touching.

2:12

Okay, so
now this is useful because in Yahtzee,

2:14

you can score all of the dice of a certain
number, like scoring all your ones.

2:19

Let's make another property that gives us
a dict where the keys are the die values

2:23

and the values are how many of
that die exist in the hand.

2:28

So let's come down here
proper key def sets.

2:34

And self and return a dict.

2:39

And the key one,
we're going to have the len self dot ones.

2:45

For the key two, self dot twos.

2:49

Three, self dot threes.

2:52

Four, self dot fours.

2:55

Five self.fives.

2:59

And six self.sixes.

3:07

This will be a very handy property when
we need to get all of our ones or twos or

3:12

whatever.

3:13

So let's go ahead and do that.

3:16

Let's go ahead and
add in that little bit of scoring.

3:18

Now I'm gonna do this in a new
class called YatzyScoresheet.

3:22

So I'm actually gonna
do this in a new file.

3:24

And I'm gonna call it scoresheets,

3:29

and YatzyScoresheet.

3:32

Nothing new here, right?

3:33

This is just a score sheet.

3:35

The idea here is that you could abstract
some of this out into a regular score

3:39

sheet that you can play for
multiple games.

3:41

Or you could abstract this into, you
could extend this with other versions of

3:46

Yahtzee that you wanted to do scores on.

3:49

But this is Yahtzee score sheet.

3:52

So we have out YatzyScoresheet class.

3:54

And now let's add a method that
gives us the score of all the dice

3:58

of a certain number.

3:59

For any number, the score for that number
is the sum of all the dice of that number.

4:03

So if I have three fives, I score 15.

4:05

So, we'll say score_ones
this will take self and

4:09

we'll take a hand that get passed in.

4:12

And we'll return the sum of hand.ones.

4:17

Okay now, at this point you'd probably
expect me to replicate this for

4:20

all the others, twos,
threes, fours, and so on.

4:23

I'm gonna leave that to you though.

4:24

So go right ahead and do that.

4:26

Now, let's try a slightly harder scoring.

4:29

Now, we can score things like pairs, three
of a kind, four of a kind, and Yahtzee,

4:33

which is five of a kind.

4:35

So first, let's set up a method that will
score a group of dice by the number of

4:39

items that are in a set.

4:40

So, let's say _score_set, okay.

4:44

And we're gonna take the hand, and
then we're gonna take the set_size.

4:47

So, how many things do we want to find?

4:49

Two, three, eight, how many?

4:52

So scores is equal to that.

4:54

And so for worth and
count in self.sets.items,

5:01

and that actually should be

5:03

hand.sets.items because our hand
has sets not our score sheet.

5:11

If the count is equal to the set_size,

5:16

then scroes.append(worth*set_size).

5:22

And then we want to return a sum(scores).

5:27

Now, again we need the sum of the dice and
the right sized group.

5:31

But what if we want to score pairs and
we have two pairs?

5:34

We should return just the score of
the highest valued pair, right?

5:38

I want to score my two fives for
ten, not my two ones for two.

5:43

So I'm gonna change this a little bit,
I'm gonna but 0 in here so

5:47

that we have a placeholder.

5:49

And then,

5:51

instead of returning the sum of the scores
I'm gonna return the max of the scores.

5:55

So whatever's the maximum value in here.

5:58

Now I can make a method to
score say a single pair.

6:02

So let's do score_one_pair and
we're gonna take a hand.

6:08

And we're gonna return self._score_set and

6:12

we want a score out of
this hand two items.

6:16

Okay so now, let's test this out by making
a hand that has what we want in it.