Пример #1
0
        public FieldProcessor(int width, int height, DnaProcessor dnaProcessor)
        {
            _dnaProcessor = dnaProcessor;
            _creatures    = dnaProcessor.GetCreatures();
            _random       = dnaProcessor.Random;
            _field        = new Field(width, height);

            _foodBuffer = 10000;

            foreach (var creature in _creatures)
            {
                int x;
                int y;
                do
                {
                    x = _random.Next(width);
                    y = _random.Next(height);
                } while (_field[x, y] != null);

                creature.X         = x;
                creature.Y         = y;
                creature.Rotation  = _random.Next(4) * 2;
                creature.FoodValue = 20;
                _field[x, y]       = creature;
                _foodBuffer       -= creature.FoodValue + Creature.CreatureSkinFoodValue;
            }

            FillFood();
            _creatureProcessor = new DnaInterpreter(_field);
        }
Пример #2
0
        public void SaveCreatures(IEnumerable <Creature> creatures)
        {
            var creatureRecords = creatures.Select(c => new CreatureRecord
            {
                Id       = Guid.NewGuid(),
                ParentId = c.Parent,
                Dna      = DnaInterpreter.Encode(DnaInterpreter.Processors, c.Dna)
            }).ToList();

            _storageController.Save(creatureRecords);
        }
Пример #3
0
        private Creature CreateCreature(CreatureRecord creatureRecord, int mutations, List <IProcessor> processors)
        {
            var dna = creatureRecord.Dna.Take(RedirectProcessor.DnaLength).ToList();

            while (dna.Count < RedirectProcessor.DnaLength)
            {
                dna.Add(0);
            }
            for (int i = 0; i < mutations; i++)
            {
                var index = Random.Next(RedirectProcessor.DnaLength);
                dna[index] = Random.Next(DnaInterpreter.TotalCommands);
            }

            return(new Creature
            {
                Parent = creatureRecord.Id,
                Dna = DnaInterpreter.Decode(processors, dna)
            });
        }
Пример #4
0
        private CreatureRecord[] CreateBase()
        {
            var dna = new int[RedirectProcessor.DnaLength];

            DnaInterpreter.Encode(DnaInterpreter.Processors, DnaInterpreter.DefaultDnaDecrypted).CopyTo(dna, 0);

            var record = new CreatureRecord
            {
                Id       = Guid.NewGuid(),
                ParentId = null,
                Dna      = dna
            };
            var records = new CreatureRecord[DnaProcessor.GenerationSetCount];

            for (var i = 0; i < DnaProcessor.GenerationSetCount; i++)
            {
                records[i] = record;
            }

            return(records);
        }