Ejemplo n.º 1
0
    //private bool setBegin = false;

    public void AddPos(SearchCell cell, bool isDown = false)
    {
        if (isDraw || isDown)
        {
            //只有开始滑动的时候才有按下事件
            if (isDown)
            {
                begin     = cell.GetComponent <RectTransform> ().anchoredPosition;
                end       = begin;
                beginCell = cell;
                endCell   = beginCell;

                // Debug.Log ("鼠标落下了 --- --- ----- ----- ");
            }
            else
            {
                end     = cell.GetComponent <RectTransform> ().anchoredPosition;
                endCell = cell;
            }
            if (begin != Vector2.one && end != Vector2.one && beginCell != null && endCell != null)
            {
                AddLine();
            }
        }
    }
Ejemplo n.º 2
0
 void Update()
 {
     if (Input.GetMouseButton(0))
     {
         isDraw = true;
     }
     if (Input.GetMouseButtonUp(0))
     {
         view.HideText();
         isDraw = false;
         if (isRight)
         {
             isRight = false;
             if (!SearchController.instance.searchManager.CheckAnswer(beginCell, endCell))
             {
                 if (cloneLine != null)
                 {
                     Destroy(cloneLine.gameObject);
                 }
             }
         }
         else
         {
             if (cloneLine != null)
             {
                 Destroy(cloneLine.gameObject);
             }
         }
         begin     = Vector2.one;
         end       = Vector2.one;
         beginCell = null;
         endCell   = null;
         cloneLine = null;
     }
 }
Ejemplo n.º 3
0
        public int ManHattanDis(SearchCell scPtr_nodeEnd)
        {
            int d = Mathf.Abs(this.m_xcoord - scPtr_nodeEnd.m_xcoord) +
                    Mathf.Abs(this.m_ycoord - scPtr_nodeEnd.m_ycoord);

            return(d);
        }
Ejemplo n.º 4
0
        public void FindPath(Vector2i currentPos, Vector2i targetPos)
        {
            if (!m_initializedStartGoal)
            {
                ClearOpenList();
                ClearVisitedList();
                ClearPathtoGoal();

                //Initialize Start;
                SearchCell start = new SearchCell();
                start.m_xcoord = currentPos.x;
                start.m_ycoord = currentPos.y;

                //Initialize Goal;
                SearchCell goal = new SearchCell();
                goal.m_xcoord = targetPos.x;
                goal.m_ycoord = targetPos.y;

                SetStartAndGoal(start, goal);
                m_initializedStartGoal = true;
            }

            if (m_initializedStartGoal)
            {
                ContinuePath();
            }
        }
Ejemplo n.º 5
0
 public SearchCell(int x, int y, SearchCell scPtr_parent)
 {
     m_xcoord    = x; m_ycoord = y;
     scPtrParent = new SearchCell();
     scPtrParent = scPtr_parent;
     m_id        = y * WORLD_SIZE + x;
     //Cambiado el + y por + x;
     G = 0; H = 0;
 }
Ejemplo n.º 6
0
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            SearchCell   cell = (SearchCell)tableView.DequeueReusableCell(CellId);
            AgendaEntity agd  = GetAgenda()[indexPath.Row];

            cell.agendaLabel.Text = agd.Agenda;


            return(cell);
        }
Ejemplo n.º 7
0
        private void PathOpened(int x, int y, int newCost, SearchCell parent)
        {
            // Debug.Log(x+","+y);
            if (x < 0 || y < 0)
            {
                return;
            }
            if (x >= GameObject.Find("Main Camera").GetComponent <LoadMap>().m_cols ||
                y >= GameObject.Find("Main Camera").GetComponent <LoadMap>().m_rows)
            {
                return;
            }
            if (GameObject.Find("Main Camera").GetComponent <LoadMap>().Grid[y][x] == 'b')
            {
                return;
            }

            int id = y * WORLD_SIZE + x;

            for (int i = 0; i < scPtr_visitedList.Count; i++)
            {
                if (id == scPtr_visitedList[i].m_id)
                {
                    return;
                }
            }

            SearchCell newChild = new SearchCell(x, y, parent);

            newChild.G = newCost;
            newChild.H = parent.ManHattanDis(scPtr_goalCell);

            for (int i = 0; i < scPtr_openList.Count; i++)
            {
                if (id == scPtr_openList[i].m_id)
                {
                    int newF = newChild.G + newCost + scPtr_openList[i].H;

                    if (scPtr_openList[i].GetF() > newF)
                    {
                        scPtr_openList[i].G           = newChild.G + newCost;
                        scPtr_openList[i].scPtrParent = newChild;
                    }
                    else             //if the new F is not better
                    {
                        return;
                    }
                }
            }

            scPtr_openList.Add(newChild);
        }
