【问题标题】:How to read text on a lgo file (eg left, right etc) for Turtle Graphics如何为 Turtle Graphics 读取 lgo 文件(例如左、右等)上的文本
【发布时间】:2020-09-26 20:48:10
【问题描述】:

我需要创建一个应用程序来解释徽标编程语言的子集并在徽标程序中执行操作。我的应用程序将读取徽标程序并执行程序内部的操作。 logo 程序的每一行只包含 1 个 logo 命令。我无法访问或查看徽标文件,也不知道如何读取此 lgo 文件的内容。

这些是已经给出的命令:

向左:将乌龟的方向向左转90度

:将乌龟的方向向右转90度

尾巴:如果尾巴向上,则放下,反之亦然

Step:将海龟向当前方向移动1步

{
    //Direction values
    const int NORTH = 0;
    const int EAST = 90;
    const int SOUTH = 180;
    const int WEST = 270;

    //Amount to move the turtle 1 step
    const int STEP_AMOUNT = 50;

    //Amount to add to direction when turning
    const int TURN_AMOUNT = EAST;

    //Set direction of turtle to East
    int direction = 90;
    //Status of the tail
    bool isTailUp = true;
    //Current x and y position of the turtle
    Point turtlePos = new Point(0, 0);

    //Filter constant
    const string FILTER = "All Files|*.*|Logo Program Files|*.lgo";

    StreamReader reader;

    public Form1()
    {
        InitializeComponent();
    }

    /// <summary>
    /// Initialises the application to it's initial state.
    /// </summary>
    private void Initialise()
    {
        pictureBoxDisplay.Refresh();
        direction = 90;
        isTailUp = true;
        turtlePos = new Point(0, 0);
    }

    /// <summary>
    /// Turns the direction of the turtle 90 degrees to the left.
    /// </summary>
    private void Left()
    {
        //If direction is north then set to west, otherwise just subtract 
        //the turn amount from the current direction
        if (direction == NORTH)
        {
            direction = WEST;
        }
        else
        {
            direction -= TURN_AMOUNT;
        }
    }

    /// <summary>
    /// Turns the direction of the turtle 90 degrees to the right
    /// </summary>
    private void Right()
    {
        //If direction is west then set to north, otherwise just add 
        //the turn amount to the current direction
        if (direction == WEST)
        {
            direction = NORTH;
        }
        else
        {
            direction += TURN_AMOUNT;
        }
    }

    /// <summary>
    /// Toggles the state of the tail.
    /// </summary>
    private void Tail()
    {
        isTailUp = !isTailUp;
    }

    /// <summary>
    /// Works out the new position of the turtle when doing a step
    /// based on the current direction of the turtle.
    /// </summary>
    /// <returns>The new position of the turtle after doing a step</returns>
    private Point NewTurtlePos()
    {
        //Create the new position at the current turtle position
        Point newPos = new Point(turtlePos.X, turtlePos.Y);

        //Change the x or y position based on the direction
        if (direction == NORTH)
        {
            newPos.Y -= STEP_AMOUNT;
        }
        else if (direction == SOUTH)
        {
            newPos.Y += STEP_AMOUNT;
        }
        else if (direction == WEST)
        {
            newPos.X -= STEP_AMOUNT;
        }
        else
        {
            newPos.X += STEP_AMOUNT;
        }

        return newPos;
    }

    /// <summary>
    /// Make the turtle move by 1 step in the current direction.
    /// </summary>
    /// <param name="paper">Where to draw the graphics</param>
    private void Step(Graphics paper)
    {
        Pen pen1 = new Pen(Color.Black, 5);

        //Get the new position of the turtle after doing the step
        Point newPos = NewTurtlePos();

        if (isTailUp == true)
        {
            //If the tail is up then just move the turtle to the new position
            turtlePos = newPos;
        }
        else
        {
            //If the tail is down then draw a line to the new position and then
            //move the turtle to the new position.
            paper.DrawLine(pen1, turtlePos, newPos);
            turtlePos = newPos;
        }
    }

    private void openLogoProgramToolStripMenuItem_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = FILTER;
        //show dialog control and check if user clicked on the open button
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            //Open selected file
            reader = File.OpenText(openFileDialog1.FileName);

            //repeat while it is not end of file
            while (!reader.EndOfStream)
            {
                //Read line from file
                if (string == )
                {

                }
            }


            //close file
            reader.Close();
        }
    }
}

我不太清楚如何知道/读取 lgo 文件是否读取'left'、'right'、'step'等。然后执行命令直到到达文件末尾。

例如: IF 命令是 left THEN 调用 Left 方法,IF 命令是 step THEN 调用 Step 方法等

最终,应用程序将绘制徽标程序上的任何命令。

任何帮助将不胜感激!!谢谢! :)

【问题讨论】:

  • 假设每行有一个命令,看看System.IO.File.ReadAllLines,它将把文本文件的行读入字符串数组。然后使用foreach 遍历行,并使用switch 语句来决定要做什么

标签: c# drawing turtle-graphics readfile openfiledialog


【解决方案1】:

以下代码将帮助您入门! 注意它应该区分大小写...

System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName);
string line;
while ((line = file.ReadLine()) != null)
{
    System.Console.WriteLine(line);
    this.GetType().GetMethod(line.Trim())?.Invoke(this, null);
}
file.Close();

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多