|
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
Due Jun.28 17:45 Note: 15min before class (but intended to be completable by Jun.21).
Your name and the assignment-number must be in a comment at the start of the file, and your hardcopy must be stapled. All functions/data must include the appropriate steps of the-design-recipe—the design recipe: final version. In particular, test cases alone might be worth half the credit for a function. Unless otherwise indicated, two test cases will suffice for most problems, if they are testing different situations.
The following problems will form the basis of our space-invader program, which we'll complete in the next homework. You may base your answer on hw03b-soln.rkt (but you certainly don't need to); of course give credit and URL if you do so.
Complete problems in racket, except for 5 and 6 which you'll complete in Java and
racket,
as instructed.
Your Java method should compile and run and be correct,
but then you can paste it into a racket block-comment (
(5pts)
In both racket and in Java:
Write the function
The Java and racket versions should both do the same thing:
return a new object, rather than mutating any fields
(Cf.
We won't use Java test-cases (since that entails
overriding
(3 pts) Cannons
As discussed, a cannon might be represented as just a single real-number (see hw03b-soln.rkt).
Write the function
Since the documentation shows that
(2pts) Define a “world” structure which contains a cannon, one bullet, and (for now) exactly one alien.
As usual for our data-definitions, make examples of the data (at least two), and a template.
For the time being, you can have
For now, you don't need test cases that involve the bullet colliding with anything — you can just have the bullet far away from any aliens or cannons.
hint:place-image is a handy function; it is similar tooverlay/xy except that it crops the result to the background.
hint: For test-cases, include drawing an alien that is: (a) in the middle of a small image; and (b) one that is mostly off the left-edge but has just a few pixels showing.
Note: Here's an image you can (modify to) use in your test-cases, in addition to a solid rectangle or whatever else you might choose: house-with-flowers.rkt. If you place this file in the same directory as other functions, you can just(require "house-with-flowers.rkt") , and then use its exported id (“house-with-flowers ”, coincidentally). You don't need to print this file.
hint: Calldraw-alien with anempty-scene for the background; calldraw-cannon passing it the result ofdraw-alien as the second argument.
(5pts) To do collision detection, write a suitable helper function to detect when two rectangles overlap (e.g. the bullet's bounding-box and the region occupied by a brick). Make at least four test-cases for this.
There are several reasonable ways to represent rectangles,
but it helps if your representation aligns with
the arguments needed for
hint: I recommend using half-open intervals, to define a rectangular region: A rectangle centered at 40,50 that is 60x80 would be considered to have x-coordinates in [40-30,40+30) — up to (but not including) the endpoint. It's not that big a deal, but it does mean it's easy to tile the entire plane while neither overlapping at all, nor leaving any 1-pixel-wide (or even infitesimal) gaps.If doing so, a useful helper function to write might be
(<=< a b c) : isa ≤b <c ?
cool hint, from robotics: We can reduce this problem involving two rectangles to a simpler one: checking if an expanded version of the first rectangle merely contains the center of the second.
For example: consider a 20×30 rectangle whose center is at (500,400) and a second rectangle which is 80×60. These two rectangles overlap exactly when the second rectangle's center is inside the (20+80)×(30+60) rectangle centered at (500,400). Sketching this example on paper will help you understand why this works.
All the above should have their tests, as well as signatures and (brief) purpose statements. Only after all tests pass, the following should work (in racket):
(require 2htdp/universe) (big-bang some-initial-world [on-key world-handle-key] [on-tick update-world] [to-draw draw-world]) |
1 Your final program doesn't need to include any "transitional" results from the template: For example, you don't need the stubbed-out version of a function from step 5. However, you should still have passed through this phase. ↩
2
On a future hw, we'll write “
4
The problem is that (in a class
public boolean equals( /* Foo this, */ Object that ) { if (that==null) { return false; } else if (this==that) { return true; // short-circuit a common case } else if (that.getClass() != this.getClass()) { return false; // CAUTION: unless we can be .equals to a subclass? } else { Foo thatt = (Foo)that; // NOW you can add your code that actually compares fields, say: return (this.someField==null ? thatt.someField==null : this.someField.equals( thatt.someField )) && (this.someOtherField==null ? thatt.someOtherField==null : this.someOtherField.equals(thatt.someOtherField) // If our constructors never create objects w/ `null`, we can shorten the preceding lines a bit. && … ; } } |
5We'll upgrade the world so that it contains a list-of-aliens in a future homework. ↩
6A comprehensive set of black-box test cases is much much more involved: one rectangle defines 9 regions of potential interest, plus 16 dividing line segments/points; the second rectangle can have its northwest corner in any of those 9+16 regions, and its southwest corner in many of those. From counting-skills learned in discrete math, I count (…lemme think…) 25*24/2 + 25 = 325 test cases, to be reasonably comprehensive. You need to provide about 1% of such tests, whew! ↩
The “grow-the-second-rectangle” trick is exactly analagous to determining if two circles overlap by: seeing if the second circle's center-point is located inside the first circle “grown” by the second circle's radius: that is, whether (x2,y2) is within r1+r2 of (x1,y1).
↩home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
©2015, Ian Barland, Radford University Last modified 2016.Jun.28 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |