|
Projects
Spring 2012 Older Courses Fall 2011 Spring 2011 Fall 2010 Spring 2010 Fall 2009 Spring 2009
Fall 2008
Spring 2008
Fall 2007 HOWTOs |
OPLspr09 /
PS7Home Assignments Lecture Blog Resources Project Discussion Group 91.301 Organization of Programming Languages Out: March 30, 2009 Problem Set 7: Object-Oriented Adventure Game and Final Project Exploration 2OverviewThe programming assignment for this week explores two ideas: the simulation of a world in which objects are characterized by collections of state variables, and the use of object-oriented programming as a technique for modularizing worlds in which objects interact. These ideas are presented in the context of a simple text-based adventure game like the ones available on many computers. Such games have provided an interesting sink of time for many computer lovers. In order not to sink too much of your own time, it is important to study the system and plan your work before starting to write any code. This problem set begins by describing the overall structure of the simulation. The warm-up exercises in Part 2 will help you to master the ideas involved. Part 3 contains the assignment itself. Also, in Part 4, you will continue exploration of libraries and tools that you may wish to use in your final project. CreditsThis assignment was developed by the Scheme group at MIT, adapted by Holly Yanco, and edited by Fred Martin. Reading
ImplementationUse the Module language in DrScheme for this assignment. Download these two files: Attach:world-plt4.ss and Attach:game-plt4.ss. Part 1The SICP Adventure GameThe basic idea of adventure games is that the user plays a character in an imaginary world inhabited by other characters. The user plays the game by issuing commands to the computer that have the effect of moving the character about and performing acts in the imaginary world, such as picking up objects. The computer simulates the legal moves and rejects illegal ones. For example, it is illegal to move between places that are not connected (unless you have special powers). If a move is legal, the computer updates its model of the world and allows the next move to be considered. Our game takes place in a strange, imaginary world called UMass Lowell, with imaginary places such as a computer lab, a robot lab, and a department office. In order to get going, we need to establish the structure of this imaginary world: the objects that exist and the ways in which they relate to each other. Initially, there are three procedures for creating objects: (make-thing name) (make-place name) (make-person name birthplace restlessness) In addition, there are procedures that make people and things and procedures that install them in the simulated world. The reason that we need to be able to create people and things separately from installing them will be discussed in one of the exercises later. For now, we note the existence of the procedures (make&install-thing name birthplace) (make&install-person name birthplace restlessness) Each time we make or make and install a person or a thing, we give it
a name. People and things also are created at some initial place. In
addition, a person has a restlessness factor that determines how often
the person moves. For example, the procedure (define computer-lab (make-place 'computer-lab)) (define robot-lab (make-place 'robot-lab)) (define holly (make&install-person 'holly robot-lab 3)) (define andy (make&install-person 'andy computer-lab 2)) All objects in the system are implemented as message-accepting procedures. Once you load the system on your machine, you will be able to control (ask holly 'look-around) At robot-lab : holly says -- I see nothing () (ask (ask holly 'place) 'exits) (south) (ask holly 'go 'south) holly moves from robot-lab to west-hall #t (ask holly 'go 'east) holly moves from west-hall to elevator-lobby #t (ask holly 'go 'north) holly moves from elevator-lobby to computer-lab At computer-lab : holly says -- Hi andy #t (ask andy 'look-around) At computer-lab : andy says -- I see holly In principle, you could run the system by issuing specific commands to each of the creatures in the world, but
this defeats the intent of the game since that would give you explicit control over all the characters. Instead,
we will structure our system so that any character can be manipulated automatically in some fashion by the
computer. We do this by creating a list of all the characters to be moved by the computer and by simulating
the passage of time by a special procedure, Before we trigger the clock to simulate a game, let's explore the properties of our world a bit more. First, let's create a (define sicp-textbook (make&install-thing 'sicp-textbook computer-lab)) Next, we'll have (ask holly 'look-around) At computer-lab : holly says -- I see sicp-textbook andy (sicp-textbook andy) (ask holly 'take sicp-textbook) At computer-lab : holly says -- I take sicp-textbook #t (ask holly 'go 'south) holly moves from computer-lab to elevator-lobby #t
(ask andy 'go 'south) andy moves from computer-lab to elevator-lobby At elevator-lobby : andy says -- Hi holly #t (ask andy 'take sicp-textbook) At elevator-lobby : holly says -- I lose sicp-textbook At elevator-lobby : holly says -- Yaaaah! I am upset! At elevator-lobby : andy says -- I take sicp-textbook #t (ask holly 'go 'west) holly moves from elevator-lobby to west-hall #t (ask holly 'go 'south) holly moves from west-hall to network-closet #t Unfortunately for (ask grendel 'move) grendel moves from dungeon to network-closet At network-closet : grendel says -- Hi holly #t After a few more moves, (ask grendel 'move)
At network-closet : grendel says -- Growl.... I'm going to eat you, holly
At network-closet : holly says --
Dulce et decorum est
pro computatore mori!
holly moves from network-closet to heaven
At network-closet : grendel says -- Chomp chomp. holly tastes yummy!
*burp*
ImplementationThe simulator for the world is contained in two files. The first file,
You can find these files on the course web site. Load and evaluate the world file; this will load in the game file as well. Part 2Warm-up ExercisesDo these exercises before starting work on the computer. These exercises should be turned in with your assignment. Exercise 1: Draw a simple inheritance diagram showing all the kinds of objects (classes) defined in the
adventure game system ( Exercise 2: Draw a simple map showing all the places created by evaluating Exercise 3: Suppose we evaluate the following expressions: (define pizza (make-thing 'pizza robot-lab)) (ask pizza 'set-owner holly) At some point in the evaluation of the second expression, the expression (set! owner new-owner) will be evaluated in some environment. Draw an environment diagram, showing the full structure of Exercise 4: Suppose that, in addition to (define pepperoni-pizza (make-named-object 'pizza)) Are Part 3Main AssignmentSolutions to these exercises should also be turned in with your solutions to the warm-up exercises. Exercise 5: We suggest that you do this exercise before you start coding the assignment, because it illustrates a bug that is easy to fall into when working with the adventure game. Note how
(define (make-person name birthplace threshold)
(let ((mobile-obj (make-mobile-object name birthplace))
...)
(lambda (message)
(cond ...
...
((eq? message 'install)
(lambda (self)
(add-to-clock-list self)
((get-method mobile-obj 'install) self) )) ; **
...))))
(define (make-mobile-object name place)
(let ((named-obj (make-named-object name)))
(lambda (message)
(cond ...
...
((eq? message 'install)
(lambda (self)
(ask place 'add-thing self)))
...))))
Louis Reasoner suggests that it would be simpler if we change the last line of the make-person version of the install method to read: (ask mobile-obj 'install) )) ; ** Alyssa P. Hacker points out that this would be a bug. If you did
that, she says, then when you What does Alyssa mean? Specifically, what goes wrong? You will likely need to draw an appropriate environment diagram to explain carefully. Exercise 6: We do not expect you to have to make significant changes in the You have a This way, whenever you change some procedure you can make sure your world reflects these changes by simply re-evaluating the entire world.ss file. Finally, to save you from retyping the same scenarios
repeatedlyfor example, when debugging you may want to create a
new character, move it to some interesting place, then ask it to
actwe suggest you define little test script
procedures at the end of the See the comments in Exercise 7: Implement Initally, all students have not passed OPL. A student has a method
Exercise 8: Make and install a new character of a student type, yourself, with a high enough threshold (say, 100) so that you have free will and are not likely to be moved by the clock. Place yourself initially in the Exercise 9: Define a procedure Note that you'll need to create the OPL place in your world. It's up to you whether or not to make this a place from which one can exit. In addition, you should make the Exercise 10: Now you have the elements of a simple game that you
play by interspersing your own moves with calls to the clock. Your
goal is to leave the To make the game more interesting, you should also create some
student(s) besides yourself and set up the student Turn in your new student Required for Honor Students and Graduate Students; Extra Credit for OthersExercise 11: Design a non-trivial extension to this simulated world. You can do what you want (so long as it is in good taste, of course). The extension can be as elaborate as you like, but don't go overboard; this is meant to be a problem set, not a term paper, after all. Try to base your extended simulation around a theme. Possibilities include classes, labs, food, robots, etc. Use your imagination! Whatever you choose to do, your simulation should include at least two
new kinds of persons, places, or things, using inheritance. For
example, you might implement a classroom as a new kind of place. Your
new objects should have some special methods or special properties in
relation to other objects. To continue the previous example, you might
make new kinds of people called
Note that it's more impressive to implement a simple, elegant idea, than to just amass tons of new objects and places. Part 4Final Project Exploration 2In this exercise, you will play with at least one library provided by the PLT Scheme developers. You will have the opportunity to explore another library in each of the next two weeks. Please choose libraries that you think you might be interested in using in your final project. Start off at the PLT Scheme home page, http://plt-scheme.org/ Then there are two places you can go for library code:
Your job is to explore one library and write up your results. Load the library and write some code to drive it around. For example, consider the PLT-Scheme GUI library. This linked from the main Docs page, as GUI: PLT Graphics Toolkit (see http://docs.plt-scheme.org/gui/). Here is code to display a frame and put a button in it. Then, a
lambda function is provided to handle the button-click event. This
function displays the string
#lang scheme
(require scheme/gui/base)
; Make a frame by instantiating the frame% class
(define frame (new frame% (label "Example")))
; Make a static text message in the frame
(define msg (new message% (parent frame)
(label "No events so far...")))
; Make a button in the frame
(new button% (parent frame)
(label "Click Me")
; Callback procedure for a button click:
(callback (lambda (button event)
(send msg set-label "Button click"))))
; Show the frame by calling its show method
(send frame show #t)
Honors and grad student exercise: Modify the lambda function provided as the button callback so that it displays a running count of the number of times the button is clicked. Do not use a global binding to keep track of the number of clicks; instead, do this by creating a binding (like an instance variable) in a closure of the callback lambda. To turn in: at least 2 pages of interspersed code and narrative explaining what you did. Reminder again: please think about what you might want to work on for your final project, and choose a relevant library to work with. |