home—lectures—recipe—exams—hws—D2L—zoom (snow day)
list processing
Pac-man II
Feb.28 (Thu) 11:00: questions 1-4 5;
Feb.29 (Fri) 23:59: the remainder.
For each part, your hardcopy only need include the code for the new requirements,
but the D2L version should have all files needed for running (as individual files, not a .zip).
Reading: §6.6, and §10.1–10.3.1.
(Additional recommended, but not required, background reading: all of Chpt.6, and §10.3.
Additional, non-required, challenge-reading: § 10.5.)
All problems are to be written in Racket.
Do not call any of the following functions:
- list (except when making test-cases)
- append (unless you write it yourself)
- remove/remove* (unless you write it yourself)
- place-images (plural) (unless you write it yourself); calling place-image (singular) is fine/intended.
- any functions with a “!”, such as set! and set-rest!.
If you want to use let* (as mentioned in class), you can
require "let-for-bsl.rkt".
Your name and the assignment-number
must be in a comment at the start of the file
All functions/data must include the appropriate steps of the design recipe.
In particular, test cases alone might be worth 40-50% of the credit for a function.
Have enough test cases to cover different corner cases; often 2–3 can suffice.
- Write the function count-bigs : real, list-of-real → natnum,
which takes in a threshold and a list of numbers, and returns
how many of them
are larger than the threshold.
To help you out, here are some test cases; no further ones are required.
(The data-definition for list-of-number has already been given in lecture,
so you don't need to repeat steps 1-3 of the design recipe for list-of-number.)
Remember: We can't re-assign to variables, in functional programming.
So we won't have a counter which we increment, like you might in your imperative-programming class.
Instead, be sure to follow the design recipe, and after copying the template,
think about the inventory-with-values
(assuming we call our parameters “threshold” and “nums”):
if we call count-bigs with the particular inputs
(count-bigs 3 (cons 10 (cons 2 (cons 5 empty)))), what is…
- threshold =
- nums =
- (first nums) =
- (rest nums) =
- (count-bigs threshold (rest nums)) =
Fill in each blank with a particular number, or list-of-numbers.
Then, ask yourself: Starting with those pieces of info,
how do I assemble them to get my desired result of 2?
You don't need to include the above in your answer —
it's just to remind you of what you do,
for the “inventory-with-values” step of the design-recipe, #6.
If you get stuck on any of the problems below, make sure you didn't skip this step;
it's the one that can really make things click!
- Write the racket function map-sqr : list-of-number → list-of-number,
which squares each number in a list;
do not call map .
To help you out, here are some test cases; no further ones are required.
(check-expect (map-sqr empty) empty)
(check-expect (map-sqr (cons 7 empty)) (cons 49 empty))
(check-expect (map-sqr (cons 9 (cons 7 empty))) (cons 81 (cons 49 empty))) |
Copy your hw04(structs) racket file to one that we'll use for this one.
Add a very noticeable dividing-line (say, 80 ;s)
between old code and the new.
You will not need to turn in hardcopy of things before the dividing-line
if they were in your hw04 solution or in the posted hw04-soln.
Please include part A (problems 1,2) in your part B hardcopy.
- For List-of-ghosts,
- Give the data-definition for list-of-ghost
but no define-structs are necessary, since using the built-in cons suffices.
- Give at least 4 example values of this type.
Your last example should contain at least four ghosts; you may use this as the ghosts
in your game's initial world.
- Write the template for a list-of-ghost processing function.
- Make some examples of List-of-walls and List-of-dots.
(You don't need to give a data-def'n or template (since they are so similar to those for List-of-ghosts), though.)
- Collisions:
- Write pacman-collide-wall?, which determines whether a single pacman overlaps with a single wall.
- Write pacman-collide-walls?, which determines whether a single pacman overlaps with any wall
in a list-of-wall.
- Similarly, write
ghost-collide-walls?,
and its natural helper-function
ghost-collide-wall?.
- Write
pacman-collide-dot?.
- Write
dots-remaining : pacman, List-of-dot → List-of-dot
which returns all dots in the given list which are not colliding with the pacman.
hint: We saw in lecture, when
counting certain items in a list —
where we either added 1 or left our value unchanged, depending —
that we could cleverly view "unchanged" as "add 0", and then once we had "add-1-or-0, depending" we could refactor.
However, cons is not quite like that:
there is no way to call cons without adding something onto another list.
So we can't use that same trick.
For each of the functions, have (of course) 2-3 test cases.
Two tests suffice for individual-collisions, since we have a well-tested
helper overlap?collide? (from prev.hw);
for lists, three tests should suffice: lists of length zero, one, and “many”.
And the tests for list-of-length-0 is trivial, and the test for list-of-length-1 is similar to your earlier tests.
- Drawing functions:
- Extending the work from structs.html,
have functions
draw-ghost,
draw-wall,
draw-dot.
As before, each of these functions take one item and a background-image,
and return the background image with the single item overlaid on it.
- Write functions
draw-ghosts,
draw-walls,
draw-dots.
Each of these functions take a list-of-items and a background-image,
and return the background image with the items overlaid on it.
When creating the expected-output for your test cases, feel
free to include the calls to draw-ghost (singular), etc..
Creating the expected-result for a test for a list-of-length-2 will help you
understand what your code needs to do.
- Write
move-ghost : ghost, list-of-wall → ghost,
which is like
glide-ghost
but accounting for walls:
If gliding would end up colliding with a wall,
then instead just
set the ghost's direction to be
the next
direction from the list (cons "up" (cons "down" (cons "left" (cons "right" '())))).
(In that case the ghost need not actually end up moving.).
Note: In lecture this week, we may writewrote straightforward
functions for index-of and get-at,
which then make writing a get-next easy.
- Similarly, write
move-pacman : pacman, list-of-wall → pacman.
- Option 1:
If your pacman struct includes both a current-direction and
a
cached
direction-keypress,
then:
You should test if moving in the direction of the cached direction will avoid walls;
if so then that will become the new direction.
Otherwise, see if moving in the current direction will avoid walls;
if so it continues moving in that direction.
Otherwise, the pacman should … stop? Move in a random/next direction? It's up to you.
- Option 2:
If your earlier pacman-handle-key had a keypress take effect immediately,
you can still do so.
(This is harder for the player, since there is only a limited time for
them to hit the direction key — as little as one frame, if two
walls are exactly the width of a pacman|!)
In this case, if gliding the pacman would result in colliding with a wall,
the pacman should probably stop.
- Worlds:
Define a “world” structure which contains
one pacman, a list of dots, list of ghosts,
and a list of walls.
As usual for our data-definitions,
make examples of the data (at least two).
- Give a template for world-processing functions.
- Write the function update-world : world → world
which returns a new world one “tick” later.
For the moment, update-world can ignore collisions with dots,
and it can leave the fleet-direction unchanged.
We’ll fix those below.
- Write the function world-handle-key : world, keypress → world
which returns a new world updated to handle the keypress.
(Should be easy — mostly defers to pacman-handle-key,
and your test cases will largely crib from that.
Firing, however, will be part of this function.)
- Write the function draw-world : world -> image,
which draws the world onto a blank background.
- Finally, write a function game-over? : world → boolean,
which returns whether the game is over
(that is, if the pacman is colliding with a ghost, or there are no more dots).
All the above should have their tests, as well
as signatures and (brief) purpose statements.
Only after all tests pass,
add
(require 2htdp/universe)
(big-bang some-initial-world
[on-key world-handle-key]
[on-tick update-world]
[to-draw draw-world]
[stop-when game-over?]
) |
and you can play your game.
home—lectures—recipe—exams—hws—D2L—zoom (snow day)
 This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland radford.edu |
 |