5.92

3 1/22: Fundamentals + Pong

Due: 1/22 at 11:59 PM.

Language: class/1.

In this assignment, you will practice designing solutions to several small problems. Then you’ll build another classic computer game: Pong.

  1. Remember our discussion in class of a mobile. Here is an example:

    image

    The struts are of integer lengths (marked by ticks), and the numbers beneath the shapes indicate their weights.

    We defined a Mobile as one of a MobileNode% or a MobileLeaf%. In this exercise, you’ll flesh out a few more methods for Mobiles.

    In addition to our definitions in class, now we want to allow heights to matter as well as strut length. Extend your classes to incorporate this information. Define examples of mobiles, including one that represents the picture above.

    Design the method height that computes the height of the mobile. (The example above has height 7.)

    Design the method total-weight that computes the total weight of a mobile. Assume struts and rods are weightless. (The example above has total weight 90.)

    Design the method balanced that computes whether a Mobile is balanced. A given node is balanced if there is no net torque on the node, and torque is defined as the product of the weight of a node and the length of the strut to which it is attached. (The example above is balanced.)

    Design the method symmetric that computes whether a Mobile is symmetric. (The example above is not symmetric.) Hint: you may want to define a helper method, and you’ll probably need to use Racket’s equal? function.

  2. Here is a class diagram for employees. Develop data and class definitions for employees.

    Design the method count-subs that computes the total number of subordinates of this employee.

    Design the method full-unit that collects all of the subordinates of this employee. Hint: you may find it useful to add an append method to the ILoE interface (and implement it).

    Design the method has-peon? that determines if this employee has a peon with the given name (a string).

  3. A football game consists of a series of Posessions. Each Posession starts at a location on the field (a yard from 0 to 100), ends at a specific location on the field, either with a score (of either 3 or 7 points), or a kick to the other team, causing them to start at a new location on the field.

    Design a data defintion for football games. Then design the method total-offense, which records the number of yards gained by the team that started with the ball first. Design the method total-score, which records the total number of points scored by both teams.

  4. Here is a data definition for a time of day:

    ; A Time is a (new time% Hours Mins)
    ; where Hours is an Integer in [0,24)
    ;   and  Mins is an Integer in [0,60)

    Develop a class definition for time%. Design the method ->minutes which converts this time to the number of minutes since midnight.

    Here is a data definition for an event:

    ; An Event is a (new event% String Time Time)
    ; Interp: an event with a name and a start and end time.

    Develop a class definition for event%. Design the method duration which computes the duration of this event in minutes. Design the method ends-before? which determines if this events ends before a given event starts. Design the method overlap? which determines whether this event overlaps with a given event.

    Here is a data definition for a schedule:

    ; A Schedule is one of:
    ; - (new no-event%)
    ; - (new cons-event% Event Schedule)

    Design the method good?, which determines if the events in this schedule are in order and do not overlap. Design the method scheduled-time, which computes the total time in minutes scheduled in this schedule. You may assume this schedule is good for scheduled-time. Design the method free-time, which computes a schedule of events named "Free time" that are all times not scheduled in this schedule. You may assume this schedule is good for free-time.

  5. Develop data and class definitions for representing lists of numbers. Design the reverse method that consumes no arguments and produces the list of this list’s elements in reverse order. You should design the method using a helper method with an accumulator.

    Note: you may not use lists from last semester, i.e. those constructed with cons and empty.

  6. Revisit your solution to free-time. Did you use an accumulator desgin? (You probably should have.) If you didn’t, redesign the program with an accumulator.

  7. Design a representation for simple organic molecules. An organic molecule is either a carbon atom, or a carbon atom connected to additional carbon atoms via bonds. A connection between two carbon atoms can be either a single, double, or triple bond. The total number of bonds attached to a single carbon atom is at most 4.

    Define butane as an example, which is just 4 carbons in a line, with all single bonds. You can see a diagram of butane here.

    Design the method count-carbons, which starting from a carbon atom, counts all the carbon atoms connected to it (including the starting atom).

    Design the method valid?, which determines if every attached carbon atom has at most 4 bonds.

    It turns out that all of the gaps, i.e. the possible bonds that aren’t connected to another carbon atom, are actually connected to hydrogen atoms. Design the method count-hydrogens which counts the number of hydrogen atoms reachable starting from a carbon atom (i.e., counts all of the gaps).

  8. Pong!

    Design and implement a two-player version of the classic Pong game. You can see Pong in action in this video of the original game or this online version.

    The important elements of the game are the following:

    • Two paddles, which move up and down but not off the screen. One paddle should be controlled by the w and s keys, and the other by the up and down keys.

    • A ball, which bounces off the top and bottom walls and off the paddles.

    • A winning condition, when the ball reaches either side wall.

    • A score, for each player, which counts the times the ball hit the other side wall.

    In other words, it should look a lot like the original Pong game in the video. You do not need to implement any embellishments, such as varying bounce angles, faster speeds, or fancier graphics.