Exemplo n.º 1
0
 public Form1()
 {
     InitializeComponent();
     g         = draw_box.CreateGraphics();
     turtle    = new Tortoise(0, new Point(draw_box.Size.Width / 2, draw_box.Size.Height / 2));
     listToken = new Queue <string>();
     Parser    = new Parser();
 }
Exemplo n.º 2
0
        public void Parse(Queue <string> tokenList, Tortoise turtle, System.Drawing.Graphics g)
        {
            while (tokenList.Count != 0)
            {
                string token = tokenList.Dequeue();

                switch (token)
                {
                case "FORWARD":
                    System.Console.WriteLine("forward");
                    parseForward(tokenList, turtle, g);
                    break;

                case "LEFT":
                    System.Console.WriteLine("left");
                    parseLeft(tokenList, turtle, g);
                    break;

                case "RIGHT":
                    System.Console.WriteLine("right");
                    parseRight(tokenList, turtle, g);
                    break;

                case "REPEAT":
                    Console.WriteLine("repeat");
                    parseRepeat(tokenList, turtle, g);
                    break;

                case "BACKCENTER":
                    parseBackCenter(turtle);
                    break;

                case "PENUP":
                    turtle.setPenUp();
                    break;

                case "PENDOWN":
                    turtle.setPenDown();
                    break;

                case "COLOR":
                    parseColor(tokenList, turtle, g);
                    break;

                default:
                    using (Font myFont = new Font("Arial", 14))
                    {
                        g.DrawString("ERROR : Instruction expected", myFont, Brushes.Green, new Point(2, 2));
                    }
                    break;
                }
            }
        }
Exemplo n.º 3
0
        private void test_run(object sender, EventArgs e)
        {
            listToken = new Queue <string>();
            turtle    = new Tortoise(0, new Point(draw_box.Size.Width / 2, draw_box.Size.Height / 2));

            foreach (myPictureBox VARIABLE in layoutPanel.Controls)
            {
                Console.WriteLine(VARIABLE.token);
                listToken.Enqueue(VARIABLE.token);
            }

            g.Clear(Color.White);
            Parser.Parse(listToken, turtle, g);
            listToken.Clear();
        }
Exemplo n.º 4
0
        private void parseRepeat(Queue <string> tokenList, Tortoise turtle, System.Drawing.Graphics g)
        {
            int    repeatNumber = getNumber(tokenList, g);
            string token        = "";

            if (tokenList.Count != 0)
            {
                token = tokenList.Dequeue();
            }

            if (token != "LEFTBRACE")
            {
                using (Font myFont = new Font("Arial", 14))
                {
                    g.DrawString("ERROR : Opening missing after number", myFont, Brushes.Green, new Point(2, 2));
                    return;
                }
            }

            Queue <string> subTokenQueue = new Queue <string>();

            while ((tokenList.Count != 0) && (tokenList.Peek() != "RIGHTBRACE"))
            {
                subTokenQueue.Enqueue(tokenList.Dequeue());
            }

            if (tokenList.Count == 0)
            {
                using (Font myFont = new Font("Arial", 14))
                {
                    g.DrawString("ERROR : Closing missing after instructions", myFont, Brushes.Green, new Point(2, 2));
                    return;
                }
            }

            for (int i = 0; i < repeatNumber; ++i)
            {
                Parse(cloneQueue(subTokenQueue), turtle, g);
            }
            tokenList.Dequeue();
        }
Exemplo n.º 5
0
 private void parseRight(Queue <string> tokenList, Tortoise turtle, Graphics g)
 {
     turtle.turnRight(getNumber(tokenList, g));
 }
Exemplo n.º 6
0
 private void parseForward(Queue <string> tokenList, Tortoise turtle, System.Drawing.Graphics g)
 {
     turtle.updateAndMove(getNumber(tokenList, g), g);
 }
Exemplo n.º 7
0
 private void parseColor(Queue <string> tokenList, Tortoise turtle, Graphics g)
 {
     turtle.changeColor(getNumber(tokenList, g));
 }
Exemplo n.º 8
0
 private void parseBackCenter(Tortoise turtle)
 {
     turtle.goCenter();
 }