import java.awt.Graphics;
import java.awt.Event;
import java.awt.Panel;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Button;
import java.awt.Dimension;

class CellButton extends Button {
    private int x;
    private int y;
    private int number;
    public CellButton(int x, int y, int number) {
        super(number+"");
        this.x = x;
        this.y = y;
        this.number = number;
    }
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
    public void setNumber(int number) {
        this.number = number;
        setLabel(number+"");
    }
    public int getNumber() {
        return number;
    }
}

public class Puzzle16Applet extends java.applet.Applet {
    private final int CELL_ROWS = 4;
    private final int CELL_COLUMNS = 4;
    private final int SHUFFLE_NUM = 500;
    private Panel       gamePanel;
    private CellButton  cellButton[][];
    private Button      shuffleButton;
    private Label       noticeLabel;

    private void rotate(int x, int y) {
        int     tempNumber;

        if (x < CELL_COLUMNS-1 && y < CELL_COLUMNS-1) {
            tempNumber = cellButton[x][y].getNumber();
            cellButton[x][y].setNumber(cellButton[x+1][y].getNumber());
            cellButton[x+1][y].setNumber(cellButton[x+1][y+1].getNumber());
            cellButton[x+1][y+1].setNumber(cellButton[x][y+1].getNumber());
            cellButton[x][y+1].setNumber(tempNumber);
        }
    }
    private void setNoticeLabel(String str) {
        Dimension dim;

        noticeLabel.setText(str);
        dim = noticeLabel.preferredSize();
        noticeLabel.resize(dim);
    }
    private void shuffle() {
        int     i;

        for (i = 0; i < SHUFFLE_NUM; i++) {
            rotate((int)(Math.random() * CELL_COLUMNS),
                   (int)(Math.random() * CELL_ROWS));
        }
    }
    private boolean isFinished() {
        int     x, y;

        for (y = 0; y < CELL_ROWS; y++) {
            for (x = 0; x < CELL_COLUMNS; x++) {
                if (y * CELL_COLUMNS + x + 1
                    != cellButton[x][y].getNumber()) {
                    return false;
                }
            }
        }
        return true;
    }
    public void init() {
        int     x, y;
        Panel   buttonPanel;

        setLayout(new BorderLayout());

        buttonPanel = new Panel();
        add("North", buttonPanel);

        buttonPanel.setLayout(new FlowLayout());
        shuffleButton = new Button("Shuffle");
        buttonPanel.add(shuffleButton);
        noticeLabel = new Label("Puzzle16");
        buttonPanel.add(noticeLabel);

        gamePanel = new Panel();
        add("Center", gamePanel);
        gamePanel.setLayout(new GridLayout(CELL_COLUMNS, CELL_ROWS));
        cellButton = new CellButton[CELL_COLUMNS][CELL_ROWS];
        for (y = 0; y < CELL_COLUMNS; y++) {
            for (x = 0; x < CELL_COLUMNS; x++) {
                cellButton[x][y]
                    = new CellButton(x, y, y * CELL_ROWS + x + 1);
                gamePanel.add(cellButton[x][y]);
            }
        }
        shuffle();
    }
    public boolean action(Event event, Object arg) {
        CellButton      eventCell;

        if (event.target instanceof CellButton) {
            eventCell = (CellButton)event.target;
            rotate(eventCell.getX(), eventCell.getY());
            if (isFinished()) {
                setNoticeLabel("Puzzle16");
            }
        } else if (event.target == shuffleButton) {
            shuffle();
            setNoticeLabel("Congratulation!!");
        }
        return true;
    }
}

パズル16のページに戻る