android - Get Index Of The Shuffled Item After collections.shuffle -
i trying jigsaw puzzle app in android. in this, have split bitmap
many small chunks. these chunks displayed in gridview
now need shuffle them. then, need know each image chunk's actualposition(where piece supposed be, actual location in image) , currentposition(where piece located). actualposition
, currentposition
2 integer arrays. there way can each image chunk's currentposition
, actualposition
after shuffling after every move user make can check wether every image chunk's actualposition
equals currentposition
. if user wins game. can please me out.
below number puzzle game in pure java works. can run command line. re-prints whole matrix after every move (not pretty). demos basic game. hope of code self explanatory. shows basic 2-dim mapping of game, position tracking, validating based on numbers. have fun.
package madhav.turangi.basic.game; import java.util.random; import java.util.scanner; public class numberpuzzle { int size; int[][] arr; int spacerow; int spacecol; int turnstook; public numberpuzzle(int size) { this.size = size; arr = new int[size][size]; } void init() { for(int r=0; r<size; r++) { for(int c=0; c<arr[r].length; c++) { arr[r][c] = r*size + c + 1; // row-column of cell value equation } } spacerow = spacecol = size - 1; // bottom-right cell index } int readuserinput() { int value = -1; boolean valid = false; { system.out.printf("to move space [0 - up, 1 - down, 2 - left, 3 - right] : ? "); scanner sc = new scanner(system.in); string line = sc.nextline(); try { value = integer.parseint(line); valid = (value>=0 && value<=3); } catch(numberformatexception ne) { } if(! valid) system.out.println("== invalid =="); } while (! valid); return value; } void swap(int arow, int acol, int withrow, int withcol) { int temp = arr[arow][acol]; arr[arow][acol] = arr[withrow][withcol]; arr[withrow][withcol] = temp; } boolean moveup() { if(spacerow != 0) { int newspacerow = spacerow - 1; swap(spacerow, spacecol, newspacerow, spacecol); spacerow--; return true; } else { return false; } } boolean movedown() { if(spacerow != size-1) { int newspacerow = spacerow + 1; swap(spacerow, spacecol, newspacerow, spacecol); spacerow++; return true; } else { return false; } } boolean moveright() { if(spacecol != size-1) { int newspacecol = spacecol + 1; swap(spacerow, spacecol, spacerow, newspacecol); spacecol++; return true; } else { return false; } } boolean moveleft() { if(spacecol != 0) { int newspacecol = spacecol - 1; swap(spacerow, spacecol, spacerow, newspacecol); spacecol--; return true; } else { return false; } } void shuffle() { random rnd = new random(system.currenttimemillis()); boolean moved = false; int attemptcount = 1; int maxmoves = 20; for(int movecount=0; movecount<maxmoves; movecount++, attemptcount++) { int randommovedir = rnd.nextint(4); moved = move(randommovedir); if(! moved) movecount--; //ensure maxmoves number of moves } system.out.printf("shuffle attempts %d\n",attemptcount); } boolean move(int dir) { boolean moved = false; switch(dir) { case 0 : // moved = moveup(); break; case 1 : // down moved = movedown(); break; case 2 : // left moved = moveleft(); break; case 3 : // right moved = moveright(); break; } return moved; } void prnarray() { system.out.println("-- -- -- -- --"); for(int[] row : arr) { for(int cellvalue : row) { string v = (cellvalue == 16 ? "" : string.valueof(cellvalue)); system.out.printf("%4s", v); } system.out.println(); } system.out.println("-- -- -- -- --"); } boolean validate() { for(int r=0; r<size; r++) { for(int c=0; c<arr[r].length; c++) { if(arr[r][c] != (r*size + c + 1)) { return false; } } } return true; } boolean oneturn() { int dir = readuserinput(); boolean moved = move(dir); boolean won = false; if(moved) { turnstook++; prnarray(); won = validate(); } else { system.out.println("= invalid ="); } return won; } void play() { init(); system.out.println("before shuffle"); prnarray(); shuffle(); prnarray(); boolean won = false; while(! won) { won = oneturn(); } system.out.printf("won in %d\n", turnstook); } public static void main(string[] args) { numberpuzzle puzzle = new numberpuzzle(4); puzzle.play(); } }
Comments
Post a Comment