Example #1
0
        public RobotCommand ExecuteTurn(RobotCommand command)
        {
            IsChanged = false;
            Moves.Enqueue(command);
            NeighborCache.Clear();

            switch (command)
            {
            case RobotCommand.Up:
                MoveRobotTo(RobotCommand.Up, RobotPosition.Up());
                break;

            case RobotCommand.Down:
                MoveRobotTo(RobotCommand.Down, RobotPosition.Down());
                break;

            case RobotCommand.Left:
                MoveRobotTo(RobotCommand.Left, RobotPosition.Left());
                break;

            case RobotCommand.Right:
                MoveRobotTo(RobotCommand.Right, RobotPosition.Right());
                break;

            case RobotCommand.Abort:
                State = MapState.Aborted;
                Score = AbortScore;
                break;

            case RobotCommand.Shave:
                for (int dy = -1; dy < 2; dy++)
                {
                    for (int dx = -1; dx < 2; dx++)
                    {
                        if (Cell.At(RobotPosition.X + dx, RobotPosition.Y + dy).IsBeard())
                        {
                            Cell.Set(RobotPosition.X + dx, RobotPosition.Y + dy, CellType.Empty);
                            IsChanged = true;
                        }
                    }
                }
                break;
            }

            if (State == MapState.Valid)
            {
                Score -= 1;
                Simulate();
            }

            return(command);
        }
Example #2
0
        public Point[] Neighbors(Point point)
        {
            if (Cell.At(point).IsTrampoline())
            {
                point = Trampolines[point];
            }

            Point[] cachedNeighbors;
            if (NeighborCache.TryGetValue(point, out cachedNeighbors))
            {
                return(cachedNeighbors);
            }

            var neighbors = new List <Point>();

            if (Cell.IsValidMove(point, point.Up()))
            {
                neighbors.Add(point.Up());
            }

            if (Cell.IsValidMove(point, point.Down()))
            {
                neighbors.Add(point.Down());
            }

            if (Cell.IsValidMove(point, point.Left()))
            {
                neighbors.Add(point.Left());
            }

            if (Cell.IsValidMove(point, point.Right()))
            {
                neighbors.Add(point.Right());
            }

            NeighborCache.Add(point, neighbors.ToArray());

            return(neighbors.ToArray());
        }