CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Turtle Graphics

  1. #1
    Join Date
    Apr 2012
    Posts
    4

    Turtle Graphics

    okay, I have a turtle graphics program that works just fine.My program takes the users input and uses that for directions for the turtle to draw. I want to make it so that the input is a text file instead of the user inputting the data. Can anyone lend me a hand ? any help is apppreciated!

    Here is my code :

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    //-Prototypes-
    
    int loadArray(int [],int );
    void docommands(int [],int [][30],int );
    void printArray(int [][30]);
    void trnrgt(int *);
    void trnlft(int *);
    int moveit(int *,int ,int [],int [][30],int *,int *,int);
    void doc(void);
    
    void main(void)
    {
    int commands[100]={0};
    int position[30][30]={{0},{0}};int count=0;
    
    system("CLS");
    printf("This is a turtle graphics program.");
    doc(); //how to use the program
    //ount=loadArray(commands,count); //count is how many numbers were entered
    docommands(commands,position,count);
    system ("PAUSE");
    
    }
    int loadArray(int array[],int temp){
    int x;
    for(x=0;;x++) {
    temp++; //temp is the count
    printf("Enter command : ");
    scanf("%d",&array[x]);
    if (array[x]==9)
    break ;
    if (array[x]==7 || array[x]==8){
    printf("Invalid entry !!!Try Again\n");
    x--;//erase what we just wrote
    temp--;//decrement our count
    }
    if (array[x]<= 0 || array[x]>9){
    printf("Invalid entry !!!Try Again\n");
    x--;
    temp--;
    }
    if (array[x]==5) {
    scanf(",%d",&array[x+1]);
    x++; //move to next element
    temp++; //increae count
    }
    }
    return temp;
    }
    void docommands(int array[],int ddarray[][30],int c){
    int x,y,pendwn,count,post=1,bounds1=0,bounds2=0;
    for (x=0;x<=c;x++){
    switch (array[x]){
    case 1: pendwn = 0;
    break ;
    case 2:pendwn = 1;
    break;
    case 3:trnrgt(&post);
    break;
    case 4:trnlft(&post);
    break;
    case 5:x=moveit(&post,x,array,ddarray,&bounds1,&bounds2,pendwn);
    break;
    case 6:printArray(ddarray);
    break;
    case 9:break;
    }
    }
    }
    void printArray(int array [][30]){
    int x,col,rows;
    for (rows=0;rows <30;rows++){
    for (col=0;col<30;col++)
    printf("%c" ,array[rows][col]);
    printf("\n");
    }
    }
    int moveit(int *post,int x,int array[],int ddarray[][30],int *bounds1,int *bounds2,int pendwn){
    int y,b,bnds1,bnds2;
    x++;
    bnds1=*bounds1; //watch the bounds for going up and down
    bnds2=*bounds2; //watch the bounds for going sideways
    switch (*post){
    case 1:
    if (bnds2<30){
    if (array[x]>29)
    array[x]=29;
    for(y=0;y<(array[x]);y++){
    if (pendwn){
    ddarray[bnds1][bnds2]=42;
    bnds2++;
    }
    else {
    ddarray[bnds1][bnds2]=32;
    bnds2++;
    }
    }
    }
    break;
    case 2: {
    if (array[x]>29)
    array[x]=29;
    for (y=(array[x]);y>0;y--){
    if (bnds2>0){
    if (bnds1>=30)
    bnds1=19;
    if (pendwn){
    ddarray[bnds1][bnds2]=42;
    bnds2--;
    }
    else {
    ddarray[bnds1][bnds2]= 32;
    bnds2--;
    }
    }
    else
    break;
    }
    break;
    case 3:{
    if (array[x]>29)
    array[x]=29;
    for (y =0;y<(array[x]);y++){
    if (bnds1>0){
    if (pendwn){
    ddarray[bnds1][bnds2]=42;
    bnds1--;
    }
    else{
    ddarray[bnds1][bnds2]=32;
    bnds1--;
    }
    }
    }
    break;
    }
    case 4:{
    if (array[x]>29)
    array[x]=29;
    for (y=0;y < (array[x]);y++){
    if (bnds2>29)
    bnds2=19;
    if (pendwn){
    ddarray[bnds1][bnds2]= 42;
    bnds1++;
    }
    else{
    ddarray[bnds1][bnds2]= 32;
    bnds1++;
    }
    }
    break;
    }
    }
    }
    *bounds1=bnds1;
    *bounds2=bnds2;
    return x;
    }
    void trnlft(int *post){
    switch (*post){
    case 1:*post = 3; // 1 = east
    break;
    case 2:*post = 4; // 2 = west
    break;
    case 3:*post = 2; // 3 = north
    break;
    case 4:*post = 1; //4 = south
    break;
    }
    }
    void trnrgt(int *post){
    switch (*post){
    case 1: *post = 4;
    break;
    case 2: *post = 3;
    break;
    case 3: *post = 1;
    break;
    case 4: *post = 2;
    break;
    }
    }
    void doc (void){
    printf("This program will draw a series of * with the commands that ");
    printf("are entered. \nHere are the commands : \n\n");
    printf("1 pushes your pen up meaning leaving no traces.\n");
    printf("2 puts the pen down to leave traces.\n");
    printf("3 turns right.\n");
    printf("4 turns left.\n");
    printf("5 moves after entering a 5 you have to specify the number of moves.\n");
    printf(" for example : 5,10 you have to put the coma before the number.\n");
    printf("6 prints the result on the screen.\n");
    printf("9 ends the program.\n\n");
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Turtle Graphics

    Quote Originally Posted by hellishviking View Post
    okay, I have a turtle graphics program that works just fine.My program takes the users input and uses that for directions for the turtle to draw. I want to make it so that the input is a text file instead of the user inputting the data. Can anyone lend me a hand ?
    I would first suggest formatting your code so that others can read it. Why is every line flushed to the left like that?
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int loadArray(int [],int );
    void docommands(int [],int [][30],int );
    void printArray(int [][30]);
    void trnrgt(int *);
    void trnlft(int *);
    int moveit(int *,int ,int [],int [][30],int *,int *,int);
    void doc(void);
    
    int main(void)
    {
        int commands[100]={0};
        int position[30][30]={{0},{0}};
        int count=0;
    
        system("CLS");
        printf("This is a turtle graphics program.");
        doc(); //how to use the program
        docommands(commands,position,count);
        system ("PAUSE");
    }
    
    int loadArray(int array[],int temp)
    {
        int x;
        for(x=0;;x++) 
        {
            temp++; //temp is the count
            printf("Enter command : ");
            scanf("&#37;d",&array[x]);
    
            if (array[x]==9)
                break ;
    
            if (array[x]==7 || array[x]==8)
            {
                printf("Invalid entry !!!Try Again\n");
                x--;//erase what we just wrote
                temp--;//decrement our count
            }
    
            if (array[x]<= 0 || array[x]>9)
            {
                printf("Invalid entry !!!Try Again\n");
                x--;
                temp--;
            }
    
            if (array[x]==5) 
           {
                scanf(",%d",&array[x+1]);
                x++; //move to next element
                temp++; //increae count
            }
        }
        return temp;
    }
    That is a sample of two of your functions properly formatted.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 29th, 2012 at 07:47 PM.

  3. #3
    Join Date
    Apr 2012
    Posts
    4

    Re: Turtle Graphics

    okay thank you. And I know that i have to use some thing along the lines of cin.get or getline, just not sure how to do so.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Turtle Graphics

    Quote Originally Posted by hellishviking View Post
    I want to make it so that the input is a text file instead of the user inputting the data.
    First, make sure that your turtle graphics "engine" knows nothing about where the input comes from. If you have tightly coupled the inputting with the engine, then you can't have that.

    For example, your turtle graphics routines should be individual functions, where a parameter is passed as to what to perform. Once you do that, then the rest becomes very simple. In fact, you could write a program that would take commands from a socket and send it to the routines, not from just a file or a keyboard.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 2012
    Posts
    4

    Re: Turtle Graphics

    So can i just edit the function that originally was creating the input for the program? and make it so that it reads the txt file directly instead of asking the user?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Turtle Graphics

    Quote Originally Posted by hellishviking View Post
    So can i just edit the function that originally was creating the input for the program? and make it so that it reads the txt file directly instead of asking the user?
    I didn't read your program because it was poorly formatted. Are the routines to do the input and the movement separated into functions?

    Second, you can do anything you want -- until you need to read from the keyboard again, or from another input device. So what I suggested to you is how you create the program so it doesn't matter where the input comes from.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Turtle Graphics

    For example:
    Code:
    int loadArray(int array[],int temp)
    {
        int x;
        for(x=0;;x++) 
        {
            temp++; //temp is the count
            printf("Enter command : ");
            scanf("%d",&array[x]);
            if (array[x]==9)
                break ;
            if (array[x]==7 || array[x]==8)
            {
                printf("Invalid entry !!!Try Again\n");
                x--;//erase what we just wrote
                temp--;//decrement our count
            }
            if (array[x]<= 0 || array[x]>9)
            {
                printf("Invalid entry !!!Try Again\n");
                x--;
                temp--;
            }
            if (array[x]==5) {
                scanf(",%d",&array[x+1]);
                x++; //move to next element
                temp++; //increae count
            }
        }
        return temp;
    }
    This function should be renamed loadArrayFromKeyboard() or something like that. So instead of this function, write one that loads the same array from a file. There is no need to change or edit this function -- just create another that loads from a file.

    Then you use that loaded array in the rest of your program.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Apr 2012
    Posts
    4

    Re: Turtle Graphics

    Thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured