Exemple #1
0
        public void PickBest()
        {
            Random     gen     = new Random();
            List <Bot> argBots = new List <Bot>();

            int[] bestBots      = { -1, -1, -1, -1, -1 };
            Bot[] bestBotss     = new Bot[5];
            int   highestEnergy = 0;
            int   totalEnergy   = 0;

            Bot bot = null;

            if (botFocus != -1)
            {
                bot = bots[botFocus];
            }

            for (int j = 0; j != 5; j++)
            {
                for (int i = 0; i != bots.Count; i++)
                {
                    totalEnergy += bots[i].energy;

                    if (bots[i].energy > highestEnergy)
                    {
                        bestBots[j]   = i;
                        highestEnergy = bots[i].energy;
                    }
                }

                if (j == 0)
                {
                    bestEnergyHistory.Add(highestEnergy);
                }

                highestEnergy = 0;
                if (bestBots[j] < bots.Count)
                {
                    if (bestBots[j] != -1)
                    {
                        bestBotss[j] = bots[bestBots[j]];
                        bots.Remove(bots[bestBots[j]]);
                    }
                    else
                    {
                        bestBotss[j] = bots[gen.Next(bots.Count)];
                    }
                }

                if (j == 0)
                {
                    totalEnergyHistory[generations] = totalEnergy;
                }
            }
            for (int i = 0; i != bestBotss.Length; i++)
            {
                argBots.Add(bestBotss[i]);
            }

            if (botFocus == -1)
            {
                bots = new List <Bot>();
                for (int i = 0; i != 5; i++)
                {
                    for (int j = 0; j != 10; j++)
                    {
                        bots.Add(new Bot(gen.Next(worldWidth), gen.Next(worldHeight), botTex, 5d, 40d, 100d, bestBotss[i].behaviour, gen, fovs));
                        bots[i * 10 + j].angle = gen.Next(360);
                    }
                }
            }
            else
            {
                bots = new List <Bot>();
                for (int i = 0; i != 50; i++)
                {
                    bots.Add(new Bot(gen.Next(worldWidth), gen.Next(worldHeight), botTex, 5d, 40d, 100d, bot.behaviour, gen, fovs));
                    bots[i].angle = gen.Next(360);
                }
            }

            food = new List <Food>();
            for (int i = 0; i != 400; i++)
            {
                food.Add(new Food(gen.Next(worldWidth), gen.Next(worldHeight), gen.Next(1000) + 1000, foodTex));
            }

            generations++;


            if (generations % 10 == 0)
            {
                Console.WriteLine("Total Energy: ");
                for (int i = 0; i != generations; i++)
                {
                    Console.Write(totalEnergyHistory[i] + ",");
                }
                Console.WriteLine();

                Console.WriteLine("Total Food: ");
                for (int i = 0; i != generations; i++)
                {
                    Console.Write(totalFoodBitsEaten[i] + ",");
                }

                Console.WriteLine();

                Console.WriteLine("Best: ");
                for (int i = 0; i != generations; i++)
                {
                    Console.Write(bestEnergyHistory[i] + ",");
                }

                Console.WriteLine();

                for (int i = 0; i != argBots.Count; i++)
                {
                    Console.Write(argBots[i].behaviour.ToString());
                }

                Console.WriteLine();
                Console.WriteLine();
            }

            energyBars = new DrawableRect[generations];
            for (int i = 0; i != generations; i++)
            {
                int size = (int)(totalEnergyHistory[i] / (double)500);
                energyBars[i] = new DrawableRect((i * 2) + 10, worldHeight - 10 - size, 2, size, Color.White, 1);
            }

            foodBars = new DrawableRect[generations];
            for (int i = 0; i != generations; i++)
            {
                int size = totalFoodBitsEaten[i];
                foodBars[i] = new DrawableRect((i * 2) + 10, -5 - size, 2, size, Color.SpringGreen, 1);
            }
        }
Exemple #2
0
        public void UpdateBots(GameTime gameTime, KeyboardState ks, MouseState ms)
        {
            //GetAverageBehaviour(bots);

            if (botFocus != -1)
            {
                cam.pos = bots[botFocus].pos;
                if (matchRotation)
                {
                    cam.rotation = -(float)(bots[botFocus].angle * Math.PI / 180);
                }
            }

            if (bots.Count < 5)
            {
                Random gen = new Random();
                for (int i = 0; i != 5; i++)
                {
                    Bot newBot = new Bot(bots[i].pos.X + gen.Next(20) - 10, bots[i].pos.Y + gen.Next(20) - 10,
                                         botTex, 5d, 60d, 100d, bots[i].behaviour, gen, fovs);

                    newBot.angle = gen.Next(360);

                    bots.Add(newBot);
                }
            }

            for (int i = 0; i != bots.Count; i++)
            {
                if (bots[i].dead)
                {
                    //bots.Remove(bots[i]);
                    //i--;
                    //if (i == bots.Count || bots.Count == 0 || i < 0)
                    //    break;
                }

                //if (bots[i].priority == (int)Bot.Priorities.mate)
                //{
                //    Random gen = new Random();
                //    for (int j = 0; j != 5; j++)
                //    {
                //        Bot newBot = new Bot(bots[i].pos.X + gen.Next(20)-10, bots[i].pos.Y + gen.Next(20)-10,
                //            botTex, 5d, 20d, 30d, bots[i].behaviour, gen);

                //        newBot.angle = gen.Next(360);

                //        bots.Add(newBot);
                //    }

                //    bots.Remove(bots[i]);

                //    i--;
                //    if(i < 0)
                //        break;

                //}

                bots[i].Update(gameTime, ks);

                if (bots[i].pos.X >= worldWidth)
                {
                    bots[i].pos.X    = 0;
                    bots[i].bounds   = new Rectangle((int)bots[i].pos.X, (int)bots[i].pos.Y, bots[i].texture.Width, bots[i].texture.Height);
                    bots[i].center.X = bots[i].bounds.Center.X; bots[i].center.Y = bots[i].bounds.Center.Y;
                }

                if (bots[i].pos.X < 0)
                {
                    bots[i].pos.X    = worldWidth - 1;
                    bots[i].bounds   = new Rectangle((int)bots[i].pos.X, (int)bots[i].pos.Y, bots[i].texture.Width, bots[i].texture.Height);
                    bots[i].center.X = bots[i].bounds.Center.X; bots[i].center.Y = bots[i].bounds.Center.Y;
                }


                if (bots[i].pos.Y >= worldHeight)
                {
                    bots[i].pos.Y    = 0;
                    bots[i].bounds   = new Rectangle((int)bots[i].pos.X, (int)bots[i].pos.Y, bots[i].texture.Width, bots[i].texture.Height);
                    bots[i].center.X = bots[i].bounds.Center.X; bots[i].center.Y = bots[i].bounds.Center.Y;
                }

                if (bots[i].pos.Y < 0)
                {
                    bots[i].pos.Y    = worldHeight - 1;
                    bots[i].bounds   = new Rectangle((int)bots[i].pos.X, (int)bots[i].pos.Y, bots[i].texture.Width, bots[i].texture.Height);
                    bots[i].center.X = bots[i].bounds.Center.X; bots[i].center.Y = bots[i].bounds.Center.Y;
                }
            }
        }