0

i'm doing a image puzzle game in android i successfully loaded image from gallery and splitted and shuffle the bitmap and made a puzzle gave now i want to check whether the puzzl;e played is finished on clicking a button first i tried like this,

 public void checkresult(View view)
{
   if(beforeshuffle.toArray().equals(aftershuffle.toArray()))
    {
        Toast.makeText(getApplicationContext(),"correct",Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(getApplicationContext(),"wrong",Toast.LENGTH_SHORT).show();
    }
}

but id didn't work so please do say me a logic to do this.

My complete code

public class SmallImageActivity extends Activity {

ImageView img;
GridView image_grid;
Bitmap bs,as;
ArrayList<Bitmap> beforeshuffle = new ArrayList<Bitmap>(9);
ArrayList<Bitmap> aftershuffle = new ArrayList<Bitmap>(9);

public void onCreate(Bundle bundle) {


    super.onCreate(bundle);

    setContentView(R.layout.child_image);


    //Getting the image chunks sent from the previous activity

    // ArrayList<Bitmap> smallImage = getIntent().getParcelableArrayListExtra("small images");


    //Getting the grid view and setting an adapter to it
    img = (ImageView) findViewById(R.id.image);
    image_grid = (GridView) findViewById(R.id.gridview);


    Intent intent = getIntent();
    String pathinphone = intent.getExtras().getString("path");
    Log.d("path", pathinphone);
    loadImageFromStorage(pathinphone);

}


private void splitImage(ImageView image, int smallimage_Numbers) {

    final ArrayList<Bitmap> smallimages = new ArrayList<Bitmap>(9);

    //For the number of rows and columns of the grid to be displayed

    int rows, cols;


    //For height and width of the small image smallimage_s

    int smallimage_Height, smallimage_Width;


    //To store all the small image smallimage_s in bitmap format in this list


    //Getting the scaled bitmap of the source image

    BitmapDrawable mydrawable = (BitmapDrawable) image.getDrawable();

    Bitmap bitmap = mydrawable.getBitmap();

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);


    rows = cols = (int) Math.sqrt(smallimage_Numbers);

    smallimage_Height = bitmap.getHeight() / rows;

    smallimage_Width = bitmap.getWidth() / cols;


    //xCo and yCo are the pixel positions of the image smallimage_s

    int yCo = 0;

    for (int x = 0; x < rows; x++) {

        int xCo = 0;

        for (int y = 0; y < cols; y++) {

            smallimages.add(Bitmap.createBitmap(scaledBitmap, xCo, yCo, smallimage_Width, smallimage_Height));

            xCo += smallimage_Width;

        }

        yCo += smallimage_Height;

    }
    Array []in=new Array[9];


    for(int i=0;i<smallimages.size();i++)
    {
        beforeshuffle.add(smallimages.get(i));


    }
    Collections.shuffle(smallimages);
    image_grid.setAdapter(new SmallImageAdapter(this, smallimages));

    image_grid.setNumColumns((int) Math.sqrt(smallimages.size()));
    image_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     int   counter=0;
        int firstclick;
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            counter ++;
            if(counter % 2 == 0){
                 firstclick   = position;
               Bitmap data1 = smallimages.get(position);
            }
            else {

                for(int i=0;i<smallimages.size();i++)

                {
                    Bitmap swapImage = smallimages.get(position);
                    smallimages.set(position, smallimages.get(firstclick));
                    smallimages.set(firstclick, swapImage);

                    image_grid.invalidateViews();
                    aftershuffle.add(smallimages.get(i));

                }


            }


        }
    });

}
public void checkresult(View view)
{
   if(beforeshuffle.toArray().equals(aftershuffle.toArray()))
    {
        Toast.makeText(getApplicationContext(),"correct",Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(getApplicationContext(),"wrong",Toast.LENGTH_SHORT).show();
    }
}
}

3 Answers3

0

If your Min supported Version is 14 or above, then Bitmap class have a method

bitmap.sameAs()

which you can use here to compare bitmaps.

see the description here

Ravinder Bhandari
  • 2,546
  • 21
  • 25
0

There is a method called sameAS in Bitmap class to compare,

You can try something below like this,

public void checkresult(View view)
{
if(beforeshuffle.size() > 0 && aftershuffle.size() > 0){
if(beforeshuffle.get(0).sameAs(aftershuffle.get(0)))
{
    Toast.makeText(getApplicationContext(),"correct",Toast.LENGTH_SHORT).show();
}
else
{
    Toast.makeText(getApplicationContext(),"wrong",Toast.LENGTH_SHORT).show();
}
}
}

I hope this may help you,All the best

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Popeye
  • 253
  • 4
  • 13
  • it returns wrong only even it it is correct . i think your logic is correct but whether my before shuffle and after shuffle logic is correct plz do check and say – Divya Das M Jul 22 '17 at 06:59
0

I think that if you remove toArray() it will do the job (comparing 2 arrays with equals is the same as doing array1 == array2 but comparing 2 lists with equals will compare their contents)

Mahesh Babariya
  • 4,560
  • 6
  • 39
  • 54