Exemple #1
0
        private Image RenderTurn(ExperimentTurn turn)
        {
            var sqlConnection      = this.databaseConnector.OpenSqlConnection();
            var turnOrganismStates = sqlConnection.GetTurnOrganismStates(turn.Id).ToDictionary(state => state.Position);

            var bitmap   = new Bitmap(AlienSpaceVector.WorldWidth * CellSize, AlienSpaceVector.WorldHeight * CellSize);
            var graphics = Graphics.FromImage(bitmap);
            var sunBrush = new SolidBrush(Color.Yellow);

            for (var x = 0; x < AlienSpaceVector.WorldWidth; x++)
            {
                for (var y = 0; y < AlienSpaceVector.WorldHeight; y++)
                {
                    var position = new AlienSpaceVector(x, y);

                    var distanceFromSun = AlienSpaceVector.GetCoordinateDelta(position, turn.SunPosition).Abs();
                    if (distanceFromSun < Experiment.SunRadius)
                    {
                        graphics.FillRectangle(sunBrush, x * CellSize, y * CellSize, CellSize, CellSize);
                    }

                    if (turnOrganismStates.ContainsKey(position))
                    {
                        var state         = turnOrganismStates[position];
                        var organismBrush = new SolidBrush(Color.FromArgb(ScaleColorComponent(state.Red), ScaleColorComponent(state.Green), ScaleColorComponent(state.Blue)));
                        graphics.FillEllipse(organismBrush, x * CellSize, y * CellSize, CellSize, CellSize);
                    }
                }
            }
            return(bitmap);
        }
        public void CloseTogether()
        {
            var vector1 = new AlienSpaceVector(5, 5);
            var vector2 = new AlienSpaceVector(7, 3);

            var result1 = AlienSpaceVector.GetCoordinateDelta(vector1, vector2);

            this.Expect(result1.X, this.EqualTo(-2));
            this.Expect(result1.Y, this.EqualTo(2));

            var result2 = AlienSpaceVector.GetCoordinateDelta(vector2, vector1);

            this.Expect(result2.X, this.EqualTo(2));
            this.Expect(result2.Y, this.EqualTo(-2));
        }
        public void CatyCorner()
        {
            var vector1 = new AlienSpaceVector(0, 0);
            var vector2 = new AlienSpaceVector(63, 35);

            var result1 = AlienSpaceVector.GetCoordinateDelta(vector1, vector2);

            this.Expect(result1.X, this.EqualTo(1));
            this.Expect(result1.Y, this.EqualTo(1));

            var result2 = AlienSpaceVector.GetCoordinateDelta(vector2, vector1);

            this.Expect(result2.X, this.EqualTo(-1));
            this.Expect(result2.Y, this.EqualTo(-1));
        }
        public void SimulateSingleTurn(long experimentId)
        {
            if (!this.databaseConnector.DatabaseHasBeenPinged)
            {
                throw new InvalidOperationException("Cannot run simulation without having pinged the database.");
            }

            //try {
            using (var sqlConnection = this.databaseConnector.OpenSqlConnection()) {
                var experimentStatus = sqlConnection.GetExperimentStatus(experimentId);

                var existingOrganismStates = experimentStatus.LatestExperimentTurnId == null ? new OrganismState[0] : sqlConnection.GetTurnOrganismStates(experimentStatus.LatestExperimentTurnId.Value);

                var newExperimentTurn = BuildNextExperimentTurn(experimentStatus);
                var random            = new Random(newExperimentTurn.RandomSeed);

                var newOrganismStates = existingOrganismStates
                                        .ToDictionary(organismState => organismState.OrganismId,
                                                      existingOrganismState => new OrganismState {
                    OrganismId = existingOrganismState.OrganismId,
                    Position   = existingOrganismState.Position,
                    Health     = existingOrganismState.Health,
                });

                foreach (var existingOrganismState in newOrganismStates.Values.OrderBy(state => state.OrganismId).ToArray())
                {
                    // TODO: Think first
                    ReleaseEnergy(newExperimentTurn, existingOrganismState, Experiment.HungerRate);
                    var isInSun = AlienSpaceVector.GetCoordinateDelta(existingOrganismState.Position, newExperimentTurn.SunPosition).Abs() < Experiment.SunRadius;
                    if (isInSun)
                    {
                        AbsorbEnergy(newExperimentTurn, existingOrganismState, Experiment.SunEnergyGift);
                    }
                    if (existingOrganismState.Health <= 0)
                    {
                        newOrganismStates.Remove(existingOrganismState.OrganismId);
                    }
                }

                var numberOfOrganismsToCreate = Math.Min((int)newExperimentTurn.ExtraEnergy, Experiment.MaxBirthRate);
                newExperimentTurn.ExtraEnergy -= numberOfOrganismsToCreate;

                var newPositions = allPoints
                                   .Except(newOrganismStates.Values.Select(state => state.Position))
                                   .Randomize(random)
                                   .Take(numberOfOrganismsToCreate);

                foreach (var newPosition in newPositions)
                {
                    var newOrganism = new Organism {
                        Red   = 0,
                        Green = 1,
                        Blue  = 0,
                    };

                    sqlConnection.SaveOrganism(newOrganism);

                    newOrganismStates[newOrganism.Id] = new OrganismState {
                        OrganismId = newOrganism.Id,
                        Position   = newPosition,
                        Health     = 1,
                    };
                }

                sqlConnection.SaveExperimentTurn(newExperimentTurn);
                foreach (var organismState in newOrganismStates.Values)
                {
                    organismState.ExperimentTurnId = newExperimentTurn.Id;
                    sqlConnection.SaveOrganismState(organismState);
                }
            }
            //} catch (Exception e) {
            //}
        }