I've been working on a game theory simulation for a bio class.
The way it works is the following:
Code: Select all
class Agent
{
unsigned int Score; //starts at 0
std::vector<...> Memory; //(I need it to remember every meeting it has had: Both with whom (so a pointer to an Agent) and also what the other // opposing Agent decided to do in this
public:
bool Decision(Agent& agent); //This is where the specific strategies will come into play: Depending on the strategy, it will use its past //knowledge and a reference to its opponent to figure out what decision it will take. True = cooperate, false = Defect
void Update(Agent& agent, and the choice of opposing agent); //This will update Memory with new information after the meeting has //happened)
}
Code: Select all
class AllD : public Agent
{
bool Decision(Agent& agent) { return false}; //shitty example as it doesn't care what his opponent is, but you get the deal
void Update etc.
}
Code: Select all
class TFT : public Agent{
};
class Agent{
};
std::vector<std::unique_ptr<Agent>> agents;
agents.push_back( std::make_unique<TFT>() );
Code: Select all
class Agent{
public:
using Strategy = std::variant<Coop, Defect, TFT>;
template<typename Strat> Agent( Strat&& strat );
};
std::vector<Agent> agents;
agents.emplace_back( TFT{} );
Code: Select all
class Agent {
public:
// The = 0 at the end simply means that the Agent class doesn't have a default behavior,
// so child classes ( derived classes ) are required to define it.
virtual void Update( Agent* other_agent ) = 0;
};
class TFT : public Agent{
public:
// Here, override at the end tells the compiler that TFT has defined a function that
// Agent declared as pure virtual ( the = 0 thing ).
void Update( Agent* other_agent )override{
MakeDecision( other_agent );
}
private:
void MakeDecision( Agent const* other_agent );
};
Code: Select all
std::vector<Agent*> agents;
agents.push_back(new AllD);
agents.push_back(new Defector);
agents.push_back(new Something(5));
Code: Select all
for (size_t i = 0; i < agents.size(); i++)
{
delete agents[i];
}
Code: Select all
// Forward declarations
class TFT;
class Defect;
class Coop;
class Agent {
public:
virtual void Meet( Agent* other ) = 0;
virtual void Meet( TFT* other ) = 0;
virtual void Meet( Defect* other ) = 0;
virtual void Meet( Coop* other ) = 0;
private:
};
class TFT : public Agent{
public:
void Meet( Agent* other )override {
// This will call the Agent::Meet( TFT* other ) virtual function
// What ever child class 'other' is, will have to define this encounter as well
other->Meet( this );
}
void Meet( TFT* other )override{
// TFT meets another TFT
}
void Meet( Defect* other ) override{
// TFT meets a Defect
}
void Meet( Coop* other ) override {
// TFT meets a Coop
}
};