static void initRooms() { Room *room; /* actual room object */ GList *parsedRoomList = g_hash_table_get_values(roomParsedTable); GList *curParsedRoom = parsedRoomList; /* iterate through each value in the rooms parsed table */ while (NULL != curParsedRoom) { /* build the room structure and index it by name */ room = initRoom((RoomParsed *)curParsedRoom->data); g_hash_table_insert(g_rooms, (char *)dstrview(room->name), room); curParsedRoom = curParsedRoom->next; } connectRooms(); g_list_free(parsedRoomList); return; }
int generateDungeon(gridCell_t*** gridPtr, room_t** roomsPtr) { int roomCount; unsigned int seed; seed = (unsigned int)time(NULL); //1453848819; srand(seed); printf("Seed: %d\n", seed); roomCount = MIN_ROOMS + (rand() % (MAX_ROOMS - MIN_ROOMS + 1)); printf("Room count: %d\n", roomCount); if (!(*roomsPtr = malloc(sizeof(room_t) * roomCount))) { return -1; } for (int i = 0; i < roomCount; i++) { generateRoom(&(*roomsPtr)[i], *roomsPtr, i); } *gridPtr = populateGrid(*roomsPtr, roomCount); connectRooms(*gridPtr, *roomsPtr, roomCount); return roomCount; }
int generateDungeon(dungeon_t* dungeonPtr) { dungeonPtr->roomCount = MIN_ROOMS + (rand() % (MAX_ROOMS - MIN_ROOMS + 1)); printf("Room count: %d\n", dungeonPtr->roomCount); if (!(dungeonPtr->rooms = malloc(sizeof(room_t) * dungeonPtr->roomCount))) { return -1; } for (int i = 0; i < dungeonPtr->roomCount; i++) { generateRoom(&dungeonPtr->rooms[i], dungeonPtr->rooms, i); } if (populateGrid(dungeonPtr)) { return -2; } connectRooms(dungeonPtr->grid, dungeonPtr->rooms, dungeonPtr->roomCount); initMonsters(dungeonPtr); turnInit(dungeonPtr); return 0; }
main() { // ** Program Initialization ** srand (time(NULL)); int i, j; //Variables used for loop iteration //Setup filename int processId = getpid(); sprintf(folderName, "mccombn.rooms.%d\0", processId); // ** Main Program Execution ** //Holds all of the rooms room_typ *rooms[DESIRED_ROOM_NO]; //Loops through and creates all of the rooms (allocating mem too) for(i = 0; i < DESIRED_ROOM_NO; i = i + 1){ rooms[i] = createRoom(); } //Connects all of the rooms connectRooms(rooms); //Creates the folder with the appropriate permissions if(!mkdir(folderName, 0770)) i; //exit(2); //At this point, we can assume that the correct folder is created, //and that it's name is stored in folderName writeRoomsToFile( rooms, folderName ); //At this point, all of the information is written to the file, so we //need to erase all of our current information :( and read it back in //from the files. for(i = 0; i < DESIRED_ROOM_NO; i = i + 1){ free(rooms[i]); } //For clarity to the grading TA, the name of the retrieved rooms was changed //(no copying internal to the program was done, as should be obvious) room_typ *gameRooms[DESIRED_ROOM_NO]; for(i = 0; i < DESIRED_ROOM_NO; i = i + 1){ gameRooms[i] = readRoomFromFile( i, folderName ); } //TODO: Add code to delete the folder and recreate if it fails to make a folder //so that we can make the stuff that is okie dokie // ** MAIN GAME CODE ** //Game-specific variables char currentRoom = START_ROOM; char userInput[150]; //Holds user input char match; //Used later to determine if a match was found char userPath[500]; //Massive variable incase the user is having a bad day strcpy(userPath, ""); //Init it with an empty string, because of how the //code is written later short userPathCount = 0; //Start the actual game loop do{ printf("CURRENT LOCATION: %s\n", gameRooms[currentRoom]->roomName); printf("POSSIBLE CONNECTIONS: "); match = 0; //Default that we haven't found a match for(i = 0; i < gameRooms[currentRoom]->connectionCount; i = i + 1){ printf("%s", gameRooms[currentRoom]->connections[i]); if( i == gameRooms[currentRoom]->connectionCount - 1 ) printf(".\n"); else printf(", "); } printf("WHERE TO? >"); scanf("%s", userInput); //Search for connections: for(i = 0; i < gameRooms[currentRoom]->connectionCount; i = i + 1){ if(!strcmp(userInput, gameRooms[currentRoom]->connections[i])){ //If the user entered a valid match //We need to go there, bitch match = 1; //First record our path sprintf(userPath, "%s%s\n", userPath, userInput); //Increment path counter userPathCount = userPathCount + 1; //Move the "current room" pointer currentRoom = roomSearch( gameRooms, userInput ); //Print a newline for spacing printf("\n"); } //Else, do nothing! } if(!match) printf("\nHUH? I DON'T UNDERSTAND THAT ROOM. TRY AGAIN.\n\n"); }while(currentRoom != END_ROOM); //Yay! The user won. //Now we need to output the ending messages. printf("YOU HAVE FOUND THE END ROOM. CONGRATULATIONS!\n"); printf("YOU TOOK %i STEPS. YOUR PATH TO VICTORY WAS: \n%s", userPathCount, userPath); // ** Program Cleanup ** for(i = 0; i < DESIRED_ROOM_NO; i = i + 1){ free(gameRooms[i]); } exit(0); }