Writing Yahtzee Game in Java

Yahtzee in Java.

To steal from Wikipedia, Yahtzee was originally created by Milton Bradley which is now owned by Hasbro. It is a 1 to 4 player dice board game. It is categorized as luck, probability and strategy game. There are 5 dices and 13 categories that you can score in. Evidently, I missed the space and time in which Yahtzee became a household timepass.

Mehran Sahami of Google and Stanford and many other fame is a marvelously smart guy and a great teacher. This is the fifth assignment of Stanford’s CS106a Programming Methodology which I have completed without help of any book. I have watched and rewatched his videos until they started making sense. I did read the first 4 chapters of The Art and Science of Java while I was downloading lecture videos. Believe me it did take two weeks to download 28 videos at 0.256 mbps connection on the-state-owned-internet-provider’s connection. I have taken miniscule or no help in doing the assignments. Why I say miniscule I would discuss later.

I finished the Hangman Assignment in middle of a wedding. But this time I had enough time to properly follow object oriented programming paradigm. The graphical part of the game was already provided in with the starter package. It also included two vital classes that proved to be critical in timely finishing the program. These two classes are YahtzeeDisplay and YahtzeeMagicStub. YahtzeeDisplay did all the graphical heavy lifting work providing methods to show information and interact with mouse click events. YahtzeeMagicStub played the vital role of making sure that dice configuration matches with user asked categories.

I spent a day or two planning the game using pen and paper. I knew I had to explicitly use object oriented programming. I decided anytime run() got clumsy or any set of instructions that would take more than a few lines would be better served writing as a separate method. I think I did a good job.

I understand the basic idea of enumeration but this is the first time I have used enumeration. It didn’t make any sense until I started implementing the program. I still feel I lack intuitive grasp to how-to of enumeration. I built up on a provided enumeration but it would be hard to start from scratch and implement it. Without enumeration it would have been very difficult to refer to 13 scoring and other categories.

I have written a lot of methods but have previously avoid writing type-return kind of methods. It finally clicked. Most of the methods that I wrote in this program are pretty simple and I fed them parameters and returned some. I passed around almost all the variables. I initially implemented a single player game. As a result of this strategy, turning a single player game into a multiplayer game only took 2-3 hours.

Yahtzee was all about arrays. I used both single array and multi-dimensional arrays. What I enjoyed most was cleverly, at least I think so, using a single dice array to use and discard for every single player turn. And storing the overall game score in the multi-dimensional score array. I did have to adjust the game for the fact that user prefer numbering system starting 1 and computer counts from 0. It mostly comprised of subtracting 1 from arrays. It became very tricky as to where change the value while converting game from single player to multiplayer.

In one of the methods, my task was to find the biggest number. My initial gut response was to draw a tricky diagram comparing each member with all others. You could see what would that mean for 4 numbers. I looked for what Java provided for comparing numbers. I couldn’t find anything that came bundled with acm library. One fine gentlemen on Stackoverflow, suggested a simple loop and comparing two numbers, only keeping the largest. I should have come to this conclusion but you could do little when you have seen even a glimpse of possible solution. That was the miniscule help I have taken from the fine folks of internet.

I decided to skip implementing YahtzeeMagicStub on my own. I am sure it is very hard and it would have stretched my logic muscle. But personally I think I can see what kind of programming would solve the magic stub problem and it mostly involves lot of logic gates and loops. I think I can solve it without learning anything new and vital for the moment. I would be better served moving along and learning new stuff.

It has been a nice experience finishing 5 out of 7 assignments. Makes me feel like a programmer.

Checkout the code on Github.