// Author: Paul Gries
// File Grid.java. Implement the grid of cells for the game
// of Life using an array and a singly linked list of the live cells.

import java.util.*;
import java.awt.*;
import java.io.*;

// ------------------------------------------------------------------
class Grid {

    // Two dimensional array containing the live and empty cells
    Cell [][] theGrid;
    
    // The array of information needed to decide who lives and dies.
    CellInfoNode[][] theInfoGrid;
    
    // The first cell in theInfoGrid.
    CellInfoNode theInfoList;

    // dimensions of the grid
    int nRows, nCols;


    // Constructor.
    // Create an empty Grid with r rows and c columns.
    // ------------------------------------------------------------------
    public Grid (int r, int c) {
        nRows = r;
        nCols = c;
        theGrid = new Cell[r][c];
        theInfoGrid = new CellInfoNode[r][c];
        setEmpty();
    }


    // = the number of rows in my grid.
    // ------------------------------------------------------------------
    public int numRows () {
        return nRows;
    }
    

    // = the number of columns in my grid.
    // ------------------------------------------------------------------
    public int numCols () {
        return nCols;
    }

    
    // Set my grid to contain no cells.
    // ------------------------------------------------------------------
    private void setEmpty () {
        int row;
        for (row = 0; row < nRows; row++) {
            int col;
            for (col = 0; col < nCols; col++) {
                theGrid[row][col] = new EmptyCell ();
            }
        }
    }
    

    // Print row r of my grid of cells to standard output.
    // ------------------------------------------------------------------
    private void showTextRow (int r) {

        int c;// The current column.
        for (c = 0; c < nCols; c++) {
            theGrid[r][c].showText ();
        }

    }


    // Print my grid of cells to standard output.
    // ------------------------------------------------------------------
    public void showText () {
        
        int r;// The current row.
        for (r = 0; r < nRows; r++) {
            showTextRow (r);
            System.out.println ("");
        }
        
        System.out.println ("");
    }


    // Return the number of neighbours of the cell at (row,col) in my
    // grid.
    // ------------------------------------------------------------------
    public int numNeighbours (int row, int col) {
    
        return (theInfoGrid[row][col] == null)
            ? 0
            : theInfoGrid[row][col].numNeighbours;
    
    }

     
    // If there is not already a live cell at location (r,c),
    // create a new live cell at location (r,c) in my grid.
    // --------------------------------------------------------------
    public void addLiveCell (int r, int c) {
        if (theGrid[r][c] instanceof LiveCell) {
            System.err.print("Warning: There is already a live cell");
            System.err.println(" at location ("+ r +","+c+ ")");
            return;
        }
        
        theGrid[r][c] = new LiveCell();
        
        CellInfoNode newCell = new CellInfoNode(r, c, true);
        theInfoGrid[r][c] = newCell;
        
        // Insert the new CellInfoNode in the linked list.
        newCell.next = theInfoList;
        theInfoList = newCell;

    }


    // Compute the next generation of cells.
    // --------------------------------------------------------------
    public void nextGeneration() {

    }


    // Show my grid of cells.
    // --------------------------------------------------------------
    public void display (Graphics g) {
        for (int r = 0; r < nRows; r++) {
            for (int c = 0; c < nCols; c++) {
                theGrid[r][c].display((2+c)*Cell.size,(3+r)*Cell.size,g);
            }
        }
    }

}

