![]() |
![]() |
|
Due:
Your submitted file should be named “hw04.rkt”,
plus any .java files.
Standard instructions for all homeworks:
Complete the short fill-in-the-blank questions on the D2L quiz hw04a
.
Your answers should be valid racket expressions.
Do be sure to answer what the question is asking.
A team is:
- a name (non-empty string), AND
- an offense rating (real number), AND
- a defense rating (real number).
Give a datatype definition (including examples of the data, and the template for any function which processes a team), as per steps 1-3 of the design recipe. Give at least three examples of a team.
We’ll say one team is “greater” than another if its offense is higher than the other’s defense, and its defense is higher than the other’s offense. Write the function team>? to implement this. Follow all the steps of the design recipe1. As always, be sure to think about enough test-cases to cover the interesting situations (half of the pts). (In this case, six or seven should suffice, if they each cover a different situation.)
Familiarize yourself with the frogger arcade game. We will write our own version, starting with this homework (structs) and finishing with next homework (lists). In our simplified version:
There are three types of things (objects) which our program will need to model: frogs, trucks, and prince(sse)s.
tickis an abstract amount of time, but it’s convenient to declare it as the amount of time between two frames).
Note: No images are involved!
You should have a Truck.java file (which compiles and runs, even if it doesn't print anything); it should be less than a page2 of well-spaced, commented code, including tests.
enabling assertions in Java: When running your program, the JVM's default behavior is to entirely ignore assertions (!?!). If using assert for your tests (just as we did in Book.java), run with3 java --enableassertions someClassName, or convince your IDE to do this.
Java's `record`s: You are encourage to use record, to make your life easier (as in lecture-example).
- It's okay if you use regular classes and don't override .equals/.hashCode; it just means your asserts involving truck objects will fail.
- You likely need to install JDK 17 to do this by openJDK, or by Oracle. Post on discussion-board if you have (or overcome) any glitches.
Remember: Include both field-names and their types, in racket as well as Java.
Good style: Your racket and Java code should use the exact same field and class names, up to idiomatic differences like camelCase vs. hyphenated-names.
Of course: The Java and racket versions should work in the same way: returning a new object, rather than mutating any fields (Cf. enbiggened books, in the lecture notes).
The following problems only need be done in racket.
Note: We will not use struct-inheritance4 This means some of our functions will essentially be repeating code, which is indeed annoying.
suggestion: To represent a direction, use the strings "left", "down", etc. rather than (say) the symbols like 'west or 'bottom, etc. . This will play nicely with the key-event?s of the 2htdp/universe library.
Handling randomness: How can we write unit tests, if the prince(ss) makes a random move? For this assignment, I recommend actually removing randomness: instead of choosing a new x randomly, we can instead make it the old x2 + 321, mod the screen-width. This can still appear random to the user, but also be unit tested. (For other approaches to testing w/ randomness5, take Software Testing (ITEC335)!)
API: Represent a key-event as a String, e.g. "x" or "right". In racket, you can compare two key-event?s with string=?, but key=? (from (require 2htdp/universe)) is more appropriate. Equal credit will be awarded, either way.
hint: If you want to handle stopping at the left/right edges (or perhaps, wrapping around). To do that, you need to know the size of the board. Use named-constants for that. Your unit-tests can refer to those named-constants; for convenience they can also assume that each dimension will be at least 100 pixels — i.e. they can assume values less than 100 aren't near the right-edge. Also, note that modulo can help handle wrapping (or, if not using integers, modulo/real as provided in student-extras.rkt).
hint: place-image is a handy function; it is similar to overlay/xy except that it crops the result to the background.
hint: For test-cases, include drawing a truck that is: (a) near the center 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 may 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 nor D2L-submit this file, if you make no changes to it.
looking ahead: It is important that your drawing-function take in any background image. Later, when we make an overall game-object, we will initially pass in an empty-scene to draw-truck, and then use that resulting image as the background for drawing the next truck, and so on. This is also what we did for the meme-drawing functions, when we wanted to overlay two different strings on the same image.
I suggest using simple shapes (like mere
rectangles or star-polygons) for
all your drawing-functions for the game.
You are welcome to be much fancier, though it won’t earn you additional credit.
You are welcome to use jpg/png/bitmaps, but make sure
you have the legal right to copy them —
e.g. from sites like
classroomclipart.com
or libraries like
planetcute
.
Please cite your source, of course!
You may download and (require "overlap.rkt")
(but you shouldn't need to modify that file, nor print/D2L-submit it).
Remember, don’t copy/paste a .rkt file; right-click to download,
and then you can open it in DrRacket if you like.
All the above should have their tests, as well as signatures and (brief) purpose statements.
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |