Exemple #1
0
        public static List <Case> CasesAlentours(int x, int y) //retourne la liste des cases qui entourent la fourmi
        {
            List <Case> liste = new List <Case>();

            for (int i = 0; i < 3; i++)
            {
                if (CaisseAOut.EstDansLeTableau(x - 1 + i, y - 1))
                {
                    liste.Add(RefTableau.tab[x - 1 + i, y - 1]);
                }

                if (CaisseAOut.EstDansLeTableau(x - 1 + i, y + 1))
                {
                    liste.Add(RefTableau.tab[x - 1 + i, y + 1]);
                }

                if (CaisseAOut.EstDansLeTableau(x - 1 + i, y) && i != 1)
                {
                    liste.Add(RefTableau.tab[x - 1 + i, y]);
                }
            }


            return(liste);
        }
Exemple #2
0
        public void DeplacementAleatoire()
        {
            int x;
            int y;

            do     // tant que la case n'est valide on refait la boucle
            {
                do // tant que la case n'est pas dans le tableau on refait la boucle
                {
                    x = caseFourmi.X;

                    //le systeme de randomisation est simple, si le random donne 0, on rajoute un
                    // si le random donne 1, on enleve un,
                    // enfin, si le random donne deux, on ne fait rien (pour x et pour y),
                    int rndX = rnd.Next(3);
                    if (rndX == 0)
                    {
                        x -= 1;
                    }
                    else if (rndX == 1)
                    {
                        x += 1;
                    }

                    int rndY;
                    int cpt = 0;
                    do // tant que les deux randoms sont à deux (donc ni x ni y n'a changé) on refait la boucle pour le random du y
                    {
                        cpt++;
                        y    = caseFourmi.Y;
                        rndY = rnd.Next(3);
                        if (rndY == 0)
                        {
                            y -= 1;
                        }
                        else if (rndY == 1)
                        {
                            y += 1;
                        }
                        if (cpt > 5)
                        {
                            break;
                        }
                    }while (rndX == 2 && rndY == 2);
                }while (!CaisseAOut.EstDansLeTableau(x, y));
            }while (!CaisseAOut.CaseValidePourFourmis(RefTableau.tab[x, y]));

            DeplacerFourmis(x, y);
        }
Exemple #3
0
        public void InitFourmis() // création et placement alétoire des fourmis autour du nid
        {
            List <int[]> posPossible = new List <int[]>();

            //liste (ci dessus) qui contient tout les points autour du nid (ajoutés ci dessous)

            posPossible.Add(CaisseAOut.DeuxValeurEnTableau(posNid[0] - 1, posNid[1] - 1));
            posPossible.Add(CaisseAOut.DeuxValeurEnTableau(posNid[0] + 2, posNid[1] + 2));
            for (int i = 1; i < 4; i++)
            {
                posPossible.Add(CaisseAOut.DeuxValeurEnTableau(posNid[0] - 1 + i, posNid[1] - 1));
                posPossible.Add(CaisseAOut.DeuxValeurEnTableau(posNid[0] - 1, posNid[1] - 1 + i));

                posPossible.Add(CaisseAOut.DeuxValeurEnTableau(posNid[0] + 2 - i, posNid[1] + 2));
                posPossible.Add(CaisseAOut.DeuxValeurEnTableau(posNid[0] + 2, posNid[1] + 2 - i));
            }


            //randomisation tant qu'une case valide n'a pas été selectionnée dans la liste
            if (posPossible.Count() > 0)
            {
                for (; ;)
                {
                    int rndCase = rnd.Next(1, posPossible.Count());
                    rndCase--;
                    if (CaisseAOut.EstDansLeTableau(posPossible[rndCase][0], posPossible[rndCase][1]))
                    {
                        if (CaisseAOut.CaseValidePourFourmis(RefTableau.tab[posPossible[rndCase][0], posPossible[rndCase][1]]))
                        {
                            RefTableau.tab[posPossible[rndCase][0], posPossible[rndCase][1]].fourmis = new Fourmis(RefTableau.tab[posPossible[rndCase][0], posPossible[rndCase][1]]);

                            return;
                        }
                    }
                }
            }
        }
Exemple #4
0
        public void InitPhero(int X, int Y) //ici on rajoute tout les phéromones de nid en fonction de leurs distance du nid
        {
            int decalage = 3;
            int test     = 0;

            int[] resultTest =
            {
                hauteur - (posNid[0] + 2),
                posNid[0],
                posNid[1],
                largeur - (posNid[1] + 2)
            };

            for (int i = 0; i < resultTest.Length; i++)
            {
                if (resultTest[i] > test)
                {
                    test = resultTest[i];
                }
            }


            // on fait le tour du nid en remplissant chaque case avec le nombre de phéromone correspondant à i ,
            // a chaque itération on s'éloigne d'une case tout en continuant de faire le tour du nid
            for (int i = test; i > -1; i--)
            {
                int Xactuel    = X - ((test + 1) - i); // - 1, 2, 3, 4
                int Yactuel    = Y - ((test + 1) - i);
                int XnegActuel = X + ((test + 2) - i); // +2, 3, 4, 5
                int YnegActuel = Y + ((test + 2) - i);

                if (CaisseAOut.EstDansLeTableau(Xactuel, Yactuel))
                {
                    RefTableau.tab[Xactuel, Yactuel].pheromone_nid = i;
                }
                if (CaisseAOut.EstDansLeTableau(XnegActuel, YnegActuel))
                {
                    RefTableau.tab[XnegActuel, YnegActuel].pheromone_nid = i;
                }

                for (int dec = 1; dec <= decalage; dec++)
                {
                    if (CaisseAOut.EstDansLeTableau(Xactuel + dec, Yactuel))
                    {
                        RefTableau.tab[Xactuel + dec, Yactuel].pheromone_nid = i;
                    }
                    if (CaisseAOut.EstDansLeTableau(Xactuel, Yactuel + dec))
                    {
                        RefTableau.tab[Xactuel, Yactuel + dec].pheromone_nid = i;
                    }
                    if (CaisseAOut.EstDansLeTableau(XnegActuel - dec, YnegActuel))
                    {
                        RefTableau.tab[XnegActuel - dec, YnegActuel].pheromone_nid = i;
                    }
                    if (CaisseAOut.EstDansLeTableau(XnegActuel, YnegActuel - dec))
                    {
                        RefTableau.tab[XnegActuel, YnegActuel - dec].pheromone_nid = i;
                    }
                }



                decalage += 2;
            }
        }