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!
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 *******//
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;
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
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;
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
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.
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.
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?
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)
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.
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.
This post has been edited by SixOfEleven: 1 Nov, 2009 - 01:50 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.
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
/*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.
Sorry, my C is a little rusty. 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?
Sorry, my C is a little rusty. 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.