Scratch Problem
1
CS110 - Introduction to Computers and Applications Spring 2016 - Scratch Project #5
Due Date: Friday, April 15th , at 11:00 P.M.
Accept Until Date: Friday, April 22 nd
, 11:00 P.M.
Evaluation: 20 points
Submit to Sakai: Scratch program (.sb file)
Related Materials: Resources posted in Sakai → Lecture Topics → Scratch Scratch Website: http://scratch.mit.edu/
TA Lab Support Schedule (in Sakai Resources Folder)
Questions? Meet with your TA during office hours; Visit a campus computing center during CS110 Lab Support Hours
Background
In this assignment you will use the build another Scratch program. In this program you will be working more with variables, loops, random numbers and lists. _____________________________________________________________________
Goal This assignment will require you to create a Scratch program using lists. A list allows us to associate one variable name with multiple items or values. Scratch provides us with the following instructions and references.
add item to end of list
Delete item from list (in this case, the first item)
adds item in a specified position in list (in this case, position 1)
replaces item in list with some other value (in this case, item 1 replaced with thing).
refers to an item in the list (in this case, item 1)
refers to the number of items in the list.
Deletes one (or all) elements from the list
Deletes the element in position position
2
from the list Example code using lists:
Adding items to a list
Open Scratch. Go to Variable block. Click on Make A List. Name the list food.
The list is currently empty.
Click on the + sign and add a food item. Do this at least three times.
Reporting items in a list
Make a new variable named count.
Add the code on the right. o count will keep track of which item
in the list you are looking at o repeat loop will execute as many times
as it has items in the list o the first item I want is item #1 so I change
count by 1. Then I want item #2 so the second time through the loop I increment count again. I do this until I've looked at each item.
Hide your food list by un-checking the checkbox next to the list variable, food.
Alternative method for adding items to a list: You can also populate your list by importing items from a text file. This way is much easier. Use a text editor to create a file named friends.txt. Add the following to your file, one per line and safe your text file.
Mickey Mouse Donald Duck Tom and Jerry Bugs Bunny Tweety Bird Garfield Speedy Gonzales Pluto Bambi
Create a Scratch list and name it friends. Right click on the empty friends list on the stage. Click import and navigate to friends.txt. Like magic, your list is populated and it has length 9. Modify your previous code segment:
3
Now that you have the basics of working with lists, you are ready to begin your assignment! A video of a sample solution is here. Your Assignment:
To prepare: Create an environment with 5 inhabitants. For example, have your background be water and have 5 different fish sprites or have your background be a forest and have 5 different forest animals. Each sprite will have its own task. The tasks to be included are below. Each sprite will have a script that will accomplish one of these five tasks. You will most likely need two lists (one working list and one helper list for the Sort option). You will need several well-named variables. Stage algorithm: The Stage will execute the tasks below when the green flag is clicked: 1. Delete all elements from all lists. 2. Fill one of the lists with 25 random numbers from 10 to 100. 3. Check to see if game is over. If so, change backdrop to appropriate ending. There may be other tasks that are not listed. TASKS for the sprite scripts (each sprite has one task)
1. Find the largest (or smallest) number in the list and report this (say for 5 sec). You may use whatever algorithm you wish to accomplish this task. The simplest is to iterate through the list keeping track of the largest value so far. You’ll need a variable to hold the current largest and a variable to keep track of your position in the list
2. Count the number of numbers in the list that are greater than (or less than) 50 and report that count value (say for 5 sec).
Traverse through the list and ask the right question! You will need a variable to keep track of your position in the list and a variable to do the counting.
3. Delete all of the values between 30 and 70 inclusive and report how many items have been deleted (say for 10 sec).
You may use any algorithm you like to accomplish this task. You will need to traverse the list. When you come across an element that is between 30 and 70 inclusive, delete it. There’s a bit of a trick here with the traversing. It’s easy to skip over elements when there are consecutive elements that satisfy the delete condition. Be careful. One of the easiest ways to do this is to traverse the list in REVERSE order (Start traversing with the last element instead of the first.). You’ll need a variable to keep track of your position in the list and a variable to count the ones you delete.
4. Duplicate each entry in the list. For example is the original list is 1 3 5 2 then the result would be 1 1 3 3 5 5 2 2. Report that you have completed doing this.
You may use any algorithm you like to accomplish this task. You will need a variable to keep track of your position when you traverse the list (pos). When you are at position pos, simply insert that item to the position immediately following pos. Now skip over that position before you duplicate the next item. Indicate that the job is done in a SAY instruction. Check your resulting list.
4
5. Sort the list in decreasing (or ascending) order (largest first). Report that you have completed doing this.
You may use any algorithm you like to accomplish this task. One possible algorithm: Use a second list. Find the largest item in the first list, add that item to the second list and delete it from the first list. Do this repeatedly until the first list is empty. Since add adds items to the end of the list, you will be adding to the second list in the correct order. Now you simply copy the items from the second list back into the empty first list! Indicate that the job is done in a SAY instruction. Check your resulting list. Warning: when checking for the largest, you need to use >= not just > to take care of duplicate values in your list.
The play: When the green flag is pressed, each of the sprites will:
a. Become visible b. go to a random position on the stage c. Pause 1 second d. Continue steps b and c until touching the mouse pointer. When the user touches the sprite with
the mouse pointer, the touched sprite will stop moving and announce what its job is, will do its job, will announce that it is done (say instructions should last 5 – 10 seconds). After the task is complete, the sprite should hide. Be careful when running the program to touch only ONE sprite at a time. Let that sprite finish its job before touching the next. Ideally, when a sprite is touched, all movement will stop until the sprite finishes its task.
e. When all five sprites have done their jobs, the animation ending will be executed. You should think of a creative ending to be executed when all 5 tasks have been completed.
Ideally, when one sprite is touched by the mouse pointer, all movement will stop and movement will continue only after the working sprite completes its task. Ideally, after the sprite does its job, it will hide and stop its script. This takes many correctly created and executed broadcasts.
Also, when testing this, sort the list before duplicating values (saves time because you will have twice the number of elements when you duplicate).
CS110 - Introduction to Computers and Applications Scratch #5 Assignment Self-Check: 20 points
Directions: Use the checklist below to review your work before submitting it to Sakai for evaluation by your TA. Read the problem description above to be sure you understand the program specifications.
Fill list with 25 random numbers from 10 – 100.
Find and report largest (or smallest) number in list in a sentence indicating what the task was.
Count numbers in the list that are greater than (or less than) 50 and report this value in a sentence indicating what the task was.
Delete items with values between 30 and 70 inclusive and count number of items that were deleted. Report the count value in a sentence indicating what the task was.
Duplicate each value in list. When the task is complete, report this fact in a sentence indicating what the task was.
Sort the list in descending (or ascending) order. When the task is complete, report this fact in a sentence indicating what the task was.
5
Sprites move randomly on stage; Sprite stops moving on mouse touch and does its job; Sprite hides when job is complete. Ideally, all movement stops when a sprite is touched.
When all 5 sprites have done their jobs; there is some creative ending. Ideally, all sprites will be hidden when all tasks have been completed.
There is some way of determining when all five sprites have done their jobs.
Meaningful variable names are used.
Code is readable and well-written.
Program has as a theme (fish in water, animals in forest, etc.) and a creative ending.