Ejemplo n.º 8
0
    public void LoadWord(SearchData data)
    {
        InitColor();
        wordPositon = data.wordPositon;
        wordRow     = wordPositon.Count;
        wordCol     = wordPositon[0].Count;
        Calculate(wordRow, wordCol);

        for (int i = 0; i < wordRow; i++)
        {
            List <string> word = wordPositon[i];
            for (int j = 0; j < wordCol; j++)
            {
                RectTransform item;
                if (i == 0 && j == 0)
                {
                    item = cell;
                }
                else
                {
                    item = Instantiate(cell, wordGroup.transform);
                    item.SetSiblingIndex(i * wordCol + j);
                }
                SearchCell search_cell = item.GetComponent <SearchCell> ();
                search_cell.SetText(word[j].Substring(0, 1), scale, i, j);
                item.name             = "Search" + i.ToString() + j.ToString();
                item.anchoredPosition = new Vector3(cell.anchoredPosition.x + j * min, cell.anchoredPosition.y - i * min);
                item.gameObject.SetActive(true);
                if (word[j].Length > 1)
                {
                    if (hintInital.Count != 0 && hintInital.Contains(i * wordCol + j))
                    {
                        search_cell.ChangeBgColor(Color.red);
                    }
                    else
                    {
                        inital.Add(search_cell);
                    }
                }
            }
        }
        float moveX = areaWidth - wordCol * min;
        float moveY = areaHeight - wordRow * min;

        wordGroup.anchoredPosition = new Vector2(wordGroup.anchoredPosition.x + moveX / 2, wordGroup.anchoredPosition.y - moveY / 2);

        SearchController.instance.drawer.SetColor(colors[rightCount]);
        SearchController.instance.drawer.SetWidth(min);
        ShowAnswerWord(data.answer);
    }
Ejemplo n.º 9
0
        private void SetStartAndGoal(SearchCell start, SearchCell goal)
        {
            SearchCell nullSc = new SearchCell();

            nullSc.m_xcoord = -9999;
            scPtr_startCell = new SearchCell(start.m_xcoord, start.m_ycoord, nullSc);
            scPtr_goalCell  = new SearchCell(goal.m_xcoord, goal.m_ycoord, goal);

            scPtr_startCell.G = 0;
            scPtr_startCell.H = scPtr_startCell.ManHattanDis(scPtr_goalCell);
            //m_startCell.parent = nullptr;

            scPtr_openList.Add(scPtr_startCell);
        }
Ejemplo n.º 10
0
        private void ContinuePath()
        {
            if (scPtr_openList.Count == 0)
            {
                return;
            }

            SearchCell currentCell = GetNextCell();

            if (currentCell.m_id == scPtr_goalCell.m_id)
            {
                scPtr_goalCell.scPtrParent = currentCell.scPtrParent;

                SearchCell getPath = new SearchCell();

                for (getPath = scPtr_goalCell; getPath.m_xcoord != -9999; getPath = getPath.scPtrParent)
                {
                    this.v2_pathToGoal.Add(new Vector2i(getPath.m_xcoord, getPath.m_ycoord));
                }
                m_foundGoal = true;
            }
            else
            {
                //rightSide
                PathOpened(currentCell.m_xcoord + 1, currentCell.m_ycoord, currentCell.G + 1, currentCell);
                //leftSide
                PathOpened(currentCell.m_xcoord - 1, currentCell.m_ycoord, currentCell.G + 1, currentCell);
                //Up
                PathOpened(currentCell.m_xcoord, currentCell.m_ycoord - 1, currentCell.G + 1, currentCell);
                //Down
                PathOpened(currentCell.m_xcoord, currentCell.m_ycoord + 1, currentCell.G + 1, currentCell);
                /* Diagonals */

                for (int i = 0; i < scPtr_openList.Count; i++)
                {
                    if (currentCell.m_id == scPtr_openList[i].m_id)
                    {
                        scPtr_openList.RemoveAt(i);
                    }
                }
            }
        }
