Not sure if this is even possible, but I am writing a card game hearts where I want a base class called Player that will hold all the variables/vectors for each instance of the class. The game would have three computer players and one user, total four players. I want a derived class called PassThreeCards. This class will need to access the values that were set for each instance of the base class Player. I know that i could create an instance of the derived class PassThreeCards and then create a pointer of the class type Player that points to the address of the instance of the class PassThreeCards and then can access the functions in the base class to be set for each of the instances of the derived class. Here is the problem: I need three other derived classes of the class PassThreeCards that need to inherit the values of the variables set within the base class to make determinations on that data. Here is an example program that I'm using to develop the the structure for the actual card game program.
#include<iostream>
usingnamespace std;
class Player
{
protected:
int x; //this variable would actually be a vector of the hand the player is dealt
public:
void setValueX(int a) //this function would actually set the values of multiple vectors holding
{ //the amount of each suit the hand has
x = a;
}
};
class ThreeCardPass: public Player
{
public:
string determinStrategy() //this would actually evaluate what is the best strategy
{ //with a lot more calculations
cout << "The value of X = " << x << endl;
if(x > 0 && x < 33)
return"Offensive";
elseif(x > 33 && x < 66)
return"Defensive";
elseif(x > 66 && x < 100)
return"RunHearts";
}
};
class Offensive: public ThreeCardPass //this class would need access of the original values
{ //for each instance of the base class
protected:
int passCard;
public:
Offensive()
{
passCard = x + 100;
}
};
int main()
{
/*not sure if what i want to do is even possible
I just want to be able to have multiple classes that are called
throughout the game for each of the three computer players that
act upon the values of their base class variables
*/
string strategy;
ThreeCardPass cardToPass;
cardToPass.setValueX(32);
strategy = cardToPass.determinStrategy();
if(strategy == "Offensive") //there would be three classes for strategy
{
Offensive *offenseStrategy = &cardToPass; //get an error on this line that says when compiling:
} //invalid conversion from ThreeCardPass to Offensive
return 0;
}
.
class Offensive: public ThreeCardPass {};
ThreeCardPass cardToPass;
Offensive *offenseStrategy = &cardToPass;
Error: ThreeCardPass is not an Offensive.
1 2 3 4 5 6 7 8 9 10 11 12 13
struct Foo { int f; };
struct Bar : public Foo { int b; };
Bar * p = new Bar; // ok
p->f; // ok
p->b; // ok
Foo * x = p; // ok, Bar IS-A Foo
x->f; // ok, Foo has f
x->b; // error, Foo does not have member b
Bar * y = new Foo; // error, Bar is not Foo
y->b; // error, interface of Bar has b, but object pointed to by y does not
Thanks for the help but, i know how to do polymorhism between two classes i want one instance of a class that inherits all base class variables and functions and then three different polymorhed class of the original instance of the inherited base class class.
EX:
class C inherits from A and B
when a function of one of the called functions from C (that inherited from B) returns a value to main an instance of class D , E or F of the pointer type points to the address of the instance of C. Using polymorhism to utilize the values of the variables of C to act upon that data to return some data to main which will act upon that data. It seems that when i do that, because i have written code that does the steps and compiles the problem is i get a whole new set of values for the instance of D,E, or F.
struct A {
void f() {}
};
struct X {
A * pa;
X( A * p ) pa(p) {}
void g() { pa->f(); }
};
struct Y {
A * pa;
Y( A * p ) pa(p) {}
void h() { pa->f(); }
};
// use
//
// two in the know
A a;
A b;
// three with attitude
X p0( &a );
Y p1( &a );
Y p2( &b );
// action
p0.g(); // calls a.f()
p1.h(); // calls a.f()
p2.h(); // calls b.f()
As mutexe said, It's not clear why this should be an inheritance relationship. In Hearts, passing three cards is an action a player does, so it would seem that it should be a method of Player, passing the player to whom the cards are being passed as an argument. How is ThreeCardPass a derivation of a player?
The difficulty here is that the player being passed the three cards can't add them to his hand until he's passed his three cards.
The only method of ThreeCardPass is determineStrategy. Not clear how that is related to passing three cards.
Having three derived classes according to strategy is certainly possible.
I would suggest eliminating the ThreeCardPass. In main, determine the strategy for the player, then create a object of the appropriate type and assign it to a Player pointer.
Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Player
{
protected:
int m_playertype; //0=human,1=offensive,2=defensive,3=neutral,4=runhearts
public:
Player (int playertype)
{ m_playertype = playertype; }
};
class Offensive : public Player (1) // playertype 1
{ ... }
main
{ Player * players[4];
// Create players of various types
players[0] = new Human; // Derives from player
players[1] = new Offensive;
... etc
}
I just want to know that once a class that has inherited attributes of a base class can i use polymorhism to create another instance of a different class than the two already linked and that one have access to the values set for the instance of the first instantiation of the class which inherited the attributes of the base class.
If i have a hand i might create a strategy for the Three Card Pass that would pass cards to help with a run hand, or to pass cards to help stop the pain of a bad dealt hand, or even a hand that could pass to help offensively to try to set up the hand to get the Jack of Diamonds.
I just want to know that once a class that has inherited attributes of a base class can i use polymorhism to create another instance of a different class than the two already linked and that one have access to the values set for the instance of the first instantiation of the class which inherited the attributes of the base class.
Yes. In my example instance of class X accesses instance of unrelated class A. A could have any number of base classes, and classes X and Y could share an interface too.
If i have a hand i might create a strategy for the Three Card Pass that would pass cards to help with a run hand
To that end, create a pure virtual function in Player for PassingThreeCards. Then in each of the derived classes, implement the PassingThreeCards function in a way appropriate to that strategy. You game logic would then call player[i]->PassThreeCards(). The version of PassThreeCards in the derived class would then be called.
@ne555 - I think I may be missing your point. What I suggested was not meant to the Strategy pattern you linked (it doesn't have the context class), but it does implement the interface (Player) and the concrete classes (Human, Offensive, etc). I was trying to keep this simple for the OP.
Was your point that my suggestion would not allow for adaptive strategy changes since the concrete class is selected at the time the players are created?
> Was your point that my suggestion would not allow for adaptive strategy changes
yes
> since the concrete class is selected at the time the players are created
since you say that the behaviour is the player. class Offensive : public Player (1) // playertype 1
> I was trying to keep this simple for the OP.
I don't see how the strategy pattern so much difficult.
Moreover, it was quite analyzed, so you can obtain an implementation and its consequences