【问题标题】:how to build game puzzle 3 * 3?如何构建游戏拼图3 * 3?
【发布时间】:2016-03-17 04:38:39
【问题描述】:

目前我正在使用 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;
        }

下面的这张图片是与匹配图片滑动的拼图示例

【问题讨论】:

  • 你到底是什么意思?发布MCVE 以获得更多帮助
  • 不要比较 图像。看看按钮在哪里——每个按钮都有一个“正确”的位置,不管上面是什么图像。
  • 是的,这就是我想要找到的,我应该怎么做才能知道按钮位置在正确的位置?
  • 您需要某种模型来跟踪每个图像或片段,然后 UI 会复制模型的状态
  • 是的,我使用数组来存储原始图像。但我的问题是如何知道它是校正位置

标签: java algorithm swing awt


【解决方案1】:

这是一个概念性的想法,需要一些充实,但是。从单件的想法开始,其中包含拼图中的“顺序”或“索引”以及图像

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 方法来检查列表是否有序

这是未经测试的,“按原样”出现,旨在推动这个想法,提供一个实现

【讨论】:

  • Puzzle#getPieceAt 返回一个PuzzlePiece,然后您可以使用getImage
  • 如果我将图像设置为按钮,则根据您的代码。是否有可能知道图像是按钮上的订单?
  • 不是通过按钮,而是通过模型。这个想法是,当您想要交换图像时,您可以通过模型进行操作(其中可能包含更多逻辑来确定切换是否合法),然后您将根据状态更新按钮模型。目的是,您的视图应该尽可能地愚蠢,只对模型的状态做出反应
  • 所以按钮上的图片只代表图片。
  • 差不多吧,显示哪张图取决于机型
猜你喜欢
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多