#include #include "team.h" extern double drand48 (); extern const int NUM_PACMEN; const int Team::GHOSTS_PER_TEAM = 4; const int Team::MUTATION_CHANCE = 50; const int Team::COPY_CHANCE = 10; Team::Team () { fitness = 0; myGhost = new Ghost[GHOSTS_PER_TEAM]; } void Team::outputPrograms (ostream& out /* = cout */) { int i; for(i=0; i< GHOSTS_PER_TEAM; i++) { out << "Code for ghost " << i << endl; myGhost[i].outputProgram(out); out << endl; } } void Team::moveTeam () { int i; for(i=0; i< GHOSTS_PER_TEAM; i++) { myGhost[i].move(); } } void Team::newGame () { int i; for(i=0; i< GHOSTS_PER_TEAM; i++) { myGhost[i].newGame(); } } void Team::increaseFitness (unsigned long amt) { fitness += amt; } unsigned long Team::getFitness () { return fitness; } void Team::newGeneration () { fitness = 0; int i; for(i=0; i< GHOSTS_PER_TEAM; i++) { myGhost[i].newGeneration(); } } void Team::copy (Team& parent) { int i; for(i=0; i < GHOSTS_PER_TEAM; i++) { myGhost[i].copy(parent.myGhost[i]); } } void Team::crossover (Team& mom, Team& dad) { int i; for(i=0; i < GHOSTS_PER_TEAM; i++) { if (int(MUTATION_CHANCE * drand48()) == 0) { if (coinFlip()) myGhost[i].mutate(mom.chooseGhost()); else myGhost[i].mutate(dad.chooseGhost()); } else if (int(COPY_CHANCE * drand48()) == 0) { if (coinFlip()) myGhost[i].copy(mom.chooseGhost()); else myGhost[i].copy(dad.chooseGhost()); } else { if (coinFlip()) myGhost[i].crossover(mom.chooseGhost(), dad.chooseGhost()); else myGhost[i].crossover(dad.chooseGhost(), mom.chooseGhost()); } } } Ghost& Team::chooseGhost () { return myGhost[int(drand48() * GHOSTS_PER_TEAM)]; }