Thread: Logic doesn't equal

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

    Logic doesn't equal

    Listed below is the complete set of code for a problem.
    It is the classic turtle graphics problem. PenUp, PenDown, Turn Left, Turn Right, Move, print Array.

    The problem occurs during the boundary checking in a two dimensional array. The program works except for boundary checking area. The code has been commented out and marked.

    In some cases the right / East and west boundary was not exceeded but the program jumped into the if > limit.

    Also, the South and West boundary equations are posing a problem. My eyes are fuzzy from looking at this code.

    Any Ideas?

    #include <iostream>

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

    const int PenUp = 1;
    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 = '*';
    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 = false;
    break;

    case PenDown:
    pen_is_down = true;
    break;

    case TurnRight:
    turnRight ( direction);
    break;

    case TurnLeft:
    turnLeft( direction );
    break;

    case Move:
    distance = commandArray[count][1];
    movePen (pen_is_down, floor, direction, distance);
    break;

    case PrintGrid:
    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)
    {
    ++d;
    if (d==4)
    d = 0;
    return d;
    }

    int turnLeft(int &d)
    {
    if (d == 0)
    d = 3;
    else
    --d;
    return d;
    }

    bool movePen(bool down, int a[][BoardSize], int dir , int dist)
    {
    int floor[BoardSize][BoardSize] = {{0,0}};
    static int row = 0;
    static int column = 0;
    static bool error_flag = false;
    int i;

    switch (dir){

    case East:

    // Code doesn't work

    if ( column + dist > BoardSize ){ // code to prevent traveling beyond boundaries
    cout << "You have exceeded the right side limits of the grid!\n";
    printArray (floor);
    error_flag = true;
    break;}


    ++column; // this line makes turtle step into next box
    if (down != false ) {
    for ( i = 1 ; i <= dist; ++i ){
    if ( a [ row ][column] == BlankChar )
    a [ row ][column] = BlankChar;
    else a [ row ][column] = DrawChar;
    ++ column;
    }
    }
    else {
    for ( i = 1; i <= dist; ++i ){
    if ( a [ row ][column] == DrawChar )
    a [ row ][column] = DrawChar;
    else a [ row ][column] = BlankChar;
    ++ column;
    }
    }
    --column; // this line brings him back
    break;

    case West:

    /* CODE WON"T WORK
    if ( column-dist <= -1 ){ // code to prevent traveling beyond boundaries
    cout << column + dist << "\n";
    cout << "You have exceeded the left side limits of the grid!\n";
    printArray (floor);
    error_flag = true;
    break;}
    */

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

    case South:

    /* CODE WON'T WORK
    if ( row+dist >= 21 ){ // code to prevent traveling beyond edge
    cout << row + dist << "\n";
    cout << "You have exceeded the right side limits of the grid!\n";
    printArray (floor);
    error_flag = true;
    break;}
    */
    ++row;
    if ( down ) {
    for ( i = 0; i < dist; ++i ){
    if ( a [ row ][column] == BlankChar )
    a [ row ][column] = BlankChar;
    else a [ row ][column] = DrawChar;
    ++row;}
    }
    else {
    for ( i = 0; i < dist; ++i ){
    if ( a [ row ][column] == DrawChar )
    a [ row ][column] = DrawChar;
    else a [ row ][column] = BlankChar;
    ++row;}
    }
    --row;
    break;

    case North:

    /* CODE WON"T WORK
    if ( column-dist <0 ){ // code to prevent traveling beyond edge
    cout << row - dist << "\n";
    cout << "You have exceeded the right side limits of the grid!\n";
    printArray (floor);
    error_flag = true;
    break;}
    */
    --row;
    if ( down ) {
    for ( i = 0; i < dist; ++i ){
    if ( a [ row ][column] == BlankChar )
    a [ row ][column] = BlankChar;
    else a [ row ][column] = DrawChar;
    --row;}
    }
    else {
    for ( i = 0; i < dist; ++i ){
    if ( a [ row ][column] == DrawChar )
    a [ row ][column] = DrawChar;
    else a [ row ][column] = BlankChar;
    --row;}
    }
    ++row;
    break;
    }
    return down;
    }

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

    for (i =0; i < BoardSize; i++){
    for (j = 0 ; j < BoardSize; j++){
    if (a[i][j] == 32)
    cout << BlankChar;
    else if ( a[i][j] == 42)
    cout << DrawChar;
    else cout << a[i][j];}
    cout << "\n";}

    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if ( row+dist >= 21 ){ // code to prevent traveling beyond edge
    Well this bit at least steps off the edge of the array
    should be
    > BoardSize

    Personally, I'd look at writing a function which moves in a given direction just one step, and returns true/false depending on whether that move was successful.
    Move 'n' steps can just call it in a loop until all the moves are done, or it returns false.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    26
    Point well taken but there are several restrictions on the problem.

    It still doesn't make sense that the program would go to the if when the value was not exceeded.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  2. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  3. how can 0.00665 not equal 0.00665??!!
    By Susan in forum C++ Programming
    Replies: 10
    Last Post: 02-10-2002, 02:33 AM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM