How to check whether the jigsaw puzzle is completed or not?

一个人想着一个人 提交于 2019-12-02 10:05:54

Ok, here is what you do for the solution.

  1. maintain a new set of drawables marking them as original drawables that you had initially. may be like

drawable og11 = imageView11.getDrawable(); do this part before shuffling. now you have original drawables, stored in the form of drawables.

  1. After every click , check if og11 == imageView11.getDrawable(),... and so on for all the images in the jigsaw, if they match , it matches, else, they don't.

HTH.

I have a simple solution. Set serial number Integer tags to ImageViews using View.setTag(index) before jumbling(before preparing the puzzle.) Then everytime the user makes a move, loop through all the imageviews and check if they are in order. If out of order then puzzle is not completed yet.

class PuzzleItem {
   Drawable puzzlepartImage;
   int correctPosition;

   public PuzzleItem(Drawable d, int index) {
      puzzlepartImage = d;
      correctPosition = index;
   }
}

ArrayList<PuzzleItem> list = new ArrayList<PuzzleItem>();
for (int i = 0; i < 9; i++) {
    list.add(new PuzzleItem(drawables[i], i)); 
}
Collections.shuffle(list);

//Create the views from this list and add to the layout serially.
// set the last view as emptyview.

On every move:

void onClick(View v) {
    /* swap drawables */
    Drawable clickedDrawable = v.getDrawable();
    v.setDrawable(null);
    mEmptyView.setDrawable(clickedDrawable);
    mEmptyView = v;

    /* swap tag integers */
    Integer temp = (Integer)mEmptyView.getTag();
    mEmptyView.setTag(v.getTag());
    v.setTag(temp);

}

After every move check for completion:

for (int i = 0; i < 9; i++) {
    if (imgView[i].getTag() != i) break;
}
if (i == 9)// puzzle completed.

why don't you use a gridView? and after each change, just check the items to see that images are in desired order

When you use == operator they are checking if both the objects are referencing the same object. But in your case they are 2 different objects.

You should set a tag to both the actual image and the other image. And check if they are both the same. That should work in your if condition.

For the drawable class i did notice a method setLevel and getLevel. You might be able to use this for your requirement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!