0

我正在创建一个益智游戏,现在将 8 块随机排列在 3x3 网格中,但并非所有图像都添加到 HashMap。有人知道为什么吗?

这是我用来排列拼图的代码:

private HashMap<BufferedImage, Point> puzzle = new HashMap<>();
public void arrangePieces() {
    Random random = new Random();
    List<BufferedImage> rImages = new ArrayList<>();
    rImages.addAll(Arrays.asList(
                       Assets.a2, Assets.a3,
            Assets.a4, Assets.a5, Assets.a6,
            Assets.a7, Assets.a8, Assets.a9
    ));

    for(int y = 1; y <= 3; y++) {
        for(int x = 1; x <= 3; x++) {
            if(y != 1 || x != 1) {
                BufferedImage image = rImages.get(random.nextInt(rImages.size()));
                puzzle.put(image, new Point(x, y));
                System.out.println("Loading new Image at " + new Point(x, y));
                rImages.remove(image);
            }
        }
    }
    System.out.println(puzzle.size());
}

这是加载时控制台打印的内容:

Loading new Image at java.awt.Point[x=2,y=1]
Loading new Image at java.awt.Point[x=3,y=1]
Loading new Image at java.awt.Point[x=1,y=2]
Loading new Image at java.awt.Point[x=2,y=2]
Loading new Image at java.awt.Point[x=3,y=2]
Loading new Image at java.awt.Point[x=1,y=3]
Loading new Image at java.awt.Point[x=2,y=3]
Loading new Image at java.awt.Point[x=3,y=3]
1

由于列出了 8 个点,我不明白为什么puzzle.size()不返回 8,因为 8 个点被添加到puzzle一个随机的 BufferedImage 中,然后被删除,所以我不会两次选择相同的图像。

编辑:我已经解决了这个问题。我的资产加载不正确。很抱歉浪费您的时间。

4

1 回答 1

0
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class Snippet {
    public static void main(String[] args) {
        arrangePieces();
    }
    private static HashMap<BufferedImage, Point> puzzle = new HashMap<>();
    public static void arrangePieces() {
        Random random = new Random();
        List<BufferedImage> rImages = new ArrayList<>();
        rImages.addAll(Arrays.asList(
                           Assets.a2, Assets.a3,
                Assets.a4, Assets.a5, Assets.a6,
                Assets.a7, Assets.a8, Assets.a9
        ));

            System.out.println(rImages.size());// try this and see what you get as response here
        for(int y = 1; y <= 3; y++) {
            for(int x = 1; x <= 3; x++) {
                if(y != 1 || x != 1) {
                    BufferedImage image = rImages.get(random.nextInt(rImages.size()));
                    puzzle.put(image, new Point(x, y));
                    System.out.println("Loading new Image at " + new Point(x, y));
                    rImages.remove(image);
                }
            }
        }
        System.out.println(puzzle.size());
    }

}
class Assets {

    public static BufferedImage a8 = new BufferedImage(1,1, 1);
    public static BufferedImage a7= new BufferedImage(1,1, 1);
    public static BufferedImage a4= new BufferedImage(1,1, 1);
    public static BufferedImage a5= new BufferedImage(1,1, 1);
    public static BufferedImage a9= new BufferedImage(1,1, 1);
    public static BufferedImage a6= new BufferedImage(1,1, 1);;
    public static BufferedImage a3= new BufferedImage(1,1, 1);
    public static BufferedImage a2= new BufferedImage(1,1, 1);


}

所以当我运行这个时,我得到这个作为输出

在 java.awt.Point[x=2,y=1]
加载新图像 在 java.awt.Point[x=3,y=1]
加载新图像 在 java.awt.Point[x= 1] 加载新图像y=2]
在 java.awt.Point[x=2,y=2]
加载新图像在 java.awt.Point[x=3,y=2] 在 java.awt.Point[x=3,y=2]
加载新图像x=1,y=3]
在 java.awt.Point[x=2,y=3]
加载新图像 在 java.awt.Point[x=3,y= 3] 加载新图像
8

这意味着问题在于您构建 Assets 类对象的方式。

如果你只是将它定义为静态,它会被初始化为 null 然后大小将为 1,这可以通过我在代码中添加的注释 SOP 轻松检查

于 2016-09-24T21:26:47.170 回答