CS51 - Spring 2010 - Lecture 19

  • Exercise 15.4.4

    You are on a team that is developing a game, in which the player must escape from a monster. Both the monster and the player move through a space that looks like a grid. In a single move, the monster can move one space vertically, horizontally, or diagonally. The player can only move vertically or horizontally. However, the play can do something that the monster cannot do. The monster can never move off the grid. If it is at the right edge of the grid, for example, it cannot move to the right. The player, on the other hand, can move to the right, re-entering the grid on the opposite side. The player can make analogous moves off the left edge of the grid as well.

    a. Write a method moveRight that takes an int parameter col that is the player's current column. The method should retrun an int that is the column into which the player should move if going right. You may assume that a constant COLS has been defined that gives the total number of columns in the grid.

    b. Now write a method moveLeft.

  • magic square
       - a magic square is an n by n matrix
       - containing the numbers 1 through n^2
       - such that all the rows, columns and diagonals sum to the same number

  • show MagicSquares demo
       - How can we represent a magic square?
          - an n by n integer array
          - int[][] magicArray = new magicArray[n][n];
       - if n is odd, you can build a magic square with the following rules
          - put a 1 in the center of the bottom row
          - place the next number (i.e. 2 follows 1, etc) in
             - the cell one slot below and one slot to the right
                - if you fall off the bottom or right, wrap around
             - if that cell already has a number, put it in the slot above the original position (again, with wraparound)
       - for an n by n integer array, how would we put a 1 in the center of the bottom row?
          - magicArray[n-1, n/2] = 1;
       - given row and col, what would be the square one slot below and right?
          - row+1
          - col+1
       - how do we deal with wrap around for an n by n array?
          - row + 1 % n
          - col + 1 % n
       - with wrap around, what is the column above col for an n by n array?
          - col - 1 % n
          - (col - 1 + n) % n

  • show MagicSquares code (specifically MagicSquare.fillSquare)

  • How is an image represented/stored in a computer?
       - a common approach is a matrix of Colors (often represented by RGB values)
       - How would we do this?
          - Color[][] image

  • show ImageEdit demo
       - we have two window areas
          - the one on the left is our image
          - the one on the right is our modified image
       - two variables
          - Color[][] source
          - Color[][] target
       - other private variables
          - private int height; // number of rows
          - private int width; // number of cols
       - grayscale
          - modifies one pixel at a time
          - What makes a color gray?
             - gray colors have identical RGB values
             - a measure of brightness
       - horizontal reversal
          - to do this we'll have two for loops and fill in "target"
             for( int row = 0; row < height; row++ ){
                for( int col = 0; col < width; col++ ){
          - let's look at a diagram - where does pixel row, col come from?
             - row stays the same
             - col is the distance from the col to the end of the image
          - target[row][col] = source[row][width-col-1]
             - target[row][col] = source[row][source[col].length-col-1]
       - vertical inversion
          - again, where does pixel row, col come from?
             - column stays the same
             - row is the distance from the row to the end of the image
             - target[row][col] = source[height - row - 1][col]
          - Is there a more efficient way of doing this?
             for( int row = 0; row < height; row++ ){
                target[row] = source[height-row-1]
          - Is there any downside/difference to this second approach?
       - rectangular averaging
          - target is the average of the pixels in the source in a rectangular range
             - private int range; // defines how far from the pixel we look in any direction
          - let's start by not worrying about the boundary cases
          for( int row = 0; row < height; row++ ){
             for( int col = 0; col < width; col++ ){
                int redSum = 0;
                int greenSum = 0;
                int blueSum = 0;
                int count = 0;

                for( int i = row-range; i < row+range+1; i++ ){
                   for( int j = col-range; j < col+range+1; j++ ){
                      redSum += source[i][j].getRed();
                      greenSum += source[i][j].getGreen();
                      blueSum += source[i][j].getBlue();
             // average the values
             target[row][col] = new Color(redSum/count, greenSum/count, blueSum/count);
          - What do we need to add to account for the boundaries?
             - Some useful functions
                - Math.min
                - Math.max
             for( int i = Math.max(O,row-range); i < Math.min(width, row+range+1); i++ ){
                for( int j = Math.max(0,col-range); j < Math.min(hieght, col+range+1); j++ ){

  • look at MineSweeper

  • larger dimensional arrays
       - int[][][]
       - the java language does not restrict the number of dimensions
       - most implementations, max at 255

  • extensible arrays
       - how would you do it?
       - java.util.ArrayList
          - get(index) // returns value at index
          - set(index, value) // sets value at index to be value
          - add(value) // adds value to the end of the array

  • Nibbles lab
       - Snake is a 1D array and is an ActiveObject
       - NibblesField is a 2D array
          - keeps track of the state of the board
          - also keeps track of what's going on on the screen
       - 2 other support classes
          - Position
          - Direction
       - look at moving the snake
          - stretch and shrink
             - two different ways of implementing
          - how can we use stretch and shrink to move?
          - how can we use stretch and shrink to move?
       - lab has two parts
          - code the Snake class (given a working implementation of the NibblesField class)
          - once you've done this, code up the NibblesField class
       - design
          - only have to do a design for the Snake class, but make sure to think about the second part of the lab too
          - may work in groups on the design (and only need to provide one design)