Thread: Need some help with a C program...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Need some help with a C program...

    Hi all, new to the forum. I'm pretty desperate at the moment trying to get my &%$@^@$ C program to work. In my C class we are supposed to develop a "game" absed on Turtle Graphics. The program must have two arrays. One single dim array that stores the users commands and a second double dim array that will be used to track the turtles movements. So far I have programmed the user input along with error checking the input. I have also written the printing procedure that will run through the double dimension array and print out "*" that indicate where the turtle moved. I initailized both arrays to zero and what I am trying to do is for "pen down" commands and movements I would like to store a "1" in the double dim array. For "pen up" movements I will keep the array element contents to zero. That way when the printing procedure executes it will only print "*" for the element contents that contain a "1". THE MILLION DOLLAR QUESTION IS: how do I link the commands in the single dim array to the movements stored in the double dim array? I have attempted/started to right some code for this but am completely lost. Could someone take a look at my code and suggest a simple way to accomplish this? Thanks a bunch!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 100                     //size of command array
    #define ROW 20                       //size of row in floor array
    #define COL 20                       //size of col in floor array
    
    
    /* FUNCTION PROTOTYPES */
    
    void initFArray(int[ROW][COL]);      //function to initialize Floor array
    void initCArray(int[SIZE]);          //function to initialize Command array
    void inputCommands(int[SIZE]);       //function that records input commands into array
    void readCArray(int[SIZE]);          //function reads command array and changes floor array accordingly
    void printArray(int [ROW][COL]);     //function to print out turtle movement
    void printHeader();                  //function that prints out what the commands are
    int  turnRight(int);
    int  turnLeft(int);
    void turtleMove(int, int, int[ROW][COL]);
    
    
    
    
    
    
    
    void readCArray(int arrayCommands[SIZE])
    {//commands 1,2,3,4,5,6,9
    
          int x;
          int command = 0;
          bool penUp = true;
          bool penDown = false;
    
    
          while(command !=9)
          {
              int x=0;
    
              command = arrayCommands[x];
    
              if(command == 1)
              {
                penUp=true;
              }
              if(command == 2)
              {
                penDown=true;
              }
              if(command == 3)
              {
    
              }
              if(command == 4)
              {
              }
              if(command == 5)
              {
              }
              if(command == 6)
              {
              }
    
    
              x++;
    
    
    
    
    }
    
    
    
    int main()
    {
          int arrayFloor[ROW][COL];      //init 20x20 floor array
          int arrayCommands[SIZE];       //init Command array to 100 elements
    
          initFArray(arrayFloor);        //call to initFloor to zero out arrayFloor
          initCArray(arrayCommands);     //call to initCArray to zero out arrayCommands
          printHeader();                 //call to printHeader to print out command header
          inputCommands(arrayCommands);  //call to inputCommands to record commands into arrayCommands
          readCArray(arrayCommands);     //call to readCArray to read values from arrayCommands to Floor Array
          //printArray(arrayFloor);
    
    
    
          system("PAUSE");
          return 0;
    }
    
    
    
    void printArray(int arrayFloor [ROW][COL])       //prints out blanks or * based on arrayFloor value
    {
          int x;
          int y;
          int counter=1;
    
          for(x=0;x<ROW;x++)
          {
              for(y=0;y<COL;y++)
              {
                if(arrayFloor[x][y]==1)
                {
                  printf("*");
                }
                else
                {
                  printf(" ");
                }
                if(counter==COL)
                {
                  printf("\n");
                  counter=0;
                }
                counter++;
              }
          }
    
    }
    
    
    
    void printHeader()                               //prints out command header
    {
          printf("Command\t\tMeaning\n\n");
          printf("1\t\tPen Up\n");
          printf("2\t\tPen Down\n");
          printf("3\t\tTurn Right\n");
          printf("4\t\tTurn Left\n");
          printf("5,X\t\tMove Forward X Spaces\n");
          printf("6\t\tPrint the 20x20 array\n");
          printf("9\t\tEnd of data(sentinel)\n\n\n");
    
    }
    
    
    
    void initCArray(int arrayCommands[SIZE])         //initializes 100 element arrayCommands array to zero
    {
          int x;
    
          for(x=0;x<SIZE;x++)
          {
             arrayCommands[x]=0;
          }
    
    }
    
    
    
    void initFArray(int arrayFloor[ROW][COL])        //initializes 20x20 arrayFloor array to zero
    {
          int x;
          int y;
    
          for(x=0;x<ROW;x++)
          {
              for(y=0;y<COL;y++)
              {
                  arrayFloor[x][y]=0;
              }
          }
    
    }
    
    
    
    void inputCommands(int arrayCommands [SIZE])     //inputs commands from user into arrayCommands array
    {                                                //and validates input commands
          int index=0;
          int command=0;
          int counter=1;
          int distance=-1;
    
          do
          {
            printf("Command %i: ",counter);
            scanf("%i,%i",&command, &distance);
            //commands 1,2,3,4,5,6,9
            if((command > 0 && command < 5) || (command == 6) || (command == 9))
            {
              arrayCommands[index]=command;
              counter++;
              index ++;
            }
            else
            {
              if ((command == 5) && (distance >= 0))
              {
                arrayCommands[index]=command;
                index=index+1;
                arrayCommands[index]=distance;
                distance = -1;
                counter++;
                index++;
              }
              else
              {
                if ((command == 5) && (distance < 0))
                {
                  printf("\tYou have not entered a distance (eg 5,10) please try again\n");
                }
                else
                  {
                    printf("\tCommand %i is invalid, please re-enter\n",counter);
                  }
    
              }
            }
          }
          while((command !=9) && (index < SIZE-1));
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In a word: overcomplicating.

    This is very simple. Track the coordinates of the turtle. Now, go write to those coordinates in the array. Here's a quick example:
    Code:
    int turtle_y, turtle_x, pen_down = 0;
    
    for( x = 0; input[ x ] != 0; x++ )
    {
        switch( input[ x ] )
        {
            case MOVE_NORTH:
                --turtle_y;
                if( pen_down )
                    output[ turtle_y ][ turtle_x ] = 1;
            break;
            ...
        }
    }
    Ta dah!

    To control the input loop, I assume you have a zero-filled array and all of your commands are non-zero. That way, when we hit a zero we've run out of commands. You however, can do it whatever way you please. This is just a simple example. Oh, and you'll need to do your own error checking.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM