6.6

Assignment 15

home work!

Programming Language ISL

Due Date Thursday 11/8 at 9pm

Purpose To process mutually-recursive data.

Expectations
  • You should submit a single .rkt file containing your responses to all exercises via the Handin Server. We accept NO email submissions.

  • You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.

  • All steps of the design recipe are required unless otherwise specified.

  • You are expected to use pre-defined abstractions when stated and/or when appropriate.

; A Section is one of:
; - Ending
; - Chapter
; and represents a section in a choose your own adventure book
 
; An Ending is a (make-ending String Boolean)
(define-struct ending [text good?])
; and represents an ending to the story with some text and whether it was a happy ever after
 
; A Chapter is a (make-chapter String [NEList-of Choice])
(define-struct chapter [text choices])
; and represents a chapter in a choose-your-own adventure book with a body
; and a list of choices at the end
 
; An [NEList-of X] (Non-Empty List) is one of:
; - (cons X '())
; - (cons X [NEList-of X])
 
; A Choice is a a (make-choice String Section)
(define-struct choice [text result])
; and represents the blurb shown to the reader and the resulting section if that is the
; choice they make
 
(define MY-STORY
  (make-chapter
   "You are alone in a room. There is a door before you."
   (list
    (make-choice
     "Stay in the room."
     (make-ending "You stay in the room. Nothing happens. Nothing ever will again." #f))
    (make-choice
     "Open the door."
     (make-chapter
      "You open the door. On the other side lays an eternity of nothingness."
      (list
       (make-choice
        "Scream."
        (make-ending
         "You scream into the void. There is no response. There never will be." #f))
       (make-choice
        "Accept your fate."
        (make-ending
         "You accept the simple beauty of the void. You achieve nirvana." #t))))))))

Exercise 1 Design the template for a Section.

Exercise 2 Design a function which, given a Section, produces the total number of possible endings to the story.

Exercise 3 Design a big-bang program which takes in a Section and allows the player to move through the story.

For chapters, it shows the player the text of the section, and beneath it, a list of the choices they have. The player can use the arrow keys (up/down) to tab through the choices available and then use enter (KeyEvent "\r") to move on to the selected result. There should be some clear visual indication of which choice the player would make upon pressing enter.

Be sure to handle up/down arrow keys at the beginning/end of the list of choices; pressing up/down at the top/bottom of the list of choices should loop which choice is currently selected back to the bottom/top of the list.

The game is over when the player reaches an ending. The program should display the text of the ending in blue if it is a happy ever after and in red if it is not. The program should output the text of the last section shown when the game ends or when the user exits.

Be sure to provide your stop-when clause your drawing function as a second argument so the ending is displayed when the program stops, as well as providing an example and template for your world state (the design of which is up to you to determine).

Exercise 4 Consider the following data definitions:
; A PlayerSimulation is a [List-of KO]
; and represents the key events a player makes when playing a choose your own adventure game
 
; A KO (Key Option) is one of:
; - "up"
; - "down"
; - "enter"
; and represents either pressing the up arrow, down arrow, or enter/return key
 
; A Result is one of:
; - "happy"
; - "sad"
; - "incomplete"
; and represents whether a story ended happilly, sadly, or did not yet end
Design a function which, given a Section and a PlayerSimulation, produces a Result representing the state of the story after following the player’s choices.

Assume that upon entering a new Chapter, if the player pressed enter/return before pressing any arrow keys, the first Choice would be selected. Hint: that might give you an idea for a design choice in the past exercise.

If the story "runs out" before the key options do, ignore the remaining key options; this is akin to the big-bang program ending when an ending is reached.

You do not have to provide examples or templates for the above data definitions.

Extra Credit

The following exercise is extra credit. Not completing it will have no negative impact on your grade. Note that as it is extra credit it is all-or-nothing as far as grading is concerned: a well-designed, correct solution will result in full credit, and everything else will result in no credit. Given this, it is obviously a good idea to work on this only after you are sure the past four exercises are in an excellent condition. Also, as you likely suspect, this is a difficult problem.

Exercise 5 Design the function find-happily-ever-after, which given a Section, produces a PlayerSimulation which results in a happy ever after. If there is no happy ever after to be found, the function should produce #f.

There should be no "up" key options in the result, and the left-most happy ending is the one that should be produced. In other words, if a happy ever after could result from choosing either the first or second choice, the first one should be chosen.