Public Group

# RPG Battle Function

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

## 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 on other sites
Figured I would add code incase anyone wanted it. I just threw this together to experiment with the more integers option.
 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"; }

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.
 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 }
Passing variables into function, I made this work! I believe I answered my own question... sorry forum

##### 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.
 // 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(); 

You could even setup all the info ahead of time.
 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; 

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.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631782
• Total Posts
3002324
×