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]) { )



LinkBack URL
About LinkBacks



