예제 #1
0
        private void Simulate()
        {
            if (State != MapState.Valid)
            {
                return;
            }

            var newState = new CellType[Width, Height];

            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    var      current     = new Point(x, y);
                    CellType currentType = Cell.At(current);

                    if (newState.At(current) == 0)
                    {
                        newState.Set(current, currentType);
                    }

                    if (currentType.IsClosedLift() && LambdaCount == 0)
                    {
                        newState.Set(current, CellType.OpenLift);
                    }

                    // Handle beard growth
                    if (currentType.IsBeard() && IsGrowthTurn)
                    {
                        for (int dy = -1; dy < 2; dy++)
                        {
                            for (int dx = -1; dx < 2; dx++)
                            {
                                if (Cell.At(current.X + dx, current.Y + dy).IsEmpty())
                                {
                                    newState.Set(current.X + dx, current.Y + dy, CellType.Beard);
                                    IsChanged = true;
                                }
                            }
                        }
                    }

                    // Handle rock movement
                    if (currentType.IsRock())
                    {
                        // If (x; y) contains a Rock, and (x; y - 1) is Empty:
                        if (Cell.At(current.Down()).IsEmpty())
                        {
                            // – (x; y) is updated to Empty, (x; y - 1) is updated to Rock.
                            MoveRock(currentType, current, current.Down(), newState);
                        }
                        else if (Cell.At(current.Down()).IsRock())
                        {
                            // If (x; y) contains a Rock, (x; y - 1) contains a Rock, (x + 1; y) is Empty and (x + 1; y - 1) is Empty:
                            if (Cell.At(current.Right()).IsEmpty() && Cell.At(current.Right().Down()).IsEmpty())
                            {
                                // (x; y) is updated to Empty, (x + 1; y - 1) is updated to Rock
                                MoveRock(currentType, current, current.Right().Down(), newState);
                            }
                            // If (x; y) contains a Rock, (x; y - 1) contains a Rock, either (x + 1; y) is not Empty or (x + 1; y - 1) is not Empty, (x - 1; y) is Empty and (x - 1; y - 1) is Empty:
                            else if ((!Cell.At(current.Right()).IsEmpty() || !Cell.At(current.Right().Down()).IsEmpty()) &&
                                     Cell.At(current.Left()).IsEmpty() && Cell.At(current.Left().Down()).IsEmpty())
                            {
                                // (x; y) is updated to Empty, (x - 1; y - 1) is updated to Rock
                                MoveRock(currentType, current, current.Left().Down(), newState);
                            }
                        }
                        //  If (x; y) contains a Rock, (x; y - 1) contains a Lambda, (x + 1; y) is Empty and (x + 1; y - 1) is Empty:
                        else if (Cell.At(current.Down()).IsLambda() && Cell.At(current.Right()).IsEmpty() &&
                                 Cell.At(current.Right().Down()).IsEmpty())
                        {
                            // (x; y) is updated to Empty, (x + 1; y  1) is updated to Rock.
                            MoveRock(currentType, current, current.Right().Down(), newState);
                        }
                    }
                }
            }

            if (newState.At(RobotPosition.Up()).IsRock() && !Cell.At(RobotPosition.Up()).IsRock())
            {
                State = MapState.Killed;
            }

            if (RobotPosition.Y < WaterLevel)
            {
                Underwater++;
                if (Underwater > Waterproof)
                {
                    State = MapState.Killed;
                }
            }
            else
            {
                Underwater = 0;
            }

            Cell = newState;
        }