示例#1
0
    private void GenerateImage() {
      animationTimer.Stop();
      pictureBox.Image = null;
      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
        if (Content != null) {
          var nodeStack = new Stack<SymbolicExpressionTreeNode>();
          int rows = Content.World.Rows;
          int columns = Content.World.Columns;
          var expression = Content.SymbolicExpressionTree;

          DrawWorld();
          using (Graphics graphics = Graphics.FromImage(pictureBox.Image)) {
            float cellHeight = pictureBox.Height / (float)rows;
            float cellWidth = pictureBox.Width / (float)columns;

            Interpreter interpreter = new Interpreter(Content.SymbolicExpressionTree, Content.World, Content.MaxTimeSteps);
            int currentAntLocationColumn;
            int currentAntLocationRow;
            // draw initial ant
            interpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn);
            DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, interpreter.AntDirection, cellWidth, cellHeight);
            // interpret ant code and draw trail
            while (interpreter.ElapsedTime < interpreter.MaxTimeSteps) {
              interpreter.Step();
              interpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn);
              DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, interpreter.AntDirection, cellWidth, cellHeight);
            }
          }
          pictureBox.Refresh();
        }
      }
    }
示例#2
0
    private void playButton_Click(object sender, EventArgs e) {
      playButton.Enabled = false;

      animationInterpreter = new Interpreter(Content.SymbolicExpressionTree, Content.World, Content.MaxTimeSteps);

      DrawWorld();
      using (Graphics graphics = Graphics.FromImage(pictureBox.Image)) {
        float cellHeight = pictureBox.Height / (float)Content.World.Rows;
        float cellWidth = pictureBox.Width / (float)Content.World.Columns;
        // draw initial ant
        int currentAntLocationColumn;
        int currentAntLocationRow;
        animationInterpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn);
        DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, animationInterpreter.AntDirection, cellWidth, cellHeight);
        pictureBox.Refresh();
      }

      animationTimer.Start();
    }