Beispiel #1
0
        //	returns the vector from the sweeper to the closest mine
        public Vector ClosestMine(ref List <Mine> mines)
        {
            double closestSoFar = 99999;
            Vector closest      = null;

            //cycle through mines to find closest
            foreach (var mine in mines)
            {
                double distance = (mine.Position - Position).Length();

                if (distance < closestSoFar)
                {
                    closestSoFar = distance;

                    closest = Position - mine.Position;

                    _ClosestMine = mine;
                }
            }

            return(closest);
        }
        //	returns the vector from the sweeper to the closest mine
        public Vector ClosestMine(ref List<Mine> mines)
        {
            double closestSoFar = 99999;
            Vector closest = null;

	        //cycle through mines to find closest
            foreach (var mine in mines)
            {
	            double distance = (mine.Position - Position).Length();

                if (distance < closestSoFar)
		        {
                    closestSoFar = distance;
			
			        closest = Position - mine.Position;

                    _ClosestMine = mine;
		        }
	        }

	        return closest;
        }
        public Controller()
        {
            FastRender = false;
            Width = ActiveForm.Width;
            Height = ActiveForm.Height;
            Whiteboard = CreateGraphics();
            Whiteboard.CompositingQuality = CompositingQuality.HighQuality;
            Whiteboard.SmoothingMode = SmoothingMode.HighQuality;

            //let's create the mine sweepers
            for (int i = 0; i < Sweeper.Length; ++i)
            {
                Sweepers.Add(new MineSweeper(ref Whiteboard, Width, Height, i == Properties.Settings.Default.Elite));
            }

            //get the total number of weights used in the sweepers
            //NN so we can initialise the GA
            Weights = Sweepers[0].GetNumberOfWeights();

            //initialize the Genetic Algorithm class
            GA = new GeneticAlgorithm(Sweepers.Count,
                Properties.Settings.Default.MutationRate,
                Properties.Settings.Default.CrossoverRate,
                Weights);
            //Get the weights from the Genetic Algorythm and insert into the sweepers brains
            Population = GA.GetChromos();

            for (var i = 0; i < Population.Count; i++)
            {
                Sweepers[i].InsertGenes(Population[i]);
            }

            //initialize mines in random positions within the application window
            for (var i = 0; i < Properties.Settings.Default.Mines; ++i)
            {
                var mine = new Mine(ref Whiteboard, Width, Height);
                Mines.Add(mine);
            }
	

            //fill the vertex buffers
            foreach (Point sweeper in Sweeper)
            {
                SweeperVerticesBuffer.Add(sweeper);
            }

            foreach (Point mine in Mine)
            {
                MineVerticesBuffer.Add(mine);
            }

            InitializeComponent();
            timer1.Enabled = true;
        }