簡體   English   中英

如何構建游戲拼圖 3 * 3?

[英]how to build game puzzle 3 * 3?

目前我正在使用java構建游戲拼圖滑動。 首先,我的算法是在這些按鈕上創建帶有設置圖像的按鈕數組 2d,並將它們添加到面板(網格布局為 3*3)。 我的問題是如何將這些按鈕上的每張圖像與原始圖像進行比較? 下面的代碼是我向 Jpanel 添加按鈕的地方。

private void set() throws Exception {
    position = new int[row][col];
    lstBtn = new JButton[row][col];
    count = new int[row * col];
    arr=new ArrayList<>(9);

    panelContainer.setLayout(null);
    panelContainer.setLayout(new GridLayout(row, col));
    BufferedImage img;
    int numCount = 0;
    int posNum=0; 

    orgImg=new BufferedImage[row][col];
    for (int i = 0; i < row; i++) {
         for (int j = 0; j < col; j++) {

           position[i][j] = posNum++;

            if (i ==0 & j == 0) {
           // For blank Button
                lstBtn[i][j] = new JButton();
                lstBtn[i][j].setBackground(Color.WHITE);
               // panelContainer.add(lstBtn[i][j]);
                lstBtn[i][j].setVisible(true);
                arr.add(lstBtn[i][j]);
                count[0] = numCount;
            }
            else {
                   numCount++;
                lstBtn[i][j] = new JButton();
                lstBtn[i][j].addActionListener(this);
             img = image_cutting.getSubImage(j,i);//160, 116 //120, 87,
             orgImg[j][i]=img;
              lstBtn[i][j].setIcon(new ImageIcon(orgImg[j][i].getScaledInstance(160, 116, BufferedImage.SCALE_SMOOTH)));
                lstBtn[i][j].setHorizontalTextPosition(JButton.CENTER);
                lstBtn[i][j].setVerticalTextPosition(JButton.CENTER);
                lstBtn[i][j].setFont(new Font("Dialog",2,20));
                lstBtn[i][j].setForeground(Color.BLUE);
                 lstBtn[i][j].setText("" + numCount); 
                arr.add(lstBtn[i][j]);
             //  panelContainer.add(lstBtn[i][j]);
               lstBtn[i][j].setVisible(true);
            }
            if (numCount == 0) {
                continue;
            }
            count[numCount] = numCount;
        }

此圖像波紋管是拼圖滑動與匹配圖像的示例

在此處輸入圖片說明

這是一個概念性的想法,需要一些充實,但是。 從一個單件的想法開始,它在拼圖中包含它的“順序”或“索引”以及它的圖像

public class PuzzelPiece {
    private int index;
    private Image img;

    public PuzzelPiece(int index, Image img) {
        this.index = index;
        this.img = img;
    }

    public Image getImage() {
        return img;
    }

    public int getIndex() {
        return index;
    }


}

接下來,將其包裝在某種模型中。 它應該管理這些部分,提供對它們的訪問,並可能提供“已訂購”檢查以確定這些部分是否有序。

這個例子需要一個源圖像,你想要的列數/行數,然后將圖像切片和切塊。 然后打亂這些碎片並添加一個空白碎片來代替第一個碎片(索引為0的碎片)

public class Puzzle {

    private List<PuzzelPiece> pieces;

    public Puzzle(BufferedImage source, int cols, int rows) {

        int rowHeight = source.getHeight() / rows;
        int colWidth = source.getWidth() / cols;

        pieces = new ArrayList<>(25);
        int index = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                Image img = source.getSubimage(col * colWidth, row * rowHeight, colWidth, rowHeight);
                pieces.add(new PuzzelPiece(index++, img));
            }
        }
        pieces.remove(0);
        Collections.shuffle(pieces);
        pieces.add(new PuzzelPiece(0, null)); // Blank
    }

    public boolean isOrdered() {
        // Instead of sorting the list like this each time
        // you could just maintain two lists to start with
        // one ordered and one shuffled
        List<PuzzelPiece> ordered = new ArrayList<>(pieces);
        Collections.sort(ordered, new Comparator<PuzzelPiece>() {
            @Override
            public int compare(PuzzelPiece o1, PuzzelPiece o2) {
                return o1.getIndex() - o2.getIndex();
            }
        });
        boolean isOrdered = true;
        for (int index = 0; index < ordered.size(); index++) {
            if (ordered.get(index) != pieces.get(index)) {
                isOrdered = false;
                break;
            }
        }
        return isOrdered;
    }

    public int size() {
        return pieces.size();
    }

    public PuzzelPiece getPieceAt(int index) {
        return pieces.get(index);
    }

    public void swap(PuzzelPiece piece, PuzzelPiece with) {
        int pieceIndex = pieces.indexOf(piece);
        int withIndex = pieces.indexOf(with);

        pieces.set(pieceIndex, with);
        pieces.set(withIndex, piece);
    }
}

它提供了一個簡單的swap方法來交換碎片和簡單的isOrdered方法來檢查列表是否有序

這是未經測試的,“按原樣”提供,旨在推動想法,提供實現

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM