School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,149 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,706 people online right now. Registration is fast and FREE... Join Now!




Txt Based RPG

 

Txt Based RPG, Working with structures and pointers in C language.

RE_Dyvone

1 Nov, 2009 - 12:17 PM
Post #1

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
I'm in a group for a school project, and we are making a game (like most of the other groups in our class.) The only language all of us have in common is standard C. I know PHP, C, and VB. Our game is a text based RPG, and for it, my roll is storing the data, and making functions that pass pointers to any function that needs it.
Right now, I have a sample of what I'm trying to do. My only problem is, I suck with structures and pointers. Don't know how I got stuck with it, I wanted to do the inventory/stat menus.

CODE

//********
RPGheadertest.cpp : Text Based RPG testing
By: TDR
*******//

#include "stdafx.h"
#include "statsnitems.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"

int check( int &p_lvl , int &p_xp , int &p_maxXp , int &p_hitPoints , int &p_maxHitPoints , int &p_str , int &p_ac , int &p_thac0 , int &p_dex , int p_agi ); /* Prototype */

int main()
{
    int checklvl = 0;
    printf("starting check for lvl up\n");
    
    int checklvl = check( );

    return 0;
}

int check ( int &p_lvl , int &p_xp , int &p_maxXp , int &p_hitPoints , int &p_maxHitPoints , int &p_str , int &p_ac , int &p_thac0 , int &p_dex , int p_agi )
{
    struct hero stats;

    if ( p_xp >= p_maxXp )
    {
        printf("Your hero has gained a level!\n");
        p_lvl = p_lvl + 1;
        p_maxHitPoints = p_maxHitPoints + 1;
        p_str = p_str + 3;
        p_ac = p_ac + 1;
        p_dex = p_dex + 2;
        p_agi = p_agi + 1;
        p_maxXp = p_maxXp + 75;

        printf(" Player's level is now = %d\n Maximum Hitpoints is %d\n Player's strength is now %d\n", p_lvl, p_maxHitPoints, p_str );
    }
    else
        printf("Player has gained xp!\n");
        p_xp = p_xp + 1;

return;
}


I also have the stat's and items header file included.

CODE

    /*The Potions usable by the character. Initial quanitity set to 0*/
    int healthPotion = 0;
    int lifePotion = 0;

    /*Weapon Stats*/
    struct rustySword {
        int wpnMinDam = 1;
        int wpnMaxDam = 4;
        int invQuantity = 1;
    };
    struct bronzeSword {
        int wpnMinDam = 3;
        int wpnMaxDam = 7;
        int invQuantity = 0;
    };
    struct ironSword {
        int wpnMinDam = 6;
        int wpnMaxDam = 10;
        int invQuantity = 0;
    };
    struct steelSword {
        int wpnMinDam = 9;
        int wpnMaxDam = 13;
        int invQuantity = 0;
    };
    struct mithrilSword {
        int wpnMinDam = 12;
        int wpnMaxDam = 16;
        int invQuantity = 0;
    };
    

    /*The player's stats.*/
    struct hero {
        int p_lvl = 1;
        int p_maxXP = 50; /*when lvl increases, max xp needed will raise by 75*/
        int p_XP = 0;
        int p_hitPoints = 50;
        int p_maxHitPoints = 50;
        int p_str = 5; /* players strength */
        int p_ac = 2; /* players armor class */
        int p_dex = 3; /* players dexterity */
        int p_thac0 = 4; /* players chance to hit */
        int p_agi = 3; /* players agility */
        int p_baseDamage = 2; /* players base damage without a weapon */
    };

    /*Enemy List*/
    struct rat {
        static const int e_lvl = 2; /*Enemy lvl times 2-5 will give xp gained.*/
        static const int e_hitPoints = 25;
        static const int e_maxHitPoints = 25;
        static const int e_minDam = 1;
        static const int e_maxDam = 3;
        static const int e_str = 5; /* enemy's strength */
        static const int e_ac = 2; /* enemy's armor class */
        static const int e_dex = 3; /* enemy's dexterity */
        static const int e_thac0 = 4; /* enemy's chance to hit */
        static const int e_agi = 3; /* enemy's agility */
    };
    struct snake {
        static const int e_lvl = 2; /*Enemy lvl times 2-5 will give xp gained.*/
        static const int e_hitPoints = 30;
        static const int e_maxHitPoints = 30;
        static const int e_minDam = 2;
        static const int e_maxDam = 7;
        static const int e_str = 5;
        static const int e_ac = 2;
        static const int e_dex = 3;
        static const int e_thac0 = 4;
        static const int e_agi = 3;
    };
    struct boar {
        static const int e_lvl = 3; /*Enemy lvl times 2-5 will give xp gained.*/
        static const int e_hitPoints = 40;
        static const int e_maxHitPoints = 40;
        static const int e_minDam = 8;
        static const int e_maxDam = 15;
        static const int e_str = 5;
        static const int e_ac = 2;
        static const int e_dex = 3;
        static const int e_thac0 = 4;
        static const int e_agi = 3;
    };
    struct goblin {
        static const int e_lvl = 4; /*Enemy lvl times 2-5 will give xp gained.*/
        static const int e_hitPoints = 50;
        static const int e_maxHitPoints = 50;
        static const int e_minDam = 6;
        static const int e_maxDam = 20;
        static const int e_str = 5;
        static const int e_ac = 2;
        static const int e_dex = 3;
        static const int e_thac0 = 4;
        static const int e_agi = 3;
    };


Can someone either help me find what I'm doing wrong. Or was this doom to fail to begin with.

This post has been edited by RE_Dyvone: 1 Nov, 2009 - 12:41 PM

User is offlineProfile CardPM
+Quote Post


RE_Dyvone

RE: Txt Based RPG

1 Nov, 2009 - 12:23 PM
Post #2

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
Oops. Gave you the one with too many errors from trying to fix it.. Hahaha.
CODE

int main()
{
    printf("starting check for lvl up\n");
    
    checklvl( );

    return 0;
}

int checklvl ( int &p_lvl , int &p_xp , int &p_maxXp , int &p_hitPoints , int &p_maxHitPoints , int &p_str , int &p_ac , int &p_thac0 , int &p_dex , int p_agi )
{
    struct hero stats;

    if ( p_xp >= p_maxXp )
    {
        printf("Your hero has gained a level!\n");
        p_lvl = p_lvl + 1;
        p_maxHitPoints = p_maxHitPoints + 1;
        p_str = p_str + 3;
        p_ac = p_ac + 1;
        p_dex = p_dex + 2;
        p_agi = p_agi + 1;
        p_maxXp = p_maxXp + 75;

        printf(" Player's level is now = %d\n Maximum Hitpoints is %d\n Player's strength is now %d\n", p_lvl, p_maxHitPoints, p_str );
    }
    else
        printf("Player has gained xp!\n");
        p_xp = p_xp + 1;

return 1;
}


this is my compile error.
error C2660: 'checklvl' : function does not take 0 arguments

This post has been edited by RE_Dyvone: 1 Nov, 2009 - 12:25 PM
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Txt Based RPG

1 Nov, 2009 - 12:41 PM
Post #3

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,066



Thanked: 171 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
You are going about this the wrong way. What you would want to do is make a struct for the different types of items and set the data for those items. For example. You would want to create a struct for weapons as follows.

CODE

struct Weapon {
    char name[33];
    int minDamage;
    int maxDamage;
}


Now what you would do is create a variable to hold an instance of Weapon for a rusty sword for example.

CODE

struct Weapon rustySword;

strcpy_s(rustySword.name, "Rusty Sword");
rustySword.minWeaponDamage = 1;
rustySword.maxWeaponDamage = 4;


User is online!Profile CardPM
+Quote Post

RE_Dyvone

RE: Txt Based RPG

1 Nov, 2009 - 12:58 PM
Post #4

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
QUOTE(SixOfEleven @ 1 Nov, 2009 - 12:41 PM) *

You are going about this the wrong way. What you would want to do is make a struct for the different types of items and set the data for those items. For example. You would want to create a struct for weapons as follows.

CODE

struct Weapon {
    char name[33];
    int minDamage;
    int maxDamage;
}


Now what you would do is create a variable to hold an instance of Weapon for a rusty sword for example.

CODE

struct Weapon rustySword;

strcpy_s(rustySword.name, "Rusty Sword");
rustySword.minWeaponDamage = 1;
rustySword.maxWeaponDamage = 4;



Thanks that makes more sense. I tried asking my professor about it on friday, but he shrugged me off and told me to ask my team members, who couldn't help me. But what's wrong with my function call? What argument am I suppose to pass it?
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Txt Based RPG

1 Nov, 2009 - 01:19 PM
Post #5

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,066



Thanked: 171 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
What you would want to do for this is create a variable of type struct hero in your main method. Then for the prototype of your method you would write it using a pointer like this.

CODE

int check(struct hero* theHero);


To actually call the method you would pass the variable using the &.

CODE

int main()
{
    struct hero theHero;

    check(&theHero);
}


Now to use the pointer inside of the method you would use the -> operator to access the fields of the struct.

CODE

int check(struct herp* theHero)
{
    if (theHero->p_XP > theHero->p_MaxXP)

}

User is online!Profile CardPM
+Quote Post

RE_Dyvone

RE: Txt Based RPG

1 Nov, 2009 - 01:28 PM
Post #6

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
I got it working! Its starting to come back to me. I really appreciate your help. Didn't think anyone would be willing to help me here, since people I actually knew didn't. Thanks for sparing time for me.
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Txt Based RPG

1 Nov, 2009 - 01:30 PM
Post #7

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,066



Thanked: 171 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
You will find a lot of people here willing to help you out. smile.gif
User is online!Profile CardPM
+Quote Post

RE_Dyvone

RE: Txt Based RPG

1 Nov, 2009 - 01:41 PM
Post #8

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
QUOTE(SixOfEleven @ 1 Nov, 2009 - 01:30 PM) *

You will find a lot of people here willing to help you out. smile.gif


I do have one last question. Have you ever seen a txt based RPG done in C/ ever made one?
User is offlineProfile CardPM
+Quote Post

Gorian

RE: Txt Based RPG

1 Nov, 2009 - 01:46 PM
Post #9

ninja DIC
Group Icon

Joined: 28 Jun, 2008
Posts: 712



Thanked: 9 times
Dream Kudos: 125
My Contributions
Ever heard of a MUD (Multi User Dungeon)? There are basically Multi-Player Text based RPGs

http://en.wikipedia.org/wiki/MUD
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Txt Based RPG

1 Nov, 2009 - 01:49 PM
Post #10

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,066



Thanked: 171 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
I have seen several text based RPG games made with C. There where many back in the 90s. I have written them before. I even made one that could be played over a modem on a BBS. A BBS is a bulletin board system that was popular back in the late 80s and early 90s before the Internet started to get popular. People would have dedicated phone lines that others would call to connect to the BBS and play online games, read messages, etc. A famous text based RPG BBS game was called LORD: Legend of the Red Dragon by Seth Able.

*EDIT*
I feel old now. tongue.gif

This post has been edited by SixOfEleven: 1 Nov, 2009 - 01:50 PM
User is online!Profile CardPM
+Quote Post

RE_Dyvone

RE: Txt Based RPG

1 Nov, 2009 - 03:12 PM
Post #11

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
QUOTE(SixOfEleven @ 1 Nov, 2009 - 01:49 PM) *

I have seen several text based RPG games made with C. There where many back in the 90s. I have written them before. I even made one that could be played over a modem on a BBS. A BBS is a bulletin board system that was popular back in the late 80s and early 90s before the Internet started to get popular. People would have dedicated phone lines that others would call to connect to the BBS and play online games, read messages, etc. A famous text based RPG BBS game was called LORD: Legend of the Red Dragon by Seth Able.

*EDIT*
I feel old now. tongue.gif


One of my group members has played this kinda stuff. He's been programming for 20 years I think and was finally able to afford to go to school. But he assumed that since he knows all this stuff by heart, everyone else does. I from the age of forgetfulness. Guess that's a problem with being a 90s kid, lol.

This post has been edited by RE_Dyvone: 1 Nov, 2009 - 03:13 PM
User is offlineProfile CardPM
+Quote Post

RE_Dyvone

RE: Txt Based RPG

2 Nov, 2009 - 06:09 PM
Post #12

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
I now have a new problem. Figuring out how to pass structure variables through a function. Here's my new code.

CODE

// RPGheadertest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "statsnitems.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"

struct potion healingPotion = {"Lesser Healing Potion", 25, 1};
struct potion lifePotion = {"Greater Healing Potion", 50, 0};

struct weapon rustySword = {"Rusty Sword", 1, 4};
struct weapon bronzeSword = {"Bronze Sword", 3, 6};
struct weapon ironSword = {"Iron Sword", 5, 10};
struct weapon steelSword = {"Steel Sword", 7, 12};
struct weapon mithrilSword = {"Mithril Sword", 10, 15};

