I'm working on a simple D&D combat game that lets the player choose from 4 character types with different stats (attack, defense, armor, hit points). I have a parent class "character", and then 4 child classes for each of the character types. I want to ask the player which character they'd like to play as, and then create a new object based on their selection. How can I achieve this properly without running into scoping problems? Here's what I wish I could do:
1 2 3 4 5 6 7 8 9 10 11 12 13
cin >> characterSelection;
switch(characterSelection)
{
case 1:
Goblin player;
break;
case 2:
Barbarian player;
break;
case 3:
// etc...
}
I just want a way to assign the correct stats for the player in a way that will allow me to use these numbers throughout the game. Thanks in advance for any advice!
Why not create a profile for each possible class and assign values to that?
I assume you will want to give them an additional bonus when they level, maybe a penalty in some cases as well.
Are you suggesting just having one class for all character types, and using the switch statement to assign the proper values to its members? If that's what you mean, the reason I can't do this is because this is a school assignment dealing with inheritance, and it requires me to use a base class for all the child classes.
Since you said you're using inheritance, you'll want to create a instance of the derived class for eac selection.
1 2 3 4 5 6 7 8 9 10 11 12 13
Character * player; // pointer to polymorphic base class
switch(characterSelection)
{
case 1:
player = new Goblin; // derived from Character
break;
case 2:
player = new Barbarian; // derived from Character
break;
case 3:
// etc...
}