Ejemplo n.º 11
0
    public bool CheckAnswer(SearchCell begin, SearchCell end)
    {
        if (begin == end)
        {
            return(false);
        }
        bool   isRight = false;
        string word    = SetAnswer(begin.pos, end.pos);

        for (int i = 0; i < answer.Count; i++)
        {
            if (answer[i].word.text.ToLower() == word.ToLower())
            {
                AddLinePos(begin.pos, end.pos, word.ToLower());
                isRight = true;
                answer[i].Done(colors[rightCount]);
                answer.RemoveAt(i);
                SearchCell sc = SearchController.instance.drawer.beginCell;
                if (inital.Contains(sc))
                {
                    inital.Remove(sc);
                }
                sc.ChangeBgColor(Color.white);
                rightCount++;
                SearchController.instance.drawer.SetColor(colors[rightCount]);
                CheckGameComplete();
                break;
            }
        }
        if (isRight)
        {
            combCount++;
            Compliment.Instance.ShowRandom(combCount);
        }
        else
        {
            combCount = 0;
        }
        return(isRight);
    }
Ejemplo n.º 12
0
        private SearchCell GetNextCell()
        {
            int        bestF     = 100000;
            int        cellIndex = -1;
            SearchCell nextCell  = new SearchCell();

            for (int i = 0; i < scPtr_openList.Count; i++)
            {
                if (scPtr_openList[i].GetF() < bestF)
                {
                    bestF     = scPtr_openList[i].GetF();
                    cellIndex = i;
                }
            }
            if (cellIndex >= 0)
            {
                nextCell = scPtr_openList[cellIndex];
                scPtr_visitedList.Add(nextCell);
                scPtr_openList.RemoveAt(cellIndex);
            }

            return(nextCell);
        }
Ejemplo n.º 13
0
    // Returns empty queue if there is no valid path. e.g. out of range or blocked.
    // Uses breadth-first search as we have a small amount of cells and need to guarantee an optimal path.
    // TODO: Consider caching a path to each cell that is queried, and returning it if it is present and valid.
    // TODO: Consider moving the allocations out to the class level. This would make the class messier but decrease garbage accumulation.
    public Queue <Vector2Int> GetPathToCell(Vector2Int targetCellPosition)
    {
        Queue <Vector2Int> pathQueue      = new Queue <Vector2Int>();
        Vector2Int         playerPosition = GetComponent <GridPosition2d>().GridPosition;

        // Position is out of range or blocked, early return.
        if (cellPositionsInRange.Contains(targetCellPosition) == false)
        {
            return(pathQueue);
        }

        Queue <SearchCell> unsearchedCellQueue = new Queue <SearchCell>();
        List <Vector2Int>  searchedCells       = new List <Vector2Int>();
        Vector2Int         searchDirection     = new Vector2Int();
        bool       hasFoundPath    = false;
        SearchCell foundTargetCell = null;

        // Start search at our current position.
        unsearchedCellQueue.Enqueue(new SearchCell(playerPosition, null));
        while (unsearchedCellQueue.Count > 0 && hasFoundPath == false)
        {
            SearchCell searchingCell = unsearchedCellQueue.Dequeue();
            // Search in x direction
            for (int x = -1; x <= 1; x++)
            {
                // Search in y direction
                for (int y = -1; y <= 1; y++)
                {
                    // This check disables searching diagonally.
                    if ((Mathf.Abs(x) + Mathf.Abs(y) > 1) == false)
                    {
                        searchDirection.x = x;
                        searchDirection.y = y;
                        SearchCell nextCell = new SearchCell(playerPosition + searchDirection, searchingCell)
                        {
                            Position     = searchingCell.Position + searchDirection,
                            SearchedFrom = searchingCell
                        };

                        // Don't re-search searched cells.
                        if (searchedCells.Contains(searchingCell.Position + searchDirection) == false)
                        {
                            searchedCells.Add(nextCell.Position);
                            if (GridUtilities.IsGridCellOccupied(ParentGrid, nextCell.Position) == false)
                            {
                                unsearchedCellQueue.Enqueue(nextCell);
                                if (nextCell.Position == targetCellPosition)
                                {
                                    foundTargetCell = nextCell;
                                    hasFoundPath    = true;
                                }
                            }
                        }
                    }
                }
            }
        }

        if (hasFoundPath)
        {
            while (foundTargetCell.Position != playerPosition)
            {
                pathQueue.Enqueue(foundTargetCell.Position);
                foundTargetCell = foundTargetCell.SearchedFrom;
            }
        }
        // Final range check.
        if (pathQueue.Count > movementPointsPerTurn)
        {
            pathQueue.Clear();
        }

        return(pathQueue);
    }
Ejemplo n.º 14
0
 public SearchCell(Vector2Int position, SearchCell searchedFrom)
 {
     Position     = position;
     SearchedFrom = searchedFrom;
 }