Azbia - a simple RPG game code

This is a discussion on Azbia - a simple RPG game code within the Game Programming forums, part of the General Programming Boards category; #include <stdlib.h> /*-Azbia-*/ #include <stdio.h> #include <time.h> #include <math.h> void map1 (); /*This is an rpg as I think it*/ ...

  1. #1
    Unregistered
    Guest

    Azbia - a simple RPG game code

    #include <stdlib.h> /*-Azbia-*/
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    void map1 (); /*This is an rpg as I think it*/
    void map2 (); /*should be, not a maximum yet*/
    void map3 (); /*the texts are rather random*/
    void map4 (); /*but it's stable, not a fake*/
    struct enemystruct {char name [15]; int hp; int ap;
    int wh; int gold; int run;};
    enemystruct e [6] = {
    {"snake", 50, 5, 10, 40, 1},
    {"orc", 100, 0, 40, 142, 0},
    {"gnome", 149, 24, 40, 300, 1},
    {"troll", 333, 33, 33, 333, 1},
    {"centaur", 500, 4, 75, 1000000, 0},
    {"zombie", 0, 0, 0, 0, 1}};
    struct weaponstruct {char name [15]; int damage;
    int price;};
    weaponstruct w [3] = {
    {"long sword", 200, 400},
    {"short sword", 100, 200},
    {"dagger", 75, 100}};
    typedef void (*maptype) ();
    struct placestruct {char name [30]; maptype map;};
    placestruct p [4] = {
    {"mountain", &map1},
    {"forest", &map2},
    {"desert", &map3},
    {"town", &map4}};
    int gold = 0, hp = 100, mhp = 100, armor = 10,
    weapon = 25, run, damage, valid, next, ehp, n, i;
    char wname [20] = "mace";

    void choice (int *n, int max)
    {
    if (!max) printf ("Where do you want to go?\n");
    for (i = 0, *n = 0; i < 4 && !max; i++)
    printf ("[%i] %s\n", i + 1, p [i].name);
    if (!max) max = 4;
    scanf ("%i", n);
    fflush (stdin);
    if (*n > 0 && *n <= max) valid = 1;
    }

    void printstat ()
    {
    printf ("Statistics:\narmor: %i\n", armor);
    printf ("weapon name: %s\n", wname);
    printf ("weapon damage: %i\n", weapon);
    printf ("hp: %i/%i\n", hp, mhp);
    printf ("gold: %i\n", gold);
    }

    void pressenter ()
    {
    printf ("\n%*s[ Press enter to continue ]", 24, " ");
    getchar ();
    fflush (stdin);
    }

    void battle (int m)
    {
    run = 0;
    ehp = e [m].hp;
    while (ehp > 0 && hp > 0 && (run < 13 || !e [m].run))
    {
    for (valid = 0; !valid; choice (&n, 3))
    {
    system ("cls");
    printf ("You are in combat with: %s\n", e [m].name);
    printf ("enemy hp:%s%i%syour hp:%s%i/%i\n",
    "\t", ehp, "\t", "\t", hp, mhp);
    printf ("enemy armor:%s%i%syour armor:"
    "%s%i\n", "\t", e [m].ap, "\t", "\t", armor);
    printf ("enemy weapon%s%i%syour weapon:%s%i\n\n",
    "\t", e [m].wh, "\t", "\t", weapon);
    printf ("[1] attack\n[2] run\n[3] stats\n");
    }
    if (n == 1 && hp > 0)
    {
    damage = weapon - e [m].ap / 4;
    printf ("You hit for %i damage\n\n", damage);
    if ((ehp -= damage) < 1)
    {
    printf ("You have killed the enemy\n");
    printf ("You gain: %i gold\n", e [m].gold);
    gold += e [m].gold;
    hp += 5;
    mhp += 5;
    }
    }
    if (n == 1 && ehp > 0)
    {
    damage = e [m].wh - armor / 4;
    printf ("Enemy hits for %i damage\n", damage);
    if ((hp -= damage) < 1) printf ("You LOST!\n");
    }
    if (n == 2 && (run = rand () % 26) > 12 && e [m].run)
    printf ("\nYou have sucessfully run away\n");
    if (n == 2 && (run < 13 || !e [m].run))
    printf ("\nYou failed to run away\n");
    if (n == 3) printstat ();
    pressenter ();
    }
    }

    void heal (int choice)
    {
    if (gold < 51) printf ("You don't have enough gold\n");
    if (gold > 50) printf ("HP is full: %i\n", hp = mhp);
    if (gold > 50) gold -= 50;
    if (n == choice) pressenter ();
    }

    void weaponsstore ()
    {
    for (valid = 0; !valid; choice (&n, 3))
    {
    system ("cls");
    for (i = 0, printf ("Weapons store\n"); i < 3; i++)
    printf ("[%i] %s%s-- Max Damage: %i%s-- "
    "Price: %i\n", i + 1, w [i].name, "\t",
    w [i].damage, "\t", w [i].price);
    printf ("\nWhich one would you like?\n");
    }
    if (gold < w [--n].price)
    printf ("You don't have enough gold\n");
    if (gold >= w [n].price)
    printf ("You buy the best one the %s\n", w [n].name);
    if (gold >= w [n].price) weapon = w [n].damage;
    if (gold >= w [n].price) strcpy (wname, w [n].name);
    printstat ();
    pressenter ();
    }

    void map1 ()
    {
    system ("cls");
    printf ("A lot of evil ones live on this mountain,\n");
    printf ("local people once worshiped them as gods.\n");
    pressenter ();
    weaponsstore ();
    battle (2);
    battle (3);
    if (hp < 1) return;
    system ("cls");
    printf ("You met a healer and he prepared you "
    "to face your final enemy.\n");
    pressenter ();
    hp = mhp;
    battle (4);
    if (hp < 1) return;
    system ("cls");
    printf ("Evil has been defeated in Azbia, "
    "but your work is not finished yet...\n");
    pressenter ();
    hp = 0;
    }

    void map2 ()
    {
    for (valid = 0; !valid; choice (&n, 3))
    {
    system ("cls");
    printf ("It's silent in this forest, there don't "
    "seem to be any enemies\n");
    printf ("Where do you want to go?\n[1] healer "
    "(50 gold)\n[2] weapons\n[3] town\n");
    }
    if (n == 1) heal (1);
    if (n == 2) weaponsstore ();
    if (n == 3) next = 4;
    if (n == 3) return;
    for (valid = 0; !valid; choice (&n, 0)) system ("cls");
    next = n;
    }

    void map3 ()
    {
    system ("cls");
    printf ("There are a lot of little creatures under "
    "the sand in this desert,\n");
    printf ("the biggest one of these bites you.\n");
    pressenter ();
    battle (0);
    battle (1);
    if (hp < 1) return;
    for (valid = 0; !valid; choice (&n, 0)) system ("cls");
    next = n;
    }

    void map4 ()
    {
    for (n = 1; hp > 0 && (n == 1 || n == 3)
    {
    e [5].hp = rand () % 26 + 30;
    e [5].ap = rand () % 26;
    e [5].gold = rand () % 26 + 30;
    e [5].wh = rand () % 26 + 12;
    for (battle (5), valid = 0; !valid; choice (&n, 3))
    {
    system ("cls");
    printf ("Do you want to battle some more?\n");
    printf ("[1] yes\n[2] no\n[3] heal\n");
    }
    if (n == 3) heal (3);
    }
    if (hp < 1) return;
    for (valid = 0; !valid; choice (&n, 0)) system ("cls");
    next = n;
    }

    int main ()
    {
    srand (time (NULL));
    printf ("When you arrive at Azbia, you see a desolate"
    " place\n");
    printf ("so that even all these supernatural "
    "creatures don't seem to be so unnatural...\n\n");
    printf ("Welcome to Azbia!\n");
    pressenter ();
    for (valid = 0; !valid; choice (&n, 0)) system ("cls");
    next = n;
    while (hp > 0) if (hp > 0) (*(p [next - 1].map)) ();
    }

  2. #2
    Unregistered
    Guest
    Please replace the smiling face in the beginning of map 4 with ; )

  3. #3
    Registered User compjinx's Avatar
    Join Date
    Aug 2001
    Posts
    214
    One word: Code Tags

    C1: thats two words

    Author: who cares?
    "The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
    Eric Porterfield.

  4. #4
    Unregistered
    Guest
    In c all type definitions and functions are code tags, so what about them?

  5. #5
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    use [ code] before your code and [ /code] after it.
    Without the spaces.

  6. #6
    Unregistered
    Guest
    Thank you! Didn't notice this little "vb code" link below.

  7. #7
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    Thanks for posting that, I'm making an RPG and I'm looking for ideas to make the code better. I like the way you put all the monsters and weapons in structures.

    napKIN

  8. #8
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    First of all. Try making one function instead of 4 for rooms which takes arguments for actions.
    It will be a hell of a lot easier to add rooms later if you succeed.
    And try killing the magic numbers and variable names:

    Code:
    for (i = 0, *n = 0; i < 4 && !max; i++) 
    printf ("[%i] %s\n", i + 1, p [i].name); 
    if (!max) max = 4; 
    scanf ("%i", n); 
    fflush (stdin); 
    if (*n > 0 && *n <= max) valid = 1; 
    }
    And for the enemy struct array define the numbers for the array items:

    Code:
    //Instead of 
    //battle (2); 
    //battle (3); 
    //
    //use defines
    
    #define ORC 2
    #define GNOME 3
    
    battle(ORC);
    battle(GNOME);

    EDIT::

    >Please replace the smiling face in the beginning of map 4
    >with ; )

    Cant you just edit it?
    Last edited by ErionD; 05-03-2002 at 01:49 PM.

  9. #9
    Unregistered
    Guest
    Code:
    Yes, as I said, it's not a maximum yet. You can use one function for all rooms if you have array of texts with room names where they shall be written, also you may add monsters to the room structure etc, but I think that separate funtions for some rooms would still be necessary in order to add some additional action.

  10. #10
    Unregistered
    Guest
    Code:
    Yes, as I said, it's not maximum yet. You can use one function for 
    all rooms if you have array of texts with room names where they 
    shall be written, also you may add monsters to the room 
    structure etc, but I think that separate funtions for some rooms 
    would still be necessary in order to add some additional action.

  11. #11
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    May i ask why you use code tags on non-code text

  12. #12
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    if you like tha code napkin, you're really gonna like mine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  2. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  3. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  4. A simple game using RNG
    By amirahasanen1 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 11:05 AM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 06:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21