private void button1_Click(object sender, EventArgs e) { int pos = 0; Random ra = new Random(); for (int x = 0; x < matrix.GetLength(0); x++) { for (int y = 0; y < matrix.GetLength(1); y++) { //Probability of put an ant int integer_rand = ra.Next(0, 99); double rand = (ra.NextDouble()) + integer_rand; if (rand < float.Parse(numericOnes.Text)) { ANT ant_obj = new ANT(x, y, Helper.generateOrientation(), Helper.generateType((int)NumericQueen.Value, (int)NumericMale.Value), (int)NumericTTL.Value); ant_obj.setColor(colors.randomColor()); ants.Add(ant_obj); } else { matrix[x, y] = DEAD; } } } PBAutomataSimulator.Invalidate(); }
private void PBAutomataSimulator_Click(object sender, EventArgs e) { MouseEventArgs me = (MouseEventArgs)e; int x = (me.X / cellArea); int y = (me.Y / cellArea); if (me.Button == System.Windows.Forms.MouseButtons.Right) { foreach (ANT ant_obj in ants) { if (ant_obj.getX() == x && ant_obj.getY() == y) { ant_obj.setColor(getColor()); break; } } } else { bool can_put_ant = true; int i = 0, j = 0; Random rnd = new Random(); for (i = 0; i < ants.Count; i++) { ANT ant_obj = ants[i]; if (ant_obj.getX() == x && ant_obj.getY() == y) { can_put_ant = false; j = i; break; } } //Add a new ant if (can_put_ant) { ants.Add(new ANT(x, y, Helper.getOrientationValue(ComboOrientation.Text), Helper.getTypeValue(ComboType.Text), (int)NumericTTL.Value)); } else { ants.RemoveAt(j); } } PBAutomataSimulator.Invalidate(); }
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); }
/// <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(); } }
/// <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); } }