struct player Hero = {1, 50, 0, 50, 50, 5, 5, 5, 7, 8, 5};

struct enemy Rat = {"Giant Rat", 1, 25, 25, 2, 5, 3, 3, 3, 4, 1};
struct enemy Snake = {"Venomous Snake", 1, 20, 20, 4, 6, 2, 1, 5, 5, 5};
struct enemy Boar = {"Rabid Boar", 2, 40, 40, 3, 8, 5, 3, 1, 2, 1};

int main()
{

    printf( "%s\n", rustySword.w_name );
    printf( "%s\n", healingPotion.i_name);

    return 0;
}


and my revised header file.

CODE


    /*The Potions usable by the character. Initial quanitity set to 0*/
    struct potion {
        char i_name[40];
        int restoreTotal;
        int onhandQuantity;
    };

    /*Weapon Stats*/
    struct weapon {
        char w_name[33];
        int minWeaponDamage;
        int maxWeaponDamage;
    };
    
    /*The player's stats.*/
    struct player {
        int p_lvl;
        int p_maxXP; /*when lvl increases, max xp needed will raise by 75*/
        int p_XP;
        int p_hitPoints;
        int p_maxHitPoints;
        int p_str; /* players strength */
        int p_ac; /* players armor class */
        int p_dex; /* players dexterity */
        int p_thac0; /* players chance to hit */
        int p_agi; /* players agility */
        int p_baseDamage; /* players base damage without a weapon */
    };

    /*Enemy List*/
    struct enemy {
        char e_name[30];
        int e_lvl; /*Enemy lvl times 5-10 will give xp gained.*/
        int e_hitPoints;
        int e_maxHitPoints;
        int e_minDam;
        int e_maxDam;
        int e_str; /* enemy's strength */
        int e_ac; /* enemy's armor class */
        int e_dex; /* enemy's dexterity */
        int e_thac0; /* enemy's chance to hit */
        int e_agi; /* enemy's agility */
    };


I want to remake my function for checking to see if the player has leveled up. I'm going to test it by setting both xp values to the same thing then comparing them against each other.
User is offlineProfile CardPM
+Quote Post

RE_Dyvone

RE: Txt Based RPG

2 Nov, 2009 - 06:43 PM
Post #13

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
NEVERMIND! I MADE IT WORK! I MADE IT WORK! I'M NOT AS STUPID AS I THOUGHT!!!!
CODE

int check(struct player* Hero);

int main()
{
    struct player Hero;

    printf( "%s\n", rustySword.w_name );
    printf( "%s\n", healingPotion.i_name);

    check(&Hero);

    return 0;
}

int check(struct player* Hero)
{

    if(Hero->p_XP >= Hero->p_maxXP)
        printf("Yatta!");

    return 0;
}


This post has been edited by RE_Dyvone: 2 Nov, 2009 - 06:46 PM
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Txt Based RPG

2 Nov, 2009 - 06:50 PM
Post #14

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,066



Thanked: 171 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
To pass a structure to a function you need to declare the function with a struct parameter as a pointer. Like this:

CODE

void checkLevel(struct player* Hero);


To call this function in your code you would use & to pass the address of the variable Hero to the function.

CODE

int main()
{
    checkLevel(&Hero);
}


To access the members of the struct you would need to use -> instead of .

CODE

void checkLevel(struct player* Hero)
{
    if (Hero->p_XP >= Hero->p_maxXP)
    {
        Hero->p_lvl++;
    }
}


We must have both posted at the same time. confused.gif
User is online!Profile CardPM
+Quote Post

RE_Dyvone

RE: Txt Based RPG

2 Nov, 2009 - 07:34 PM
Post #15

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
QUOTE(SixOfEleven @ 2 Nov, 2009 - 06:50 PM) *

We must have both posted at the same time. confused.gif


Lol sorry. When I try to print now though it prints the pointer's location. Not what's inside it. I used this statement.

CODE

    printf("Level %d\n", Hero->p_lvl);

User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Txt Based RPG

3 Nov, 2009 - 06:20 AM
Post #16

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,066



Thanked: 171 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
QUOTE(RE_Dyvone @ 2 Nov, 2009 - 09:34 PM) *

QUOTE(SixOfEleven @ 2 Nov, 2009 - 06:50 PM) *

