我需要创建一个应用程序,该应用程序将解释Logo编程语言的子集并在Logo程序中执行操作。我的应用程序将读取徽标程序并在程序内执行操作。徽标程序的每一行仅包含1个徽标命令。我无法访问或看到徽标文件,并且不知道如何读取此lgo文件的内容。
这些是给出的命令:
左:将乌龟的方向向左旋转90度
右:将乌龟的方向向右旋转90度
Tail:如果尾巴向上,则放下,反之亦然
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'等。然后执行命令,直到到达文件末尾。
例如:如果命令已离开,则调用Left方法,如果命令是步骤,然后调用Step方法,等等
最终,应用程序将绘制徽标程序上的所有命令。
任何帮助将不胜感激!谢谢! :)
以下代码将帮助您入门!当心它应该区分大小写...
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();
欢呼声