CS 1 - Lab 9

Outcomes:

Upon completion of the lab, the student will know how to:

1.

For this lab you will write implement a Yahtzee game using arrays.

  • Create an HTML file for this lab assignment called lab9.html (See the instructions for Lab 2 if you are unsure how to do this.)

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:
    1. Roll 5 dice.
    2. If all five dice are the same number, you have rolled a Yahtzee.
    3. If four of the dice are the same you have rolled a Four of a Kind.
    4. If three of the dice are the same and the other two are also the same you have rolled a Full House.
    5. If three of the dice are the same and the other two are not, you have rolled a Three of a Kind.
    6. If you rolled a 1, 2, 3, 4 and 5 or a 2, 3, 4, 5, and 6 you have rolled a Large Straight.
    7. 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
    8. If none of the above apply, you have rolled a chance.
    9. 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.

3.

When you are done, upload the lab9.html file to the CS server using FileZilla. (See the instructions for Lab 1 if you are unsure how to do this)