import java.awt.Color;
import java.awt.Image;

public class Ground {

    /**
     * 创建Ground的实例.
     * 
     * @param rowCount
     *        : Ground的行数
     * @param columnCount
     *        : Ground的列数
     */
    public Ground(int rowCount, int columnCount) {
        this.rowCount = rowCount;
        this.columnCount = columnCount;
        data = new int[rowCount][columnCount];
        colors = new Color[rowCount][columnCount];
        images = new Image[rowCount][columnCount];
    }

    public int getRowCount() {
        return rowCount;
    }

    public int getColumnCount() {
        return columnCount;
    }
    
    public int getCellValue(int offsetX, int offsetY) {
        return data[offsetY][offsetX];
    }
    
    public Color getCellColor(int offsetX, int offsetY) {
        return colors[offsetY][offsetX];
    }
    
    public Image getCellImage(int offsetX, int offsetY) {
        return images[offsetY][offsetX];
    }
    
    public void clear() {
        for (int i = 0; i < rowCount; ++i) {
            for (int j = 0; j < columnCount; ++j) {
                data[i][j] = 0;
            }
        }
    }

    /**
     * 合并shape的图形到Ground里
     * 
     * @param shape
     */
    public int accept(Shape shape) {
        int shapeX = shape.getX(); // shape相对于Ground左上角原点的x坐标
        int shapeY = shape.getY(); // shape相对于Ground左上角原点的y坐标
        int x, y;   // shape在Ground中的坐标
        int sx, sy; // shape中cell在它里面的坐标
        
        for (sx = 0; sx < shape.getColumnCount(); ++sx) {
            for (sy = 0; sy < shape.getRowCount(); ++sy) {
                if (shape.getCellValue(sx, sy) == 1 ) {
                    x = sx + shapeX;
                    y = sy + shapeY;
                    
                    if ((x >= 0 && x < columnCount) && (y >= 0 && y < rowCount)) {
                        // 必须判断，因为如shape还有部分在上面时
                        setCellValue(x, y, 1);
                        setCellColor(x, y, shape.getColor());
                        setCellImage(x, y, shape.getImage());
                    }
                }
            }
        }
        
        return deleteFullLine();
    }
    
    private void setCellValue(int offsetX, int offsetY, int value) {
        data[offsetY][offsetX] = value;
    }
    
    private void setCellColor(int offsetX, int offsetY, Color color) {
        colors[offsetY][offsetX] = color;
    }
    
    private void setCellImage(int offsetX, int offsetY, Image image) {
        images[offsetY][offsetX] = image;
    }
    
    // 删除满的行
    private int deleteFullLine() {
        int score = 0;
        int step = 0;
        
        // 从上到下逐行扫描
        for (int i = 0; i < rowCount; ++i) {
            boolean full = true;
            for (int j = 0; j < columnCount; ++j) {
                if (data[i][j] == 0) {
                    full = false;
                    break;
                }
            }

            if (full) {
                score += 10 + step;
                step += 5;
                // 删除当前满的行，然后上面的所有行向下移动一行
                for (int m = i; m > 0; --m) {
                    for (int n = 0; n < columnCount; ++n) {
                        data[m][n]   = data[m - 1][n];
                        colors[m][n] = colors[m-1][n];
                        images[m][n] = images[m-1][n];
                    }
                }
            }
        }
        
        return score;
    }

    private int rowCount, columnCount;
    private int[][] data;
    private Color[][] colors;
    private Image[][] images;
}
