本帖最后由 cphj 于 2010-06-02 12:49:49 编辑

解决方案 »

  1.   

    === Nano/Animal.cpp ===
    @@ -39,6 +39,11 @@
         return energy > 0;
     }
     
    +bool Animal::IsFull() const
    +{
    +    return energy >= SATURATION_ENERGY;
    +}
    +
     void Animal::Accelerate(const long & x, const long & y)
     {
         double velprod = vel * vel;
    === Nano/Animal.h ===
    @@ -9,8 +9,6 @@
     private:
         static const size_t LAYERS[];
     
    -    static const double SATURATION_ENERGY;
    -
         static const double DRAG_COEFFICIENT;
     
         static long width;
    @@ -24,6 +22,8 @@
         math_vector<2> pos;
         math_vector<2> vel;
     
    +    static const double SATURATION_ENERGY;
    +
         double energy;
     
         static void SetBound(long width, long height);
    @@ -33,6 +33,7 @@
         void Create();
     
         bool IsAlive() const;
    +    bool IsFull() const;
     
         void Accelerate(const long & x, const long & y);
         void Move();
      

  2.   

    === Nano/Ecology.cpp ===
    @@ -26,30 +26,30 @@
     void Ecology::Create()
     {
         plants.resize(width, height);
    -    for (size_t i=1; i<SATURATION_PLANTS_N/2; i++)
    +    for (size_t i=0; i<SATURATION_PLANTS_N/2; i++)
             plants.insert(RandRange(0, width - 1), RandRange(0, height - 1), Plant());
     
         Animal::PreCreate();
         animals.resize(SATURATION_ANIMALS_N / 2);
    -    animals_n = SATURATION_ANIMALS_N / 2;
    -    for (size_t i=0; i<animals_n; i++)
    +    for (size_t i=0; i<animals.size(); i++)
             animals[i].Create();
     }
     
     void Ecology::DrawStill(HDC hdc)
     {
    -    for (size_t i=1; i<plants.count()+1; i++)
    +    for (size_t i=0; i<plants.size(); i++)
             SetPixel(hdc, plants.index2coordx[i], plants.index2coordy[i], RGB(0, 128, 0));
     }
     
     void Ecology::DrawMotive(HDC hdc)
     {
    -    for (size_t i=0; i<animals_n; i++)
    +    for (size_t i=0; i<animals.size(); i++)
         {
             if (!animals[i].IsAlive())
             {
    -            SetPixel(hdc, long(animals[i].pos.v[0]), long(animals[i].pos.v[1]), RGB(255, 0, 0));
    -            continue;
    +            SetPixel(hdc, long(animals[i].pos.v[0]), long(animals[i].pos.v[1]), RGB(0, 0, 0));
    +            animals[i] = animals[animals.size() - 1];
    +            animals.pop_back();
             }
     
             animals[i].Accelerate(width / 2, height / 2);
    @@ -59,9 +59,14 @@
             animals[i].Move();
             SetPixel(hdc, long(animals[i].pos.v[0]), long(animals[i].pos.v[1]), RGB(255, 255, 255));
     
    -        if (animals[i].energy >= 1000)
    +        if (animals[i].IsFull())
             {
    -
    +            animals.push_back(animals[i]);
    +            animals.back().brain.Mutate();
    +            animals.back().vel.v[0] = 0;
    +            animals.back().vel.v[1] = 0;
    +            animals[i].energy -= Animal::SATURATION_ENERGY / 2;
    +            animals.back().energy = Animal::SATURATION_ENERGY / 2;
             }
         }
     }
      

  3.   

    @@ -76,9 +81,9 @@
         if (abs(animal.vel.v[0]) < 1 && abs(animal.vel.v[1]) < 1)
         {
             CheckBound(x, y);
    -        if (plants.coord2index[long(x)][long(y)] > 0)
    +        if (plants.coord2index[long(x)][long(y)] != plants.npos)
             {
    -            plants.remove(long(x), long(y));
    +            plants.erase(long(x), long(y));
                 animal.energy += 100.0;
             }
             return;
    @@ -91,10 +96,10 @@
             for (long i=0; i<n; i++)
             {
                 CheckBound(x, y);
    -            if (plants.coord2index[long(x)][long(y)] > 0)
    +            if (plants.coord2index[long(x)][long(y)] != plants.npos)
                 {
                     SetPixel(hdc, long(x), long(y), RGB(0, 0, 0));
    -                plants.remove(long(x), long(y));
    +                plants.erase(long(x), long(y));
                     animal.energy += 100.0;
                 }
                 x++;
    @@ -108,10 +113,10 @@
             for (long i=0; i<n; i++)
             {
                 CheckBound(x, y);
    -            if (plants.coord2index[long(x)][long(y)] > 0)
    +            if (plants.coord2index[long(x)][long(y)] != plants.npos)
                 {
                     SetPixel(hdc, long(x), long(y), RGB(0, 0, 0));
    -                plants.remove(long(x), long(y));
    +                plants.erase(long(x), long(y));
                     animal.energy += 100.0;
                 }
                 x+=k;
    @@ -134,12 +139,13 @@
     
     void Ecology::DrawGrowth(HDC hdc)
     {
    -    double y = double(plants.count()) / SATURATION_PLANTS_N;
    +    double y = double(plants.size()) / SATURATION_PLANTS_N;
         double growprob = y * (1 - y);
    +    size_t ind;
         if (RandRange(0.0, 1.0) < growprob)
         {
    -        plants.insert(RandRange(0, width - 1), RandRange(0, height - 1), Plant());
    -        SetPixel(hdc, plants.index2coordx[plants.count()], plants.index2coordy[plants.count()], RGB(0, 128, 0));
    +        ind = plants.insert(RandRange(0, width - 1), RandRange(0, height - 1), Plant());
    +        SetPixel(hdc, plants.index2coordx[ind], plants.index2coordy[ind], RGB(0, 128, 0));
         }
     }
     
    @@ -159,6 +165,6 @@
         swprintf_s(str, L"Max Velocity: %8.2f ", sqrt(it->vel * it->vel));
         TextOut(hdc, 10, height + 50, str, wcslen(str));
     
    -    swprintf_s(str, L"Plants: %4u / %4u ", plants.count(), SATURATION_PLANTS_N);
    +    swprintf_s(str, L"Plants: %4u / %4u ", plants.size(), SATURATION_PLANTS_N);
         TextOut(hdc, width / 2 + 10, height + 10, str, wcslen(str));
     }
    === Nano/Ecology.h ===
    @@ -23,8 +23,6 @@
         coord_index<Plant> plants;
         vector<Animal> animals;
     
    -    size_t animals_n;
    -
         size_t bestid;
     
         vector<double> bestscore;