protected bool _HandleNeighbor(ILeeBoardItem parent, int row, int column)
        {
            if (!currentBoard.IsValid(row, column) || !currentBoard[row, column].isEmpty)
            {
                return(false);
            }

            var item = currentBoard[row, column];

            if (_was[row, column] != 0)
            {
                if (_was[row, column] != _was[parent.row, parent.column])
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            _was[row, column] = _was[parent.row, parent.column];
            item.distance     = Mathf.Min(item.distance, parent.distance + 1);
            item.state        = ELeeBoardItemState.VISITED;
            _q.Enqueue(item);

            return(false);
        }
Exemple #2
0
 public void CopyTo(ILeeBoardItem item)
 {
     if (elementId >= 0)
     {
         item.elementId = elementId;
     }
     item.type = type;
 }
 protected int _MoveTo(ILeeBoardItem current, ILeeBoardItem next)
 {
     if (current.column == next.column)
     {
         return((current.row < next.row) ? (2) : (3));
     }
     else
     {
         return((current.column < next.column) ? (0) : (1));
     }
 }
Exemple #4
0
 protected void _ViewType(ILeeBoardItem item)
 {
     if (item.type == ELeeBoardItemType.TRACK)
     {
         sprite = item.info.GetSpriteByTrackType(
             item.trackType);
     }
     else
     {
         sprite = item.info.GetSpriteByType(
             item.type);
     }
 }
Exemple #5
0
        protected void _ViewText(ILeeBoardItem item)
        {
            if (item.type == ELeeBoardItemType.ELEMENT)
            {
                _idText.text = item.elementId.ToString();
            }
            else
            {
                _idText.text = string.Empty;
            }

            _distanceText.text = item.distance.ToString();
        }
        protected ILeeBoardItem _HandleItem(ILeeBoardItem item)
        {
            int r = item.row;
            int c = item.column;

            for (int i = 0; i < _mask.Count; ++i)
            {
                if (_HandleNeighbor(item, r + _mask[i].row, c + _mask[i].column))
                {
                    return(currentBoard[r + _mask[i].row, c + _mask[i].column]);
                }
            }

            return(null);
        }
        //
        // </ Initialize >
        //

        public void EditItem(ILeeBoardItem __item)
        {
            _ResetItem();
            item = __item;
            if (__item == null)
            {
                return;
            }

            _elementId.transform.position      = item.transform.position;
            _elementId.transform.localPosition = new Vector3(
                _elementId.transform.localPosition.x,
                _elementId.transform.localPosition.y,
                0.0f);

            _UpdateUI();
            _ActivateEdges();
        }
        public void AddEdge(ILeeBoardItem __item)
        {
            if (item == null || !item.isElement || item == __item || !__item.isElement)
            {
                return;
            }

            if (!item.ContainsEdge(__item))
            {
                item.AddEdge(__item);
                __item.AddEdge(item);
                __item.state = ELeeBoardItemState.SELECTED;
            }
            else
            {
                item.RemoveEdge(__item);
                __item.RemoveEdge(item);
                __item.state = ELeeBoardItemState.DEFAULT;
            }
        }
        protected IEnumerator _IEHandlePair(ILeeBoardItem a, ILeeBoardItem b)
        {
            _ResetPairHandler();
            a.state    = ELeeBoardItemState.SELECTED; b.state = ELeeBoardItemState.SELECTED;
            a.distance = 0; b.distance = 0;

            int cur = 0; bool exist = false;

            _q.Enqueue(a); _q.Enqueue(b);
            _was[a.row, a.column] = 1;
            _was[b.row, b.column] = -1;

            while (!exist && _q.Count != 0)
            {
                while (!exist && _q.Count != 0 && cur == _q.Peek().distance)
                {
                    exist = ((_middlePoint = _HandleItem(_q.Dequeue())) != null);
                }

                ++cur;
                yield return(new WaitForSeconds(_stepDelay));
            }

            isValid = exist;

            if (exist)
            {
                List <ILeeBoardItem> path = new List <ILeeBoardItem>();
                Debug.Log("EXIST");
                yield return(StartCoroutine(
                                 _IERestorePath(a, b, _middlePoint, path)));


                var way = new LeeAlgorithmWay(a, b, path);
            }

            a.state = ELeeBoardItemState.DEFAULT; b.state = ELeeBoardItemState.DEFAULT;
        }
Exemple #10
0
            public void AddWay(ILeeBoardItem a, ILeeBoardItem b, LeeAlgorithmWay way)
            {
                if (!ContainsKey(a))
                {
                    Add(a, new Dictionary <ILeeBoardItem, List <LeeAlgorithmWay> >());
                }
                if (!ContainsKey(b))
                {
                    Add(b, new Dictionary <ILeeBoardItem, List <LeeAlgorithmWay> >());
                }

                if (!this[a].ContainsKey(b))
                {
                    this[a].Add(b, new List <LeeAlgorithmWay>());
                }
                if (!this[b].ContainsKey(a))
                {
                    this[b].Add(a, new List <LeeAlgorithmWay>());
                }

                this[a][b].Add(way);
                this[b][a].Add(way);
            }
Exemple #11
0
        //
        // < View >
        //

        public void View(ILeeBoardItem item)
        {
            _ViewType(item);
            _ViewState(item);
            _ViewText(item);
        }
        // from 0 - left, 1 - right, 2 - bottom, 3 - top
        protected IEnumerator _RestorePath(ILeeBoardItem current, ILeeBoardItem target, int from, List <ILeeBoardItem> path, int wasid)
        {
            if (current == target)
            {
                yield return(null);
            }
            else
            {
                if (current.isEmpty)
                {
                    current.state = ELeeBoardItemState.TRACKED;
                }
                yield return(new WaitForSeconds(_stepDelay));

                path.Add(current);

                List <ILeeBoardItem> items = new List <ILeeBoardItem>();

                int[] dir = new int[4];
                for (int i = 0; i < dir.Length; ++i)
                {
                    dir[i] = -1;
                }

                int mn = 100000;

                for (int i = 0; i < _mask.Count; ++i)
                {
                    var r = current.row + _mask[i].row;
                    var c = current.column + _mask[i].column;

                    if (currentBoard.IsValid(r, c) && _was[r, c] == wasid)
                    {
                        mn = Mathf.Min(mn, currentBoard[r, c].distance);
                    }
                }

                for (int i = 0; i < _mask.Count; ++i)
                {
                    var r = current.row + _mask[i].row;
                    var c = current.column + _mask[i].column;

                    if (currentBoard.IsValid(r, c) && mn == currentBoard[r, c].distance && _was[r, c] == wasid)
                    {
                        dir[_MoveTo(current, currentBoard[r, c])] = items.Count;
                        items.Add(currentBoard[r, c]);
                    }
                }

                if (from < 0)
                {
                    yield return(StartCoroutine(
                                     _RestorePath(items[0], target, _MoveTo(current, items[0]), path, wasid)));
                }
                else
                {
                    if (dir[from] >= 0)
                    {
                        yield return(StartCoroutine(
                                         _RestorePath(items[dir[from]], target, from, path, wasid)));
                    }
                    else
                    {
                        yield return(StartCoroutine(
                                         _RestorePath(items[0], target, _MoveTo(current, items[0]), path, wasid)));
                    }
                }
            }
        }
        // < restore >

        protected IEnumerator _IERestorePath(ILeeBoardItem a, ILeeBoardItem b, ILeeBoardItem middle, List <ILeeBoardItem> path)
        {
            //	Debug.Log("Maddile: " + middle.row + " " + middle.column);

            var mToa = new List <ILeeBoardItem>();
            var cor  = StartCoroutine(_RestorePath(middle, a, -1, mToa, 1));
            var mTob = new List <ILeeBoardItem>();

            yield return(StartCoroutine(_RestorePath(middle, b, -1, mTob, -1)));

            yield return(cor);

            mToa.RemoveAt(0);
            mToa.Add(a);
            mTob.Add(b);

            mToa.Reverse();

            path.AddRange(mToa);
            path.AddRange(mTob);

            for (int i = 1; i < path.Count - 1; ++i)
            {
                var f = _MoveTo(path[i - 1], path[i]);
                var s = _MoveTo(path[i], path[i + 1]);

                if (f == s)
                {
                    if (f <= 1)
                    {
                        path[i].trackType = ELeeBoardItemTrackType.HORIZONTAL;
                    }
                    else
                    {
                        path[i].trackType = ELeeBoardItemTrackType.VERTICAL;
                    }
                }
                else
                {
                    if (f == 0 && s == 2)
                    {
                        path[i].trackType = ELeeBoardItemTrackType.LEFT_TOP;
                    }
                    if (f == 0 && s == 3)
                    {
                        path[i].trackType = ELeeBoardItemTrackType.BOTTOM_LEFT;
                    }
                    if (f == 1 && s == 2)
                    {
                        path[i].trackType = ELeeBoardItemTrackType.TOP_RIGHT;
                    }
                    if (f == 1 && s == 3)
                    {
                        path[i].trackType = ELeeBoardItemTrackType.RIGHT_BOTTOM;
                    }

                    if (f == 2 && s == 1)
                    {
                        path[i].trackType = ELeeBoardItemTrackType.BOTTOM_LEFT;
                    }
                    if (f == 2 && s == 0)
                    {
                        path[i].trackType = ELeeBoardItemTrackType.RIGHT_BOTTOM;
                    }
                    if (f == 3 && s == 1)
                    {
                        path[i].trackType = ELeeBoardItemTrackType.LEFT_TOP;
                    }
                    if (f == 3 && s == 0)
                    {
                        path[i].trackType = ELeeBoardItemTrackType.TOP_RIGHT;
                    }
                }
            }
        }
Exemple #14
0
 protected void _ViewState(ILeeBoardItem item)
 {
     _spriter.color = item.info.GetColorByState(
         item.state);
 }
Exemple #15
0
 public bool ContainsEdge(ILeeBoardItem edge)
 {
     return(_edges.Contains(edge));
 }
Exemple #16
0
 public void RemoveEdge(ILeeBoardItem edge)
 {
     _edges.Remove(edge);
 }
Exemple #17
0
 public void AddEdge(ILeeBoardItem edge)
 {
     _edges.Add(edge);
 }
Exemple #18
0
 public bool IsUsed(ILeeBoardItem item)
 {
     return(_used[item.row, item.column]);
 }
Exemple #19
0
 public void AddToWay(ILeeBoardItem item)
 {
     way.Add(item);
 }
Exemple #20
0
 public LeeAlgorithmWay(ILeeBoardItem __from, ILeeBoardItem __to, List <ILeeBoardItem> __way)
 {
     to   = __to;
     from = __from;
     way  = __way;
 }
Exemple #21
0
 public ItemInfo(ILeeBoardItem item)
 {
     elementId = -1;
     type      = item.type;
 }