Wednesday, July 17, 2013

Player and Scorer

So the next thing I'm going to do is create a Player class.  A player needs a name, a DiceTray, and a Scorer. I'm thinking of a Scorer as some sort of automated scoring gizmo.  Maybe I'd better deal with the Scorer class before the Player class.

The Scorer is going to have to keep a total, a subtotal of the upper categories (the ones through the sixes),  and a bonus.  Those will all be ints.  It's also going to have to keep track of which of the categories have been used and which have not been used.  I'll use an array of booleans for that.

Here is a screen shot of my C++ version while it's running:

I like having those values by each of the categories so you can see how many points you got for something.  So my Scorer is also going to have to keep an array of individual values.  I'll keep them in the order they're listed in that screen shot.  Both the boolean array that's keeping track of which categories have been used and the points array will be in that order.

My Scorer class constructor is going to create the two arrays (of size 13) set all the values to 0's, including the ints where I'm keeping tracks of totals.  I know Java sets things to 0 by default but it's better to make it explicit.  I might need a reset method that will set everything back to 0, so I'll make that.  I'll probably need getters for my int fields.  I'll definitely need a toString method that will print out a score sheet that looks like the above.  And I think it makes sense to have a tally method to which you pass a DiceTray and a category. The tally method will examine the DiceTray to see how many points can be awarded for that category, given the current values of the dice in the tray, and will update the fields accordingly.

I'll implement the Scorer and then make another posting.

1 comment:

  1. I approached the scoring for the game initially as a Score (Sheet). As in, you're playing the box version of Yahtzee at home and you take one of the paper scoring sheets out of the box to record your score. Again, this is a "has-a" relationship, as in each player will have a score sheet.

    The sheet only knows what information has or has not been recorded on it, it does not know the rules of the game. I left that up to the Game class. I don't see this as a limitation. I think it makes it more powerful.

    I used arrays to keep track of the individual scores and the score totals. Essentially every numerical value, scoring categories and totals, you see above is in an array. You could put the totals and the bonus in their own fields if you like.

    I used a separate array of boolean for the 13 scoring categories, just like Dr. Parks, to track the ones that have been scored. Parallel or near-parallel arrays like this can be useful because the same index can tell you multiple pieces of information. It makes is easy for one loop to do everything you need, like updating the score and marking that category as used.

    The most important part of my Score class is the update method. I choose to create a method that takes the category and the score and the updates appropriately if the category hasn't been used. You don't want a player repeatedly using the same category and padding their score. You can prevent that in the Score class, or you can validate their choice in the Game class and ask for another choice if it's been scored. As my program evolved, I actually did both.

    One rule that the sheet does know is the Upper Section(Ones-Sixes) bonus rule. I included this in the update method because it's based on a total and not an individual roll. The sheet is keeping the total so it seemed logical to put it there.

    The Score class also has a toString that makes printing easy. The \t(tab) escape sequence can help you line things up.

    Useful methods that came from implementing the optional Yahtzee Bonus/Joker rules included checking that a category was filled. That can assist you with category selection validation. I also wrote methods to check whether a series of categories were filled or if they contained a certain value.

    Again, you should write a short main method in this class to at least test out your toString. You can make sure it's printing. You can also call the update method to make sure it's putting scores in the right place and not allowing new/overwritten/multiple scores in the same category.

    ReplyDelete