Exemplo n.º 1
0
        private List <ANT> QueenActions(int queen_index)
        {
            List <ANT> new_ants    = new List <ANT>();
            ANT        first_queen = ants[queen_index];
            int        queen_fx    = first_queen.getX();
            int        queen_fy    = first_queen.getY();
            bool       flag        = true;

            for (int i = 0; i < ants.Count; i++)
            {
                flag = true;
                ANT current_ant = ants[i];
                if ((i != queen_index) && (queen_fx == current_ant.getX() && queen_fy == current_ant.getY()))
                {
                    switch (current_ant.getType())
                    {
                    case Helper.QUEEN:
                        if (new Random().Next(0, 100) <= 50)
                        {
                            new_ants.Add(current_ant);
                        }
                        else
                        {
                            new_ants.Add(ants[queen_index]);
                        }
                        flag = false;
                        break;

                    case Helper.MALE:
                        new_ants.Add(new ANT(queen_fx, queen_fy, Helper.generateOrientation(),
                                             Helper.generateType((int)NumericQueen.Value,
                                                                 (int)NumericMale.Value),
                                             (int)NumericTTL.Value));
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    new_ants.Add(current_ant);
                }
            }
            return(new_ants);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method calls nextGeneration method and
        /// updates the GUI and the count of our alive cells
        /// </summary>
        private void step()
        {
            List <ANT> aux = new List <ANT>(ants);

            for (int i = 0; i < ants.Count; i++)
            {
                ANT ant_obj = ants[i];
                if (ant_obj.getType() == Helper.QUEEN)
                {
                    aux = QueenActions(i);
                }
                matrix = ant_obj.nextGeneration(matrix, CBToroid.Checked);
                PBAutomataSimulator.Invalidate();
            }
            ants = new List <ANT>(aux);
            updateTextGeneration();
            countOnes();
            if (ants.Count == 0)
            {
                PBAutomataSimulator.Invalidate();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// This method paints the matrix in the Paint Box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PBAutomataSimulator_Paint(object sender, PaintEventArgs e)
        {
            List <ANT> aux_ant = new List <ANT>();
            int        x_size  = matrix.GetLength(0);
            int        y_size  = matrix.GetLength(1);

            total_cells = x_size * y_size;

            Graphics graphics = e.Graphics;

            for (int x = 0; x < x_size; x++)
            {
                for (int y = 0; y < y_size; y++)
                {
                    uint current_cell = matrix[x, y];
                    if (current_cell != DEAD)
                    {
                        SolidBrush aliveCellColor = new SolidBrush(colors.fromIntToColor(current_cell));
                        graphics.FillRectangle(aliveCellColor, x * cellArea, y * cellArea, cellArea, cellArea);
                    }
                    else
                    {
                        graphics.FillRectangle(dead, x * cellArea, y * cellArea, cellArea, cellArea);
                    }
                }
            }

            for (int i = 0; i < ants.Count; i++)
            {
                ANT ant_obj = ants[i];
                if (ant_obj.getType() == Helper.WORKER)
                {
                    graphics.FillEllipse(ant_worker, ant_obj.getX() * cellArea, ant_obj.getY() * cellArea, cellArea, cellArea);
                }
                else if (ant_obj.getType() == Helper.MALE)
                {
                    graphics.FillEllipse(ant_male, ant_obj.getX() * cellArea, ant_obj.getY() * cellArea, cellArea, cellArea);
                }
                else
                {
                    graphics.FillEllipse(ant_queen, ant_obj.getX() * cellArea, ant_obj.getY() * cellArea, cellArea, cellArea);
                }
                graphics.DrawEllipse(new Pen(Color.Black), ant_obj.getX() * cellArea, ant_obj.getY() * cellArea, cellArea, cellArea);
                try
                {
                    if (ant_obj.getTTL() > 0)
                    {
                        aux_ant.Add(ant_obj);
                    }
                }
                catch (Exception) { }
            }
            ants = new List <ANT>(aux_ant);
            for (int y = 0; y < y_size; y++)
            {
                graphics.DrawLine(grid, 0, y * cellArea, total_cells * cellArea, y * cellArea);
            }

            for (int x = 0; x < x_size; x++)
            {
                graphics.DrawLine(grid, x * cellArea, 0, x * cellArea, total_cells * cellArea);
            }
        }