private void drawAnt(Ant ant, Graphics g)
 {
     int x, y;
     toScreenCoords(ant.x, ant.y, out x, out y);
     SolidBrush brush = new SolidBrush(new Color[] { Color.DarkRed, Color.Black }[ant.color]);
     Matrix m = new Matrix();
     m.Translate(x + CELL_SIZE / 2, y + CELL_SIZE / 2);
     m.Rotate(60 * ant.direction);
     g.Transform = m;
     g.FillEllipse(brush,
         -5, -3, 10, 6);
     if (ant.hasFood)
     {
         Pen pen = new Pen(Color.Yellow);
         g.DrawRectangle(pen, 2, -1, 2, 2);
     }
     g.ResetTransform();
 }
 void checkForSurroundedAnt(Ant ant)
 {
     if (ant == null)
         return;
     int count = 0;
     for (int i = 0; i < 6; i++)
     {
         int x = ant.x + Map.directions[i, 0];
         int y = ant.y + Map.directions[i, 1];
         Cell cell = map.cells[x, y];
         if (cell.ant != null && cell.ant.color != ant.color)
             count++;
     }
     if (count >= 5)
     {
         Cell cell = map.cells[ant.x, ant.y];
         cell.ant = null;
         cell.food += 3;
         if (ant.hasFood)
             cell.food++;
         cell.updated = true;
     }
 }
 public override void execute(Ant ant, Simulator simulator)
 {
     ant.direction += dDir;
     ant.direction = (ant.direction + 6) % 6;
     ant.state = nextState;
     simulator.map.cells[ant.x, ant.y].updated = true;
 }
        public Simulator(Map map, Automaton redProgram, Automaton blackProgram, int seed)
        {
            this.seed = (uint)seed;
            for (int i = 0; i < 4; i++)
                random(1);

            this.map = map;
            programs = new Automaton[] { redProgram, blackProgram };
            int id = 0;
            for (int j = 0; j < map.height; j++)
                for (int i = 0; i < map.width; i++)
                {
                    if (map.cells[i, j].type == CellType.RED_ANTHILL)
                    {
                        Ant ant = new Ant(id++, i, j, 0);
                        map.cells[i, j].ant = ant;
                        ants.Add(ant);
                    }
                    else if (map.cells[i, j].type == CellType.BLACK_ANTHILL)
                    {
                        Ant ant = new Ant(id++, i, j, 1);
                        map.cells[i, j].ant = ant;
                        ants.Add(ant);
                    }
                }
        }
 public void step(Ant ant)
 {
     if (map.cells[ant.x, ant.y].ant != ant)
         return; // ant was eliminated
     if (ant.resting > 0)
         ant.resting--;
     else
         programs[ant.color].instructions[ant.state].execute(ant, this);
 }
 public override void execute(Ant ant, Simulator simulator)
 {
     Cell cell = simulator.map.cells[ant.x, ant.y];
     if (ant.hasFood || cell.food == 0)
         ant.state = failState;
     else
     {
         cell.food--;
         cell.updated = true;
         ant.hasFood = true;
         ant.state = nextState;
     }
 }
 public override void execute(Ant ant, Simulator simulator)
 {
     int x = ant.x;
     int y = ant.y;
     if (dir == Dir.Ahead)
     {
         x += Map.directions[ant.direction, 0];
         y += Map.directions[ant.direction, 1];
     }
     else if (dir == Dir.LeftAhead)
     {
         x += Map.directions[(ant.direction + 5) % 6, 0];
         y += Map.directions[(ant.direction + 5) % 6, 1];
     }
     else if (dir == Dir.RightAhead)
     {
         x += Map.directions[(ant.direction + 1) % 6, 0];
         y += Map.directions[(ant.direction + 1) % 6, 1];
     }
     Cell cell = simulator.map.cells[x, y];
     if (cellMatches(cell, ant.color))
         ant.state = trueState;
     else
         ant.state = falseState;
 }
 public override void execute(Ant ant, Simulator simulator)
 {
     int newX = ant.x + Map.directions[ant.direction, 0];
     int newY = ant.y + Map.directions[ant.direction, 1];
     Cell oldCell = simulator.map.cells[ant.x, ant.y];
     Debug.Assert(oldCell.ant == ant);
     Cell newCell = simulator.map.cells[newX, newY];
     if (newCell.type == CellType.ROCK || newCell.ant != null)
         ant.state = failState;
     else
     {
         oldCell.updated = true;
         newCell.updated = true;
         oldCell.ant = null;
         newCell.ant = ant;
         ant.x = newX;
         ant.y = newY;
         ant.resting = 14;
         ant.state = nextState;
         simulator.checkForSurroundedAnts(ant.x, ant.y);
     }
 }
 public override void execute(Ant ant, Simulator simulator)
 {
     Cell cell = simulator.map.cells[ant.x, ant.y];
     if (unMark)
         cell.markers[ant.color] &= ~(1 << bit);
     else
         cell.markers[ant.color] |= 1 << bit;
     cell.updated = true;
     ant.state = nextState;
 }
 public virtual void execute(Ant ant, Simulator simulator)
 {
 }
 public override void execute(Ant ant, Simulator simulator)
 {
     if (simulator.random(p) == 0)
         ant.state = nextState1;
     else
         ant.state = nextState2;
 }
 public override void execute(Ant ant, Simulator simulator)
 {
     if (ant.hasFood)
     {
         ant.hasFood = false;
         Cell cell = simulator.map.cells[ant.x, ant.y];
         cell.food++;
         cell.updated = true;
     }
     ant.state = nextState;
 }