CS51 - Spring 2010 - Lecture 18

  • quiz: problem 14.9.3

  • lost watch

  • Quick review of scope
       - What is the scope of a variable?
          - the section of code that variable can be referred to by that name
       - What scoping options do we have?
          - public member variable
          - protected member variable
          - private member variable
          - local variable (and varying nestings, for example within for and while loops)
       - When designing a program, how do you decide what scope is appropriate for a variable?
       - you should pick the smallest scope possible that accomplishes your task

  • Show TicTacToe demo
       - We need to keep track of the state of the board
       - How can we do this?
          - 9 entries, could have 9 variables
          - could have a 9 entry array
       - What is the natural way to talk about entries?
          - rows and columns

  • Multidimensional arrays
       - declaration
          - int[][] a2dIntArray;
          - String[][] a2dStringArray;
          - FramedRect[][] a2dRectArray;
       - initialization
          - a2dIntArray = new int[10][100]; // a 10 by 100 array
       - draw picture
          - notice that it's really an array of arrays
       - accessing elements (remember, arrays start at 0)
          - a2dIntArray[0][0] // the first element in the first row
          - a2dIntArray[1][5] // the sixth element in the second row
       - how would we find the sum of all the elements in an integer array, given numRows and numCols?
          public int sum(int[][] a2dIntArray, int numRows, int numColumns){
             int sum = 0;

             for( int row = 0; row < numRows; row++ ){
                for( int col = 0; col < numColumns; col++ ){
                   sum += a2dIntArray[row][col];

             return sum;
       - what if we wanted to traverse by columns and then rows?
       - how would we find the sum of all the elements in an integer array, with just the array specified?
          public int sum(int[][] a2dIntArray){
             int sum = 0;

             for( int i = 0; i < a2dIntArray.length; i++ ){
                for( int j = 0; j < a2dIntArray[i].length; i++ ){
                   sum += a2dIntArray[i][j];

             return sum;

  • Back to TicTacToe...
       - Using a multidimensional array, how can we keep track of the board?
          - private int NUM_ROWS = 3;
          - private int NUM_COLS = 3;
          - 3 by 3 array of integers: int[][] marks = new int[NUM_ROWS][NUM_COLS];
             - private int EMPTY_MARK = 0;
             - private int X_MARK = 1;
             - private int O_MARK = 2;
       - how does the game play progress?
          - user clicks: onMouseClick
             - check if we've clicked within the board
             - figure out which square the user clicked
             - figure out if it's a valid square to add the symbol to
             - place the symbol (if appropriate)
             - check for a win
       - How do we check for a win? (group work?)
          - need to check four cases:
             - rows all the same
             - columns all the same
             - top-left to bottom-right diagonal all the same
             - top-right to bottom-left diagonal all the same
          - Note: when we're checking for a win, we're checking if a particular player has won for the last move
             - we know the mark the player made (i.e. X or O)
             - we know the row and col position where the mark was made
       - figuring out which square the user clicked
          - given...
             LEFT // x value of the left of the board
             TOP // y value of the top of the board
             CELL_SIZE // the size of the an individual cell in the board
          - draw a picture of the window with the nested board
          - if we know that the user clicked at x, y, which we know is in the board, how do we calculate the row/col indices
             x = x - LEFT;
             y = y - TOP;
             int col = (int)(x/CELL_SIZE);
             int row = (int)(y/CELL_SIZE);
       - look at TicTacToe code
          - in addition to checking for a win, the win methods also draw the winning line
       - what would be another representation for the baord?
          - option 1: two boolean 2d arrays, one if there is a mark or not and one whether it was an X or an O
             private boolean[][] isMarked;
             private boolean[][] isX;
          - option 2: create another class that represents an entry, say TicTacToeEntry
             private TicTacToeEntry[][] board;

  • 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

  • take a few minutes and try to do a 3 by 3 magic square

  • 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)