CS 2135 (C04) Homework 2: Lists

Due: January 30 (Friday) at 11:59pm via turnin (assignment name hwk2).

Assignment Goals

To make sure you can
• write data definitions and templates for lists of structures,
• write Scheme programs over lists of structures,
• work with functions as values, and
• use map and filter, Scheme's built-in looping constructs.

The Assignment

You are developing an adventure game and need to manage the various characters in the game. The game has three kinds of characters:

• trolls, who have a picture of an object that they try to steal and and an indication of whether or not they have that object.
• knights, who have some number of lives and a count of how many trolls they have slain.
• wizards, who have a spell that they can cast on other characters, a rating of how powerful they are, and an indication of whether they are invisible. A spell is a function that takes in a character and returns a character.
In addition, each character contains a picture of itself. The section on images after the exercise list provides pictures (feel free to use your own instead) and explains how to work with images in DrScheme.

Exercises

1. Develop data definitions (with examples) for characters and lists of characters. Include in your examples:

• a wizard whose spell gives one more life to the character if it is a knight (and otherwise leaves the character alone).

• a wizard whose spell flips the picture of the character upside down. For this, the image-package functions that convert between images and color-lists will be helpful, as will the built-in Scheme function `reverse`, which reverses a list.

2. Write the template for programs over a list of characters.

3. Write a program `all-knights-alive?` that consumes a list of characters and returns a boolean indicating whether all of the knights have at least one life.

4. Write a program `subst-color` that takes two colors (from the image package) and a list of colors and returns a list of colors. In the returned list, all occurrences of the first color in the input list are replaced with the second color.

5. Write a program `show-characters` that consumes a list of characters and returns a list of images. The returned list contains an image for each of the characters in the input list. Trolls should be shown holding the object they seek if they have that object. Wizards are shown with different color robes based on their strength (purple for strength 3, red for strength 2, and white for strength 1). Invisible wizards should not appear in the output list.

To get the color of the wizard's robe, use the expression `(list-ref (image->color-list wizard) 6000)`, where "wizard" is the name of the wiard picture (the 6000 assumes that you did not resize the wizard picture). On my machine, the wizard's robe was in `(make-color 132 97 181)`.

6. Write a program `show-trolls` that behaves like `show-characters`, but displays only the trolls. Use your existing `show-characters` function in your solution (call it, don't duplicate the code).

7. Write a program `destroy-phone-trolls` that consumes a list of characters and returns a list of the same characters minus the trolls that are looking for cell phones.

8. Write a program `cast-spell` that consumes a spell and a list of characters and returns a list of the characters with the spell applied to each one.

9. Write a program `wanton-wizardry` that consumes a list of characters and casts all the spells from all the wizards in the list on all the characters in the list (returning a list of characters).

10. Write a program `make-vane-wizard` that consumes a wizard and produces a wizard. The returned wizard has the same power and visibility as the original wizard, and a spell that replaces the character's picture with a picture of the original wizard (leaving all other information about the character intact).

Working with Images

See the notes on Images. The following images may be useful for this assignment:

To add an image to your file within DrScheme, select the "Insert Image" option under the "Special" menu and choose the image file. You may find it cleaner to give all of the images names (using define) at the top of the file, then refer to those names in your actual code.

What to Turn In

Turn in a single file hwk2.ss (or hwk2.scm) containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file.

Guidelines

• Recall the Laws of Homework and the academic honesty policy when preparing your solutions.

• Use map and filter whenever appropriate in your solutions. Solutions that match the structure of map and filter but don't use them will lose some points.

• Follow the templates! We teach them to you because they help you organize your programs. Programs that don't follow the templates will lose points, so follow the templates (programs that use map and filter are obviously excepted from this rule).

• Aim for reuse where possible! If a program you write for one part is useful for another part, reuse it.

Back to the Assignments page