I don't really need to go into detail about what i'm making but its basically a text based fight game that will be in a loop until one person dies.
Now, theres an option to use a weapon or not and so clearly different sets of moves are available. What I wanted to ask was would it be bad practice if I had a switch statment with two difference cases which were more or less identical accept they access diffrent move sets(probabbly a class) or is there a better way to do this?
Btw the case argument would be a private class variable to check if a weapon is equiped.
Perhaps an if statement to check the variable which then calls one of two functions?
It's not bad practice, it all depends on the paradigm. It's standard practice in procedural programming.
If you're using objects, you can delegate specialised behaviour to the objects. So weapon specific actions can be done in specific weapons. In my example, they'd do it int he Action method.
If your characters were objects, your characters could have a current weapon which would be null if not equipped or point to the weapon currently selected.
e.g.
1 2 3 4 5
void Character::UseWeapon()
{
if (m_pCurrentWeapon)
m_pCurrentWeapon->Action();
}
while(fighter1.get_health()>0 && fighter2.get_health()>0)
{
std::cout << "============================ Move " << i << " ============================\n\n";
std::cout << fighter1.get_name() << " attacks " << fighter2.get_name() << " with " << fighter2.get_attackStrength()
<< " damage and leaves his health as ";
fighter2.get_damageDone()<0 ? std::cout << "0\n" : std::cout << fighter2.get_damageDone() << "\n";
fighter2.new_health();
Sleep(1000);
if(!fighter2.get_health()) zeroHealth = true;
if(zeroHealth) break;
std::cout << fighter2.get_name() << " attacks " << fighter1.get_name() << " with " << fighter1.get_attackStrength()
<< " damage and leaves his health as ";
fighter1.get_damageDone()<0 ? std::cout << "0\n" : std::cout << fighter1.get_damageDone() << "\n";
fighter1.new_health();
Sleep(1000);
std::cout << "\n================================================================\n\n\n";
i++;
}
This is the code i was using in the first verson (jsmith you'll probably remember helping me). This is one attack with power decided, within boundaries, by a seeded rand(). What I will go on to code will be a more detailed version of this where the user will be able to choose between hand-to-hand, or armed with one of two weapons. (won't be able to change mid-fight).
So back to my original question, would it be stupid to do a switch statement where most of that code is the same except the attacks the player will call would be different.