using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Weasels
{
class Weasel : Organism, Asexual, Sexual
{
public const string TARGET = "methinks it is like a weasel";
public const int GENOME_LENGTH = 28;
public const int MUATION_PERIOD = GENOME_LENGTH;
public Char[] genome;
public Weasel() : this(RandomGenome()) { }
public Weasel(Char[] genome) : base()
{
this.genome = genome;
}
public static Char[] RandomGenome()
{
Char[] genome = new Char[GENOME_LENGTH];
Random r = new Random();
for(int i = 0; i < GENOME_LENGTH; i++)
{
int val = r.Next(95) + 32;
genome[i] = (char)val;
}
return genome;
}
public override int Fitness()
{
int sim = 0;
for(int i = 0; i < GENOME_LENGTH; i++)
{
sim += (genome[i] == TARGET[i]) ? 1 : 0;
}
return sim;
}
public override Organism Mutate()
{
Random r = new Random();
double frequency = 1.0 / MUATION_PERIOD;
char[] genome = this.genome.Select(
c => (r.NextDouble() < frequency) ? (char)(r.Next(95) + 32) : c
).ToArray();
return new Weasel(genome);
}
public Asexual Reproduce()
{
return (Asexual) Mutate();
}
public Sexual Reproduce(Sexual otherParent)
{
Random r = new Random();
char[] childGenome = new char[GENOME_LENGTH];
for(int i = 0; i < GENOME_LENGTH; i++)
{
childGenome[i] = (r.NextDouble() < 0.5) ? this.genome[i] : ((Weasel)otherParent).genome[i];
}
return (Sexual) new Weasel(childGenome).Mutate();
}
}
}