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

This is a discussion on need help in swapping puzzle contain of the array... within the C Programming forums, part of the General Programming Boards category; eg: i have a 4x4 2D-array, with all of the random generate number inside it and the 0 number in ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    8

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

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    Name:  23123led.jpg
Views: 223
Size:  40.9 KB

    this is the example... Hope someone can help me thank you

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    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);
    	
    		
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    12 goes down, 11 goes right, 3 goes up. Then bring 4 to the right edge on the second row. Put the blank under tile 1, and lower it down one row. move all 3 tiles still in the top row, to the left, and push up the 4 tile. Move the blank below the 11 tile, and lower 11 down one row. Now push up #1. Voila!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swapping strings (which are stored in an array)
    By nair in forum C Programming
    Replies: 2
    Last Post: 10-04-2010, 01:27 PM
  2. Swapping Rows and Columns in a 2D array
    By xxshankar in forum C Programming
    Replies: 2
    Last Post: 03-11-2010, 02:40 PM
  3. Swapping rows in a 2D array
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 11:04 AM
  4. The puzzle again...Swapping elements of 2D array
    By crazygopedder in forum C Programming
    Replies: 44
    Last Post: 11-05-2008, 12:53 PM
  5. swapping elements in a 2D array
    By axon in forum C++ Programming
    Replies: 9
    Last Post: 03-10-2003, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21