コード例 #1
0
ファイル: Simulator.cs プロジェクト: davewalker5/LangtonsAnt
        /// <summary>
        /// Run the simulation
        /// </summary>
        public void Run(Universe universe, int maximumSteps)
        {
            // Initial ant position and direction, at the center of the universe
            // and pointing down
            Ant ant = new Ant
            {
                Position = new Coordinate {
                    X = universe.Size / 2, Y = universe.Size / 2
                },
                Direction = 2
            };

            // Report the initial position of the ant
            Steps = 0;
            AntPositionChanged?.Invoke(this, new AntPositionChangedEventArgs
            {
                Position   = ant.Position,
                StepNumber = Steps
            });

            bool simulationCompleted = false;

            Started = DateTime.Now;
            while (!simulationCompleted)
            {
                Steps++;

                // Determine the current cell position and colour
                int currentX          = ant.Position.X;
                int currentY          = ant.Position.Y;
                int index             = universe.ConvertCoordinateToIndex(ant.Position);
                int currentCellColour = universe.Matrix[index];

                // Move the ant
                int movementOffset = ant.Direction * 6 + currentCellColour * 3;
                ant.Position.X += _movementArray[movementOffset];
                ant.Position.Y += _movementArray[movementOffset + 1];
                ant.Direction   = _movementArray[movementOffset + 2];

                AntPositionChanged?.Invoke(this, new AntPositionChangedEventArgs
                {
                    Position   = ant.Position,
                    StepNumber = Steps
                });

                // Invert the previous cell
                universe.Matrix[index] = Math.Abs(currentCellColour - 1);
                CellColourChanged?.Invoke(this, new CellColourChangedEventArgs
                {
                    Position = new Coordinate {
                        X = currentX, Y = currentY
                    },
                    StepNumber = Steps,
                    Colour     = universe.Matrix[index]
                });

                simulationCompleted = universe.IsOutOfBounds(ant.Position) ||
                                      ((Steps >= maximumSteps) && (maximumSteps > 0));
            }

            Completed   = DateTime.Now;
            RunTime     = (long)(Completed - Started).TotalMilliseconds;
            TimePerStep = (decimal)RunTime / (decimal)Steps;
        }