RU beehive logo promo banner for Computing & Info Sciences
ITEC 380
2022fall
ibarland

hw04: structs
…for maRiU

Due:

(All times-of-day are start-of-class.) at the start of class, on D2L.
Your submitted file should be named “hw04.rkt”, plus any .java files.

Standard instructions for all homeworks:


part (a)

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.

part (b)

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

A team is:

  1. 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.

  2. 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.)


part (c) Mostly-finalized, but we'll talk about needed fields in class on Tue.; choices we make there might impact the following.

Familiarize yourself with the arcade game super-mario. We will write our own version, starting with this homework (structs) and finishing with next homework (lists). In our combine-yet-very-simplified version:

There are three types of things (objects) which our program will need to model: players, turtles, and flagsplatforms.

Racket & Java

We start by representing a turtle, and writing a method involving one.
  1. In both Java and Racket, develop a datatype-definition for a turtle. For each field, give a short (two-to-three word) comment describing what it stands for, and its units. Natural units for screen-based games are pixels, and ticks (where a tick is 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!
  2. Make at least two different examples of the data; do this both in Java and in racket. (In Java, where all code must be inside a method, put these inside a static method void tests().)
  3. Write the template for turtle-processing functions, in both racket and Java.
    (In Java, you'll have to comment out the template, since ... won't compile.)
  4. Create (in both racket and Java), a function move-turtle which takes in a turtle, and returns a new turtle one "tick" of time later, ignoring any exterior factors like reaching the edge of a platform.
    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).

Racket-only

The following problems only need be done in racket.

  1. model Give datatype-definitions for a player and a platform, each with at least two examples of the data, and a template.
    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.
  2. Write move-player, similar to move-turtle. Although flags don't usually move, they might move every 10s or so. You should test both those situations. This should move the player in the absence of any external objects, like platforms. That means that this method, by itself, will accelerate the player downward! (This'd be true for turtles as well, though if you plan on always keeping turtles on top of platforms then you might omit this.5)
  3. controller Write a function player-handle-key which takes in a player and a key-event, and returns a new player having responded to the keypress.
    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).
  4. view:
    1. Write the function draw-turtle : turtle, image -> image, which takes a turtle and a “background” image, and returns that background image with the turtle 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. Another handy function is flip-horizontal.
      hint: For test-cases, include drawing a turtle 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-turtle, and then use that resulting image as the background for drawing the next turtle, 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.
    2. Write draw-player, similarly.
    3. Write draw-flag, similarly.Not required for this homework… Though we will eventually need to define flags and write this for the full homework, of course.

    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!

  5. collision detection

    We can approximate collision-detection through a standard practice: checking whether two rectangles (bounding-boxes) overlap. To help you with this, 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.


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 If you added a boilerplate equals and hashCode, that'll take a second a page; recall Book.java examples from lecture. I'm fine with using simple asserts, but if you want to use JUnit instead that necessitates an additional file playerTest.java.      
3 The long flag --enableassertions can be abbreviated -ea.      
4 If you really want to use struct-inheritance, check with the instructor first, and see student-extras.rkt. It requires bumping up to intermediate-student, which means you'll no longer get error messages for leaving off an open-paren in front of a function's name.      
5 Though if you have a bug and somehow an turtle does walk off the edge of a platform, then the severity of the glitch will fall back to how correctly you handled the physics of turtles!      

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.