Ejemplo n.º 1
0
        public static DnaNode[] Decode(IList <IProcessor> processors, IList <int> dna)
        {
            DnaNode[] commands = new DnaNode[dna.Count];
            for (int i = 0; i < dna.Count; i++)
            {
                var value = dna[i];
                int index = 0;
                while (true)
                {
                    if (index >= processors.Count)
                    {
                        index = 0;
                        value = 0;
                        break;
                    }
                    if (value < processors[index].Length)
                    {
                        break;
                    }

                    value -= processors[index].Length;
                    index++;
                }
                commands[i] = new DnaNode
                {
                    LocalCommand = value,
                    Processor    = processors[index]
                };
            }
            return(commands);
        }
Ejemplo n.º 2
0
        public Movement GetMovement(Creature creature)
        {
            ProcessingCreature processingCreature = new ProcessingCreature(creature, TotalCommands, _field);

            while (processingCreature.NotProcessed())
            {
                var dnaNode = processingCreature.ProcessingIndex >= processingCreature.Dna.Length
                    ? DnaNode.Create <MoveProcessor>(0)
                    : processingCreature.Dna[processingCreature.ProcessingIndex];
                var command = dnaNode.Processor.Process(dnaNode.LocalCommand, processingCreature);
                if (command != null)
                {
                    return(command.Movement);
                }
            }
            // Infinitive loop
            return(MoveProcessor.NullMovement(processingCreature.Direction));
        }