/**
 * Representation of a scoreboard.  Contains a rectangular array
 * of Cell objects.
 *
 * @author Phil Nico, Kevin O'Gorman
 * @version 27 February, 2003
 */
import java.awt.*;

public class Board 
{
  private Graphics page;	// where is this board to be displayed
  private final int BOARDSIZE;	// how big a square is the board

  private Cell[][] grid;	// the game board

  /**
   * Construct a board.
   *
   * @param where I.e. where to show things.
   * @param size The size of the board.  A 1-cell inactive boundary will
   *		be added.
   * @param dotsize Dot size in pixels.
   * @param fg Foreground color.
   * @param bf Background color.
   */
  public Board(Graphics where, int size, int dotsize, Color fg, Color bg){
    page      = where;
    BOARDSIZE = size;

    // set up static Cell parameters 
    Cell.setUpCellParams(page,fg,bg,dotsize);

    // creat the grid of cells with a boarder one larger than the 
    // specified size
    grid = new Cell[BOARDSIZE + 2][BOARDSIZE + 2];
    initializeBlank();
  }

  private void initializeBlank(){
    // Create all the cells as dead (including the "dead border" ones)
    for(int i = 0; i < BOARDSIZE+2 ; i++)
      for(int j = 0; j < BOARDSIZE+2 ; j++)
	grid[i][j] = new Cell(i,j);
  }

  /**
   * Move future data to current.  Subsequent calls to drawBoard() will
   * then draw the board.
   */
  public void updateBoard(){
    // now make the future data current
    for(int i = 1; i < BOARDSIZE+1; i++)
      for(int j = 1; j < BOARDSIZE+1 ; j++)
	grid[i][j].updateCell();
  }
  
  /**
   * Draw the board by calling the showCell() method of each non-border
   * cell.
   */
  public void drawBoard(){
    // draws the board
    for (int i = 0; i < BOARDSIZE+2; i++)
      for (int j = 0; j < BOARDSIZE+2 ; j++)
	grid[i][j].showCell();
  }

  /**
   * Change the Graphics object.
   *
   * @param page The new location for graphics operations.
   */
  public void setPage(Graphics page){
    this.page = page;
    Cell.setDisplay(page);
  }
  
  /**
   * Draw the board by calling the showCell() method of each non-border
   * cell.  Uses a specified Graphics object.
   *
   * @param page The graphics page to draw on.
   */
  public void drawBoard(Graphics page){
    // draws the board on the specified page
    // sets the Cells' static display parameter, since
    // the display info may have changed
    setPage(page);
    drawBoard();
  }

  /**
   * Return the Cell at specified coordinates, or null.
   *
   * @param i The X coordinate.
   * @param j The Y coordinate.
   * @return The Cell object at the given coordinates, if 
   *    it exists, and <code>null</code> otherwise.
   */
  public Cell getCell(int i, int j){
    // returns the cell at the given location
    if ( (i>=0) && (j>=0) && (i < BOARDSIZE+2) && (j < BOARDSIZE+2) )
      return grid[i][j];
    else 
      return null;
  }

}