-1

我有一个名为 BaseRobot 的类,代码如下

{   
//=== Defines the possible orientation of the robot.
//=== Note the order is important to allow cycling to be performed in a meaningful manner.
public enum Compass
{
    North, East, South, West
};

//=== The basic robot.
public class BaseRobot
{
    //--- The behaviour properties that were identified, together with associated state.
    //--- The robot identification number.
   private int mId;
   public int id
   {
       get { return mId; }
   }

   //--- the direction in which the robot is currently facing.
   private Compass mOrientation;
   public Compass Orientation
   {
       get { return mOrientation; }
       set { mOrientation = value; }
   }

   //--- The robot's current position.
   private Point mPosition;
   public Point Position
   {
       get { return mPosition; }
   }


   //--- the robot's home position, where it was originally created.
   public Point mHome;
   public Point Home
   {
       get { return mHome; }
   }

    //--- Turn the orientation left (anti-clockwise) or right (clockwise).
    //--- Implementation relies on the N, E, S, W ordering of the enumeration values to allow the arithmetic to work.

    public void TurnLeft()
    {
        --mOrientation;
        if (mOrientation < 0) mOrientation = Compass.West;
    } // end turnLeft method.    

    public void TurnRight()
    {
        mOrientation = (Compass)(((int)mOrientation + 1) % 4);
    } // end turnRight method.

    //--- Move one unit forward in the current orientation.
    public void Move()
    {
        switch (mOrientation)
        {
            case Compass.North: mPosition.Y++; break;
            case Compass.East: mPosition.X++; break;
            case Compass.South: mPosition.Y--; break;
            case Compass.West: mPosition.X--; break;
        }
    } // end Move method.

    //--- Constructor methods.
    public BaseRobot(int aId)
    {
        mId = aId;
        mHome.X = 0;
        mHome = new Point(0, 0);
        mPosition = mHome;
    }
    public BaseRobot(int aId, int aX, int aY)
    {
        mId = aId;
        mHome = new Point(aX, aY);
        mPosition = mHome;
    } // end BaseRobot constructor methods.



} // end BaseRobot class.

} // end namespace.

在我的主程序课上,我有这个

  //calling  BaseRobot constructors 
        var robot1 = new BaseRobot(0);
        var robot2 = new BaseRobot(0,0,0);

        Console.WriteLine("===Defualt Robot===");
        StringBuilder db = new StringBuilder();
        db.AppendFormat("Robot#1 has home at <{0},{0}>. ", robot1.Home.X, robot1.Home.Y);
        db.AppendFormat("It is facing {0} ", robot1.Orientation);
        db.AppendFormat("and is currently at <{0},{0}>.", robot1.Position.X, robot1.Position.Y);
        Console.WriteLine(db.ToString());

这很好,但我想知道如何改变机器人面向南方、东方和西方的方式?此外,当我将机器人的主坐标更改为 (35,12) 时,我收到一条未处理的错误消息,指出索引(基于零)必须大于或等于零且小于参数列表的大小。

4

1 回答 1

0

TurnLeft 和 TurnRight 方法通过修改 mOrientation 字段来旋转您的机器人,并且由于您提供了一个 setter 属性,您可以通过以下方式直接更改方向:

robot.Orientation = Compass.West;
于 2013-08-08T10:52:47.453 回答