This site requires JavaScript, please enable it in your browser!
Greenfoot back
TheStrangeOne
TheStrangeOne wrote ...

2018/2/18

Shuffling Pieces of an Sliding Puzzle

TheStrangeOne TheStrangeOne

2018/2/18

#
Sup!As the title says i am currently working on a Sliding Puzzle for a school project but i tackled some sort of problem when it comes to shuffling using Collections.shuffle() like this
 public void shufflePieces()
    {
       List<Piesa> list=getObjects(Piece.class);
       List<Piesa> rez=getObjects(Piece.class);
        Collections.shuffle(list);
        for(int i=0;i<worldRatio*worldRatio-1;i++)       rez.get(i).setLocation(list.get(i).getX(),list.get(i).getY());
        
    }
The problem seems to be that some pieces just place one over another like this If someone could help me with an advice or propose an even better solution i am glad to hear it
Vercility Vercility

2018/2/18

#
I honestly do not understand your code. Youre iterating every Piece of the original list and setting its location according to the shuffled lists corresponding piece While you are manipulating the locations of rez' Pieces you are also changing the locations of the pieces in List. Im assuming you thought it would be two seperate lists with copied pieces but your arrays actually only contain references to the pieces that already exist, so manipulating either of the arrays will affect both By the way, u can just use setLocation(list.get(i).getLocation)); instead of calling both coordinates What exactly are you trying to do? If you just wanna put the pieces at random places it would be easier to create an Array of all pieces, shuffle it and then iterate the array through 2 for loops (for either coordinate respectively) (Although you should be aware that true randomness in a shift puzzle can lead to unsolvable initial positions)
danpost danpost

2018/2/18

#
Vercility wrote...
While you are manipulating the locations of rez' Pieces you are also changing the locations of the pieces in List. Im assuming you thought it would be two seperate lists with copied pieces but your arrays actually only contain references to the pieces that already exist, so manipulating either of the arrays will affect both
They are two separate lists containing the same elements. Shuffling one list does not change the other one at all. It only changes the order of the elements in the shuffled list.
By the way, u can just use setLocation(list.get(i).getLocation)); instead of calling both coordinates
'getLocation'? I am not aware of any method with that name.
If you just wanna put the pieces at random places it would be easier to create an Array of all pieces, shuffle it and then iterate the array through 2 for loops (for either coordinate respectively) (Although you should be aware that true randomness in a shift puzzle can lead to unsolvable initial positions)
As have already noted, half of the arrangements of pieces will create an unsolvable puzzle. There are two ways to ensure that an unsolved puzzle is solvable -- (1) start with a completed puzzle and randomly make legal move to scramble it up; or (2) ensure that the sum of the offsets, horizontally and vertically, of all pieces, including the empty space, makes an even number.
Vercility Vercility

2018/2/18

#
No, what I meant is if you use set Location on a puzzle piece it will be affected in both arrays since it's the same piece just two different arrays with the same reference, worded that badly I was pretty sure get Location exists (which would make sense, kinda) but if it doesn't, well then my bad.
danpost danpost

2018/2/18

#
Vercility wrote...
No, what I meant is if you use set Location on a puzzle piece it will be affected in both arrays since it's the same piece just two different arrays with the same reference, worded that badly
Gotcha (now)
You need to login to post a reply.