CS 202 Fall 2010: Assignment 6
Homework Assignment #6 : Due Friday 11/5 Before Lecture
The purpose of this assignment is to give you experience with Lists in Scratch. You will complete a project we have started. To get the initial code, go to:
http://scratch.mit.edu/projects/dusseau/1379837
This code simulates an Inventory that needs to be tracked in order for Holiday Elves to deliver the correct presents to boys and girls.
In the initial code you are given, the “When Green Flag Click” script controls the execution of all the other scripts; it broadcasts messages to start each script and waits for each script to complete before displaying the new contents of the variables and/or lists. You should not modify this main script. You also may not modify the Wish List, the Kids list, or the Bad Kids list at any time. Your scripts must work for lists with any number of elements (i.e., do not assume there are always 20 kids).
You are welcome to change the background and the costume of the main Sprite to any theme you desire, but this is purely optional.
You are expected to complete five scripts (and one more for extra credit). The scripts become progressively more difficult to implement. You are free to create any new variables which you think are necessary. None of the scripts require many Scratch blocks, but every little detail does matter!
If you can't figure out the Scratch code that you need to write, you wil probably find it useful to think through the algorithm that you would follow if you needed to do these operations by hand. The code should be very similar to the steps you would do!
- Say Wish List: This script takes as input two Lists which
have already been created and initialized for you. Kids
contains the names of the children you must deliver presents
to. Wish List contains the toy each of those children
wants. Each element of Wish List is the toy the corresponding child
in the Kids list wants. For example, the first child in the Kids
list wants the first toy in the Wish List; the second child inthe
Kids list wants the second toy in the Wish List; and so on.
You should modify this script so that the Elf Sprite "says" (in a
loop) what each child wants.
Hint: You will need to allocate a new variable to allow you to index into each location of the two lists.
- Kid Wants Toy: This script takes as input the Kids list
and the Wish List described above; it also takes a variable Kid as
input. This script should "set" the variable Toy to be the
toy this particular Kid wants (according to the values in the Kids
and Wish Lists). When this script is implemented correctly, the
main control script will then "say" the toy that different (randomly
selected) kids want.
- Good Kids: This script takes as input the Kids list and
a Bad Kids list. The script should add those Kids who are
not in the Bad Kids list to the Good Kids
list. Hint: You may want to check if [Bad Kids] contains [some
new variable].
- Make Inventory: This script takes as input just the Wish
List. For output, it adds items to a new
list Inventory such that each item on the Wish List appears
exactly once in the Inventory. For example, if the Wish List
contains "bike", "book", "bike", "bike"; then the Inventory should
just contain "bike" and "book". The order of items in the Inventory
list does not matter. Hint: You are likely to want to check if the
Inventory list already contains a particular item before adding that
item again.
- Tally Inventory: This script takes as input the Wish List
and the Inventory you created in the Make Inventory script.
(So, yes, you must get the Make Inventory script correct before
implementing this step!) It should add counts to a new
list Tallies to show how many of each toy needs to be built
by the Elves; the order of the elements in Tallies must match the
order of elements in the Inventory List. For example, if the Wish
List contains "doll", "bike", "book", "doll" and Inventory contains
"doll", "bike", "book" then Tallies must contain "2", "1", "1"
(because the Elves must make 2 dolls, 1 bike, and 1 book to satisfy
the Wish List).
Hint: You are likely to want to implement a nested loop in this script. Specifically, you will probably have an outer loop that examines each element of the Inventory list; within each iteration of the outer loop you will enter another (inner) loop that examines each element of the Wish List; this inner loop will count how many toys it finds in the Wish List that match the particular Inventory toy. You will need separate index variables for each of the two loops as well as a variable to record the current tally for the current toy.
- Find Most Popular: This script is worth 2 points of Extra Credit. It takes as input just the Tallies list; for output, it places the index of the most popular toy in the Inventory in the variable Index of Most Popular Toy. For this script to work correctly, you must have the Tally Inventory script working correctly. After this script is working correctly, the main script will say how many of the most popular toy must be built.
Turning in Your Work
Please turn in your work into the Folder Homework 6 through Learn@UW.
Menu
Fall 2010Time: MWF 9:55-10:45
Room: 105 Psychology
Lab: 1370 CS (1st floor)
Instructor:
Prof Andrea Arpaci-Dusseau
Office Hours Tue 2:30-3:30, Wed 11-12
Office: 7375 Computer Sciences
Email: dusseau "at" cs.wisc.edu