![]() |
![]() |
|
home—lectures—recipe—exams—hws—D2L—breeze (snow day)
Part A, Problems 1,2,9,10a: due Sep 30 (Tue) 23:59 and (section 01:) hardcopy the following class
Part B, the remainder: due Oct.05 (Sun)04 (Sat(!)) 23:59.
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 steps1 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.
(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
(2pts) Improved paddle representation.
In the hw03-soln.rkt, it was noted that a paddle could be represented by just a single number (its x-coordinate). However, we will want to keep track of a little bit more: which direction the paddle is moving (right or left — or even, how fast right or left). This is so that if a player wants to move the paddle far to the left, they won't have to hit the right-arrow repeatedly.5
So: now design a paddle struct (or object) that includes a horizontal direction (and, if you desire, speed). Make examples of your input. (You might have already had something like this in your hw03 submission.)
Write
Note:Handling a keypress should probably not move the paddle at all; the keypress as an instantaneous event — much shorter than one tick!
Note: This function is intended to handle key-down events. If you want your program to also handle key-up events, then write a second functionpaddle-handle-key-up .
(2pts) Define a “world” structure which contains one ball, one paddle, and (for now7) exactly one brick.
As usual for our data-definitions, make examples of the data (at least two), and a template.
For now, you don't need test cases that involve the ball colliding with anything — you can just have the ball far away from any bricks, walls, paddles. (However, you might have a test where the paddle interacts with a wall/border.)
hint:place-image is a handy function; it is similar tooverlay/xy except that it crops the result to the background.
hint: Calldraw-brick with anempty-scene for the background; calldraw-paddle passing it the result ofdraw-brick as the second argument.
(5pts)
To do collision detection, write a a suitable helper function would be to
detect when two rectangles overlap (e.g. the ball's bounding-box and
the region occupied by a brick).
Make at least four test-cases8
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 from 10,20 that is 50x70 would be considered to have x-coordinates from in [10,60) — 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 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 one particular point: the southeast corner of the second rectangle.
For example: consider a 20×30 rectangle whose northwest corner is at (500,400) and a second rectangle which is 80×60. These two rectangles overlap exactly when the second rectangle's southeast corner is inside the (20+80)×(30+60) rectangle rooted 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 |
I encourage you to actually put all the nonterminals in upper-case (or at least first-letter-upcased), just to make it easier to tell terminals from non-.
<JsFunc> → function <JsIdent>( <JsIdents> ) { <JsBody> } |
(2pts)
As we consider how to define
Working towards a solution, the programmer MC Carthy comes up with the following attempt for a nonterminal that generates comma-seperated lists of “x”s (also ending in a semicolon):
<XS> → ; | x; | x, <XS> |
Prove that this grammar is not correct, by giving a derivation11 of some string which is not a valid comma-separated list (ending in a semicolon).
Give your grammar rules as in class: in basic BNF
using → (and
(6pts) Generating a grammar
Give a grammar for Java's boolean expressions.
(That is: a grammar describing exactly what you are allowed to put
inside a the parentheses of a Java
For this problem, assume that you already have grammar rules for
a Java numeric expression
(“Fig. 3.4Example 2.4, p.46),
for a Java identifier
(“
Using your grammar, give a parse tree (not a derivation) for:
In the tree, for children of
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.equals( thatt.someField ) && this.someOtherField.equals(thatt.someOtherField); } } |
5 Alternately, you can decide that you want to keep the paddle moving for as long as the arrow key will be held down. However: in our library, our program will get only key-up and key-down events; we can't query the keyboard to ask what keys are currently being held down &frowney;. So, even if trying the key-held-down approach, you'll still need your paddle-struct to keep track of which key(s) are currently being held. (That is: you'll need to keep track of the state, rather than having the OS's keyboard object keep track of that state for you!) This key-held-down approach is a little more involved, but if you really want to follow it, you can start a thread on the discussion board, about any design issues. ↩
6 If you would prefer to wrap-around, rather than stop at the edge of the screen, that's okay. However, I suspect the gameplay would be significantly more boring. ↩
7We'll upgrade the world so that it contains a list-of-bricks in a future homework. ↩
8A 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! ↩
10 If you really, really want keep everything in one single file, you can insert your image into DrRacket via Edit &goto; Insert.... ↩
9 You can photograph or scan your your drawing (jpg, png, gif or pdf only, please), and submit it on D2L alongside your .rkt file.10 ↩
11 The best answer is the shortest: if you have a simple example that uncovers the grammar's flaw, that is better than a long example doing so. ↩
12 You can turn in your printout + your drawing, or if If you really want just one file to turn in, you can photo and then insert it into DrRacket. ↩
home—lectures—recipe—exams—hws—D2L—breeze (snow day)
©2014, Ian Barland, Radford University Last modified 2014.Oct.14 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |