| 2. |
Yahtzee!
- Today you will be creating a Yahtzee game (or at least part of a Yahtzee game).
- If you are not familiar with Yahtzee, here are the rules:
- Roll 5 dice.
- If all five dice are the same number, you have rolled a Yahtzee.
- If four of the dice are the same you have rolled a Four of a Kind.
- If three of the dice are the same and the other two are also the same you have rolled a Full House.
- If three of the dice are the same and the other two are not, you have rolled a Three of a Kind.
- If you rolled a 1, 2, 3, 4 and 5 or a 2, 3, 4, 5, and 6 you have rolled a Large Straight.
- If you rolled a 1, 2, 3, and 4 or a 2, 3, 4, and 5 or a 3, 4, 5, and 6 you have rolled a Small Straight
- If none of the above apply, you have rolled a chance.
- The straights can be in any order.
- You must put the following on your web page for this lab:
- Five dice images that change with each roll
- A button labelled "Roll" that rolls the dice
- A text box that displays what you rolled according to the rules above (e.g. Yahtzee, Four of a Kind, Full House, etc.)
- You will find that the following function will make this much easier:
function GetStats(dice) {
// assumes: dice is an array of numbers each from one to six each representing a separate die
// returns: an array indicating the frequency of each number (rolls of 1 are at index 0, rolls of 2 are at index 1, etc.)
var stats, index;
stats = [0,0,0,0,0,0];
for(diceIndex in dice) {
// get the next die rolled
die = dice[diceIndex];
// add one to the entry for that die in stats
stats[die - 1]++;
}
return stats;
}
- For example, if you rolled the combination 3 4 4 3 4, you could store that in an array as [3, 4, 4, 3, 4]
- If you called that array rolls, then GetStats(rolls) would return the array [0, 0, 2, 3, 0, 0]
- If you called the new array rollStats, then you could access the number of times the number n was rolled
with the expression rollStats[n - 1] (it is necessary to subtract one because the first array index is zero)
- It may help to write down what a stats array would look like for each of the combinations described in the rules
- It may also help to use the Boolean operators we described in class:
&& means AND, || means OR, ! means NOT.
|