Random walk solution

This is a discussion on Random walk solution within the C Programming forums, part of the General Programming Boards category; I am trying to produce a random walk solution but I get some strange results. The program must randomly "walk" ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    37

    Random walk solution

    I am trying to produce a random walk solution but I get some strange results.

    The program must randomly "walk" from element to element, always going up, down, left or right by one element.
    The elements visited by the program will be labeled with the letters A through Z, in the order visited.
    If all four directions are blocked, the program must terminate.
    Here's the code :

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (int argc, const char * argv[]) {
        // insert code here...
        char myArray[10][10];
    	char letters[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    	int counter1 = 0;	//row
    	int counter2 = 0;	//column
    	int direction;	//variable to hold the direction of the next move
    	int counter = 0;
    	
    	
    	srand( (unsigned)time(NULL) );
    	
    	//fill myArray with dots
    	for (int theCounter = 0; theCounter < 10; theCounter++)
    	{
    		for (int innerCounter = 0; innerCounter < 10; innerCounter++) {
    			myArray[theCounter][innerCounter] = '.';
    		}
    	}
    	
    	myArray[0][0] = letters[counter];
    	
    	while (counter < 26) {
    		//decide direction
    		direction = rand() % 4;	//picks a random direction (0 - up, 1 - down, 2 - left, 3 - right)
    		
    		//up case
    		if (direction == 0)	//bounders
    		{
    			if (counter1 == 0) {
    				continue;
    			}
    		
    			else if (myArray[counter1 - 1][counter2] == '.')
    			{
    				counter++;
    				counter1--;
    				myArray[counter1][counter2] == letters[counter];
    			}//end if
    		}//outer if
    
    
    		//down case
    		if (direction == 1)	//bounders
    		{
    			if (counter1 > 9)
    			{
    				continue;
    			}
    			else {
    				counter++;
    				counter1++;
    				myArray[counter1][counter2] = letters[counter];
    			}//end if
    			
    		}//end outer if
    		
    		//left case
    		if (direction == 2)
    		{
    			if (counter2 == 0)
    			{
    				continue;
    			}
    		
    			else if (myArray[counter1][counter2 - 1] == '.')
    			{
    				counter++;
    				counter2--;
    				myArray[counter1][counter2] == letters[counter];
    			}//end if
    		}//end outer if
    		
    		//right case
    		if (direction == 3)	//bounders
    		{
    			if (counter1 > 8)
    			{
    				continue;
    			}
    			
    			else if (myArray[counter1][counter2 + 1] == '.')
    			{
    				counter++;
    				counter2++;
    				myArray[counter1][counter2] == letters[counter];
    			}//end inner if
    
    		}//end outer if
    		
    	}//end while
    	
    	
    	//print the grid
    	for (int theCounter = 0; theCounter < 10; theCounter++)
    	{
    		for (int innerCounter = 0; innerCounter < 10; innerCounter++) {
    			printf("%c", myArray[theCounter][innerCounter]);
    		}
    		printf("\n");
    	}
    }
    One strange thing that I found duting debugging is that when direction = 3 the command after the counter2++ expression that assigns a value to an 2 dimensional array is never executed.
    Why is this happening?
    Thank you.
    Last edited by skiabox; 11-01-2010 at 10:27 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Do you have compiler warnings enabled (e.g -Wall with GCC)?

    The compiler reports three statements that have no effect.

    Code:
    myArray[counter1][counter2] == letters[counter];
    //and others
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    I made the classic mistake of == instead of =.
    Sorry!!

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    This is a solution but it does not work at all times and I can't find a logical flow in the code.
    Any ideas?
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (int argc, const char * argv[]) {
        // insert code here...
        char myArray[10][10];
    	char letters[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    	int counter1 = 0;	//row
    	int counter2 = 0;	//column
    	int direction;	//variable to hold the direction of the next move
    	int counter = 0;
    	
    	
    	srand( (unsigned)time(NULL) );
    	
    	//fill myArray with dots
    	for (int theCounter = 0; theCounter < 10; theCounter++)
    	{
    		for (int innerCounter = 0; innerCounter < 10; innerCounter++) {
    			myArray[theCounter][innerCounter] = '.';
    		}
    	}
    	
    	myArray[0][0] = letters[counter];
    	
    	while (counter < 25) {
    		//decide direction
    		direction = rand() % 4;	//picks a random direction (0 - up, 1 - down, 2 - left, 3 - right)
    		
    		//up case
    		if (direction == 0)	//bounders
    		{
    			if (counter1 == 0) {
    				continue;
    			}
    		
    			else if (myArray[counter1 - 1][counter2] == '.')
    			{
    				counter++;
    				counter1--;
    				myArray[counter1][counter2] = letters[counter];
    			}//end if
    		}//outer if
    
    
    		//down case
    		if (direction == 1)	//bounders
    		{
    			if (counter1 > 8)
    			{
    				continue;
    			}
    			else {
    				counter++;
    				counter1++;
    				myArray[counter1][counter2] = letters[counter];
    			}//end if
    			
    		}//end outer if
    		
    		//left case
    		if (direction == 2)
    		{
    			if (counter2 == 0)
    			{
    				continue;
    			}
    		
    			else if (myArray[counter1][counter2 - 1] == '.')
    			{
    				counter++;
    				counter2--;
    				myArray[counter1][counter2] = letters[counter];
    			}//end if
    		}//end outer if
    		
    		//right case
    		if (direction == 3)	//bounders
    		{
    			if (counter2 > 8)
    			{
    				continue;
    			}
    			
    			else if (myArray[counter1][counter2 + 1] == '.')
    			{
    				counter++;
    				counter2++;
    				myArray[counter1][counter2] = letters[counter];
    				
    				
    			}//end inner if
    
    		}//end outer if
    		
    	}//end while
    	
    	
    	//print the grid
    	for (int theCounter = 0; theCounter < 10; theCounter++)
    	{
    		for (int innerCounter = 0; innerCounter < 10; innerCounter++) {
    			printf(" %c", myArray[theCounter][innerCounter]);
    		}
    		printf("\n");
    	}
    	
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    This is my latest version (not perfect yet) :
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (int argc, const char * argv[]) {
        // insert code here...
        char myArray[10][10];
    	char letters[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    	int counter1 = 0;	//row
    	int counter2 = 0;	//column
    	int direction;	//variable to hold the direction of the next move
    	int counter = 0;
    	
    	
    	srand( (unsigned)time(NULL) );
    	
    	//fill myArray with dots
    	for (int theCounter = 0; theCounter < 10; theCounter++)
    	{
    		for (int innerCounter = 0; innerCounter < 10; innerCounter++) {
    			myArray[theCounter][innerCounter] = '.';
    		}
    	}
    	
    	myArray[0][0] = letters[counter];
    	
    	while (counter < 25) {
    		//decide direction
    		direction = rand() % 4;	//picks a random direction (0 - up, 1 - down, 2 - left, 3 - right)
    		
    		if (counter1 == 0 && counter2 > 0)
    		{
    			if (myArray[counter1 + 1][counter2] != '.' && myArray[counter1][counter2 + 1] != '.' && myArray[counter1][counter2 - 1] != '.')
    				break;
    		}//first side
    		
    		else
    		
    		if (counter1 > 0 && counter2 == 0)
    		{
    			if (myArray[counter1 - 1][counter2] != '.' && myArray[counter1 + 1][counter2] != '.' && myArray[counter1][counter2 + 1] != '.')
    				break;
    		}//second side
    		
    		else
    		
    		{
    			if (myArray[counter1 + 1][counter2] != '.' && myArray[counter1 - 1][counter2] != '.' && myArray[counter1][counter2 - 1] != '.' && myArray[counter1][counter2 + 1] != '.')
    			{
    				printf("!");
    				break;
    			}
    		}
    			
    			
    		//up case
    		if (direction == 0)	//bounders
    		{
    			if (counter1 == 0) {
    				continue;
    			}
    		
    			else if (myArray[counter1 - 1][counter2] == '.')
    			{
    				counter++;
    				counter1--;
    				myArray[counter1][counter2] = letters[counter];
    			}//end if
    		}//outer if
    
    
    		//down case
    		if (direction == 1)	//bounders
    		{
    			if (counter1 > 8)
    			{
    				continue;
    			}
    			else {
    				counter++;
    				counter1++;
    				myArray[counter1][counter2] = letters[counter];
    			}//end if
    			
    		}//end outer if
    		
    		//left case
    		if (direction == 2)
    		{
    			if (counter2 == 0)
    			{
    				continue;
    			}
    		
    			else if (myArray[counter1][counter2 - 1] == '.')
    			{
    				counter++;
    				counter2--;
    				myArray[counter1][counter2] = letters[counter];
    			}//end if
    		}//end outer if
    		
    		//right case
    		if (direction == 3)	//bounders
    		{
    			if (counter2 > 8)
    			{
    				continue;
    			}
    			
    			else if (myArray[counter1][counter2 + 1] == '.')
    			{
    				counter++;
    				counter2++;
    				myArray[counter1][counter2] = letters[counter];
    				
    				
    			}//end inner if
    
    		}//end outer if
    		
    	}//end while
    	
    	
    	//print the grid
    	for (int theCounter = 0; theCounter < 10; theCounter++)
    	{
    		for (int innerCounter = 0; innerCounter < 10; innerCounter++) {
    			printf(" %c", myArray[theCounter][innerCounter]);
    		}
    		printf("\n");
    	}
    	
    	return 0;
    }

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    This is a solution but it does not work at all times and I can't find a logical flow in the code.
    I find it a bit awkward to read, You need to clarify, What exactly does it do when it does not work? crash with seg fault? hang? repeat itself?

    This is my latest version (not perfect yet) :
    And so when people read through can you say what you changed between posts? comment changes in your code or write a note explaining, its so others can maybe pick up ideas. learn from where you went wrong
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create random array
    By Hunter_wow in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2007, 05:29 AM
  2. Random Access List
    By avalanche333 in forum C++ Programming
    Replies: 16
    Last Post: 01-22-2007, 06:29 PM
  3. time Delays and Random functions
    By markphaser in forum C++ Programming
    Replies: 17
    Last Post: 02-20-2006, 06:10 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Random Problem?
    By dizz in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2002, 04:00 PM

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