Пример #1
0
        public int GameSimulation(GameStateMCTS g, int n)
        {
            //Cette méthode simule une partie de manière aléatoire
            //En cas de victoire, le score de 10 est retourné et en cas de défaite, un score de -10 est retourné
            //Si la simulation est trop lourde (i.e. le nombre de récursion de la simulation atteint 100), on retourne 0

            var gs = new GameStateMCTS(g.ListeDesJoueurs, g.ListeDesPlanètes);
            int nbSoldatsPNeutre = 10;
            int nbSoldatsPEnnemi = 20;

            var nn = n;

            if (n >= 100)
            {
                return(0);
            }
            else
            {
                nn++;
            }
            if (gs.IsGameOver(gs))
            {
                return(gs.FinaleScore(gs));
            }

            var planètesNeutres = gs.ListeDesPlanètes.Where(p => p.EstNeutre == true);
            int choix           = Generateur.Next(0, 4);



            AttackInfo atq = new AttackInfo(null, 0, null, null);

            if (n % 2 == 0 && gs.ListeDesJoueurs[0].PlanètesControllées.Count() != 0)
            {
                if (planètesNeutres.Count() == 0)
                {
                    if (choix == 0 && gs.ListeDesJoueurs[0].NbSoldats >= 20)
                    {
                        atq = new AttackInfo("EnemyTag", nbSoldatsPEnnemi, gs.ListeDesJoueurs[1].PlanètesControllées.ElementAt(0), "Player");
                    }
                    else if (choix == 1 && gs.ListeDesJoueurs[0].NbSoldats >= 10)
                    {
                        atq = new AttackInfo("EnemyTag", nbSoldatsPNeutre, gs.ListeDesJoueurs[0].PlanètesControllées.Where(a => gs.ListeDesPlanètes.Find(x => x.Id == a).Niveau < 4).ElementAt(0), "EnemyTag");
                    }
                    else
                    {
                        atq = new AttackInfo(null, 0, null, null);
                    }
                }
                else
                {
                    if (gs.ListeDesJoueurs[1].PlanètesControllées.Count() == 0 && gs.ListeDesJoueurs[0].NbSoldats >= 10)
                    {
                        atq = new AttackInfo("EnemyTag", nbSoldatsPNeutre, planètesNeutres.ElementAt(0).Id, "Player");
                    }
                    else
                    {
                        if (choix == 0 && gs.ListeDesJoueurs[0].NbSoldats >= 10)                         //Attaque d'une planète neutre
                        {
                            atq = new AttackInfo("EnemyTag", nbSoldatsPNeutre, planètesNeutres.ElementAt(0).Id, "Player");
                        }
                        else if (choix == 1)                         //Self upgrade
                        {
                            atq = new AttackInfo("EnemyTag", gs.ListeDesJoueurs[0].NbSoldats, gs.ListeDesJoueurs[0].PlanètesControllées.Where(a => gs.ListeDesPlanètes.Find(x => x.Id == a).Niveau < 4).ElementAt(0), "EnemyTag");
                        }
                        else if (choix == 2 && gs.ListeDesJoueurs[0].NbSoldats >= 20)                         //Attaque planète ennemi
                        {
                            atq = new AttackInfo("EnemyTag", nbSoldatsPEnnemi, gs.ListeDesJoueurs[1].PlanètesControllées.ElementAt(0), "Player");
                        }
                        else                                           //On attent
                        {
                            atq = new AttackInfo(null, 0, null, null); //Debug.Log(".................................................");
                        }
                    }
                }
            }
            else if (gs.ListeDesJoueurs[1].PlanètesControllées.Count() != 0)
            {
                if (planètesNeutres.Count() == 0)
                {
                    if (choix == 0 && gs.ListeDesJoueurs[1].NbSoldats >= 20)
                    {
                        atq = new AttackInfo("Player", nbSoldatsPEnnemi, gs.ListeDesJoueurs[0].PlanètesControllées.ElementAt(0), "EnemyTag");
                    }
                    else if (choix == 1)
                    {
                        atq = new AttackInfo("Player", gs.ListeDesJoueurs[1].NbSoldats, gs.ListeDesJoueurs[1].PlanètesControllées.Where(a => gs.ListeDesPlanètes.Find(x => x.Id == a).Niveau < 4).ElementAt(0), "Player");
                    }
                    else
                    {
                        atq = new AttackInfo(null, 0, null, null);
                    }
                }
                else
                {
                    if (gs.ListeDesJoueurs[0].PlanètesControllées.Count() == 0 && gs.ListeDesJoueurs[1].NbSoldats >= 10)
                    {
                        atq = new AttackInfo("Player", nbSoldatsPNeutre, planètesNeutres.ElementAt(0).Id, "EnemyTag");
                    }
                    else
                    {
                        if (choix == 0 && gs.ListeDesJoueurs[1].NbSoldats >= 10)
                        {
                            atq = new AttackInfo("Player", nbSoldatsPNeutre, planètesNeutres.ElementAt(0).Id, "EnemyTag");
                        }
                        else if (choix == 1)
                        {
                            atq = new AttackInfo("Player", gs.ListeDesJoueurs[1].NbSoldats, gs.ListeDesJoueurs[1].PlanètesControllées.Where(a => gs.ListeDesPlanètes.Find(x => x.Id == a).Niveau < 4).ElementAt(0), "Player");
                        }
                        else if (choix == 2 && gs.ListeDesJoueurs[1].NbSoldats >= 20)
                        {
                            atq = new AttackInfo("Player", nbSoldatsPEnnemi, gs.ListeDesJoueurs[0].PlanètesControllées.ElementAt(0), "EnemyTag");
                        }
                        else
                        {
                            atq = new AttackInfo(null, 0, null, null);                             //Debug.Log(".................................................");
                        }
                    }
                }
            }
            return(GameSimulation(GameStateMCTS.NextGameState(gs, atq), nn));
        }
