Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
 public void Move(char currentHeading, RobotPosition currentRobotPosition)
 {
     switch (currentHeading) {
         case 'N':
             currentRobotPosition.Up();
             break;
         case 'E':
             currentRobotPosition.Right();
             break;
         case 'W':
             currentRobotPosition.Left();
             break;
         case 'S':
             currentRobotPosition.Down();
             break;
     }
 }
Ejemplo n.º 3
0
        public bool IsValidCommand(RobotCommand command)
        {
            switch (command)
            {
            case RobotCommand.Up:
                return(Cell.IsValidMove(RobotPosition, RobotPosition.Up()));

            case RobotCommand.Down:
                return(Cell.IsValidMove(RobotPosition, RobotPosition.Down()));

            case RobotCommand.Left:
                return(Cell.IsValidMove(RobotPosition, RobotPosition.Left()));

            case RobotCommand.Right:
                return(Cell.IsValidMove(RobotPosition, RobotPosition.Right()));

            default:
                return(true);
            }
        }
Ejemplo n.º 4
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;
        }