import java.awt.*;
import java.awt.event.*;
/*  invariant
        A Grid object ...
            Is a rectangular grid of 20 pixel by 20 pixel square cells that displays
                    upon a background canvas.
            and foreach grid cell: if the cell is empty, then it displays as a white
                            square region with a black border
            and ForEach grid cell: the cell is nonempty iff and only if it displays as a gray
                            filled square a red filled square or a numbered square  
            and a button is displayed upon the canvas */
public class Grid  implements ActionListener {
    private Frame canvas;
    private ClickTile[][] grid;
    private Button theButton;
    
    /*	post:	grid is a rectangular array of rowCount rows and columnCount columns
                and each cell in grid is empty 
                and theButton.getText == s  */
    public Grid(int rowCount, int columnCount, String s)   {
        canvas = new Frame();
        canvas.setBounds(10, 10, 20*columnCount+100, 20*rowCount+160);
        canvas.setLayout(null);
        theButton = new Button(s);
        theButton.setBounds((20*columnCount-100)/2, 20*rowCount+100, 200, 20);
        theButton.addActionListener(this);
        canvas.add(theButton);
        grid = new ClickTile[rowCount][columnCount];
        for (int r=0; r!=rowCount; r++)   {
            for (int c=0; c!=columnCount; c++)   {
                grid[r][c] = new ClickTile(40+c*20, 40+r*20, canvas);
            }
        }
        canvas.show();
    }
    
    /*	post:	result == number of rows in the grid - 1 */
    public int rowMax() {
        return grid.length-1;
    }
    
    /*	post:	result == number of columns in the grid - 1 */
    public int columnMax() {
        return grid[0].length-1;
    }
    
    
    /*	pre:	0<=r<=grid.length()  and  0<=c<=grid[0].length
                                        (throws IndexOutOfBoundsException)
        post:	result == grid[r][c] is an empty cell */
    public boolean isEmptyCell(int r, int c)   {
        return grid[r][c].isEmptyCell();
    }
        
    /*	pre:	0<=r<=grid.length()  and  0<=c<=grid[0].length
                                        (throws IndexOutOfBoundsException)
        post:	grid[r][c] is numbered with num */
    public void setCellToNumber(int r, int c, int num)   {
        grid[r][c].setStringValue(""+num);
    }
        
    /*	pre:	0<=r<=grid.length()  and  0<=c<=grid[0].length
                                        (throws IndexOutOfBoundsException)
        post:	grid[r][c] is an empty cell */
    public void setCellToEmpty(int r, int c)   {
        grid[r][c].setToEmpty();
    }
        
    /*	pre:	0<=r<=grid.length()  and  0<=c<=grid[0].length
                                        (throws IndexOutOfBoundsException)
        post:	grid[r][c] is a gray cell */
    public void setCellToGray(int r, int c)   {
        grid[r][c].setColor(Color.gray);
    }
        
    /*	pre:	0<=r<=grid.length()  and  0<=c<=grid[0].length
                                        (throws IndexOutOfBoundsException)
        post:	grid[r][c] is a red cell */
    public void setCellToRed(int r, int c)   {
        grid[r][c].setColor(Color.red);
    }
    
    /*	event
            This method is called in response to a user click on theButton.
            It is expected that this method will be overridden. */
    public void actionPerformed(ActionEvent e )   {
    }
}