We must have both posted at the same time. confused.gif


Lol sorry. When I try to print now though it prints the pointer's location. Not what's inside it. I used this statement.

CODE

    printf("Level %d\n", Hero->p_lvl);



What you are doing is printing out the address. If you want to print the value you need to put a * in front of it.

CODE

printf("Level %d\n", *Hero->p_lvl);


That should print what you need.
User is online!Profile CardPM
+Quote Post

RE_Dyvone

RE: Txt Based RPG

3 Nov, 2009 - 07:19 PM
Post #17

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
That gives me an error.

error C2100: illegal indirection
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Txt Based RPG

3 Nov, 2009 - 08:15 PM
Post #18

lives.ToCode();
Group Icon

Joined: 18 Oct, 2008
Posts: 3,066



Thanked: 171 times
Dream Kudos: 775
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
Sorry, my C is a little rusty. smile.gif However, I tried this small sample program and it printed out the proper results. Could you post the code for the method and where you are calling it from?

CODE

#include "string.h"
#include "stdio.h"
#include "stdlib.h"

struct Weapon {
    char name[33];
    int minWeaponDamage;
    int maxWeaponDamage;
};

void WriteWeapon(struct Weapon* weapon);
int main()
{
    struct Weapon rustySword;

    strcpy_s(rustySword.name, "Rusty Sword");
    rustySword.minWeaponDamage = 1;
    rustySword.maxWeaponDamage = 4;
    WriteWeapon(&rustySword);
    return 0;
}

void WriteWeapon(struct Weapon* weapon)
{
    printf("%s %d %d", weapon->name, weapon->minWeaponDamage, weapon->maxWeaponDamage);
}


It prints:
CODE

Rusty Sword 1 4

User is online!Profile CardPM
+Quote Post

RE_Dyvone

RE: Txt Based RPG

3 Nov, 2009 - 08:33 PM
Post #19

New D.I.C Head
*

Joined: 20 Oct, 2009
Posts: 13


My Contributions
QUOTE(SixOfEleven @ 3 Nov, 2009 - 08:15 PM) *

Sorry, my C is a little rusty. smile.gif However, I tried this small sample program and it printed out the proper results. Could you post the code for the method and where you are calling it from?



No, your C isn't rusty. I'm just an idiot. I was initializing structures in the header. Not in the functions. So basically my stuff was pointing to nothing. But it works now.
CODE

/***********************************************************************

    

      RPGheadertest.cpp   A simple test to run test the header file and
      the functions that will use them.

      Inputs:     Structure definitions from Header file

      Outputs:    None



      Written by: Terrence Ray                         Date: 10-29-09



************************************************************************/

#include "stdafx.h"
#include "statsnitems.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"


void check(struct player* Hero);
void stats(struct player* Hero);

int main()
{
    struct potion healingPotion = {"Lesser Healing Potion", 25, 2};
    struct potion lifePotion = {"Greater Healing Potion", 50, 0};

    struct weapon rustySword = {"Rusty Sword", 1, 4};
    struct weapon bronzeSword = {"Bronze Sword", 3, 6};
    struct weapon ironSword = {"Iron Sword", 5, 10};
    struct weapon steelSword = {"Steel Sword", 7, 12};
    struct weapon mithrilSword = {"Mithril Sword", 10, 15};

    struct player Hero = {1, 5, 50, 50, 50, 5, 5, 5, 7, 8, 5};

    struct enemy Rat = {"Giant Rat", 1, 25, 25, 2, 5, 3, 3, 3, 4, 1};
    struct enemy Snake = {"Venomous Snake", 1, 20, 20, 4, 6, 2, 1, 5, 5, 5};
    struct enemy Boar = {"Rabid Boar", 2, 40, 40, 3, 8, 5, 3, 1, 2, 1};


    printf( "%s\n", rustySword.w_name );
    printf( "%s\n", healingPotion.i_name);

    stats(&Hero);
    check(&Hero);


    return 0;
}

void check(struct player* Hero)
{    
    if(Hero->p_XP >= Hero->p_maxXP)
        printf("Yatta!");
    else
    {
        printf("%d of %d from leveling up.", Hero->p_XP, Hero->p_maxXP);
    }
}

void stats(struct player* Hero)
{
    printf("Level %d\n", Hero->p_lvl);
    printf("%d Hitpoints\n", Hero->p_hitPoints);
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:17PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month