In this assignment, there are several animation problems. Since DrScheme doesn't allow you to put more than one animation in a single Definitions window, you'll need to turn in a separate Definitions window (and a corresponding, separate Interactions window) for each animation. If things you wrote for one animation are useful for another, feel free to copy and paste among them. There's no way to conveniently save the dynamic behavior of an animation to a file and e-mail it to me, but your interactions window should still show me the results of test cases on functions by themselves (before you use them in an animation).
For all the programming assignments, be sure to follow the design recipe. Write your function contract, examples, and function definition in the Definitions Window, save it to a file, and e-mail me this file. Also test your program: since you've already included examples in the Definitions window, you should be able to hit the Run button and see all the results (along with what you said they "should be"). Save the resulting Interactions window to a text file and e-mail me this file too. Be sure to choose meaningful names for functions and parameters, and watch for opportunities to re-use functions you, I, or the textbook have already written.
Also turn in a log of how many errors of different kinds you encountered in the assignment, with brief comments describing each one ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.
Develop a function named
counterchange
which takes in two images and returns a 2x2
arrangement of them, as shown at right. (Click on the image for a
full-scale view.)
Hint: Use the tiles.ss
teachpack.
Develop a function named checkerboard
which takes in two colors (e.g. strings) and returns a 2x2 arrangement
of them, as shown at right. Hint: This is extremely
easy if you re-use a previously-written function!
Develop a function that does something fun with images,
using some combination of image-above
,
image-beside
, reflect-vert
,
reflect-horiz
, rotate-cw
,
rotate-ccw
, and rotate-180
.
Develop a function named
smallest-of-3
which takes in 3 numbers and returns the smallest of them.
Three candidates (Anne, Bob, and Charlie) are running for mayor
of Schemeville, which, by court order, has a new computerized
voting system. Develop a function named
who-won
which takes in three numbers (the
number of votes for Anne, the number of votes for Bob, and the number of
votes for Charlie, respectively) and returns a string indicating the
name of the person who won -- either "Anne",
"Bob", or "Charlie".
For example,
(who-won 7 4 5) "should be" "Anne" (who-won 5 11 36) "should be" "Charlie"For now, you may assume that there are no ties.
Recall the traffic-light animation we did in class, with a single disk changing colors (green, yellow, red, green, ...). In this exercise we'll develop something that looks more like a real traffic light.
Write an expression that returns an image of a
rectangle with three circles outlined in black positioned inside it
vertically, something like the one at right. (Use the
empty-scene
function, described in the Help Desk, to get the background rectangle.)
Store the result in a variable for future use.
Develop a function choose-y
which takes
in a color (however you choose to represent that) and returns the
y-coordinate of the center of that light in this picture.
The red one should be on top, yellow in the middle, green on the bottom.
Develop a function show-world
which will
return an image of a traffic light with either the red, yellow, or
green light illuminated, as in the example at right. Be sure to use
the previous two problems to help!
Develop a handle-tick
function and
whatever else is necessary to create an animation of a traffic light
that changes colors every second.
Develop an animation of a ball that starts at location (3, 10) and moves by 2 in the x dimension and 1 in the y dimension every half second. This will require, as usual,
show-world
function to display the
world;handle-tick
function to modify the
world;big-bang
; andon-redraw
and
on-tick-event
respectively.Don't start on this until we've gotten to section 6.3 in the textbook! In some countries, after the traffic light has been red for a while, it switches to red-and-yellow before switching to green. We can't do that if our representation of a world is just a color, so let's come up with a more flexible representation. In fact, this representation will also allow us to build traffic lights that flash the red light on and off (with the green and yellow lights never on), or flash the yellow light on and off (with the green and red lights never on), or light up the red and green at the same time for Christmas, or light up all three lights at once, etc.
Develop an animation of a light that switches from green, to yellow, to red, to red-and-yellow, to green, etc. To do this, you'll need to first...
Define a data type light
which consists of three booleans: green?
,
yellow?
, and red?
. These will indicate
whether each of the respective lights is on or not; since they're all
independent of one another, we should be able to turn on or off
whichever combination we wish. As usual in defining a struct, you
must give several examples of constructing instances of the struct,
write down contracts for all the functions you get "for
free", and write two function templates: one for functions that
take in a light
, and one for functions that
return a light
. (Since these function
templates aren't complete function definitions, they should be
written in a comment in your Definitions window.)
Hint: The show-world
function may
require an auxiliary function or two (or three), and will presumably
re-use some of the things you wrote for the previous traffic-light
problem.
Don't start on this until we've gotten to section 6.3 in the textbook! Modify your animation from problem 7 to act sorta like a space ship: the ball moves at a fixed velocity in both x and y dimensions, and pressing an arrow key changes the velocity, not the position. For example, if the ship is currently at position (50, 30) and moving at velocity (3,1), a second later it would be at position (53, 31). If the user then pressed the left-arrow key, it would change the velocity to (2,1), so that a second later the ship would be at position (55,32). Three more quick left-arrow presses, and the velocity becomes (-1, 1), and a second later the position would be (54, 33).
To do this, you'll need to first define a data type
space-ship
to represent both the position and the velocity of a
space ship, since both of these things can change. As usual, you must
give several examples of constructing instances of the struct, write
down contracts for all the functions you get "for free", and
write two function templates: one for functions that take in a
space-ship
and one for functions that return a
space-ship
. These templates should be in comments.
Hint: You'll need both a
handle-tick
function, which changes the position, and a
handle-key
function, which changes the velocity.
Be careful of your function contracts!
Hint: It may be helpful to write several auxiliary functions, such as
move-ship
which moves the location of a ship by its current
velocity, and accelerate-ship
which adjusts the velocity of a
ship by specified amounts in the x and y dimensions.
Extra credit: modify the animation from problem 9 with either or both of the following features:
There's a planet at a fixed location in the center of the screen. Every second, not only does the space ship's position change according to its current velocity, but its velocity also changes due to gravity. The formula for gravitational attraction is
change in x velocity = x distance to planet * G / sqr(total distance to planet)where G is a constant. (I found that a value of 20 worked well: by adjusting the initial position and velocity of the ship, I was able to get it to orbit the planet.)
change in y velocity = y distance to planet * G / sqr(total distance to planet)
Doing this will require writing several auxiliary functions.
When the user presses an arrow key, a small red triangle
appears momentarily next to the space ship (on the left, right, top,
or bottom as appropriate) to indicate rocket exhaust. This will
require redefining the space-ship
data structure to contain
not only the location and velocity of the ship, but also the direction
of the most recent rocket blast -- right
, left
,
up
, down
, or none
. The
show-world
function will have to take this into
consideration in drawing the rocket ship, the handle-key
function will have to modify the direction, and the
handle-tick
function will have to reset the direction to
none
so that the exhaust disappears after a second.
Error log: /20
(I'm not grading on how many or how few errors you encountered,
only on whether you recorded them adequately.)
I'll fill in the rest of the grading rubric soon, but it'll be similar to that for homework 1.
Function name | Contract | Examples | Definition | Test results |
---|---|---|---|---|
counterchange |
/5 | /5 | /10 | /5 |
checkerboard |
/5 | /5 | /10 | /5 |
Something Fun with Images | /5 | /5 | /10 | /5 |
smallest-of-3 |
/5 | /5 | /10 | /5 |
who-won |
/5 | /5 | /10 | /5 |
Note that each function must, as always, have a contract, examples, definition, and results of test runs. If you're stuck on the definition, at least show me the skeleton.
Problem | Contract | Examples | Definition | Test results |
---|---|---|---|---|
Blank traffic light | /10 | /5 | ||
choose-y |
/5 | /5 | /10 | /5 |
show-world for traffic light |
/5 | /5 | /10 | /5 |
handle-tick for traffic light |
/5 | /5 | /10 | /5 |
data type for problem 7 | Data definition: /5 | |||
show-world for moving ball |
/5 | /5 | /10 | /5 |
handle-tick for moving ball |
/5 | /5 | /10 | /5 |
data type for problem 8 | Data definition: /5 | Examples: /5 | define-struct : /5 |
Contracts for access functions: /5 |
show-world for problem 8 |
/10 | /10 | /20 | /10 |
handle-tick for problem 8 |
/10 | /10 | /20 | /10 |
data type space-ship |
Data definition: /5 | Examples: /5 | define-struct : /5 |
Contracts for access functions: /5 |
show-world for space-ship |
/5 | /5 | /10 | /5 |
handle-tick for space-ship |
/10 | /10 | /20 | /10 |
handle-key for space-ship |
/10 | /10 | /20 | /10 |
Planet with gravity (extra credit) | /20 | /20 | /40 | /20 |
Rocket exhaust (extra credit) | /15 | /15 | /30 | /15 |
Following directions | /10 |
Writing contracts from word problems | /10 |
Choosing examples | /10 |
Choosing names | /10 |
Coding | /10 |
Code re-use and function composition | /10 |