Permalink
Browse files

Shuffle the Pieces and Check for End Game

  • Loading branch information...
dragosholban committed Mar 8, 2018
1 parent de6fe4f commit 440ab3f7a3ab37a3b98910b542f35c1568885e83
@@ -24,7 +24,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
@@ -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;
@@ -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);
}
}
});
@@ -265,4 +274,20 @@ private void setPicFromAsset(String assetName, ImageView imageView) {
return ret;
}
public void checkGameOver() {
if (isGameOver()) {
finish();
}
}
private boolean isGameOver() {
for (PuzzlePiece piece : pieces) {
if (piece.canMove) {
return false;
}
}
return true;
}
}
@@ -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) {
@@ -45,6 +50,7 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
piece.setLayoutParams(lParams);
piece.canMove = false;
sendViewToBack(piece);
activity.checkGameOver();
}
break;
}

0 comments on commit 440ab3f

Please sign in to comment.