Domanda

I have a program that I have been working on all day. The program freezes more times then it doesn't freeze....when it doesn't freeze it works fine and looks like this... http://picpaste.com/Multidimensional_Array-KfdKSmoE.bmp

When it does hang up upon opening it will look something like this... http://picpaste.com/Multidimensional_Array_hung-Jcmz0rQP.bmp

// 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
    while (i != PATH_SIZE) {
        for (i = 0; i < PATH_SIZE;) {
            // Check the direction and replace that char
            switch (dir) {
                case 0: if ((y - 1) >= 0 && (y - 1) < ROW_SIZE
                            && board[x][y - 1] == '.') {
                    board[x][--y] = i + 'B';
                    ++i;
                } break;
                case 1: if ((y + 1) >= 0 &&(y + 1) < ROW_SIZE
                            && board[x][y + 1] == '.') {
                    board[x][++y] = i + 'B';
                    ++i;
                } break;
                case 2: if ((x - 1) >= 0 && (x - 1) < ROW_SIZE
                            && board[x - 1][y] == '.') {
                    board[--x][y] = i + 'B';
                    ++i;
                } break;
                case 3: if ((x + 1) >= 0 && (x + 1) < ROW_SIZE
                            && board[x + 1][y] == '.') {
                    board[++x][y] = i + 'B';
                    ++i;
                } break;
            }
        // 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;
}
È stato utile?

Soluzione

This will hang when there are no possible directions to move. Taking a simple example of your board with 4 by 4. Initially the board would be like

....
....
....
....

Initial position marked

A...
....
....
....

I make the first move, say I move RIGHT

AB..
....
....
....

I make the next move, say I move DOWN

AB..
.C..
....
....

I make the next move, say I move DOWN

AB..
.C..
.D..
....

I make the next move, say I move LEFT

AB..
.C..
ED..
....

I make the next move, say I move UP

AB..
FC..
ED..
....

And I am stuck, and I do not have any possible locations to move. Now a few more additions would fix your problem.

  1. Add a method to calculate the list of possible moving directions
  2. If no more moves are possible, exit the program

Hint

void calc_poss_dirs()
{
        int dir;

        poss_dir.count = 0;

        for (dir = UP; dir <= RIGHT; dir++) {
                if (can_move(dir)) {
                        poss_dir.dirs[poss_dir.count] = dir;
                        poss_dir.count++;
                }
        }
}
int can_move(int dir)
{
        int x = cur_x;
        int y = cur_y;

        if (dir == UP)
                x--;
        else if (dir == DOWN)
                x++;
        else if (dir == LEFT)
                y--;
        else
                y++;

        if (!in_area(x, y))
                return 0;

        if (visited(x, y))
                return 0;

        return 1;
}

Altri suggerimenti

Thanks Sakthi Kumar your input was invaluable as was the rest of all you who helped 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;
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top