Assignment 11: Traversing a Cave, Escaping a Prison (due Nov 24)



Before you tackle the homework, remind yourself of our General Advice, Advice on Homeworks, and Grading Guidelines. Above all, keep your work neat and honest.


This assignment contains one required problem and one extra credit problem.
  1. (10 pts) In this problem, you will implement a simple maze navigation game. In particular, you will implement two aspects of this game: creating mazes and creating players. You can control the players from the interactions window (see the sample interaction below) -- you are not required to write any programs to direct players through the maze.

    Mazes : The maze is a collection of caves. Each cave has a name, and may contain one item, e.g., coins, a loaf of bread, a compass, president Gillis, etc. It also has a number of doors that connect the cave to other caves. A cave named 'Exit serves as the exit from the maze.

    Players: As a player walks through the maze, he can collect up to two items in his two pockets. When the player enters a cave, he may pick up or exchange items. Specifically, if one of the pockets is empty, the player can put the cave's item into his pocket. If both pockets are filled with items, the player may exchange the content of one of his pockets with the item in the cave. Once the player is happy with the state of things, he moves on by choosing one of the exits out of the cave. The game is over when the player reaches the exit of the maze.

    #| --------------------------------------------------------------- 
       Players    
       create-player : cave -> object
    
       Purpose: object deals with the following messages: 
         where -- get the name of the current location (cave)
         what  -- get the item stored in the current cave 
         show-next-exit -- shows one exit at a time, i.e., shows list of exits
                           in circular fashion, one exit each time the message
    		       is sent (you choose information to show for each exit)
         go! -- switch current-location to last cave seen 
         left  -- show contents of left pocket
         right -- show contents of right pocket
         switch-left -- switch contents of left pocket and item in current cave
         switch-right -- switch contents of right pocket and item in current cave
    |#   
    

    Here is a sample dialogue, based on the above sample cave definition.

    > (define the-player (create-player dwarf-cave))
    > (the-player 'show-next-exit)
    'happy-cave
    > (the-player 'show-next-exit)
    'grumpy-cave
    > (the-player 'show-next-exit)
    'doc-cave
    > (the-player 'show-next-exit)
    'happy-cave
    > (the-player 'go!)
    > (the-player 'where)
    'happy-cave
    > (the-player 'what)
    'smiley-face
    

    End of Problem Definition This problem is open-ended and flexible. The design of the dialogue is up to you. You are in control of how you output descriptions of items, caves, or connections, if you choose to do so. It may be a simple text-based dialogue as above. It is acceptable to implement the minimum described above, but don't feel constrained by this standard; if you implement more, tell us. Be creative! For example, you may wish to use graphic icons (gifs or xmps) for the items in the caves instead of plain symbols for the items.


  2. (Extra Credit: 3pts) In the kingdom of Tuhundredtenia, the jail system is extensive, with 150 cells all in a row. Each cell contains one prisoner. Each cell's door has a key, which when turned toggles the door's lock. That is, if the door was unlocked, then turning the key locks it; if locked, then turning unlocks it.

    At the start of the evening, all cells are locked, but the Royal Jailer has a curious habit. Every evening, he turns the key in every door---numbers 1 through 150. Then, he goes back and turns the key in every other door---numbers 2, 4, 6, ..., 150. Then, he goes back and turns the key in every third door---numbers 3, 6, 9, ..., 150. Then, he turns the key in every fourth door---numbers 4, 8, 12, ..., 148. And so on, until he finihes by turning the key in every 150th door (just number 150). After that, he falls asleep, and anybody in an unlocked cell can escape during the night.

    You and your friends have been framed, and falsely convicted of Meandering with Intent to Jay-walk, and are scheduled to be executed at dawn. Fortunately, each of you can name which cell to be locked away in. Which cell numbers do you ask for?

    Develop a program that computes the indices of all open cells in the prison, assuming there are CELL# prison cells. Start with CELL# defined as 10; at the end switch to 150.

    Your program does not need to consist of a single function. Instead, it should simply end in an expression that computes the open prison cells, followed by one that prints them. The sequence starts with 1. Why?

    Hint: Use the following abstraction:

    ;; all : N (N -> boolean) (N -> void) -> void
    ;; Effect: the effect of action on j, such that 0 < j <= i and (predicate j)
    (define (all size predicate action)
      (cond
        [(= size CELL#) (void)]
        [else (begin
    	    (cond [(predicate size) (action size)]
    	          [else (void)])
    	    (all (add1 size) predicate action))]))
    
    You can use the abstraction as follows to print the open cells:
    ;; print the state of the vector 
    (all 0 
         (lambda (i) (= (vector-ref cells i) UNLOCK)) 
         (lambda (i) (printf "open ~s ~n" (+ i 1))))
    
    assuming cells is the name of the vector of cells.




Kathi Fisler (problems due to Matthias Felleisen) This page was generated on Tue Nov 16 17:32:14 CDT 1999.