Trying to print grid

This is a discussion on Trying to print grid within the C Programming forums, part of the General Programming Boards category; What I'm trying to do is print a 20 by 20 grid of dashes, except for one location that is ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    8

    Trying to print grid

    What I'm trying to do is print a 20 by 20 grid of dashes, except for one location that is a letter. My problem is that this has become an infinite loop, that prints nothing. Any suggestions?


    Code:
    main ()
    {
    
    	int horizontalSpaces = 0, selectedOption = 0, direction = 0, verticalSpaces = 0, stateOfIgnition = 0,
    		currentHorizontalPosition = 0, currentVerticalPosition = 0;
    
    	char color = ' ';
    
    	char ColorAssignment(color);
    
    	printf("%c\n", color);
    	
    	srand (time (NULL));
    	(currentHorizontalPosition = 1 + (rand () % 20));
    	(currentVerticalPosition = 1 + (rand () % 20));
    	printf("Position : %d, %d\n",  currentHorizontalPosition, currentVerticalPosition);
    
    	IgnitionOff (stateOfIgnition);
    	StateOfCar (color, currentHorizontalPosition, currentVerticalPosition, stateOfIgnition);
    
    /* COLOR ASSIGNMENT*/
    char ColorAssignment(char color)
    
    {
    srand (time (NULL));
    switch ( rand() % 5 )
    {
       case 0: color = 'R'; 
    	   break;
       case 1: color = 'G'; 
    	   break;
       case 2: color = 'B';
    	   break;
       case 3: color = 'W'; 
    	   break;
       case 4: color = 'S'; 
    	   break;
    }
    return color;
    
    }
    
    /* State Of Car*/
    
    void StateOfCar (int stateOfIgnition, int currentHorizontalPosition, int currentVerticalPosition, char color)
    
    
    {
    int horizontalGrid = 0, verticalGrid = 0;
    
    printf("\nCar stats:\n\n");
    printf("color: %c\n", color);
    
    if (stateOfIgnition = 0)
    printf("Ignition: On\n");
    	   else printf("Ignition: Off\n");
    
    	   printf("Position:%d, %d\n", currentHorizontalPosition, currentVerticalPosition);
    
    	   /* prints grid and location of car on grid*/
    
    	   {
    while ((verticalGrid <= 20) && (horizontalGrid <= 20))
    
    if ((horizontalGrid = currentHorizontalPosition) && (verticalGrid = currentVerticalPosition)){
    		printf ("%c", color);
    		(horizontalGrid = (horizontalGrid + 1)) ;}
    
    else if (horizontalGrid = 20){
    printf("\n");
    horizontalGrid = 0;
    verticalGrid++;
    }
    
    else {
    	  printf("-");
    	  horizontalGrid++;
    	  
    }}
    
    return;
    
    }
    Last edited by blackgingr; 11-14-2002 at 06:49 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just use a 20x20 array, and change the single character you want to change, then just print the whole array? Seems like that'd be the easiest way to do this.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Here is my code to display a chessboard:

    Code:
    void BOARD::showBoard() const
    {
    	int x;
    	int y;
            int i;
    	
    	cout << "\t";
    	
    	for(x = 0; x < 8; x++)
    	{
    		cout << x << "\t";
    	}
            cout << endl << "    ";
            for(x = 0; x < 65; x++)
            {
            cout << "-";
            }
    	
    	cout << endl;
    		
    	for(x = 0; x < 8; x++)
    	{
    		for(y = 0; y < 8; y++)
    		{
    			if(y == 0)
    			{
    				cout << x << "   |" << "\t" << board[x][y] << "   |" << "\t";
    			}
    			else
    			{
    				cout << board[x][y] << "   |";
    				cout << "\t";
    			}      
    		}
            cout << endl << "    ";
            for(i = 0; i < 65; i++)
            {
                    cout << "-";
            } 
    		cout << endl;
    	}
    }
    It's in C++, but it could be easily converted to C and made to be 20x20 instead of 8x8. Oh and the -'s may look inaccurate depending on the system you run on it.

    Anyways, just an example for you to look at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  3. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  4. How to print a grid??
    By Dangerous Dave in forum C Programming
    Replies: 3
    Last Post: 11-13-2001, 07:02 AM
  5. How to print a grid????
    By Dangerous Dave in forum C Programming
    Replies: 5
    Last Post: 11-09-2001, 01:03 PM

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