Exemple #1
0
int main(int argc, char * argv[])
{
    // READ THE FILE--------------------------
    int mazesize;
    FILE * filelab;
    char * mazestring;
    char filename [25];
    printf("Type the name of the file:  ");
    scanf("\n%s", &filename);
    filelab = fopen(filename, "r");

    int charindex=0;
    char c;

    while (!filelab)
    {
        printf("Type the name of the file (correctly):  ");
        scanf("\n%s", &filename);
        filelab = fopen(filename, "r");
    }

    fseek(filelab, 0L, SEEK_END);
    mazesize = ftell(filelab);
    fseek(filelab, 0L, SEEK_SET);

    mazestring =(char*) malloc ((mazesize+1)*sizeof(char));

    c = getc(filelab) ;
    while (c!= EOF)
    {
   		mazestring[charindex]=c;
		c = getc(filelab);
		charindex++;
    }
    mazestring[charindex]='\0';
    fclose(filelab);

    //CREATE THE DUNGEON---------------

    Dungeon * dungeon;
    dungeon = (Dungeon*) malloc (sizeof(Dungeon));
    CreateDungeon(mazestring,mazesize,dungeon);


    printDungeon(dungeon,dungeon->startx,dungeon->starty);



	return 0;
}
Exemple #2
0
int main(int argc, char* argv[]) {
    int errLevel;
    dungeon_t dungeon;
    int numMonSpecified = 0;

    //init random
    unsigned int seed;
    seed = (unsigned int)time(NULL); //1453848819;
    srand(seed);
    printf("Seed: %d\n", seed);

    // parse arguments
    int save = 0;
    int load = 0;
    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "--save") == 0) {
            save = 1;
        } else if (strcmp(argv[i], "--load") == 0) {
            load = 1;
        } else if (strcmp(argv[i], "--nummon") == 0) {
            if (i + 1 >= argc) {
                showUsage(argv[0]);
                return 0;
            }
            dungeon.monsterCount = atoi(argv[++i]);
            numMonSpecified = 1;
        } else {
            showUsage(argv[0]);
            return 0;
        }
    }

    if (!numMonSpecified) {
        dungeon.monsterCount = rand() % (42 / 2);
    }

    // load or generate dungeon
    if (load) {
        errLevel = loadDungeon(&dungeon, dungeonFileName());
        if (errLevel) {
            printf("Failed to load the dungeon.  Read error %d\n", errLevel);
            return -1;
        }
        populateRooms(dungeon);
    } else {
        errLevel = generateDungeon(&dungeon);
        if (errLevel) {
            printf("Failed to allocate memory for the dungeon grid.\n");
            return errLevel;
        }
    }
    pathMallocDistGrid(&dungeon.tunnelingDist);
    pathMallocDistGrid(&dungeon.nontunnelingDist);

    // save dungeon
    if (save) {
        errLevel = saveDungeon(dungeon, dungeonFileName());
        if (errLevel) {
            printf("Failed to save the dungeon.  Save error %d\n", errLevel);
            return -1;
        }
    }

    // make calculations
    pathTunneling(&dungeon);
    pathNontunneling(&dungeon);

    // print dungeon
    initTerminal();
    printDungeon(&dungeon);

    // do move
    PC_action userAction = actionMovement;
    while (dungeon.PC.alive && dungeon.monsterCount > 1 && userAction != actionSave) {
        int PCTurn = turnIsPC(&dungeon);
        if (PCTurn) {
            printDungeon(&dungeon);

            userAction = turnDoPC(&dungeon);
            switch (userAction) {
                case actionStairsUp:
                case actionStairsDn:
                    // Destroy old dungeon
                    pathFreeDistGrid(dungeon.nontunnelingDist);
                    pathFreeDistGrid(dungeon.tunnelingDist);
                    destroyDungeon(dungeon);
                    dungeon.monsterCount--; // PC was counted as a "monster".  He must be removed for reinitialisation.
                    // Build new dungeon
                    errLevel = generateDungeon(&dungeon);
                    if (errLevel) {
                        endwin();
                        printf("Failed to allocate memory for the dungeon grid.\n");
                        return errLevel;
                    }
                    pathMallocDistGrid(&dungeon.tunnelingDist);
                    pathMallocDistGrid(&dungeon.nontunnelingDist);
                    pathTunneling(&dungeon);
                    pathNontunneling(&dungeon);
                    break;
                case actionListMonsters:
                    monsterList(&dungeon);
                    break;
                default: break;
            }
        } else {
            turnDo(&dungeon);
        }
    }

    screenClearRow(0);
    if (userAction == actionSave) {
        mvprintw(0, 0, "Game saved (haha, not really!).  Press any key to exit.");
    } else if (!dungeon.PC.alive) {
        mvprintw(0, 0, "You died!  Press any key to exit.");
    } else {
        mvprintw(0, 0, "Yay!  You defeated all the monsters.  Press any key to exit.");
    }

    // Clean up
    getch(); // "press any key"
    endwin();
    pathFreeDistGrid(dungeon.nontunnelingDist);
    pathFreeDistGrid(dungeon.tunnelingDist);
    destroyDungeon(dungeon);
    return 0;
}