0

Please help me figure this out, I have tested it and tested it and re-read and re-wrote for the past 11 hours and I give up. I found a working code that someone else wrote, but it still doesn't explain to me why his works and mine doesn't because the problem that I am having works on his but not on mine

Got it people, code edited for anyone who has had a similiar problem... The original code that I had is here http://pastebin.com/h7fXHKzf the problem I was having was that it kept hanging up on the if(board[x][y - 1] == '.') checks.

Spoke too soon....The program will sometimes crash...it's rare but has crashed 3x in a row before...most of the time when I run it everything works.

    // Chapter 8 Programming Project #9

    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <time.h>

    #define SIZE 10
    #define PATH_SIZE ((int) (sizeof(brd_path)))
    #define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))

    int main(void)
    {
        char board[SIZE][SIZE] = {};
        char brd_path[25] = {'B', 'C', 'D', 'E', 'F', 'G', 'H',
                             'I', 'J', 'K', 'L', 'M', 'N', 'O',
                             'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                             'W', 'X', 'Y', 'Z'};
        // 0 = Up, 1 = Down, 2 = Left, 3 = Right
        bool path_dir_chk[4] = {false};
        bool blocked = false;
        unsigned short i, j, x = 0, y = 0;
        // Generate a random number
        srand((unsigned) time(NULL));
        int dir = rand() % 4;
        // Set all positions of board to '.'
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                board[x][y] = '.';

        }
        x = 0;
        y = 0;
        board[0][0] = 'A';
        // Generate the path
        while (blocked != true && i != PATH_SIZE) {
            for (i = 0; i < PATH_SIZE;) {
                // Reset path_dir_chk values if empty
                for (j = 0; j < 4; j++) {
                    if (board[x][y - 1] == '.')
                        path_dir_chk[0] = false;
                    if (board[x][y + 1] == '.')
                        path_dir_chk[1] = false;
                    if (board[x - 1][y] == '.')
                        path_dir_chk[2] = false;
                    if (board[x + 1][y] == '.') 
                        path_dir_chk[3] = false;
                }
                // Check the direction and replace that char
                switch (dir) {
                    case 0: if ((y - 1) >= 0 && board[x][y - 1] == '.') {
                        board[x][--y] = brd_path[i];
                        path_dir_chk[0] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                    case 1: if ((y + 1) >= 0 && board[x][y + 1] == '.') {
                        board[x][++y] = brd_path[i];
                        path_dir_chk[1] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                    case 2: if ((x - 1) >= 0 && board[x - 1][y] == '.') {
                        board[--x][y] = brd_path[i];
                        path_dir_chk[2] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                    case 3: if ((x + 1) >= 0 && board[x + 1][y] == '.') {
                        board[++x][y] = brd_path[i];
                        path_dir_chk[3] = true;
                        printf("I is now: %d\n", ++i);
                    } break;
                }
            // if all path's are true exit
            if (path_dir_chk[0] == true &&
                path_dir_chk[1] == true &&
                path_dir_chk[2] == true &&
                path_dir_chk[3] == true)
                blocked = true;
            // Reset the random direction
            dir = rand() % 4;
            }
        }
        // Print the board
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                printf("%c ", board[x][y]);
            printf("\n");
        }

        return 0;
    }

OK I have made changes to reflect what I have so far, no it is printing 'I is now:' numbers 1 - 25 and then it starts over but it stops on 12 the second time around and freezes into some kind of loop

Below is the working code I found online, you can compare the two and see the similarity's but the lines of code on his that are exactly like mine do not work on mine.....

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    #define ROWS 10
    #define COLS 10
    int main (void)
    {
        int i, j, k, direction;
        char board[ROWS][COLS];
        const char letters[] = {'A', 'B', 'C', 'D', 'E', 'F',
                                'G', 'H', 'I', 'J', 'K', 'L',
                                'M', 'N', 'O', 'P', 'Q', 'R',
                                'S', 'T', 'U', 'V', 'W', 'X',
                                'Y', 'Z'};

        srand ((unsigned) time(NULL));

        for (i = 0; i < ROWS; i++)
          for (j = 0; j < COLS; j++)
            board[i][j] = '.';

        i = 0;
        j = 0;
        k = 1;
        //set array[0][0] to first element
        board[i][j] = letters[0];
        while (k < 26) {
            direction = rand() % 4;

            if (board[i][j] == '.')
                board[i][j] = letters[k++];
            if ((board[i][j + 1] != '.' || j == ROWS - 1 )&&
                (board[i + 1][j] != '.' || i == COLS -1) &&
                (board[i - 1][j] != '.' || i == 0) &&
                (board[i][j - 1] != '.' || j == 0))
                break;
            switch (direction) {
              case 0: if (j < ROWS - 1 && board[i][j + 1] == '.'){  //move right
                      j++;
                      break;     }
              case 1: if (i < COLS -1 && board[i + 1][j] == '.') {  //move down
                      i++;
                      break; }
              case 2: if (i > 0 && board[i - 1][j] == '.'){  //move up
                      i--;
                      break;  }
              case 3: if (j > 0 && board[i][j - 1] == '.') { //move left
                      j--;
                      break; }
            }
        }
        for (i = 0; i < ROWS; i++) {
          for (j = 0; j < COLS; j++)
            printf ("%4c", board[i][j]);
          printf ("\n");
        }

        return 0;
    }
21
  • Your indentation is misleading in that "TestX" block, you might need extra {}s. Commented Mar 11, 2014 at 6:23
  • What error are you getting? Commented Mar 11, 2014 at 6:25
  • Sorry, good point, but I fixed it and still nothing, screen is completely blank, and I'm stuck in a loop because i only iterates after a switch if statement has been completed. Commented Mar 11, 2014 at 6:27
  • no error, just blank screen Commented Mar 11, 2014 at 6:27
  • by the way in case you were wondering the case 0: through case 3: does work, its the if statements that correlate with the board[x][y - 1] == '.' or y+1 or x-1 or x+1 that aren't working, when it checks to see if that position is equal to '.' it thinks that it isn't I switched it from == to != and the tests started printing but it won't work if it doesn't know that a character exsists there Commented Mar 11, 2014 at 6:32

3 Answers 3

1

You aren't setting x and y back to 0 after this code

for (x = 0; x < ROW_SIZE; x++) {
        for (y = 0; y < ROW_SIZE; y++)
            board[x][y] = '.';
    }

Thus x and y will start at 10. Also you aren't range checking x and y, which means that x and y might wander off the board.

This code

    if ((board[x][y - 1] != '.' || y - 1 < 0) &&
        (board[x][y + 1] != '.' || y + 1 > ROW_SIZE) &&
        (board[x - 1][y] != '.' || x - 1 < 0) &&
        (board[x + 1][y] != '.' || x + 1 > ROW_SIZE))

should be this

    if ((y - 1 <  0        || board[x][y - 1] != '.') &&
        (y + 1 >= ROW_SIZE || board[x][y + 1] != '.') &&
        (x - 1 <  0        || board[x - 1][y] != '.') &&
        (x + 1 >= ROW_SIZE || board[x + 1][y] != '.'))

There are two subtle differences.

First y+1 and x+1 are not allowed to be equal to ROW_SIZE, since ROW_SIZE is 10, but the valid array indices are 0 to 9.

Second, order is important. When evaluating a logical OR, the left side is evaluated first, and if it's true, then the right side is not evaluated. This is important, since on some machines, even reading outside of the array bounds will cause a crash.

Sign up to request clarification or add additional context in comments.

12 Comments

I edited the source above to reflect the new changes...I placed x = 0; and y = 0; right below that and it still is doing the same thing
you put them inside the loop :D
sorry, my head is so fried from sitting here for going on 12 hours now trying to figure this out
seemed to work!!! it printed out all the tests and a I is now: 1 and then about a dozen more tests and crashed :(
Yup, either x or y wandered off the board. You can see in the other person's code that some checking is done to see if i or j are at the edge of the board. However, that code is still not quite right. You need to make sure that you never access the board array with x,y values <0 or >=ROWSIZE
|
0
for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                board[x][y] = '.';
        }

After the initialization you are not resetting the value of x and y. While value of X= ROW_SIZE, you are trying to access board[x + 1][y] and board[x][y + 1].

x = 0;
y = 0;

Comments

0

Thanks user3386109 your input was invaluable as was the rest of y'alls help I appreciate it, the working code is below :)

    // Chapter 8 Programming Project #9

    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <time.h>

    #define SIZE 10
    #define PATH_SIZE 25
    #define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))

    int main(void)
    {
        char board[SIZE][SIZE];
        // 0 = Up, 1 = Down, 2 = Left, 3 = Right
        unsigned short i = 0, x, y;
        // Generate a random number
        srand((unsigned) time(NULL));
        int dir = rand() % 4;
        // Set all positions of board to '.'
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                board[x][y] = '.';
        }
        x = 0;
        y = 0;
        board[0][0] = 'A';
        // Generate the path
        for (i = 0; i < PATH_SIZE;) {
                // Check that the last character has not been cornered
            if ((board[x][y - 1] != '.' || y - 1 < 0) &&
                (board[x][y + 1] != '.' || y + 1 > ROW_SIZE) &&
                (board[x - 1][y] != '.' || x - 1 < 0) &&
                (board[x + 1][y] != '.' || x + 1 > ROW_SIZE))
                break;
            // Check the direction and replace that char
            switch (dir) {
                case 0: if ((y - 1) >= 0
                            && board[x][y - 1] == '.') {
                            board[x][--y] = i + 'B';
                            ++i;
                        } break;
                case 1: if ((y + 1) < ROW_SIZE
                            && board[x][y + 1] == '.') {
                            board[x][++y] = i + 'B';
                            ++i;
                        } break;
                case 2: if ((x - 1) >= 0
                            && board[x - 1][y] == '.') {
                            board[--x][y] = i + 'B';
                            ++i;
                        } break;
                case 3: if ((x + 1) < ROW_SIZE
                            && board[x + 1][y] == '.') {
                            board[++x][y] = i + 'B';
                            ++i;
                        } break;
                default: if (board[x][y] == '.')
                             board[x][y] = i + 'B';
                             break;
            }
        // Reset the random directions
        dir = rand() % 4;
        }
        // Print the board
        for (x = 0; x < ROW_SIZE; x++) {
            for (y = 0; y < ROW_SIZE; y++)
                printf("%4c ", board[x][y]);
            printf("\n");
        }

        return 0;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.