如何判断拼图是否完成?

发布于 2024-12-05 20:27:48 字数 2004 浏览 0 评论 0原文

我正在准备一款像拼图这样的小游戏,为此我在布局中使用了 9 个图像视图和 9 个不同的图像。在启动时将图像设置为 imageview 这些是实际图像,随机播放后用户将滑动图像以完成拼图,我想检查修改后的图像与实际图像的,天气是否相等,如果它们相等弹出一条消息,就像游戏结束一样。

我尝试像这样

1.通过在图像(Drawables)之间使用 AND 运算符,但不幸的是。

2.对图像使用 setLevel() ,将这些 setLevel 值与滑动后图像的 getLevel 值进行比较仍然不走运..这里的问题是,如果我单击 imageview 一次 getLevel() 给出正确的值,如果我单击多次给出零值。为什么会发生这样的情况..

如果您在我的代码中发现任何错误,请帮助我,否则请用好的技术指导我..

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

在像这样的jave代码中 如果用户单击 imageview,它将交换图像的

 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..

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 message, 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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁我热情 2024-12-12 20:27:48

好的,这就是您为解决方案所做的事情。

  1. 维护一组新的可绘制对象,将它们标记为您最初拥有的原始可绘制对象。
    可能就像

drawable og11 = imageView11.getDrawable();
在洗牌之前完成这部分。现在你有了原始的可绘制对象,以可绘制对象的形式存储。

  1. 每次单击后,检查拼图中的所有图像是否 og11 == imageView11.getDrawable(),...等等,如果匹配,则匹配,否则不匹配。

HTH。

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.

时光匆匆的小流年 2024-12-12 20:27:48

我有一个简单的解决方案。在混乱之前(准备拼图之前)使用 View.setTag(index) 将序列号 Integer 标签设置为 ImageView。然后每次用户移动时,循环遍历所有 imageview 并检查它们是否按顺序排列。如果顺序不对,则拼图尚未完成。

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.

每次移动时:

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

}

每次移动后检查是否完成:

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

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.
且行且努力 2024-12-12 20:27:48

为什么不使用 gridView 呢?每次更改后,只需检查项目以查看图像是否按所需顺序排列

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

停顿的约定 2024-12-12 20:27:48

当您使用 == 运算符时,它们会检查两个对象是否引用同一个对象。但就你而言,它们是两个不同的对象。

您应该为实际图像和其他图像设置标签。并检查它们是否相同。这应该适用于你的 if 条件。

对于可绘制类,我确实注意到了 setLevel 和 getLevel 方法。您也许可以使用它来满足您的要求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文