# need help in swapping puzzle contain of the array...

• 11-28-2011
tianwu
need help in swapping puzzle contain of the array...
eg: i have a 4x4 2D-array, with all of the random generate number inside it and the 0 number in the array will be the blank, and i wish to swap the number which i want to move with 0, how do i write the number?

eg of the array generated randomly :

[ 1 ][ 2 ][ 11 ][ 12 ]
[ 10 ][ 15 ][ 3 ][ ]
[ 9 ][ 13 ][ 4 ][ 7 ]
[ 8 ][ 14 ][ 5 ][ 6 ]

first i need to find the blank location and i need to check whether the number the user key in is it ok to swap with the blank. eg if user key in 15 which mean they want to swap 15 with the blank and the program doesnt let it to swap, only 12,3,and 7 is ok to do the swapping... so far i haven't write anything in my swapping part...

Code:

```#include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> #define sizex 4 #define sizey 4 void getrand(int ar[sizex][sizey]); void print_ar(int aa[sizex][sizey]); void swap_ar(int a[sizex][sizey]); void swap_numb(int *a, int *b); main() { int puzzle[sizex][sizey]; printf("************************WELCOME TO SLIDDING PUZZLE GAME*************************\n\n"); srand((unsigned)time(NULL)); getrand(puzzle); print_ar(puzzle); swap_ar(puzzle); } void getrand(int ar[sizex][sizey]) { int x,y,m,n; int temp; int rep; for (x=0;x<sizex;x++) {     for (y=0;y<sizey;y++)         {                 do                 {                 rep = 0;                                     temp =rand()%16;                         for(m =0; m <sizex; m++)                         {                         for(n=0; n<sizey; n++)                             {                             if(temp == ar[m][n])                                 {                                 rep =1;                                 }                             }                         }                                                 }while(rep == 1);                 ar[x][y] = temp;                     }             }             }          void print_ar(int aa[sizex][sizey]) { int x,y; for(x=0;x<sizex;x++) {     for(y=0;y<sizey;y++)     {         if(aa[x][y]==0)         {         printf("|\t\t");         }         else         {         printf("|\t%i\t",aa[x][y]);         }     }     printf("|");     printf("\n"); } } void swap_ar(int a[sizex][sizey]) { )```
• 11-28-2011
tianwu
Attachment 11156

this is the example... Hope someone can help me thank you :(
• 11-28-2011
Your sliding puzzle should have just one d in the word "sliding".

I wrote my puzzle a long time ago, and it's really messy**. What I did was move the blank primarily, instead of moving the other pieces. So if I wanted the blank to move up, I brought a tile down one row. If I wanted the blank to go to the right, I moved a tile to the left.

Sounds odd, but I found it easier because I only had to consider the movement of one thing, (the blank spot), instead of a whole group of tiles. Let's say I had to move the #1 tile, up into a higher row. If the row was greater than row 0, then it moved the blank into the row one less than the #1 tile, and into the same column. Then it moved the blank down (which moved the #1 tile, up).

I don't want to give you too much advice on this, because you're likely to find a better way to do this than I did. Mine is fast, but it is VERY messy and anything BUT elegant.
• 11-28-2011
tianwu
is there any other way of doing this?
i mean the concept of moving the number with the blank...this is one of my friend's concept of moving the number, I couldnt use it due to the plagarism...
that's why i need a different way to do it ...
Code:

```void puzzle_command(int arr[NROW][NCOL], int count) //count is the input that user key-ed in, which mean the number that user wanted to move with the blank {         int value, num, temp;         int px1, py1, px2, py2, x, y;                 for (x=0; x<NROW; x++)                 for (y=0; y<NCOL; y++)                 {                         value = arr[x][y] ;                                                 if (0 == value)                         {                                 value = arr[x][y];                                 px1 = x;                                 py1 = y;                         }                         }                         for (x=0; x<NROW; x++)                 for (y=0; y<NCOL; y++)                 {                         value = arr[x][y] ;                                                 if (count == value)                         {                                 value = arr[x][y];                                 px2 = x;                                 py2 = y;                         }                         }                                                 if ((arr[px2][py2] - arr[px2-1][py2])==count || (arr[px2][py2] - arr[px2+1][py2])==count                         || (arr[px2][py2] - arr[px2][py2-1])==count || (arr[px2][py2] - arr[px2][py2+1])==count)                 {                         temp = arr[px2][py2];                         arr[px2][py2] = arr[px1][py1];                         arr[px1][py1] = temp;                 }                                 printf("\n\n\n\n\n\n\n\n\n\n\n");                 print_puzzle(arr);                         }```
• 11-28-2011