コード例 #1
0
    void UpdateMars()
    {
        for (int i = 0; i < Rovers.Count; ++i)
        {
            Rover rover = Rovers[i];
            if (IsOutOfBoundry(rover.PosX, rover.PosY))
            {
                rover.Broken();
            }
            else
            {
                Rover.Command nowCommand = rover.PeekCommand();
                switch (nowCommand)
                {
                case Rover.Command.M:
                    if ((rover.Dir == Rover.Direction.N && !HasObstacle(rover.PosX, rover.PosY + 1)) ||
                        (rover.Dir == Rover.Direction.E && !HasObstacle(rover.PosX + 1, rover.PosY)) ||
                        (rover.Dir == Rover.Direction.W && !HasObstacle(rover.PosX - 1, rover.PosY)) ||
                        (rover.Dir == Rover.Direction.S && !HasObstacle(rover.PosX, rover.PosY - 1)))
                    {
                        Map[rover.PosX, rover.PosY] = FLOOR;
                        rover.DoMove();
                        if (!IsOutOfBoundry(rover.PosX, rover.PosY))
                        {
                            Map[rover.PosX, rover.PosY] = ROVER;
                        }
                    }
                    break;

                case Rover.Command.L:
                    rover.DoTurnL();
                    break;

                case Rover.Command.R:
                    rover.DoTurnR();
                    break;

                case Rover.Command.B:
                    if ((rover.Dir == Rover.Direction.N && !HasObstacle(rover.PosX, rover.PosY - 1)) ||
                        (rover.Dir == Rover.Direction.E && !HasObstacle(rover.PosX - 1, rover.PosY)) ||
                        (rover.Dir == Rover.Direction.W && !HasObstacle(rover.PosX + 1, rover.PosY)) ||
                        (rover.Dir == Rover.Direction.S && !HasObstacle(rover.PosX, rover.PosY + 1)))
                    {
                        Map[rover.PosX, rover.PosY] = FLOOR;
                        rover.DoBack();
                        if (!IsOutOfBoundry(rover.PosX, rover.PosY))
                        {
                            Map[rover.PosX, rover.PosY] = ROVER;
                        }
                    }
                    break;

                case Rover.Command.NULL:
                    Debug.Log("No more command");
                    break;
                }
            }
            rover.LogState();
        }
    }