Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Shuffle the Pieces and Check for End Game
  • Loading branch information
dragosholban committed Mar 8, 2018
1 parent de6fe4f commit 440ab3f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -22,6 +22,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import static java.lang.Math.abs;

Expand All @@ -48,10 +50,17 @@ public void run() {
setPicFromAsset(assetName, imageView);
}
pieces = splitImage();
TouchListener touchListener = new TouchListener();
TouchListener touchListener = new TouchListener(PuzzleActivity.this);
// shuffle pieces order
Collections.shuffle(pieces);
for (PuzzlePiece piece : pieces) {
piece.setOnTouchListener(touchListener);
layout.addView(piece);
// randomize position, on the bottom of the screen
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) piece.getLayoutParams();
lParams.leftMargin = new Random().nextInt(layout.getWidth() - piece.pieceWidth);
lParams.topMargin = layout.getHeight() - piece.pieceHeight;
piece.setLayoutParams(lParams);
}
}
});
Expand Down Expand Up @@ -265,4 +274,20 @@ private int[] getBitmapPositionInsideImageView(ImageView imageView) {

return ret;
}

public void checkGameOver() {
if (isGameOver()) {
finish();
}
}

private boolean isGameOver() {
for (PuzzlePiece piece : pieces) {
if (piece.canMove) {
return false;
}
}

return true;
}
}
Expand Up @@ -12,6 +12,11 @@
public class TouchListener implements View.OnTouchListener {
private float xDelta;
private float yDelta;
private PuzzleActivity activity;

public TouchListener(PuzzleActivity activity) {
this.activity = activity;
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Expand Down Expand Up @@ -45,6 +50,7 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
piece.setLayoutParams(lParams);
piece.canMove = false;
sendViewToBack(piece);
activity.checkGameOver();
}
break;
}
Expand Down

0 comments on commit 440ab3f

Please sign in to comment.