Lab Assignment 4

  1. [solve] For Lab 3 you wrote a function called binary that converts a number into its binary representation. For this problem, copy that function definition into your program here, and surround it by Define ... %Define. Then add an Execute part that defines n = 35 and displays the binary representation of n. The Execute part should use your function to convert n to a string that should be displayed. Write it so that if you change the definition of n, then that new value will be shown.

  2. [solve] Your definition of function binary in the preceding problem uses cases. Rewrite that program so that the definition of binary does not use cases, but instead computes using statements. The definition should have the form

      Define
        binary(n) = str |
         ...
      %Define
    
    where the ... part is replaced by statements that define str. You will need an If-statement. You might want to write open in front of If so that definitions made inside the If-statement are available outside of the If-statement.

  3. [solve] The Towers of Hanoi puzzle has three posts and some number n of disks. Each disk has a hole in the middle, so that it can be put on a post, and the disks are of different sizes.

    Initially, the disks are all stacked on the leftmost post, with larger disks closer to the bottom and smaller disks closer to the top. For example, if there are three disks, the puzzle starts out looking like this.

    The object is move all of the disks from the leftmost post to the rightmost post. So when the puzzle is solved, it looks like this.

    But there are some rules that must be followed.

    1. Only one disk can be held in your hand at a time. The other disks must be on posts.

    2. A disk can only be put down by placing it on one of the posts. For example, you can't put a disk on the table beside the puzzle.

    3. No disk can ever be put on top of a smaller disk. You can only put a disk on top of one that is larger than it.

    It is convenient to refer to the posts by names, such as L, M and R, for left, middle and right. Instructions to move three disks from L to R are as follows.

      Move a disk from L to R.
      Move a disk from L to M.
      Move a disk from R to M.
      Move a disk from L to R.
      Move a disk from M to L.
      Move a disk from M to R.
      Move a disk from L to R.
    

     

    Write a procedure Hanoi(n, a, b, c) that takes a nonnegative integer n and three characters a, b and c, which are names of posts. (The names can be any characters that you choose.) It should write instructions to move n disks from post a to post b, where the third post is called c. For example, statement

      Hanoi(1, 'P', 'Q', 'E').
    
    should write instructions to move one disk from post 'P' to post 'Q', where the third post is called 'E'. It writes
      Move a disk from P to Q.
    
    since that is all that is required to move one disk. Statement
      Hanoi(2, 'A', 'B', 'C').
    
    should write instructions to move 2 disks from post 'A' to post 'B' with the third post called 'C'. It writes
      Move a disk from A to C.
      Move a disk from A to B.
      Move a disk from C to B.
     
    Statement
      Hanoi(3, 'L', 'R', 'M').
    
    should write instructions to move 3 disks from post 'L' to post 'R', with a third post called 'M'. It writes
      Move a disk from L to R.
      Move a disk from L to M.
      Move a disk from R to M.
      Move a disk from L to R.
      Move a disk from M to L.
      Move a disk from M to R.
      Move a disk from L to R.
    

     

    Write Define ... %Define around the definition of Hanoi. The overall form will be

      Define
        Hanoi(n: Integer, a: Char, b: Char, c: Char). =
          ...
      %Define
    
    where you fill in the body (...). See the hints below.

     

    Follow the procedure definition by an Execute part that defines n = 4 and writes instructions to move n disks from post 'L' to post 'R' with the third post called 'M'. Write it so that, if you change the definition of n to say that n = 5, then the program will write instructions to move 5 disks, without any other changes being necessary.

    Test your program. Does it work? Try it with smaller values of n, and check that the instructions are correct. (The correct instructions are more obvious for smaller values of n.) When you are confident that it works, submit it.

     

    Hints for writing the definition of procedure Hanoi.

    1. If n = 0 then you are asked to write instructions to move no disks. Just do not write anything at all.

    2. If n > 0, then break the problem down into the following steps.

      1. Ask somebody else to move n-1 disks from a to c, with the third post called b. That moves the top n-1 disks out of the way, on the extra post c, exposing the n-th disk on the start post. Just use your Hanoi procedure to do this. Notice that the instructions written by somebody else will follow the rules of the puzzle, so you don't need to worry about that. But what those instructions will accomplish, in the end, if moving the top n-1 disks out of the way just as if they had been picked up as a group and moved.

      2. Write one line saying to move a disk (the n-th one) from a to b. Now the bottom disk is where it is supposed to be.

      3. Finally, ask somebody else to move n-1 disks from c to b, with the third post called a. That moves the top n-1 disks on top of the one on post c.

    Note that a, b and c refer to the post names given to Hanoi. They are not literally 'a', 'b' and 'c'.

  4. [solve] 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.