0

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.

1 Answers1

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);


}

So when i run this i get this as output

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]
8

It means issue is in the way you are constructing Assets class objects.

If you just define it static, it is initailed to null and then the size will be 1, this can easily be checked by the comment SOP that i have added in our code

Naruto
  • 4,221
  • 1
  • 21
  • 32