I am creating a puzzle game and am now having the 8 pieces randomly arranging themselves in a 3x3 grid, but not all the images are be added to the HashMap. Does anyone see why?
Here is the code I use to arrange the puzzle:
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());
}
This is what the console prints when this loads:
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
As 8 points are being listed I do not see why puzzle.size()
would not return 8 as the 8 points are being added to puzzle
with a random BufferedImage that is then removed so I don't choose the same image twice.
EDIT: I have solved this. I had my Assets loading incorrectly. Sorry to waste your time.