RU beehive logo ITEC dept promo banner
ITEC 380
2019fall
ibarland

structs
hw04

Due:
Sep.27 (Fri) at the start of class, D2L and hardcopy.
Your submitted file should be named “hw04.rkt”, plus any .java files.

Standard instructions for all homeworks:


(This is the completion of hw04a, from D2L.) Consider the following information, which might be part of a sports-simulation:

A team is:

  1. (5pts) Give a data 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.

  2. (5pts) 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 arcade game Galaga, such as this example. We will write a simplified version which only needs to deal with: aliens parading back and forth, diving (in an angled-but-perhaps-straight line), the player's ship ("ship"), and the missiles (both the player's and the alien's). It does not need to deal with: score, having more than one level, number-of-lives, or the “entrance” phase of the aliens.

There are three types of things (objects) which our program will need to model: ships, missiles, and aliens.

  1. (10pts) Give a data-definition for a missile in Java — that is, a class Missile. Include examples of the data, and a template for any Missile-processing function(method). As always, be sure to mention the units for any fields, as needed.

    hints: General tips for deciding what fields an object needs:
    • Imagine a friend is playing your game, and paused+saved it mid-play. When they later re-open that save-file, what info needs to be in there about a particular ball, so the program can exactly re-construct the ball how it was the moment your friend saved it?

    • Do not include information that is not part of the object itself.

    • Do NOT include information which never changes (is “invariant”) for a class. For example, if all instances of an object are always the same color, you wouldn’t have a field for the color. (You wouldn’t want 1000 objects, each with their own field, which all necessarily hold the exact same value.)

    • Sometimes x,y coordinates suffice to describe something’s position. However, sometimes more context might be needed. For example: what is the latitude and longitude of Virginia?

      On a related note: For a problem below, you might be using place-image, which places the center of an image at specified coordinates. If your internal representation is consonant with libraries you use, it can make things a smidgen easier.

    Since this is Java, you will also need to create: a constructor, equals(Object), and hashCode() (and optionally, toString()). These will all be boilerplate methods similar to lecture’s Book.java.

    Note: You do not need to create getters/setters (and moreover, you should not). Moreover, for this homework and course, we will use immutable data, so you should not reassign to any fields2

  2. (5pts) Now write a Java Missile method Missile move() which returns a Missile whose position is changed, to account for its movement over one frame (one “tick”). This function should not worry about collisions.

The remaining functions are to be in racket (not Java); the Java version is just to compare and emphasize how similar the immutable-Java and racket programs will be.

  1. (5pts) Give racket data-definitions, examples-of-data, and template for missile, ship, and alien. For aliens, you should handle the fact that it might be in the middle of a dive (or not).
    Note: Although full racket does support struct-inheritance, we will not use it here
  2. (5pts) Write move-missile in racket, which behaves the same as Missile#move()4.
  3. (5pts) Write ship-handle-key : ship, key-event? -> shippaddle, which updates a ship in response to a keypress.
  4. (5pts) Drawing functions:
    1. Write the function draw-missile : missile, image -> image, which takes a missilebrick and a “background” image, and returns that background image with the missilebrick drawn on top of it.
      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 missile 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 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, but do submit it on D2L so that I can run your program.
    2. Write draw-ship, similarly.
    3. Write draw-alien, similarly.
      Note: My own version of draw-alien took in a second parameter — the "fleet's" location. Your version does not need to do this.
      Note: If you want a diving-alien to be drawn differently — at an angle perhaps — you may, but that complication is certainly not required.

    I suggest using simple shapes (like mere rectangles) 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 site your source, of course!

  5. (0pts) Start writing, or at least thinking about, move-alien. This function is tricky because of diving. My version, like draw-alien, will take in an additional parameter: the fleet location.

1 Note that the stub-function, step 5e, gets replaced with the real body (step 7), so it won’t be visible in what you submit. Also, you don’t need to include the names of constructors/selectors; that was part of quiz03 but not part of the design recipe.      
2 With the possible exception of caching an object’s hashcode, as in Book.java.      
3 The long flag -enableassertions can be abbreviated -ea.      
4 The notation ``Missile#move()'' is standard Java for "a method move inside class Missile which takes no parameters".      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.