CS51  Spring 2010  Lecture 18
http://www.xkcd.com/816/
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, reentering 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.
preregistration pizza
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
 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);
 How do we check for a win? (group work?)
 need to check four cases:
 rows all the same
 columns all the same
 topleft to bottomright diagonal all the same
 topright to bottomleft 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
 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)
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[n1, 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)
Nibbles lab
 Nibbles in real life :)

http://www.youtube.com/watch?v=dO3dhWHrNeM
 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?
 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)
CS course options next semester
 Descriptions:
http://www.cs.pomona.edu/courses.html
 CS62: Data Structures and Advances Programming
 CS55: Discrete Mathematics