The game of Craps is played with two dice. The player throws the dice to begin. If the outcome (the sum of the dice) is 7 or 11, the player passes (wins), and the game is over. If the outcome is 2, 3 or 12, the player does not pass (loses), and the game is over.

If the outcome of the first throw is anything except 2, 3, 7, 11 or 12, then the outcome of the first throw becomes the players point. The player repeatedly tosses the dice until either he or she throws a value equal to the point (in which case the player passes) or the player throws a 7 (in which case the player does not pass).

For this exercise, you will write a program that simulates one game of Craps. It will display the coin toss results until the game is finished, then will report Pass or No Pass. For example, it might display

7
Pass
but then the next time you run it it might display
9 5 8 10 3 7
No Pass
and yet another run might display
6 10 6
Pass

This program will involve writing a few functions and procedures.

  1. You will need to simulate tossing a pair of dice. Expression randomRange(1,6) yields a randomly chosen number from 1 to 6. To toss two dice, just do that twice and add the results. Be sure to toss two separate dice. Do not just toss one and double the answer.

    A function can have no arguments. You express that by writing ( ) after the function's name. Write a function tossDice( ) that yields the (random) result of tossing a pair of dice.

    Write an Execute part to test your function. It should be as follows.

    Execute
      Randomize.
      Let t = tossDice().
      Displayln t.
    %Execute
    
    The Randomize line initializes the random number generator.

    Try your program a few times. Does it appear to be producing reasonable results? If so, then move on.

  2. Now, between the definition of tossDice and the Execute part, write a definition of a procedure PlayWithPoint(point) that plays the part of a game after a point has been chosen (and was not 2, 3, 7, 11 or 12). It should display each roll of the dice, all on one line, with a space between them. Then it should report Pass or No Pass on a new line.

    The procedure will start by rolling the dice and displaying the result. If the roll is the same as the point, then it should end the line and display Pass. If the roll is 7 then it should end the line and show No Pass. Otherwise, it should just use itself, with the same point, to finish the game.

    Modify the Execute part to do statement

      PlayWithPoint(8).
    instead of just tossing dice. Try it a few times. Notice that it does not show the initial 8. That is expected. When it appears to work, move to the next part.

  3. Between the definition of PlayWithPoint and the Execute part, define another procedure PlayCraps(). It should play a complete game of craps.

    To play a game, it rolls the dice and displays the result. If the roll is 7 or 11, it ends the line and displays Pass. If the roll is 2, 3 or 12, it ends the line and displays No Pass. In all other cases it finishes the game by using procedure PlayWithPoint.

    Modify the Execute part so that it does statement

      PlayCraps().
    instead of using PlayWithPoint. Try the program a few times. When the results look right, submit the program.

 

  [Language: Cinnameg  Kind: program]