Пример #1
0
        private void MoveRobotTo(RobotCommand direction, Point destPos)
        {
            CellType destType = Cell.At(destPos);

            if (!destType.IsTraversible())
            {
                throw new InvalidMoveException(RobotPosition, destPos);
            }

            if (!destType.IsEmpty())
            {
                IsChanged = true;
            }

            if (destType.IsRock())
            {
                if (direction == RobotCommand.Left && Cell.At(destPos.Left()).IsEmpty())
                {
                    Cell.Set(destPos.Left(), destType);
                    Rocks.Remove(destPos);
                    Rocks.Add(destPos.Left());
                    if (destType.IsHoRock())
                    {
                        HoRocks.Remove(destPos);
                        HoRocks.Add(destPos.Left());
                    }
                }
                else if (direction == RobotCommand.Right && Cell.At(destPos.Right()).IsEmpty())
                {
                    Cell.Set(destPos.Right(), destType);
                    Rocks.Remove(destPos);
                    Rocks.Add(destPos.Right());
                    if (destType.IsHoRock())
                    {
                        HoRocks.Remove(destPos);
                        HoRocks.Add(destPos.Right());
                    }
                }
                else
                {
                    throw new InvalidMoveException(RobotPosition, destPos);
                }
            }

            if (destType.IsLambda())
            {
                Lambdas.Remove(destPos);
                Score += 25;
                LambdasCollected++;
            }

            if (destType.IsOpenLift())
            {
                State  = MapState.Won;
                Score += LambdasCollected * 50 - 1; // minus one point for the final move
            }

            if (destType.IsTrampoline())
            {
                Cell.Set(destPos, CellType.Empty);
                destPos = Trampolines[destPos];

                // remove all trampolines that have the same target
                var remove = new List <Point>();
                foreach (var trampoline in Trampolines)
                {
                    if (trampoline.Value == destPos)
                    {
                        Cell.Set(trampoline.Key, CellType.Empty);
                        remove.Add(trampoline.Key);
                    }
                }

                foreach (Point point in remove)
                {
                    Trampolines.Remove(point);
                }
            }

            if (destType.IsRazor())
            {
                RazorCount++;
                Razors.Remove(destPos);
            }

            Cell.Set(RobotPosition, CellType.Empty);
            Cell.Set(destPos, CellType.Robot);
            RobotPosition = destPos;
        }