Ejemplo n.º 1
0
 /// <summary>
 /// Добавить связь между вершинами с определенной стороны
 /// </summary>
 /// <param name="node">Вершина, которую необходимо добавить</param>
 /// <param name="directionNode">Сторона</param>
 public void Link(PathNode node, DirectionNode directionNode)
 {
     if (DirectionFromNode.ContainsKey(directionNode))
     {
         if (TryAddNeighbors(node))
         {
             DirectionFromNode[directionNode] = node;
         }
     }
 }
Ejemplo n.º 2
0
        public void Handle(string instruction)
        {
            AbstractNode left = null, right = null;
            AbstractNode direction = null, action = null, distance = null;
            // 声明一个栈用以存储抽象语法树
            Stack <AbstractNode> nodeStack = new Stack <AbstractNode>();

            // 以空格分隔指令字符串
            string[] words = instruction.Split(' ');

            for (int i = 0; i < words.Length; i++)
            {
                // 这里采用栈的方式来处理指令,如果遇到"and",
                // 则将其后的3个单词作为3个终结符表达式连成一个简单句子
                // SentenseNode作为"and"的右表达式,而将从栈顶弹出的表达式作为"and"的左表达式,
                // 最后将"and"表达式压栈
                if (words[i].Equals("and", StringComparison.OrdinalIgnoreCase))
                {
                    // 弹出栈顶表达式作为左表达式
                    left = nodeStack.Pop();
                    // 构造右表达式
                    string word1 = words[++i];
                    direction = new DirectionNode(word1);
                    string word2 = words[++i];
                    action = new ActionNode(word2);
                    string word3 = words[++i];
                    distance = new DistanceNode(word3);

                    right = new SentenseNode(direction, action, distance);
                    // 新表达式压栈
                    AndNode newNode = new AndNode(left, right);
                    nodeStack.Push(newNode);
                }
                // 如果是从头开始进行解释,则将前3个单词组成一个简单句子SentenseNode并将该句子压栈
                else
                {
                    // 构造左表达式
                    string word1 = words[i];
                    direction = new DirectionNode(word1);
                    string word2 = words[++i];
                    action = new ActionNode(word2);
                    string word3 = words[++i];
                    distance = new DistanceNode(word3);

                    left = new SentenseNode(direction, action, distance);

                    nodeStack.Push(left);
                }
            }
            // 全部表达式出栈
            this.node = nodeStack.Pop();
        }
Ejemplo n.º 3
0
        public void DirectionNodeSequence()
        {
            string        filename = @"Content\FireDirectionSequence.xml";
            BulletPattern pattern  = new BulletPattern();

            pattern.ParseXML(filename);

            ActionNode    testActionNode    = pattern.RootNode.GetChild(ENodeName.action) as ActionNode;
            FireNode      testFireNode      = testActionNode.GetChild(ENodeName.fire) as FireNode;
            DirectionNode testDirectionNode = testFireNode.GetChild(ENodeName.direction) as DirectionNode;

            Assert.AreEqual(ENodeType.sequence, testDirectionNode.NodeType);
        }
Ejemplo n.º 4
0
        public void DirectionNodeAbsolute()
        {
            var           filename = new Filename(@"FireDirectionAbsolute.xml");
            BulletPattern pattern  = new BulletPattern();

            pattern.ParseXML(filename.File);

            ActionNode    testActionNode    = pattern.RootNode.GetChild(ENodeName.action) as ActionNode;
            FireNode      testFireNode      = testActionNode.GetChild(ENodeName.fire) as FireNode;
            DirectionNode testDirectionNode = testFireNode.GetChild(ENodeName.direction) as DirectionNode;

            Assert.AreEqual(ENodeType.absolute, testDirectionNode.NodeType);
        }
Ejemplo n.º 5
0
 /// <summary>Initializes a new instance of the <see cref="Robot"/> class.</summary>
 /// <param name="x"> x coordinate</param>
 /// <param name="y"> y coordinate</param>
 /// <param name="direction"> default robot direction.</param>
 /// <param name="boundary"> map boundary.</param>
 public Robot(int x, int y, DirectionEnum direction, Boundary boundary)
 {
     DirectionList = new DirectionLinkedList <DirectionEnum>(
         new List <DirectionEnum>()
     {
         DirectionEnum.NORTH,
         DirectionEnum.EAST,
         DirectionEnum.SOUTH,
         DirectionEnum.WEST,
     });
     position      = new Position(x, y);
     Direction     = DirectionList.GetNodeByKey(direction);
     this.boundary = boundary;
 }
Ejemplo n.º 6
0
 /// <summary>Initializes a new instance of the <see cref="Robot"/> class.</summary>
 public Robot()
 {
     DirectionList = new DirectionLinkedList <DirectionEnum>(
         new List <DirectionEnum>()
     {
         DirectionEnum.NORTH,
         DirectionEnum.EAST,
         DirectionEnum.SOUTH,
         DirectionEnum.WEST,
     });
     position  = new Position(0, 0);
     Direction = DirectionList.Head;
     boundary  = new Boundary(0, 0, dimension - 1, dimension - 1);
 }
Ejemplo n.º 7
0
        List <DirectionNode> GetDirections(HtmlNode documentNode)
        {
            var nodes      = documentNode.SelectNodes(directionPath);
            var directions = new List <DirectionNode>();

            foreach (var node in nodes)
            {
                var direction = new DirectionNode();
                direction.directions = node.InnerText;
                directions.Add(direction);
            }

            return(directions);
        }
Ejemplo n.º 8
0
    private void OnTriggerEnter2D(Collider2D other)
    {
        DirectionNode dn = other.GetComponent <DirectionNode>();

        Debug.Log(dn.current);

        //check direction
        float   rot = 0;
        Vector3 dir = Vector3.zero;

        if (dn.current == "right")
        {
            dir = Vector3.back;
            rot = 90;
        }
        else if (dn.current == "left")
        {
            dir = Vector3.forward;
            rot = 90;
        }


        transform.Rotate(dir * rot);//back is right, forward is left
    }
Ejemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BulletMLTask"/> class.
 /// </summary>
 /// <param name="node">Node.</param>
 /// <param name="owner">Owner.</param>
 public SetDirectionTask(DirectionNode node, BulletMLTask owner) : base(node, owner)
 {
     Debug.Assert(null != Node);
     Debug.Assert(null != Owner);
 }
Ejemplo n.º 10
0
 /// <summary>Rotates the robot right.</summary>
 public void RotateRight()
 {
     Direction = Direction.next;
 }
Ejemplo n.º 11
0
 /// <summary>Rotates the robot left.</summary>
 public void RotateLeft()
 {
     Direction = Direction.prev;
 }