// Simple turtle interpreter
// P.D. Terry, Rhodes University, 2015

using Library;

public class Turtle {

  public static void Main(string[] args) {
    const int
      East  = 0, North = 1, West  = 2, South = 3, // directions

      Bad   = 0,
      Left  = 1, Right = 2, Move  = 3, Home  = 4, Where = 5, Quit  = 6; // operations

    bool running  = true;

    string[] code = { "", "turnleft", "turnright", "move", "home", "where", "quit" };
    string[] facing = {"East", "North", "West", "South"};

    int direction = East;
    double x = 0.0, y = 0.0, step = 0.0;

    while (running) {
      code[0] = IO.ReadWord().ToLower().Trim();
      int operation = Quit;
      while (!code[0].Equals(code[operation])) operation--;
      if (operation == Move) step = IO.ReadDouble();
      switch (operation) {
        case Quit :
          running = false;
          break;

        case Where:
          IO.Write("Now at (x, y) = ("); IO.WriteFixed(x, 1, 2);
          IO.Write(" , ");               IO.WriteFixed(y, 1, 2);
          IO.WriteLine(") facing " + facing[direction]);
          break;

        case Left :
          if (direction == South) direction = East; else direction++;
          // or, more neatly     direction = (direction + 1) % 4;
          break;

        case Right :
          if (direction == East) direction = South; else direction--;
          // or, more neatly:    direction = (direction + 3) % 4;
          break;

        case Move :
          // we must now choose which coordinate to alter
          switch (direction) {
              case East : x += step; break;
              case North: y += step; break;
              case West : x -= step; break;
              case South: y -= step; break;
            }
          break;

        case Home:
          x = 0.0; y = 0.0; direction = East;
          break;

        default :
          IO.WriteLine("bad command");
          break;
      } // switch (operation)
    } // while (running)
  } // Main

} // Turtle