Python Object-Oriented Python Dice Roller Yatzy Scoring

Michal Janek
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Michal Janek
Front End Web Development Techdegree Graduate 30,653 Points

Can anyone explain this video a bit more?

I find myself at loss trying to follow this particular section of the course. I was ok till this part but this Yatzy Scoring really overwhelmed me. Can anyone explain the functions Kenneth is adding from the beginning, more like what is their purpose and usage? I mean he tells us its for this its for that, but...why do we need them?

jopapadaki
jopapadaki
7,311 Points

Been there regarding following along with this section, keep trying they say :)

10 Answers

Hey Michal,

Many fo the functions Kenneth creates at the beginning is to make it more clear and simple to add functionality to the game. He essentially breaks down the process of counting the values in a roll and totaling their values. He first created _byvalue a function that a user/other devs wouldn't really call but flows into the properties. So ones would just tell you the number of ones in a roll and so on. Then for score sheets he follows the same logic for totaling each roll value.

Byron Farrow
Byron Farrow
13,267 Points

I must admit that the difficulty for me really increased in the last couple of sections. I more or less understand the rules of Yahtzee, so that's not the issue for me, I just don't get the WHY in several areas. Like all of these magic methods that seems to replicate perfectly good functions. Maybe I'm just not Object Oriented enough ...

Michal Janek
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Michal Janek
Front End Web Development Techdegree Graduate 30,653 Points

those magic methods probably just to show that how those perfectly good functions actually work ''back-stage''. I think what Kenneth tried to show us was that we are creating our own objects with our own methods. And even those already built in methods we made our own versions of them.

Jimmy Smutek
Jimmy Smutek
Python Web Development Techdegree Student 6,629 Points

I agree, it's pretty difficult to follow and is not making sense to me either.

class Hand(list):
    # certain number of dice all of the dice have to be the same kind of die
    # create a hand class to run different sets of dice
    # size: number of dice 
    def __init__(self, size=0, die_Class= None,  *args, **kwargs):
        if not die_class:
            raise ValueError("You must provide a die class")

        for _ in range(size):
            self.append(die_class())
        self.sort()

    def _by_value(self, value):
        dice = []
        for die in self:
            if die == value:
                dice.append(die)
    return dice
    # return a list with value as item

class YatzyHand(Hand):
    # extent hand 
    def __init__(self, *args, **kwargs):
        super().__init__(size=5, die_class=D6, *args, **kwargs)


    def _by__value(self, value):
        dice = []
        for die in self:
            if die == value:
                dice.append(die)
         return dice  

         @property
         def ones(self):
             return self._by__value(1)

         @property
         def twos(self):
             return self._by__value(2)

         @property
         def threes(self):
             return self._by__value(3)

         @property
         def fours(self):
             return self._by__value(4)

         @property
         def fives(self):
             return self._by__value(5)

         @property
         def sixs(self):
             return self._by__value(6)

       @property
       def _sets(self):
           return{
                   1: len(self.ones)
                   2: len(self.twos)
                   3: len(self.threes)
                   4:len(self.fours)
                   5:len(self.fives)
                   6:len(self.sixs)
                 }
          # _sets:dictionary
          # keys:value 
          # value: how many of that value in dice[]

# yatzy folder
# scoresheets.py
class YatzyScoresheet:
    def score_ones(self, hand):
        return sum(hand.ones) # size for value 1 

    def _score_set(self, hand, set_size):
    # set_size is a setted size:len(self.n), such as self.two
        scores = []
        for worth, count in hand._sets.items():
            if count == set_size: 
                scores.append(worth*set_size)
                # scores: list with items: n*value such as [2*2, 2*5]
            #return sum(scores)  
             return max(scores)

    def score_one_pair(self, hand):
        return self._score_set(hand, 2)



from hands import YatzyHand
from dice import D6 
from Scoreesheets import YatzyScoresheet
hand = YatzyHand()
three = D6(value=3)
four = D6(value=4)
one = D6(value=1)
hand[:] = [one, three, three, four, four]
YatzyScoresheet().score_one_pair(hand)
8 # scores[3+3, 4+4], return 4+4 

I hope the #stuff can help you understand this video.

# value: the face that show up  -> Die and D6
# size: the number of different value -> hand
# set: the number of certain value -> hand
# set < size  
# self._set: dictionary: value as dictionary's key and size(count) as dictionary's  value.
# set_size: a size set as selection constraint.
# score: list: the value * set_size 
# max(score) & sum(score) is function that used on a list

Try to understand each variable's meaning at first, I believe it's not that much difficult after watching it several times.

nicole lumpkin
PLUS
nicole lumpkin
Courses Plus Student 5,328 Points

This is rough...but it's nice to see I'm not alone. Hopefully all of you who've posted before me are now comfortable with our Yatze game! For me, back to the first video in this section...

Byron Farrow
Byron Farrow
13,267 Points

Thank you Michal - fair point. I think this is a difficult topic, (for me anyway) so I'll rewatch the videos, and maybe look at some other sources of information (youtube videos, books, etc) to get a different perspective and more practice. Any suggestions welcome!

Johannes Scribante
Johannes Scribante
19,175 Points

One of the most helpful things I have found is to put a comment on every line as Kenneth goes through and explains it. Once again at first you are just doing it sometimes without really knowing why, but when you try an puzzle it together, reading through your comments and talking through it while you step through it really help to find where things are unclear or later on when you revisit you can adjust the comment so that it clearly states why this is done and how each step works.

I think the issue here is that we don't know game rules. The game is confusing by it own. It would be great if he could make a new game that everybody knows!!

nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

I totally feel for those of you who've never played Yahtzee(actual product spelling). I've played this game since I was a kid and I'm still struggling. For those of you who come across this thread and have never played I would totally go on Amazon and purchase the game as it is a fun one :P Happy coding

Same here