Take-home Exam


Contents:


Overview

This take-home exam is open book, notes, and assignments, and you are also welcome to use other textbooks or resources on the web in completing the exam. But remember, this is an exam, so all work must be your own. You may not communicate with anyone other than the instructor in regards to this exam.

Please contact the instructor if you have any questions regarding any of the problems on the exam.


Take-Home Exam Problems (100 points)

For this take-home exam, you are given code for a simplified version of the PacMan game. The original version (which is unfortunately no longer available on the web) implemented most of the aspects of the game, but was not well designed. You are being provided with an updated version of the code, which has had its design partially, but far from completely, improved. You will be asked questions about the code, and will asked to make further design improvements to the code as well.

The code is contained in the following files (or download everything at once in PacMan_exam2300_sp18.zip):

Given this code, complete the following problems:

  1.   (5 points)

    Let's start off by getting to know the (new version of the) code. One way to do this is to start by investigating the programmer's comments in the code...

    Oops! This programmer wasn't very good at commenting...   Add some comments throughout the code that describes what each section of code is doing.

    Note 1: It doesn't need to be heavily commented -- one comment every 10 lines of code or so is sufficient.

    Note 2: While you should add your comments to the new version of the code (with the improved design), you may find it beneficial to look over the code description given on the website containing the original code.

  2.   (8 points)

    In animating the PacMan character to continuously open and close it's mouth while moving in a given direction, the author uses a series of 13 images. The author provides 3 images with the mouth open to varying degrees in each of the four movement directions. The last image is a base image, in which the mouth is closed (PacMan1.gif).

    The author defines one method for loading all the images, loadImages(), but stores these images in 13 different Image variables. In order to select the right image based on the movement direction and current frame in the animation sequence, the author created five methods (starting with drawPacman() and ending with drawPacmanRight()) that span over 80 lines of code...

    If the author had instead used four 1-D arrays, with one array per direction (or alternately, one 2-D array), then he could have easily consolidated the five methods down into one method, with a single code body of less than 10 lines of code. Make this change, using either four 1-D arrays or one 2-D array.

    Note, the PacMan1.gif image should be the first image in the sequence (index 0) in each of the directions.

  3.   (5 points)

    Take a look at the doAnim() method. Describe the sequence of values that the spriteDelayCount variable follows in a single cycle of Pacman opening and closing his mouth (while moving in a single direction). Likewise, what is the corresponding sequence of images that this displays (resulting in the animation of Pacman gobbling up dots) ?

  4.   (4 points)

    At present, the GhostShape class uses circles, rectangles, and Colors to define the shape and color of the ghosts. Change your code so that some, but not all, of the ghosts are displayed using one of the ghost images, Ghost1.gif or Ghost2.gif, in the pacpix/ subdirectory. Use Math.random() to choose which display mechanism is used for any given ghost.

    Note: Make sure that the same ghost always displays in the same way -- the ghost shouldn't flip-flop between the purple "icon" and the two images randomly while moving...

  5.   (5 points)

    At present, the design uses "magic numbers" in representing the different types of "wall" configurations in the code for the Board and GhostShape classes. For example, in the lines of code such as:
          (screenData[py][px] & 4)
    and
          (ch & 4)
    the 4 represents a possible wall in one of the four directions of movement in the GhostShape and Board classes, respectively. The & operator is being used here to check if a given square contains a wall in that direction (and hence, should hinder movement in that direction).

    Having such un-defined "magic numbers" in the code impedes code readability. Change these "magic numbers" to symbolic constants to improve code legibility.

  6.   (6 points)

    Take a look at the PacKeyAdapter class. Notice that the primary result it produces is to set the req_x and req_y variables. Describe how this key listener class, and the two req variables, operate in conjunction with the movePacman() method to prevent the player from walking through walls.

  7.   (5 points)

    What code in the various .java files determines whether Pacman is on top of a "dot" in the game, and if so, removes that dot from the board?

    Likewise, where in the code does it determine whether Pacman has collected all the dots, and should thereby start a new level?

    Similar to what you did with walls, change any "magic numbers" into symbolic constants to make this area of the code more readable.

  8.   (6 points)

    Note: Skip this question... the link to the original code is no longer valid.

    Compare the given code with the improved design to the original code at the website linked above. Describe the major code and design changes that were made in developing the improved code.

    Note: There were many additional minor changes, including code re-formatting, but you don't need to describe these. You only need to focus on major changes and design changes. There are in the neighborhood of 2-6 of these...

  9.   (15 points)

    According to the Single Responsibility Principle, the Board class still has way too much responsibility... In the same way that the code for the "ghost" creatures was moved out of the Board class, similarly create a separate class for the player/Pacman shape.

    Note: The key listener, PacKeyAdapter, needs to remain in Board as it's implicitly bound to the JPanel's "focus". Don't try to move it out.

  10.   (10 points)

    Give a UML diagram for this program, which includes any code changes you made in the preceding questions/problems. Please include relevant Java Swing classes.

    Note 1: As noted earlier in the semester, I recommend using LucidChart.

    Note 2: You may exclude the following Java and Swing classes from your diagram:
                         Graphics, Graphics2D, Integer, String, ArrayList, Rectangle2D, Ellipse2D, Color
               Please include all others.

  11.   (6 points)

    Which of the classes, if any, are:

    Note: Include any code you added in the preceding questions.

  12.   (10 points)

    How do the values within the levelData array work? Explain what the different values mean...

    Create a second level map. It doesn't need to be hugely different from the first, but should have enough differences to demonstrate your understanding of the values in levelData.

    Make it so your second level map comes up after the player clears the first level map.

  13.   (15 points)

    Finally, create a new mechanism in levelData that corresponds to a "power dot", which when collected, convert the ghosts so that they display a "scared ghost" graphic and makes the ghosts collectible by the player (for points).

    Create a counter that measures the amount of time that has elapsed after a power dot is collected. Have the ghosts operate in "scared" mode until a certain amount of time has elapsed. Once that time has elapsed, they should switch back to regular mode and display their original graphics.

    There are two "scared ghost" images in the pacpix/ directory. Have the ghost display one of those two images when they are "scared".

    When in "scared" mode, the player should be able to kill ghosts by colliding with them. In regular omde, this would cause the player to die, but in "scared" mode, the ghost should "die" (be removed), and the player should be awared some amount of points.

    Add a few power dots to the board, and make sure they work as expected.