Sign in to follow this  

RPG Battle Function

This topic is 2495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello All, I'm new to these forums so I should introduce myself I suppose, my name is Clark and I am 22 years old from Texas, USA. I began seriously programing 2/1/11 *not much experience =p*, I'm focusing on C++ for now with the goals of Freelance Game Development and hopefully an Education/Career in Software Engineering.

On to my Question.

I'm writing a basic Text based RPG to test my knowledge of C++ thus far.
The problem I'm having is now that I have a working battle function created, I'm trying to get it to take on different monsters health, not at the same time, just. Let me type some pseudo code.
Class Monster
int health
Monster.Goblin(20)
Monster.Orc(50)
Monster.Troll(100)

Up till this point I typed out 3 different functions entirely for each monster's battle.
But I'm now learning about pointer's and references and I believe that is the direction I need to go for this.

I desire a function called
Battle()
while (playerhealth > 0 && monsterhealth > 0)
also I need like
monsterhealth = (monsterhealth - pdamage)
etc, where monsterhealth is whichever monster the place choses to battle.

I am NOT asking someone to write this code for me. Please note that.

All I am asking is, do I do this with a pointer? a reference? another variable? or "->" I haven't learned this operator yet but I have seen it used in similar functions on a tutorial website.

Share this post


Link to post
Share on other sites
Figured I would add code incase anyone wanted it. I just threw this together to experiment with the more integers option.
[code]

cout << "What do you wish to fight?\n\n";
cout << "1. Goblin (Lv. 1)\n";
cout << "2. Orc (Lv. 2)\n";
cout << "3. Troll (Lv. 3)\n";
cin >> choice;
switch (choice)
{
case 1:
{
Monster goblin("Goblin",20,5,5);
goblin.Greet();
mhp = goblin.health;
mname = goblin.name;
Fight();
break;
}
case 2:
{
Monster orc("Orc",50,10,15);
orc.Greet();
mhp = orc.health;
mname = orc.name;
Fight();
break;
}
case 3:
{
Monster troll("Troll",100,15,50);
troll.Greet();
mhp = troll.health;
mname = troll.name;
Fight();
break;
}
}

break;
}
case 2:
{
cout << "This is town\n\n";
break;
}
}
}
void Fight()
{
// How to get different monsters health here based on what player chose...
cout << mname << " has " << mhp << " health.\n";
}[/code]

This code works, but is there a better more efficent way? Because as this is going I am ignoring scopes and working with alot of Global ints.

To prevent triple post I'm editing this 15 minutes later to show what I came up with.
[code]

case 1:
{
Monster goblin("Goblin",20,5,5);
goblin.Greet();
Fight(goblin.name, goblin.health);
break;
}
case 2:
{
Monster orc("Orc",50,10,15);
orc.Greet();
Fight(orc.name, orc.health);
break;
}
case 3:
{
Monster troll("Troll",100,15,50);
troll.Greet();
Fight(troll.name, troll.health);
break;
}
}

break;
}

}
}
void Fight(string mName, int mHealth)
{
cout << mName << " has " << mHealth << " health.\n"; // using passing variables
}[/code]
Passing variables into function, I made this work! I believe I answered my own question... sorry forum

Share this post


Link to post
Share on other sites
Not a bad start for neophyte, actually. There are some simplifications you could make, and learn some new constructs at the same time.

Rather than creating an opponent with all it's parameters and calling the same functions for every case, use the case statement just to select parameters.
[code]
// Pseudo-code
switch( user-choice)
{
case 0: setup-monster0-parameters; break;
case 1: setup-monster1-parameters; break;
case 2: setup-monster2-parameters; break;
}
Monster monster( monster-parameters );
monster.Greeting();
Fight();
[/code]

You could even setup all the info ahead of time.
[code]
struct monsterInfo
{
string name;
int health, armor, ammo;
};
monsterInfo info[3];
info[0].name = "orc";
info[0].heath = ...;
info[1].name = "something else";
info[1].heath = ...;
// additional setups

Monster monster; // one monster to re-use in a loop

// then
bool done = false;
while( !done )
{
get-user-choice;
monster.SetParemeters(info[ user-choice ]);
monster.Greeting();
Fight();
done = user-asks-for-another-round;
}
cout << "Hook 'em Horns!" << endl;
// or, alternatively
cout << "Go Bucks!" << endl;
[/code]

Lots of different ways to do it. When you want to get into STL (Standard Template Library), you can use std::vector<monsterInfo> to push_back the preconstructed parameters, as an excercise.

Share this post


Link to post
Share on other sites

This topic is 2495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this