Exemple #1
0
/**
 * Generate a being from a couple
 */
unique_ptr<Being> BeingFactory::generateBeing(Being & firstParent, Being & secondParent)
{
    if (dynamic_cast<Gender &>(*(firstParent.getBehavior("gender"))).get() == dynamic_cast<Gender &>(*(secondParent.getBehavior("gender"))).get()) {
        throw logic_error("Parents share the same gender");
    }

    map<string, vector<string> > mixedChromosomes = map<string, vector<string> >();

    for (
        map<string, vector<string> >::iterator chromosomeTypeIterator = my_chromosomes.begin();
        chromosomeTypeIterator != my_chromosomes.end();
        ++chromosomeTypeIterator
    ) {
        mixedChromosomes[chromosomeTypeIterator->first].push_back(
            ((firstParent.getChromosomes())[chromosomeTypeIterator->first])[rand() % 2]
        );
        mixedChromosomes[chromosomeTypeIterator->first].push_back(
            ((secondParent.getChromosomes())[chromosomeTypeIterator->first])[rand() % 2]
        );
    }

    unique_ptr<Being> child = move(generateBeing(mixedChromosomes));

    child->addParent(firstParent);
    child->addParent(secondParent);
    firstParent.addChild(*child);
    secondParent.addChild(*child);

    return child;
}