I am making this android app which requires solving puzzle8 using A star algorithm. In order to implement this, I used a comparator as shown below:
public class PuzzleBoardComparator implements Comparator<PuzzleBoard> {
@Override
public int compare(PuzzleBoard A, PuzzleBoard B) {
Integer priorityA=A.priority();
Integer priorityB=B.priority();
if(priorityA.compareTo(priorityB)<0)
return -1;
if(priorityA.compareTo(priorityB)>0)
return 1;
else
return 0;
}
}
This comaparator uses a priority() which is based on calculating Manhattan Distance and is given below:
public int priority() {
int manhattanDistance=0;
for(int x=0;x<NUM_TILES;x++){
for(int y=0;y<NUM_TILES;y++){
if(tiles.get(XYtoIndex(x,y))!=null){
int numTile=tiles.get(XYtoIndex(x,y)).getNumber();
numTile-=1;
int xNumTile=numTile/NUM_TILES;
int yNumTile=numTile%NUM_TILES;
manhattanDistance+=Math.abs(x-xNumTile)+Math.abs(y-yNumTile);
}
}
}
return manhattanDistance+steps;
}
Now, this is the implementation of A-star algorithm to solve the puzzle:
public void solve() {
//Log.d("DEMO1","LOOP");
PuzzleBoardComparator comparator=new PuzzleBoardComparator();
//Log.d("DEMO2","LOOP");
PriorityQueue<PuzzleBoard> queue=new PriorityQueue<>(10,comparator);
//Log.d("DEMO3","LOOP");
puzzleBoard.steps=0;
puzzleBoard.previousBoard=null;
queue.add(puzzleBoard);
while(!queue.isEmpty()){
//Log.d("DEMO4","LOOP");
PuzzleBoard cBoard=queue.remove();
if(cBoard.resolved()){
ArrayList<PuzzleBoard> solution=new ArrayList<>();
//solution.add(cBoard);
while(cBoard.getParent()!=null){
solution.add(cBoard);
cBoard=cBoard.getParent();
}
solution.add(cBoard);
Collections.reverse(solution);
animation=solution;
break;
}
ArrayList<PuzzleBoard> neigh;
neigh=cBoard.neighbours();
Iterator<PuzzleBoard> it=neigh.iterator();
while(it.hasNext()){
queue.add(it.next());
}
}
}
But, My application is not working at all. Whenever I press a button to solve the puzzle, nothing happen. After certain amount of time I get this error message on logcat:
07-14 15:14:08.491 6172-6172/com.google.engedu.puzzle8 E/dalvikvm-heap: Out of memory on a 64-byte allocation.
07-14 15:14:09.482 6172-6172/com.google.engedu.puzzle8 E/dalvikvm-heap: Out of memory on a 184-byte allocation.
07-14 15:14:09.662 6172-6172/com.google.engedu.puzzle8 E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.google.engedu.puzzle8, PID: 6172
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.OutOfMemoryError: [memory exhausted]
at dalvik.system.NativeStart.main(Native Method)
Please help me to resolve the issue
EDIT 1: This is the element of xml file where onclick attribute is present.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/solve_button_label"
android:id="@+id/solve_button"
android:onClick="solve"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/shuffle_button"
android:layout_toEndOf="@+id/shuffle_button" />
EDIT 2: This is the github link to the project https://github.com/akhileshydv/Puzzle8.git