mercredi 28 octobre 2015

Connect Random Room from Directory (C)

I have almost similar questions like this thread (Connect Rooms Randomly in Adventure Game), but not similar since I'm using file from directory (not just pure loose file.txt)

Summary: I have to create 7 rooms that must have a random (3 to 6) connections between files, and also provide a connection back. (If "a" connect to "b", then "b" must have connection back to "a").

What I'm doing is that I'm solving this problem by creating like a "matrix" to provide the connection between the files.

My code: (I'm still following the answer given from the thread above, but going to modify it soon)

#define MAX_ROOM 7

int conn[MAX_ROOM][MAX_ROOM] = {{0}};

int uniform(int n) {
    return n * (rand() / (double) RAND_MAX);
}

// function for connection between file

void connect(int visited[], int final, int initial) {
    conn[initial][final] = 1;
    conn[final][initial] = 1;

    if (visited[initial] ==0) {
        int room[MAX_ROOM - 1];
        int next[MAX_ROOM - 1];
        int i, j;
        visited[initial] = 1;

        for (i = 0; i < MAX_ROOM; i++) {
            room[i] = i;
        }
        room[initial] = MAX_ROOM - 1;

        i = MAX_ROOM - 1;
        while (i) {
            int swap;
            j = uniform(i--);
            swap = room[i];
            room[i] = room[j];
            room[j] = swap;
        }

        j=0;
        for (i=0; i < MAX_ROOM; i++) {
            if (visited[room[i]] == 0) {
                next[j++] = room[i];
            }
        }

        for (i=0; i < MAX_ROOM; i++) {
            if (visited[room[i]] !=0) {
                next[j++] = room[i];
            }
        }

        for (i = 0; i < 3; i++) {
            connect(visited, initial, next[i]);
        }
    }
}

int main () {
    char *room_name[7];
    room_name[0]="L";
    room_name[1]="R;
    room_name[2]="K";
    room_name[3]="W";
    room_name[4]="D";
    room_name[5]="M";
    room_name[6]="B";

    int visited[MAX_ROOM] = {0};
    int i,j;

    connect(visited,0,1);

    i = 10;
    while (i) {
        char *pointer;
        j = uniform(i--);
        pointer = room_name[i];
        room_name[i] = room_name[j];
        room_name[j] = pointer;
    }
    int counter;
    for (counter=0; counter < 7; counter++){

    snprintf(currentFile, bufSize ,"%s/file-%d.txt",DIRECTORY_NAME,counter);
    FILE *f = fopen(currentFile,"a");
    if (f==NULL) {
        perror("Error in opening the file. \n");
    } else {

    for (i=0; i < MAX_ROOM; i++) {
        printf("%s\n", room_name[i]);
        for (j=0; j < MAX_ROOM; j++) { 
            if (conn[i][j]) {
                printf("CONNECTION 'j' -> %s\n", room_name[j]);
            }
        }
        printf("\n");
     }
 fclose(f);
}

What I'm expecting is:

(inside "file-1.txt" in directory & assume I got 4 connections)

CONNECTION 1: L
CONNECTION 2: D
CONNECTION 3: B
CONNECTION 4: R

But instead, I got:

(inside EVERY FILE in my directory)

CONNECTION 1: L
CONNECTION 2: D
CONNECTION 3: B
CONNECTION 4: R
CONNECTION 5: M
CONNECTION 6: S

So no matter how many connection created, it just simply print all connections in every room to every room.

Is it something wrong in my nested for loop between counter and i? Or is it something else?




Aucun commentaire:

Enregistrer un commentaire