Thread: Scramble Elements in Array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    Exclamation Scramble Elements in Array

    Hi, I am working on a project for my C programming class. We have not covered pointers yet, and this project is primarily focused on Arrays mainly.

    I have coded most of it but I'm stuck at the function I've created to "shuffle" or "scramble" the elements in the "game board"

    my function is as follows:

    Code:
    void scramble(char gameBoard[][COLS], int w) // incomplete
    {
    	int g;
    	char *b;
    	
    	if ( w < 50) 
    	{
    		srand(time(NULL));
    		// search for zero in array and store position in x (row) & y (col)
    		char x = find_Zero_In_Row(gameBoard);
    		char y = find_Zero_In_Col(gameBoard);
    		
    		char unsigned z = rand()%4; // random number between 0 and 3, 0 and 3 included
    		
    		if ( z == 0 &&  y+1 < COLS) // case 1
    		{
    			gameBoard[x][y] = gameBoard[x][y+1];
    			gameBoard[x][y+1] = 0;
    		}
    		else if ( z == 1 && y-1 > COLS) // case 2
    		{
    			gameBoard[x][y] = gameBoard[x][y]-1;
    			gameBoard[x][y-1] = 0;
    		}
    		else if ( z == 2 && x+1 < ROWS) // case 3
    		{
    			gameBoard[x][y] = gameBoard[x+1][y];
    			gameBoard[x+1][y] = 0;
    		}
    		else if ( z== 3 &&  x-1 > ROWS) // case 4
    		{
    			gameBoard[x][y] = gameBoard[x-1][y];
    			gameBoard[x-1][y] = 0;
    		}
    		w += 1;
    		scramble(gameBoard, w);
    	}
    	else print_Board(gameBoard);
    }
    I called my function from the Menu() funciton as:
    Code:
       scramble(gameBoard, 0);
    I know its pretty naiive, and it obviously isn't working as I would hope because my output is as follows:

    $./a.out
    Initial configuration [1-Random, 2-Specified configuration]: 1
    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15


    as you can see my board is still in order and not scrambled. Is there anything in my code that isn't working? I'm thinking it might just be bypassing all my conditions and just printing the array as it was passed by reference initially.

    TIA
    Last edited by fcommisso; 10-07-2009 at 12:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding More Array Elements?
    By Vermillion in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2008, 10:02 PM
  2. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 07:53 PM
  3. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 11:30 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM

Tags for this Thread