mardi 5 mai 2015

Connect Rooms Randomly in Adventure Game

I have the following code where I randomly create 7 room names, and give them a type (start, middle, end). I now need to randomly connect these rooms to each have anywhere between 3 and 6 connections. I am at a loss of what to do. I have found an example of how to do it using bitcode, but as in my other post, I still do not understand that version. If anyone could help, that would be greatly appreciated. Below is the relevant code for the rooms:

//Function to print room info for the random subset of rooms to a file
void writeRoomFiles(int *rooms, char *dir, char **filenames, char **roomNames) {
    int i;
    int j;
    int k;

    FILE *file;
    char filename[100]; 

    srand(time(NULL));

    for (i = MAX_NUM_ROOMS - 1; i > 0; --i) {
        j=rand() % (i + 1);
        swap(rooms+i, rooms+j);
    }


    //Create file for each room
    for (k = 0; k < MAX_ROOM_SEL; ++k) {

        //Get complete file name
        strcpy(filename, dir);
        strcat(filename, filenames[rooms[i]]);

        //Create file
        if ((file = fopen(filename, "w")) == NULL) {

            //If can't open file, print error statement
            fprintf(stderr, "could not open %s for writing\n", filename);
            exit(2);
        }

        //Write room name to file
        fprintf(file, "ROOM NAME: %s", roomNames[rooms[i]]);

        fclose(file);
    }
}
//function to give rooms a category - start, middle, end
void categorizeRooms(int *rooms, char *dir, char **filenames, char **roomNames) {
           int i;
           FILE *file;
           `enter code here`char filename[100]; 

    //Rooms are already random due to createRoomFiles function
    //Therefore the first room will always be the start room
    //The last will be the end room
    //The rest will be middle rooms
    for (i = 0; i < MAX_ROOM_SEL; ++i) {

        //Concatenate path and filename to get complete filename
        strcpy(filename, dir);
        strcat(filename, filenames[rooms[i]]);

        //Open file 
        if ((file = fopen(filename, "a")) == NULL) {

            //If can't open file, print error statement
            fprintf(stderr, "could not open %s for writing\n", filename);
            exit(2);
        }

        //Write type of room to file
        if (i == 0) {
            fprintf(file, "\nROOM TYPE: START_ROOM");
        } else if (i > 1) {
            fprintf(file, "\nROOM TYPE: MID_ROOM");
        } else {
            fprintf(file, "\nROOM TYPE: END_ROOM");
        }

        fclose(file);
    }
}




Aucun commentaire:

Enregistrer un commentaire