Thread: Need help on my first Text Adventure game

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    3

    Exclamation Need help on my first Text Adventure game

    Hello guys and gals, I have been in my computer science class for about 3 weeks, I'm trying to create a Text Adventure but i seem to have hit a bug, could anyone help? Its a bit long, but the part i need help with is in the bottom, under my spiderfight custom function, i put a comment for you guys after the while loop i made.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void displayhealth(int);
    void spiderfight(int, int, char, int, int, int);
    int main()
    {
        /* Monsters */
        int spider = 100;
        int troll = 150;
    
    
        /* Players */
    
    
        int playerhealth = 100;
        char player = 'c';
        char pinput;
    
    
        /* Locations */
        char Cave = 'C';
        char Hill = 'H';
        char Bridge = 'B';
        char Forest = 'F';
    
    
        /* Skills */
        int Punchdmg = 10;
        int Kickdmg = 25;
        int Bitedmg = 35;
    
    
        printf("Chapter I: Amnesia\n\n");
        printf("Location: Dark Forest\n\n");
        displayhealth(playerhealth);
    
    
    
    
        printf("You awaken in a dark forest, your leg is badly wounded with what\nlooks like a fang stuck in it. You seem to be undergoing amnesia and dont know\nwhere you are, you check inside your pocket and find a credit card with the nameKevin Chen on it. Ahead there is a fork in the path.\n");
        printf("\nChoose a path\n\nCave (C)\nHill (H)\nBridge (B)\n");
        scanf("%c", &player);
    
    
    
    
        while(player == 'C' || player == 'c' || player == 'B' || player == 'b' || player == 'H' || player == 'h')
        {
    
    
            if(player == 'C' || player == 'c') /* <-- I would like to later change this to a string "Cave", how would i do that? */
            {
                getchar();
                system("cls");
                printf("Location: Creepy Cave\n\nA dark and ominous walk, you arrive at the entrance of the Cave. There are\nthick stands of Silk hanging off the perimeter of the entrance and you get\nan eerie feel that hundreds of eyes are watching you.\n\n");
                printf("Would you like to enter the Creepy Cave? Y/N\n");
                scanf("%c", &player);
                if(player == 'Y' || player == 'y')
                    {
                        spiderfight(spider, playerhealth, pinput, Punchdmg, Kickdmg, Bitedmg);
                    }
                    else if(player == 'N' || player == 'n')
                        {
                            getchar();
                            system("cls");
                            printf("Location: Dark Forest\n");
                            printf("You arrive back at the Dark forest after fleeing from the Dark Cave\nChoose a path\n\nCave (C)\nHill (H)\nBridge (B)\n");
                            scanf("%c", &player);
                        }
    
    
    
    
            }
            else if(player == 'H' || player == 'h')
            {
                getchar();
                printf("Not yet written");
                break;
    
    
            }
            if(player == 'B' || player == 'b')
            {
                getchar();
                system("cls");
                printf("Location: Bridge\nYou made it to the bridge in one piece, somewhere underneath the bridge, the\nsnoring of something big is being emitted, looking under the bridge you find\nyourself face to face with a sleeping troll. Shaken with fear, you try to cross the bridge as quickly as possible, you take a step...\n\nPress Enter\n");
                getchar();
                printf("*CREAK*\n\nDamnit, why did the boards have to be creaky.\nThe Troll was awaken!\nTROLL: Your going to have to pay a toll to get across MY bridge!\nTROLL: I think your head would suffice!\n");
                printf("Run? Or FIGHT! R/F\n");
                scanf("%c", &player);
                if(player == 'R' || player == 'r')
                    {
                        printf("TROLL: HEY! Come back here!\n\nYou got away safely");
                        getchar();
                        system("cls");
                        printf("Location: Dark Forest\n");
                        printf("You arrive back at the Dark forest after fleeing from the Dark Cave\nChoose a path\n\nCave (C)\nHill (H)\nBridge (B)\n");
                        scanf("%c", &player);
                    }
                else if(player == 'F' || player == 'f')
                {
                    printf("Hello");
                }
    
    
    
    
    
    
            }
        }
    
    
    
    
        return 0;
    }
    
    
    void spiderfight(int spider2, int playerhealth2, char pinput2, int punchdmg2, int kickdmg2, int bitedmg2)
    {
        getchar();
        system("cls");
        while(playerhealth2 > 1 || spider2 > 1) /* Im trying to make it so that it will repeat until either you, or the spider dies. But ATM, it will keep repeating even if the spider is past 0 health, also it prints the Choose a move option twice every turn. And yes i do realize i haven't made a way for the player to die yet */
            {
        printf("Kevin's Health: %i  --  Spider Health: %i\n\n", playerhealth2, spider2);
        printf("Choose a Move\nA. Punch   B. Kick\nC. Bite    D. ----\n\n");
        scanf("%c", &pinput2);
        if(pinput2 == 'A' || pinput2 == 'a')
            {
                printf("Kevin used Punch!\n");
                spider2 = spider2 - punchdmg2;
            }
        if(pinput2 == 'B' || pinput2 == 'b')
            {
                printf("Kevin used Kick!\n");
                spider2 = spider2 - kickdmg2;
            }
        if(pinput2 == 'C' || pinput2 == 'c')
            {
                printf("Kevin used Bite! - Gross... Did you really just bite a spider?\n");
                spider2 = spider2 - bitedmg2;
            }
    
    
        }
    
    
    }
    void displayhealth(int playerhealth3)
    {
            printf("\nHealth: [%i] Status: ", playerhealth3);
            if(playerhealth3 <= 10)
            {
                printf("DANGEROUSLY LOW\n\n", playerhealth3);
            }
            if(playerhealth3 >= 11 && playerhealth3 < 30)
            {
                printf("LOW\n\n", playerhealth3);
            }
            if(playerhealth3 >= 30 && playerhealth3 < 50)
            {
                printf("MODERATE\n\n", playerhealth3);
            }
            if(playerhealth3 >= 50 && playerhealth3 <= 75)
            {
                printf("GOOD\n\n");
            }
            if(playerhealth3 > 75 && playerhealth3 <= 100)
            {
                printf("GREAT\n\n");
            }
    
    
    }

    Need help on my first Text Adventure game-error-jpg

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Put a space before the %c in your scanf to stop the double-printing (which is caused by the newline in the input). The space before the %c tells scanf to skip all whitespace before reading a letter.
    Code:
        scanf(" %c", &pinput2);
    Change your while loop condition in spiderfight from || to &&. With || is keeps going as long as either player or spider are > 1. With && it only continues if both are > 1, and stops if either of them is <= 1.
    Code:
    while(playerhealth2 > 1 || spider2 > 1)
    I don't understand why you're passing pinput into spiderfight. Just declare it locally to spiderfight.

    You can use a switch in spiderfight.
    Code:
        switch (toupper(pinput2)) { // toupper is prototyped in ctype.h
        case 'A':
            printf("Kevin used Punch!\n");
            spider2 -= punchdmg2;
            break;
        case 'B':
            // ...
            break;
        // ...
        }
    Note the use of toupper. You can use that function elsewhere so you don't have to compare input to both upper and lowercase.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help With My Text Adventure Game?
    By evilcubed in forum C Programming
    Replies: 20
    Last Post: 12-02-2012, 11:08 PM
  2. Text adventure game GUI
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 11-07-2007, 06:34 PM
  3. Help with my text adventure game.
    By Haggarduser in forum Game Programming
    Replies: 15
    Last Post: 10-26-2007, 01:53 AM
  4. text adventure game
    By linucksrox in forum Game Programming
    Replies: 18
    Last Post: 07-27-2006, 01:36 PM
  5. Please help (Text Adventure Game)
    By C++angel in forum C++ Programming
    Replies: 9
    Last Post: 02-23-2006, 04:33 PM