/* Copyright (C) 2007 Andre Seidelt, All Rights Reserved. This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ /* * the genome of the cell. */ class Genome { // fill colors of the cell private RangeSequence mBodyRed; private RangeSequence mBodyGreen; private RangeSequence mBodyBlue; // width of outline private RangeSequence mLineWidth; // line color of the outline private RangeSequence mLineRed; private RangeSequence mLineGreen; private RangeSequence mLineBlue; // the shape of the cell private SetSequence mShape; // the size of the cell private RangeSequence mSize; // the maximum age of the cell private RangeSequence mAge; // reproduction rate of the cell private RangeSequence mReproductionRate; // ammount of food this cell needs to 'age normaly' private RangeSequence mFoodDemand; // number of mutations to parent genome private int mMutations; // .ctor() public Genome() { mBodyRed = new RangeSequence(MIN_COLOR, MAX_COLOR); mBodyGreen = new RangeSequence(MIN_COLOR, MAX_COLOR); mBodyBlue = new RangeSequence(MIN_COLOR, MAX_COLOR); mLineWidth = new RangeSequence(MIN_WIDTH, MAX_WIDTH); mLineRed = new RangeSequence(MIN_COLOR, MAX_COLOR); mLineGreen = new RangeSequence(MIN_COLOR, MAX_COLOR); mLineBlue = new RangeSequence(MIN_COLOR, MAX_COLOR); mShape = new SetSequence(new int[] { TRIANGLE, CIRCLE, QUADRAT, ELLIPSEH, RECTANGULARH, ELLIPSEV, RECTANGULARV } ); mSize = new RangeSequence(MIN_SIZE, MAX_SIZE); mAge = new RangeSequence(MIN_AGE, MAX_AGE); mReproductionRate = new RangeSequence(MIN_REP_RATE, MAX_REP_RATE); mFoodDemand = new RangeSequence(MIN_FOOD, MAX_FOOD); mMutations = 0; } // .ctor(): create from parent genome, change with given mutation rate public Genome(Genome pParent) { // cells with big borders mutate slower int rate = MUT_RATE - pParent.mLineWidth.mValue; MutationCount cnt = new MutationCount(); mBodyRed = pParent.mBodyRed.reproduce(cnt, rate); mBodyGreen = pParent.mBodyGreen.reproduce(cnt, rate); mBodyBlue = pParent.mBodyBlue.reproduce(cnt, rate); mLineWidth = pParent.mLineWidth.reproduce(cnt, rate); mLineRed = pParent.mLineRed.reproduce(cnt, rate); mLineGreen = pParent.mLineGreen.reproduce(cnt, rate); mLineBlue = pParent.mLineBlue.reproduce(cnt, rate); mShape = pParent.mShape.reproduce(cnt, rate); mSize = pParent.mSize.reproduce(cnt, rate); mAge = pParent.mAge.reproduce(cnt, rate); mReproductionRate = pParent.mReproductionRate.reproduce(cnt, rate); mFoodDemand = pParent.mFoodDemand.reproduce(cnt, rate); mMutations = cnt.count; } // display genome as string public String toString() { return "b"+hex(mBodyRed.mValue, 2)+hex(mBodyGreen.mValue, 2)+hex(mBodyBlue.mValue, 2)+ "/l"+hex(mLineRed.mValue, 2)+hex(mLineGreen.mValue, 2)+hex(mLineBlue.mValue, 2)+ "/s"+nf(mSize.mValue, 2)+ "/w"+mLineWidth.mValue+ "/f"+mShape.mValue+ "/a"+nf(mAge.mValue, 3)+ "/r"+nf(mReproductionRate.mValue, 3)+ "/d"+nf(mFoodDemand.mValue, 3); } } // helper class to get multiple return values class MutationCount { int count = 0; }