Пример #2
0
        public void ExplorerNode(Node t)
        {
            //Cette méthode sert à créer des nodes enfants pour le nodes reçu en paramètre.
            //C'est dans cette méthode que l'arbre s'agrandit.

            if (Stop)
            {
                return;
            }
            if (t.NodeGameState.IsGameOver(t.NodeGameState))
            {
                return;
            }
            var           l  = new List <JoueurMCTS>(t.NodeGameState.ListeDesJoueurs);   //La liste des joueurs
            var           pp = new List <PlanèteMCTS>(t.NodeGameState.ListeDesPlanètes); //La liste des planètes
            GameStateMCTS gs = new GameStateMCTS(l, pp);
            var           planètesNeutres = gs.ListeDesPlanètes.Where(plan => plan.EstNeutre);
            var           jA = gs.ListeDesJoueurs.Find(j => j.Id == "EnemyTag");    //Joueur lancant l'attaque



            string idPlanèteToAttack;

            //Avant de pouvoir attaquer la planète d'un adversaire, il ne faut plus qu'il aille des planètes neutre
            if (planètesNeutres.Count() == 0)
            {
                idPlanèteToAttack = gs.ListeDesJoueurs[1].PlanètesControllées.ElementAt(0);
            }
            else
            {
                idPlanèteToAttack = planètesNeutres.ElementAt(0).Id;
            }

            //On a trois actions possibles, attaquer une planète, s'améliorer ou attendre.
            GameStateMCTS childGameState1, childGameState2, childGameState3;

            if (jA.NbSoldats < pp.Find(plan => plan.Id == idPlanèteToAttack).NbPts)
            {
                //Si on a pas assez de soldat pour attaquer, on attend...
                childGameState1 = GameStateMCTS.NextGameState(gs,
                                                              new AttackInfo(null, 0, null, null));
                childGameState2 = GameStateMCTS.NextGameState(gs,
                                                              new AttackInfo(null, 0, null, null));
            }
            else
            {
                //Sinon, on peut attaquer une planète neutre ou s'améliorer
                childGameState1 = GameStateMCTS.NextGameState(gs,
                                                              new AttackInfo("EnemyTag", jA.NbSoldats, idPlanèteToAttack, "Player"));

                childGameState2 = GameStateMCTS.NextGameState(gs,
                                                              new AttackInfo("EnemyTag", jA.NbSoldats, jA.PlanètesControllées.Where(a => gs.ListeDesPlanètes.Find(x => x.Id == a).Niveau < 4).ElementAt(0), "EnemyTag"));
            }
            //Le 3ieme choix est d'attendre
            childGameState3 = GameStateMCTS.NextGameState(gs,
                                                          new AttackInfo(null, 0, null, null));

            Node childNodeA = new Node(childGameState1);
            Node childNodeB = new Node(childGameState2);
            Node childNodeC = new Node(childGameState3);

            t.Add_Child(childNodeA);
            childNodeA.ParentNode = t;
            t.Add_Child(childNodeB);
            childNodeB.ParentNode = t;
            t.Add_Child(childNodeC);
            childNodeC.ParentNode = t;
        }