public PlayerController Clone(GameTime gameTime)
 {
     var player = this.Player.Clone(gameTime);
     PlayerController controller = new PlayerController(player);
     controller.ActionsHistory = this.ActionsHistory;
     return controller;
 }
 protected virtual void InitializePlayers()
 {
     this.aliveControllers = new List<PlayerController>();
     this.deadControllers = new List<PlayerController>();
     for (int i = 0; i < playersCount; i++)
     {
         PlayerController playerController = new PlayerController(new PlayerElement());
         this.aliveControllers.Add(playerController);
         this.Elements.Add(playerController.Player);
     }
 }
        private void NextGeneration()
        {
            foreach (PlayerController controller in this.players)
            {
                this.Elements.Remove(controller.Player);
            }

            RandomShuffle(this.players);

            List<double> scores = new List<double>(new double[this.players.Count]);
            List<double> probabilities = new List<double>(scores);
            List<PlayerController> newList = new List<PlayerController>();

            double totalScore = 0;
            for (int i = 0; i < scores.Count; i++)
            {
                if (this.players[i].StatusHistory.Count == 0)
                {
                    scores[i] = probabilities[i] = 0;
                    continue;
                }

                PlayerStatus status = this.players[i].StatusHistory[this.players[i].StatusHistory.Count - 1];
                scores[i] = GetScore(status);
                totalScore += scores[i];
            }

            for (int i = 0; i < scores.Count; i++)
            {
                probabilities[i] = scores[i] / totalScore;
            }

            for (int i = 0; i < playersCount; i++)
            {
                PlayerController info = new PlayerController(players[i].Player.Clone());
                info.Player.Reset();
                info.UsePredefinedActions = true;

                if (i <= playersCount * MUTATION_PERCENT)
                {
                    info.PredefinedActions.AddRange(PickMutated(this.players));
                }
                else if (i <= playersCount * (MUTATION_PERCENT + CROSS_PERCENT))
                {
                    info.PredefinedActions.AddRange(PickCrossed(this.players, probabilities));
                }
                else if (i <= playersCount * (MUTATION_PERCENT + CROSS_PERCENT + RESET_PERCENT))
                {
                    info.PredefinedActions.AddRange(PickReset());
                }
                else
                {
                    info.PredefinedActions.AddRange(PickUnchaged(this.players, probabilities));
                }

                newList.Add(info);
                this.Elements.Add(info.Player);
            }

            this.players = newList;
            currentGeneration++;
            PopulatonLogger.SetGeneration(currentGeneration);
            PopulatonLogger.ResetCoins();
            SaveGeneration(this.players);
        }
        private static IEnumerable<PlayerAction> Cross(PlayerController c1, PlayerController c2)
        {
            if (c1.Player.IsDead)
            {
                c1.StatusHistory.RemoveRange(c1.StatusHistory.Count - Math.Min(5, c1.StatusHistory.Count), Math.Min(5, c1.StatusHistory.Count));
            }
            if (c2.Player.IsDead)
            {
                c2.StatusHistory.RemoveRange(c2.StatusHistory.Count - Math.Min(5, c2.StatusHistory.Count), Math.Min(5, c2.StatusHistory.Count));
            }

            List<PlayerAction> actions = new List<PlayerAction>();

            for (int i = 0; i < Math.Max(c1.StatusHistory.Count, c2.StatusHistory.Count); )
            {
                int changeLen = RandomGenerator.Next(30, 50);
                if (i + changeLen < c1.StatusHistory.Count && i + changeLen < c2.StatusHistory.Count)
                {
                    if (2*GetScore(c1.StatusHistory[i + changeLen]) - 0.5*GetScore(c1.StatusHistory[i]) <
                        2*GetScore(c2.StatusHistory[i + changeLen]) - 0.5*GetScore(c2.StatusHistory[i]))
                    {
                        for (int j = i; j < i + changeLen; j++)
                        {
                            actions.Add(c2.ActionsHistory[j]);
                        }
                    }
                    else
                    {
                        for (int j = i; j < i + changeLen; j++)
                        {
                            actions.Add(c1.ActionsHistory[j]);
                        }
                    }
                }
                else if (i + changeLen < c1.StatusHistory.Count)
                {
                    for (int j = i; j < i + changeLen; j++)
                    {
                        actions.Add(c1.ActionsHistory[j]);
                    }
                }
                else if (i + changeLen < c2.StatusHistory.Count)
                {
                    for (int j = i; j < i + changeLen; j++)
                    {
                        actions.Add(c2.ActionsHistory[j]);
                    }
                }
                else
                {
                    break;
                }

                i += changeLen;
            }

            for (int j = actions.Count; j < MAX_ACTIONS; j++)
            {
                actions.Add(RandomGenerator.NextMove());
            }

            return actions;
        }
        private void InitializePlayers()
        {
            StreamReader sr = null;
            if (System.IO.File.Exists(STATE_FILE))
            {
                sr = File.OpenText(STATE_FILE);
                string gen = sr.ReadLine();
                int.TryParse(gen, out currentGeneration);
            }

            this.players = new List<PlayerController>();
            for (int i = 0; i < playersCount; i++)
            {
                PlayerController info = new PlayerController(new PlayerElement());
                info.UsePredefinedActions = true;
                if (sr != null && !sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    info.PredefinedActions.AddRange(ReadActions(line));
                }
                else
                {
                    info.PredefinedActions.AddRange(PickReset());
                }

                this.players.Add(info);
                this.Elements.Add(info.Player);
            }

            if (sr != null)
            {
                sr.Close();
                sr.Dispose();
            }

            PopulatonLogger.SetGeneration(currentGeneration);
        }