Thread: text rpg problem

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    17

    text rpg problem

    Code:
    here it is, kind of long
    
    Code:
    
    #include<stdio.h>
    #include<stdlib.h>
    
    //attack function
    int attackm(int,int,int,int,int,int,int,int,int,int);
    
    //menu function
    int menu(void);
    
    //stats function
    void stats(int,int,int,int,int,int,int);
    
    //continue function
    void cont(void);
    
    main(){
           //name
           char name[20];
           //class scan
           int class1,class2;
           //1st classes
           int warrior,archer,thief,mage,priest;
           //2nd classes
           int knight,sniper,assassain,wizard,pope;
           //gold
           int gold=0;
           //attack
           int attack;
           //weapons
           int sword=20,bow=15,dagger=12,staff=5;
           //def
           int armor=0;
           //armor
           int mail=50,leather=30,cloth=10;
           //exp
           int exp=0;
           //hp
           int hp=0;
           int chp=0;
           //mp
           int mp=0;
           int cmp=0;
           //***Town Menus***//
           //radan
           int radan=0,fradan;
           //**********Enemys******************//
           //dire wolf
           int dwhp=80,dwmp=0,dwatck=10,dwdef=0,dwexp=35;
           
           //ask for name
           printf("What is your name?\n");
           scanf("%s",name);
           system("CLS");
           
           //ask for class
           printf("What class would you like to learn in?\n");
           printf("1.Warrior\n");
           printf("2.Archer\n");
           printf("3.Thief\n");
           printf("4.Mage\n");
           printf("5.Priest\n");
           scanf("%d",&class1);
           
           //class switch
           switch(class1){
                          case 1:
                               printf("You are now a Warrior\n");
                               hp=100;
                               chp=100;
                               mp=10;
                               cmp=10;
                               printf("You recieve a Sword\n");
                               attack=sword;
                               printf("You recieve Mail\n");
                               armor=armor+mail;
                               printf("You recieve 500 gold\n");
                               gold=gold+500;
                               break;
                          case 2:
                               printf("You are now an Archer\n");     
                               hp=80;
                               chp=80;
                               mp=25;
                               cmp=25;
                               printf("You recieve a Bow\n");
                               attack=bow;
                               printf("You recieve Leather\n");
                               armor=armor+leather;
                               printf("You recieve 500 gold\n");
                               gold=gold+500;
                               break;
                          case 3:
                               printf("You are now a Thief\n");
                               hp=70;
                               chp=70;
                               mp=30;
                               cmp=30;
                               printf("You recieve a Dagger\n");
                               attack=dagger;
                               printf("You recieve Leather\n");
                               armor=armor+leather;
                               printf("You recieve 500 gold\n");
                               gold=gold+500;
                               break;
                          case 4:
                               printf("You are now a Mage\n");
                               hp=60;
                               chp=60;
                               mp=90;
                               cmp=90;
                               printf("You recieve a Staff\n");
                               attack=staff;
                               printf("You recieve Cloth\n");
                               armor=armor+cloth;
                               printf("You recieve 500 gold\n");
                               gold=gold+500;
                               break;
                          case 5:
                               printf("You are now a Priest\n");
                               hp=50;
                               chp=50;
                               mp=100;
                               cmp=100;
                               printf("You recieve a Staff\n");
                               attack=staff;
                               printf("You recieve Cloth\n");
                               armor=armor+cloth;
                               printf("You recieve 500 gold\n");
                               gold=gold+500;
                               break;
                               }
           
           cont();
           
           //radan
           printf("You enter the town of Radan\n");
           //radan menu
           radan=menu();
           //radan menu switch
           while(radan==menu()){
           switch(radan){
                        case 1:
                             system("CLS");
                             printf("1.Dire Wolf\n");
                             printf("2.Bear\n");
                             scanf("%d",&fradan);
                             
                             switch(fradan){
                                            case 1:
                                                 attackm(chp,cmp,attack,armor,exp,dwhp,dwmp,dwatck,  dwdef,dwexp);
                                                 break;
                                                 }
                        case 5:
                             stats(hp,mp,attack,armor,exp,chp,cmp);
                             break;
                             }
           }
                               
           return 0;
           }
           
    //attack function
    int attackm(int hp,int mp,int atck,int def,int exp,int ehp,int emp,int eatck,int edef,int eexp){
        int amenu=0;
    while(amenu==0){
        system("CLS");
        printf("1.Attack\n");
        printf("2.Magic\n");
        printf("3.Items\n");
        printf("4.Run (roll to run)\n");
        scanf("%d",&amenu);
    }
    
        
        if(ehp<=0){
                   printf("You win!\n");
                   printf("You gain %d EXP\n!",eexp);
                   exp=exp+eexp;
                   cont();
                   }
        
        switch(amenu){
                      case 1:
                           system("CLS");
                           ehp=ehp-atck;
                           if(ehp<=0){
                                      ehp=0;
                                      }
                           printf("You do %d damage, enemy has %d HP left\n",atck,ehp);
                           hp=hp-eatck;
                           printf("You take %d damage, you have %d HP left\n",eatck,hp);
                           cont();
                           amenu=0;
                           break;
                           }
                                    
    }
    
    //menu function
    int menu(void){
         int menu;
         printf("1.Fight\n");
         printf("2.Shop\n");
         printf("3.Inn\n");
         printf("4.Inventory\n");
         printf("5.Stats\n");
         printf("6.Head to next area (look out for monsters on the way)\n");
         scanf("%d",&menu);
            
         return menu;
         }
    
    //stats function
    void stats(int fhp,int fmp,int fattack,int fdefense,int fexp,int chp,int cmp){
                           system("CLS");
                           printf("%d/%d HP\n",chp,fhp);
                           printf("%d/%d MP\n",cmp,fmp);
                           printf("%d Armor\n",fdefense);
                           printf("%d Attack\n",fattack);
                           printf("%d EXP\n",fexp);
                           
                           cont();
                           }
    //cont funcion
    void cont(void){
         printf("\nHit any key to continue(sometimes you have to hit it twice)\n");
         getchar();
         getchar();
         system("CLS");
         }



    some probles i am having are:

    *pretty much the whole radan menu, when i run it at first and make a selection, it asks again, then it works after the second time you type in the selection. when you go back after going to stats from that, you cant go into fight, just stats again.

    *when you go into fight, then finish attacking, it will show the stats menu, then close

    *the reason i have while(radan==menu()) is so that it continues going through the switch statement and takes you back to the menu so you can make more selections, apparently that dosnt work

    thanks in advance for the help, please point out anything else that is wrong if you can

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Stop cross-posting. It won't expedite your answer.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Your menu prints twice because you call it twice in a row, as your comments indicate.
    You do not need the first radan=menu();
    Code:
    //radan menu
    radan=menu();
    //radan menu switch
    while(radan==menu()){ //while what was chosen the first time equals new choice
    This is what you want for your while loop
    Code:
    while(radan=menu())
    Though you might want to add the menu option 0 to Quit

    (sometimes you have to hit it twice)
    This is because scanf leaves the newline character (from pressing enter) in the buffer each time it's called.
    The easy fix is to call getchar() after each scanf to clear the newline.

    Or, preferrably, use fgets to get a line of input then parse the line. This is more difficult. If you choose to attempt it, I'd start small with a new program, and once you've got it figured out use it in this program.
    Last edited by spydoor; 12-09-2005 at 09:14 AM.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    thanks spy, ill look into that. what you said fixed the menu problem, but now i have one when using the attack menu. after i attack it takes me to the stats menu instead of back to the attack menu so there is the option to attack again. any ideas?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Work on your indention, it's already difficult follow what's going on. Bugs like these will become harder and harder to find.
    Code:
            switch(radan)
            {
                case 1:
                    printf("1.Dire Wolf\n");
                    printf("2.Bear\n");
                    scanf("%d",&fradan);
                    getchar();
                    switch(fradan)
                    {
                        case 1:
                            attackm(chp,cmp,attack,armor,exp,dwhp,dwmp,dwatck,    dwdef,dwexp);
                            break;
                    }
                    break;
                case 5:
                    stats(hp,mp,attack,armor,exp,chp,cmp);
                    break;
            }

    And since you are going to need to learn about pointers anyway, as you are trying to modify values with attackm...Here's an example

    Code:
        switch(class1){
            case 1:
                initWarrior(&hp, &chp, &mp, &cmp, sword, &attack, mail, &armor, &gold);
                break;

    Code:
    void initWarrior(int *hp, int *chp, int *mp, int *cmp, int weapon, int *attack, int item, int *armor, int *gold)
    {
        printf("You are now a Warrior\n");
    
        *hp=100;
        *chp=100;
        *mp=10;
        *cmp=10;
    
        printf("You recieve a Sword\n");
        *attack=weapon;
    
        printf("You recieve Mail\n");
        *armor=*armor+item;
    
        printf("You recieve 500 gold\n");
        *gold=*gold+500;
    }
    It looks like you've got pretty big plans for this. You really sould looke into using structs, enums, macros, pointers.

    remember to break as many things into sub routines as you can, reusable one's if possible. For example you could make a single initialzeCharacterClass() function.

    With the path your on it will become extremely difficult to maintain and update your code.
    Last edited by spydoor; 12-09-2005 at 01:19 PM.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    Alright, ill look into those. Thanks a lot for all the help. I use dev-c++ which did the spacing for me tho

    last thing I dont know how to do. Ive tryed it in various games and cant figure it out. Is there an easy way to get it to keep cycling through the attack menu so you can keep fighting the enemy and not having it reset after each turn?
    Last edited by xxwerdxx; 12-10-2005 at 09:47 AM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Put it in a loop until someone loses.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    ...or runs away, depending on if you have the implemented.


    Not Quzah.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    cant believe i didnt think of that,thanks. now im having a problem returning values in the attack function. do I need to use pointers for that or something? trying to return hp,mp,and exp.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Either use pointers, or roll them all up in a structure, and return that.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    This is why things like RPGs are better taken with an Object-oriented approach.
    Sent from my iPadŽ

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    void cont(void){
         printf("\nHit any key to continue(sometimes you have to hit it twice)\n");
         getchar();
         getchar();
         system("CLS");
         }
    ->
    Code:
    void cont(void) {
        int c;
    
        printf("\nHit <enter> key to continue\n");
        while((c = getchar()) != '\n' && c != EOF);
    
        system("CLS");  /* non-standard . . . see the FAQ */
    }
    (fixes the "sometimes twice".)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    Heres what i got using pointers, and it still dosn't display it in the stats screen after the fight. I looked into structues also, but didn't understand them so i tryed this. Any ideas of what I did wrong now



    Code:
    //attack function
    int attackm(int hp,int mp,int atck,int def,int exp,int ehp,int emp,int eatck,int edef,int eexp){
        int amenu;
        //pointers
        int *php;
        int *pmp;
        int *pexp;
        //pointer storages
        php=&hp;
        pmp=&mp;
        pexp=&exp;
        //attack menu
    while(ehp>0&&hp>0){
        system("CLS");
        printf("1.Attack\n");
        printf("2.Magic\n");
        printf("3.Items\n");
        printf("4.Run (roll to run)\n");
        scanf("%d",&amenu);
    
    
        
    
        
        switch(amenu){
                      case 1:
                           system("CLS");
                           ehp=ehp-atck;
                           if(ehp<=0){
                                         ehp=0;
                                         }
                           printf("You do %d damage, enemy has %d HP left\n",atck,ehp);
                           hp=hp-eatck;
                           printf("You take %d damage, you have %d HP left\n",eatck,hp);
                           cont();
                           break;
                           }
                           
                           if(ehp<=0){
                                         printf("You win!\n");
                                         printf("You gain %d EXP\n!",eexp);
                                         exp=exp+eexp;
                                         cont();
                                         }
    
    }
         return (*php,*pmp,*pexp);
    }

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Structures are just bundles of variables grouped by a name of your choice. Like so:
    Code:
    struct foo
    {
        int anint;
        char achar;
        float afloaty;
    };
    Here we have defined the structure foo. Just like 'int', this is a 'struct foo'.

    Now let's declare an instance of one, and play with it:
    Code:
    struct foo astructy;
    int x;
    Just like we made x an int, we made astructy an instance of a struct foo. Now to access each piece of it:
    Code:
    x = 5;
    astructy.anint = x;
    astructy.achar = 'c';
    astructy.afloaty = 1.2;
    There we've accessed each portion of our structure one at a time. Nice and simple. If you have a pointer, instead of . you use the arrow:
    Code:
    struct foo *aptrtoastructy;
    
    aptrtoastructy = &astructy;
    aptrtoastructy->anint = 5;
    Simple, eh?


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    17
    thanks, that clears up a lot about structures, but i dont know how i would use that to return the values of the attackmenu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with text cursor in C
    By sureshkumarct in forum C Programming
    Replies: 3
    Last Post: 12-22-2006, 12:17 AM
  2. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  4. Text Rpg Of Courseee
    By Paninaro in forum Game Programming
    Replies: 6
    Last Post: 06-24-2002, 01:54 PM
  5. my text based RPG!
    By Leeman_s in forum Game Programming
    Replies: 11
    Last Post: 05-13-2002, 08:24 AM