15-110 Sections M-Q / Fall 2009 / Quiz 5

5 Questions / 30 Minutes
 

·        Unicode ‘A’ is 65, ‘a’ is 97, and ‘0’ is 48.

·        The Snake.java code we wrote (except for two methods) is attached.

  1. Using the attached Snake.java code, list the line numbers where…
     
    1. … we test if the snake crosses its own path.
       
    2. … the snake grows one larger.
       
    3. … we test if the snake crawls off the board.
       
    4. … we draw the lines in the grid.
       
    5. … the snake “inches forward” (moves without growing).
       
  2. Answer very briefly.
     
    1. How would the program’s user-visible behavior change if we changed line 96 to:
             96:     if ((row == foodRow) || (col == foodCol)) {
       (The && was changed to ||.)
       
    2. How would the program’s user-visible behavior change if we changed line 100 to:
                  100:     if (snake[row][col] > 0) {
      (The else was deleted.)
       
    3. Our version in class contained a bug where we omitted line 39:
                   39:           if (snake[row][col] > 0)
      With this bug, new food was not placed randomly. Instead, it always was placed exactly at the “foot” of the snake.  Very briefly, explain why this bug caused this symptom.

     

  3. The body of the placeFood method (on line 85) was removed.  Write the missing 5 or 6 lines of code here (your code need not match verbatim, but it must have the same exact functionality):

     
  4. The body of the paint method (on line 113) was removed.  Write the missing 3 lines of code here (your code need not match verbatim, but it must have the same exact functionality):
     
  5. In recitation (and in email), we discussed two ways to implement the growing set of snake traps:
       Approach #1:  Store trap locations in two arrays (one for trapRows, the other for trapCols); or
       Approach #2:  Store -1 directly in the 2d snake board at the locations where the traps occur.
    State which approach is better, and list two clearly different reasons why it is better.
     
  6. Bonus/Optional:
      // BONUS: What will this draw?
      public void paint(Graphics2D page) {
        int width = 500, height = 500;
        Polygon p = new Polygon();
        int k = 2;
        for (int x=0; x<width; x += 50) {
          for (int y=height/k; y<height; y+=height/k) {
              p.addPoint(x,y);
          }
          k = 1 + ((k + 1) % 4);
        }
        page.fillPolygon(p);
      }


  1: // Snake.java, adapted for Quiz5

  2:

  3: import java.awt.*;

  4: import java.util.*;

  5:

  6: public class Snake extends JComponentWithEvents {

  7:

  8:   private boolean debug = true;

  9:   private int rows = 10;

 10:   private int cols = 10;

 11:   private int[][] snake = new int[rows][cols];

 12:   private int dir = UP;

 13:   private int headRow, headCol;

 14:   private int foodRow, foodCol;

 15:

 16:   public void moveSnake() {

 17:     int snakeLength = snake[headRow][headCol];

 18:     int newHeadRow = headRow, newHeadCol = headCol;

 19:     if (dir == UP) newHeadRow--;

 20:     else if (dir == DOWN) newHeadRow++;

 21:     else if (dir == RIGHT) newHeadCol++;

 22:     else newHeadCol--;

 23:     if ((newHeadRow < 0) || (newHeadRow >= rows) ||

 24:         (newHeadCol < 0) || (newHeadCol >= cols)) {

 25:       die();

 26:     }

 27:     else if (snake[newHeadRow][newHeadCol] > 0) {

 28:       die();

 29:     }

 30:     else if ((newHeadRow == foodRow) && (newHeadCol == foodCol)) {

 31:       headRow = newHeadRow;

 32:       headCol = newHeadCol;

 33:       snake[newHeadRow][newHeadCol] = snakeLength+1;

 34:       placeFood();

 35:     }

 36:     else {

 37:       for (int row=0; row<rows; row++)

 38:         for (int col=0; col<cols; col++)

 39:           if (snake[row][col] > 0)

 40:             --snake[row][col];

 41:       snake[newHeadRow][newHeadCol] = snakeLength;

 42:       headRow = newHeadRow;

 43:       headCol = newHeadCol;

 44:     }

 45:   }

 46:

 47:   public void die() {

 48:     reset();

 49:   }

 50:

 51:   public void keyPressed(char key) {

 52:     if (key == 'm') moveSnake();

 53:     else if (key == UP) dir = UP;

 54:     else if (key == DOWN) dir = DOWN;

 55:     else if (key == RIGHT) dir = RIGHT;

 56:     else if (key == LEFT) dir = LEFT;

 57:   }

 58:


 

 59:   public void timerFired() {

 60:     moveSnake();

 61:   }

 62:

 63:   public void start() {

 64:     reset();

 65:   }

 66:

 67:   public void reset() {

 68:     for (int row=0; row<rows; row++)

 69:       for (int col=0; col<cols; col++)

 70:         snake[row][col] = 0;

 71:     headRow = 0;

 72:     headCol = 1;

 73:     snake[0][1] = 5;

 74:     snake[1][1] = 4;

 75:     snake[2][1] = 3;

 76:     snake[2][2] = 2;

 77:     snake[3][2] = 1;

 78:     dir = RIGHT;

 79:     placeFood();

 80:   }

 81:

 82:   private Random random = new Random();

 83:

 84:   public void placeFood() {

 85:     // YOU WRITE THIS!

 86:   }

 87:

 88:   public void paintCell(Graphics2D page, int row, int col) {

 89:     int width = getWidth(), height = getHeight();

 90:     int left = col * width / cols;

 91:     int right = (col + 1) * width / cols;

 92:     int top  = row * height / rows;

 93:     int bottom = (row + 1) * height / rows;

 94:     page.setColor(Color.black);

 95:     page.drawRect(left, top, right-left, bottom-top);

 96:     if ((row == foodRow) && (col == foodCol)) {

 97:       page.setColor(Color.green);

 98:       page.fillOval(left, top, right-left, bottom-top);

 99:     }

100:     else if (snake[row][col] > 0) {

101:       page.setColor(Color.blue);

102:       page.fillOval(left, top, right-left, bottom-top);

103:       if (debug) {

104:         page.setColor(Color.white);

105:         page.setFont(new Font("Arial", Font.BOLD, 16));

106:         drawCenteredString(page, ""+snake[row][col],

107:                            left, top, right-left, bottom-top);

108:       }

109:     }

110:   }

111:

112:   public void paint(Graphics2D page) {

113:     // YOU WRITE THIS!

114:   }

115:

116:   public static void main(String[] args) { launch(300, 300); }

117: }