开发者

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

i am preparing one small game like jigsaw , for that i am using 9 imageview's with 9 different images in the layout. set the images to imageview at the time of starting those are actual images, after shuffle user will do sliding the images to complete puzzle, i want to check the modified image with actual image's, weather it's equal or not if those are equal popup a mess开发者_如何学Pythonage, like gameover.

i tried like this

1.by using AND operator between the images(Drawables) but unlucky.

2.Using setLevel() for the images, compare those setLevel values with getLevel values for images after sliding still Unlucky.. here the problem is if i click on imageview one time getLevel() gives correct value, if i click more than once it gives zero value. Why it happens like this..

Please help me if you find any mistake in my code, otherwise guide me with good technique..

Xml code like this
<RelativeLayout
   //9 imageview's  

in jave code like this if user will click on imageview it will swap the image's

 img_View11.setOnClickListener(new View.OnClickListener()  {            
    public void onClick(View v)  { 

        if(img_View21.getDrawable() == default_img || 
                                    img_View12.getDrawable() == default_img) {

            System.out.println("yes");
            ++click;
            Noof_moves.setText("Moves: " +click);
            ImageView iv = (ImageView)v;                 
            d11 = iv.getDrawable();   
            prev_img = prev_imgView.getDrawable();

            la11 = d11.getLevel();
            System.out.println("d11 value is " +la11);                  

            prev_imgView.setImageDrawable(d11);                 

            img_View11.setImageDrawable(prev_img);                
            prev_imgView = img_View11;

            check();
            }
        else { System.out.println("no");  }
        }
    });

void check(){

    System.out.println("in checking condition");
    if((lb11==la11) && (lb12==la12) && (lb13==la13) && (lb21==la21) && (lb22==la22)
        && (lb23==la23) && (lb31==la31) && (lb32==la32) && (lb33==la33)) {
            Context c = getBaseContext();
            System.out.println("gameover");
            Toast.makeText(c, " GameOver ", Toast.LENGTH_SHORT).show();
    }else{ System.out.println(" codition not checked "); }
} 
//here lb=level before sliding
//     la=level after sliding..


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