// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Pentris.java

import java.awt.*;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

class Board extends JPanel
{

    public Board()
    {
        score = new Score();
        nextPiece = new NextPiece();
        pIsFocused = false;
        showingDialog = false;
        haveWon = false;
        newGame();
    }

    public void setPIsFocused(boolean flag)
    {
        pIsFocused = flag;
        nextPiece.setEnabled(flag);
    }

    void newGame()
    {
        pieces = new Vector();
        current = null;
        futurePieces = Piece.buildGamePieceList();
        score.resetScore();
        nextPiece.setPiece(null);
        getNewPiece();
    }

    public Score getScorePane()
    {
        return score;
    }

    public NextPiece getNextPiecePane()
    {
        return nextPiece;
    }

    public void paintComponent(Graphics g)
    {
        if(!pIsFocused && !showingDialog)
        {
            g.clearRect(0, 0, 160, 320);
            g.setColor(Color.black);
            g.drawString("Click to play", 5, 315);
            return;
        }
        g.clearRect(0, 0, 160, 320);
        for(int i = 0; i < pieces.size(); i++)
            ((Piece)pieces.get(i)).draw(g, false, this);

        if(current != null)
            current.draw(g, false, this);
    }

    public boolean occupied(Point point)
    {
        if(point.y < 0)
            return true;
        if(point.x < 0 || point.x >= 10)
            return true;
        for(int i = 0; i < pieces.size(); i++)
            if(((Piece)pieces.get(i)).contains(point))
                return true;

        return false;
    }

    public void moveHorizontally(int i)
    {
        if(!pIsFocused)
            return;
        if(current == null)
        {
            return;
        } else
        {
            Point point = new Point(i, 0);
            current.moveIfAllowed(point, this);
            return;
        }
    }

    public boolean skipRow(int i)
    {
        return clearRows[i] && (clearCounter & 1) == 1;
    }

    public void poke()
    {
        if(clearCounter > 0)
        {
            clearCounter--;
            if(clearCounter == 0)
            {
                int i = 0;
                for(int j = 19; j >= 0; j--)
                {
                    if(!clearRows[j])
                        continue;
                    for(int k = 0; k < pieces.size(); k++)
                        ((Piece)pieces.get(k)).purgeRow(j);

                    i++;
                }

                int ai[] = {
                    0, 1, 10, 100, 1000, 10000
                };
                score.addPoints(ai[i]);
                getNewPiece();
            }
        } else
        {
            if(fallingCounter > 0)
                fallingCounter--;
            if(fallingCounter == 0)
                fall();
        }
    }

    public boolean fall()
    {
        if(!pIsFocused)
            return false;
        if(current == null)
            return false;
        fallingCounter = 4;
        Point point = new Point(0, -1);
        if(!current.moveIfAllowed(point, this))
        {
            pieces.add(current);
            current = null;
            int i = 0;
            int ai[] = new int[20];
            for(int j = 0; j < pieces.size(); j++)
                ((Piece)pieces.get(j)).addCounts(ai);

            for(int k = 19; k >= 0; k--)
                if(ai[k] == 10)
                {
                    clearRows[k] = true;
                    i++;
                }

            if(i > 0)
                clearCounter = 3;
            else
                getNewPiece();
        }
        return true;
    }

    void getNewPiece()
    {
        if(futurePieces.isEmpty())
            gameOver();
        else
            current = (Piece)futurePieces.remove(0);
        if(!current.moveIfAllowed(new Point(0, 0), this))
            gameOver();
        fallingCounter = 4;
        clearCounter = 0;
        clearRows = new boolean[20];
        if(futurePieces.isEmpty())
            nextPiece.setPiece(null);
        else
            nextPiece.setPiece((Piece)futurePieces.get(0));
    }

    void gameOver()
    {
        current = null;
        getParent().repaint();
        if(score.isPerfect())
            haveWon = true;
        String s = haveWon ? "You        90000" : "Mr. E. Hunt     89999";
        showingDialog = true;
        if(score.isPerfect())
            JOptionPane.showMessageDialog(null, "High Score List\n" + s + "\n\nCongratulations!", "Game Over!", 1);
        else
            JOptionPane.showMessageDialog(null, "High Score List\n" + s + "\n\nYou lose!  Try again?", "Game Over!", 0);
        showingDialog = false;
        newGame();
        getParent().requestFocus();
    }

    Vector pieces;
    Piece current;
    Vector futurePieces;
    Score score;
    NextPiece nextPiece;
    boolean pIsFocused;
    boolean showingDialog;
    int fallingCounter;
    static final int FALL_COUNT = 4;
    int clearCounter;
    static final int CLEAR_COUNT = 3;
    boolean clearRows[];
    boolean haveWon;
    public static final int SQUARESIZE = 16;
    public static final int ROWS = 20;
    public static final int COLS = 10;
}
