home—lectures—exams—hws—breeze (snow day)
hw06
finishing asteroids
higher order functions
Due Oct.21 (Fri)23 (Sun)
In this assignment, we'll finish off the asteroids game (by adding bullets
and detecting collisions), using higher-order functions like
map, filter, and foldl as
appropriate.
All non-drawing functions require at least two test cases!
Good test cases and signature are worth about half the points for a function.
As mentioned on the discussion board:
- Include a signature for every function you write, as well
as a short description of what it does
(which can be just a couple of words, and/or taken from the hw).
Note that using self-documenting names goes a long way towards
not needing other comments.
(This shouldn't really need saying; it's been required
of every function you've written since ITEC120!)
-
You may use code from the posted solutions,
but (as with all code1 you don't write)
you should cite your source.
-
Please put a clear dividing point between code for requirements from previous homeworks,
and any changed/updated code that is required for this assignment.
-
You may want to remind yourself of
how the original game
plays
(keeping in mind that we
don't need to implement most inessential features).
-
What is the signature for map?
(that is, the version of map we wrote in class,
and that is in the student languages).
-
Read the documentation for foldl and foldr
(in DrRacket, hit F1 while the cursor is on each of those words;
look at the version provided by racket3).
-
What is the difference between what each function does?
-
What is a difference in the run-time resources used internally?
-
(What term from lecture presumably accounts for the difference?
)
-
Which version should you prefer, if either will work?
-
Consider the higher-order function non,
which takes in any boolean predicate and returns a new predicate
that behaves in the opposite4way:
(non positive?) is a function that
takes in a real number, and returns
true if that number is negative or zero.
Similarly, (non ship?) takes in any value,
and returns true if it's not a ship structure.
-
What is the signature of non?
Remember, you can't just say its input is “function”;
you have to give the signature of that function.
-
Give two tests of calling the result returned from
calling non.
-
Write non.
-
Copy your hw04 solution to a new directory.
Make sure your language level is “Intermediate plus lambda”.
-
Update move-asteroids so that it is a one-line call to map.
Update draw-asteroids so that it is a one-line call to foldl.
-
Write a structure to represent a bullet.
Note that in the original game, bullets time out after a while;
what does that require as far as the information needed to
represent a bullet?
-
Write move-bullet,
which simply updates a bullet by one tick passing
(but does not worry about collisions).
-
Write move-bullets.
-
Write a function representing the ship firing.
It should return just one bullet.
Include test cases.
-
Write draw-bullet.
-
Write draw-bullets.
-
Update your world structure,
as well as move-world,
draw-world
and
handle-keypress
(or whatever you called the functions that did these tasks),
to handle bullets.
First just update the code and test to handle the extra field;
we'll worry about all of firing, collisions, and bullets timing out later.
Now we will start to handle collisions.
-
Update your move-world
so that it removes bullets that have timed out.
(Use filter and lambda.
I recommend make a separate function for removing bullets,
but you don't need to.
Regardless, you should have a couple tests.)
-
Update your move-world
so that it removes asteroids that have been shot.
This involves two steps:
First write code that
takes in a list of asteroids and a single bullet,
and removes all the asteroids which overlap the bullet.
Then write a function that takes in a list of bullets
(You can use the list-processing template, or foldl.)
-
Add a keypress-handler, to let a ship fire.
It' be nice (but not required) to have a way to slow the rate
at which bullets are released.
You might have the ship require a “recharge time”,
or just not allow new bullets while there are too many
existing bullets.5
-
Extra credit:
Have large asteroids break into smaller asteroids.
-
Extra credit:
Have bullets disappear, after they blow up an asteroid.
-
Extra credit:
add sounds.
See play-sound;
credit all sound files used, and do not use sound files
that you don't have the copyright to.
The only time you are allowed to call begin
is when calling play-sound.
1Or at the least,
all code except for possibly one-to-two line snippets
that are straightforward. ↩
3except that the intermediate-student
version only folds a single list at a time, which is the only way we've
seen it in class. ↩
2
The version functions are provided by the intermediate-language
is actually the same3,
but the documentation/explanation is better for the racket
version.
↩
4
Well, that's not quite true, since the input function might
loop forever or throw an exception, in which case non will
do that. So perhaps we should say
“(non f) returns a function which behaves as f,
except that (non f) returns the boolean-negation of
what f returns (if anything).”
↩
5
I was also going to mention just the simple hack of using
on-release (rather than on-key)
to slow the player's rate of firing.
Alas, on-release isn't working;
a bug report has been submitted, and this has been fixed
in the experimental nightly-build.
↩
home—lectures—exams—hws—breeze (snow day)