Thread: Classic problem doesn't accept direction

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    26

    Classic problem doesn't accept direction

    Listed below is the classic turtle graphics problem. It takes a command 1 through 9. The problem is that it won't place the distance or the direction into the move function. It must have something to do with how I have setup the array. Further, the array does not output when printed - it is all "0" (20X20)

    Just need a little direction to fix the move function.

    Code is here

    #include <iostream>

    using std::cin;
    using std::cout;
    using std::endl;

    const int PenUp = 1; // global variables
    const int PenDown = 2;
    const int TurnRight = 3;
    const int TurnLeft = 4;
    const int Move = 5;
    const int PrintGrid = 6;
    const int Stop = 9;
    const int East = 0;
    const int South = 1;
    const int West = 2;
    const int North = 3;
    const int Max = 100; /* the maximum number of commands */
    const int BoardSize = 20;
    const char DrawChar = '*'; // this prints out 42
    const char BlankChar = ' ';

    void getCommands(int [][2]);
    int turnRight(int);
    int turnLeft(int);
    bool movePen(bool, int [][BoardSize], int, int);
    void printArray(int [][BoardSize]);

    main()
    {
    int floor[BoardSize][BoardSize] = {{0,0}},
    command, direction = East,
    commandArray[Max][2] = {0,0}, distance, count = 0;
    bool error_flag = false,
    pen_is_down = false;

    getCommands(commandArray);
    command = commandArray[count][0];

    while (command != Stop && !(error_flag)) {
    switch (command) {
    case PenUp:
    pen_is_down = true;

    break;
    case PenDown:
    pen_is_down = false;

    break;
    case TurnRight:
    turnRight ( direction); // listed below

    break;
    case TurnLeft:
    turnLeft( direction ); // listed below

    break;
    case Move: // Need help here
    distance = commandArray [count][2];
    movePen (pen_is_down, floor, direction, distance);

    break;
    case PrintGrid: // prints all zeros
    printArray (floor);

    break;
    }
    command = commandArray[++count][0];
    }
    return 0;
    }

    void getCommands(int commands[][2])
    {
    int rowmarker = 0, cmd;
    char letter;

    do{
    cout << "Enter command (" << Stop <<" to end input): ";
    cin >> cmd;
    commands[rowmarker][0] = cmd;
    if (cmd == Move){
    letter = cin.get(); /* gobble the comma */
    cin >> commands[rowmarker][1];
    }
    rowmarker++;
    }while((cmd != Stop) && (rowmarker < Max));
    if(cmd != Stop) {
    cout << "Too many commands!!\n";
    cout << "Last command changed to " << Stop << endl;
    commands[Max - 1][0] = Stop;
    }
    letter = cin.get();
    }

    int turnRight(int d)
    {
    static int dir;
    ++dir;
    if (dir=4)
    dir = 0;
    return d;
    }

    int turnLeft(int d)
    {
    static int dir;
    --dir;
    if (dir=-1)
    dir = 3;
    return d;
    }

    bool movePen(bool down, int a[][BoardSize], int dir, int dist)
    {
    static int row = 0;
    static int column = 0;
    int counts = dist;
    int i, colEnd, rowEnd;
    int command;


    switch (dir){ // dir stays at 0 and dist ( distance ) stays at 0


    case East:
    colEnd = column + dist;
    if (colEnd > 20){
    command = Stop;
    break;
    }


    if ( PenDown ) { // fills steps with * because pen is down
    for ( i = 0 + 1; i < counts ; ++i ){
    a [ row ][column] = DrawChar;
    ++ column;
    }
    }
    else { // fill steps with blank because pen is up
    for ( i = 0; i < counts ; ++i ){
    a [ row ][column] = BlankChar;
    ++ column;
    }
    }
    break;

    case West:
    colEnd = column + dist;
    if (colEnd > 20){
    command = Stop;
    break;
    }


    if ( PenDown ) {
    for ( i = 0; i < counts ; ++i ){
    a [ row ][column] = DrawChar;
    ++ column;}
    }
    else {
    for ( i = 0; i < counts ; ++i ){
    a [ row ][column] = BlankChar;
    ++ column;}
    }

    break;

    case South:
    rowEnd = row + dist;
    if (rowEnd > 20){
    command = Stop;
    break;
    }


    if ( PenDown ) {
    for ( i = 0; i < counts ; ++i ){
    a [ row ][column] = DrawChar;
    ++row;}
    }
    else {
    for ( i = 0; i < counts ; ++i ){
    a [ row ][column] = BlankChar;
    ++row;}

    }
    break;

    case North:
    rowEnd = row - dist;
    if (rowEnd < 0){
    command = Stop;
    break;
    }

    if ( PenDown ) {
    for ( i = 0; i < counts ; ++i ){
    a [ row ][column] = DrawChar;
    --row;}
    }
    else {
    for ( i = 0; i < counts; ++i ){
    a [ row ][column] = BlankChar;
    --row;}

    }
    break;
    }

    return down;

    }

    void printArray(int a[][BoardSize]){
    int i,j;


    for (i =0; i < BoardSize; i++){
    for (j = 0 ; j < BoardSize; j++)
    cout << a[i][j];
    cout << "\n";}
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    well, I've not looked at this too thoroughly, but it doesn't look like direction ever changes in main. Therefore, it is always 0 in main, and always passed to move as 0....you need to either set it equal to your turn calls or pass it by reference (what I would do).

    ie. direction = turnLeft(direction) or in your turnLeft()/turnRight() definitions, change it from int d to int &d, that way it will be changed in main when it is changed in those.

    The distance problem...well, fix that then see where you stand.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    26
    I tried that but the direction did not change. The declaration and the function were changed. Any other places?

    int turnRight(int &);


    int turnRight(int &d)
    {
    ++d;
    if (d=4)
    d = 0;
    return d;
    }


    Plus the character * prints out 42. All attampts to fix this have failed.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    Can't believe I missed this before, you're setting d=4 every time you call that, then since it equals 4 every time it is set to 0.

    Originally posted by cheesehead

    int turnRight(int &d)
    {
    ++d;
    if (d=4)
    d = 0;
    return d;
    }
    It should be if(d==4) d=0; not if(d=4) d=0;

    ...see where that gets ya

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    26
    Great hint - it took care of the left & right problem.

    Now it only has 2 problems:

    1) The Pen Up / Pen Down

    2) Printing characters it now prints only the number 42 instead of the *

    Any ideas?

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    You define floor as an array of int like this:

    int floor[BoardSize][BoardSize] = {{0,0}}...

    and you pass it to movePen() like this:

    movePen (pen_is_down, floor, direction, distance);

    where movePen() prototype is this:

    bool movePen(bool down, int a[][BoardSize], int dir, int dist);

    and within the the definition of movePen() you do this:

    a [ row ][column] = DrawChar;

    where DrawChar is defined thusly:

    const char DrawChar = '*';

    This means you are trying to store a char in an int array. The computer thinks that's just fine as it doesn't know what '*' is anyway. Whenever it sees '*' it stores it using the ASCII value of 42, which is an int.

    To prevent this you need to change floor to a char array, not an int array. Obviously, you need to change all function prototypes and definitions you pass floor to as well.

    See how much that corrects and post an update if it doesn't solve it all, or if it points out new problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 06:45 AM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM