Yahtzee - a dice game

by Larry Moss

dice dice dice dice dice dice


Contents:

Rules of the game

Yahtzee is a game played with 5 dice. Players take turns rolling the dice. Each player may roll the dice up to three times. The first roll of each turn is always with all 5 dice. On subsequent rolls within that turn, the player may roll any subset of the dice. The remainder of the dice that are not rolled are placed aside.

The goal is to create the highest scoring configuration of all 5 dice. At the end of a turn, each player must place his/her roll on the score sheet. (If the player does not have a roll that matches any remaining slots, a zero gets placed in the slot of the player's choice.) Each slot in the score sheet, with the exception of Yahtzee Bonus may only be used once in a game.

The rolls:

The top section of the score sheet consists of fields 1 through 6. A player chosing to place a roll in one of those fields enters the sum of the faces of all dice containing that number. For example, if the dice at the end of the turn show the numbers 1, 2, 2, 2, 5, a player may chose to enter a score of 1 in the ones field, a score of 6 in the twos field, or a score of 5 in the fives field.

3 of a kind - If a player has at least 3 dice with the same number on them, the sum of all dice may be entered here.

4 of a kind - If a player has at least 4 dice with the same number on them, the sum of all dice may be entered here.

full house - A player may score 25 points in this field for a roll consisting of a 3 of a kind and a two of a kind.

small straight - A sequence of 4 numbers (1 through 4, 2 through 5, or 3 through 6) scores 30 points.

large straight A sequence of 5 numbers (1 through 5 or 2 through 6) scores 40 points.

yahtzee - All dice have the same number on them. Yahtzee is the only roll that a player can score more than once. The first time a player gets Yahtzee, he/she gets 50 points in the field on the score sheet for the roll. If any additional yahtzees are rolled during the same game, 100 bonus points are given for each. The player may then also play that Yahtzee as another roll elsewhere on the score sheet.

chance - This is just a sum of all dice, regardless of what they read.

How to play

For some odd reason, many people seem to prefer playing card games on a computer, so we'll skip the traditional approach involving physically rolling dice and see if the virtual method catches on.

Before rolling, no dice appear on screen. After pressing the roll button for the first time, the dice will come up with random numbers in the in play rack. You may then press the roll button two more times. Any dice remaining in the in play rack will be rolled again. The keep rack is where dice get placed that you don't wish to roll again.

To move dice from one rack to the other, click on each die separately.

The computer moves rather quickly. This is unintentional. I wanted to put a delay in the code so that the computer's moves would be visible. Unfortunately, that doesn't seem possible. A status window on screen does tell you the last move played.

At the end of a turn, place your roll on the score sheet by pressing the button on the score sheet that represents the roll you'd like to score. Computation of scores is done automatically by the computer.

Browser requirements

This game was tested under Netscape Navigator 4.04. It is meant to run on any browser that supports Javascript 1.1 (or later), however no additional testing has been done.

It is highly recommended that you make your browser window fairly large in order to accommodate the game.

Info about computer play and other geek stuff

I wrote this in an attempt to learn JavaScript. As a result of that, the code isn't the best JavaScript code in the world. Now that I'm more familiar with the language, I'd probably do it a lot differently. Regardless, it still works, and after all, writing clean code in a scripting language is practically unheard of. In short, anyone that wants can experiment with my code, but I won't be offended if you tell me you can't understand what I'm doing.

The algorithm for determining the best move for the computer player can be greatly improved. Again, the goal was to learn the language, not to write the best AI game player. There are some fairly obvious ways of improving performance of it without major rewriting of the code. I'll probably never get around to it though.