private void FindPathRecWithTurn(ChessPosLogic mData, ChessPosLogic tarData, List <ChessPosLogic> path)
    {
        int[] myIPos  = GetGlobalTB(mData.GlobalPos);
        int[] tarIPos = GetGlobalTB(tarData.GlobalPos);

        if (myIPos[0] == tarIPos[0] && myIPos[1] == tarIPos[1])
        {
            return;
        }
        ChessPosLogic ps = transform.parent.GetComponent <ChessPosViewBehaviour>().PosLogic;

        if (mData.IsOccupied && mData != ps)
        {
            return;
        }
        if (InTurnPos(mData.GlobalPos) && InNotTurned(myIPos, tarIPos))
        {
            string pos = GetTurnPoint(mData.GlobalPos);
            foreach (var item in mData.GetNodes)
            {
                if (item.GlobalPos == pos)
                {
                    path.Add(item);
                    FindPathRecWithTurn(item, tarData, path);
                }
            }
        }
        else
        {
            foreach (var item in mData.GetNodes)
            {
                int[] posInfo = GetGlobalTB(item.GlobalPos);

                if (myIPos[0] < tarIPos[0] && posInfo[1] == myIPos[1] && myIPos[0] < posInfo[0])
                {
                    path.Add(item);
                    FindPathRecWithTurn(item, tarData, path);
                }
                else if (myIPos[0] > tarIPos[0] && posInfo[1] == myIPos[1] && myIPos[0] > posInfo[0])
                {
                    path.Add(item);
                    FindPathRecWithTurn(item, tarData, path);
                }
                else if (myIPos[1] < tarIPos[1] && posInfo[0] == myIPos[0] && myIPos[1] < posInfo[1])
                {
                    path.Add(item);
                    FindPathRecWithTurn(item, tarData, path);
                }
                else if (myIPos[1] > tarIPos[1] && posInfo[0] == myIPos[0] && myIPos[1] > posInfo[1])
                {
                    path.Add(item);
                    FindPathRecWithTurn(item, tarData, path);
                }
            }
        }
    }
Exemple #2
0
    public static List <ChessPosLogic> AStarEx(ChessPosLogic start, ChessPosLogic end)
    {
        openList  = new List <ChessPosLogic>();
        clostList = new List <ChessPosLogic>();
        List <ChessPosLogic> res = new List <ChessPosLogic>();

        openList.Add(start);

        while (openList.Count > 0 && !openList.Contains(end))
        {
            ChessPosLogic temp = null;
            int           H    = 100000;
            foreach (var item in openList)
            {
                int dis = HeuristicsFoo(item, end);
                if (H > dis)
                {
                    H    = dis;
                    temp = item;
                }
            }
            openList.Remove(temp);
            clostList.Add(temp);

            foreach (var item in temp.GetNodes)
            {
                if ((!clostList.Contains(item) && item.CsType == ChessBoardPosType.RailWay && item.IsOccupied == false) || item.GlobalPos == end.GlobalPos)
                {
                    if (openList.Contains(item))
                    {
                    }
                    else
                    {
                        openList.Add(item);
                        item.ParentNode = temp;
                    }
                }
            }
        }

        if (openList.Count > 0)
        {
            ChessPosLogic tp = end;
            while (tp != start)
            {
                res.Add(tp);
                tp = tp.ParentNode;
            }
            res.Reverse();
        }
        return(res);
    }
    private List <ChessPosLogic> FindPath(ChessPosLogic mData, ChessPosLogic tarData)
    {
        string tarPos             = tarData.GlobalPos;
        string mPos               = mData.GlobalPos;
        List <ChessPosLogic> path = new List <ChessPosLogic>();

        if (mData.CsType == ChessBoardPosType.RailWay && tarData.CsType == ChessBoardPosType.RailWay)
        {
            if (GetChessType() == ChessType.GongBing)
            {
                path = AStar.AStarEx(mData, tarData);
            }
            else
            {
                int[] myIPos  = GetGlobalTB(mPos);
                int[] tarIPos = GetGlobalTB(tarPos);
                if (IsNeedTurn(mData, tarData))//需要转弯
                {
                    FindPathRecWithTurn(mData, tarData, path);
                }
                else if (myIPos[0] != tarIPos[0] && myIPos[1] != tarIPos[1])//不能到达
                {
                }
                else  //一般寻路
                {
                    FindPathRec(mData, tarData, path);
                }
            }
        }
        else
        {
            foreach (var item in mData.GetNodes)
            {
                if (item == tarData)
                {
                    if (tarData.CsType == ChessBoardPosType.XY && tarData.IsOccupied)
                    {
                        return(path);
                    }
                    path.Add(item);
                }
            }
        }
        return(path);
    }
    private void SetPosInfo()
    {
        for (int i = 0; i < 30; i++)
        {
            int row = i / 5;
            int cow = i % 5;
            TeamGreen.Add(new ChessPosLogic("GR" + row + "-" + cow, (row + 11) + "-" + (cow + 6)));
            TeamBlank.Add(new ChessPosLogic("BA" + row + "-" + cow, (6 + cow) + "-" + (5 - row)));
            TeamBlue.Add(new ChessPosLogic("BL" + row + "-" + cow, (5 - row) + "-" + (10 - cow)));
            TeamYellow.Add(new ChessPosLogic("YE" + row + "-" + cow, (10 - cow) + "-" + (11 + row)));
        }
        int cnt = 0;

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                if (i % 2 == 0 && j % 2 == 0)
                {
                    Mid.Add(new ChessPosLogic("MD" + (cnt / 3) + "-" + (cnt % 3), (6 + i) + "-" + (6 + j), false, ChessBoardPosType.RailWay));
                    cnt++;
                }
            }
        }

        for (int i = 0; i < 17; i++)
        {
            GlobalChessBorad.Add(new List <ChessPosLogic>());
            for (int j = 0; j < 17; j++)
            {
                ChessPosLogic tep = FindPosWithPos(i + "-" + j);
                if (tep != null)
                {
                    GlobalChessBorad[i].Add(tep);
                }
                else
                {
                    GlobalChessBorad[i].Add(null);
                }
            }
        }
    }
    private void FindPathRec(ChessPosLogic mData, ChessPosLogic tarData, List <ChessPosLogic> path)
    {
        int[] myIPos  = GetGlobalTB(mData.GlobalPos);
        int[] tarIPos = GetGlobalTB(tarData.GlobalPos);

        if (myIPos[0] == tarIPos[0] && myIPos[1] == tarIPos[1])
        {
            return;
        }
        if (mData.IsOccupied && mData != transform.parent.GetComponent <ChessPosViewBehaviour>().PosLogic)
        {
            return;
        }

        foreach (var item in mData.GetNodes)
        {
            int[] posInfo = GetGlobalTB(item.GlobalPos);
            if (myIPos[0] < tarIPos[0] && posInfo[1] == myIPos[1] && myIPos[0] < posInfo[0])
            {
                path.Add(item);
                FindPathRec(item, tarData, path);
            }
            else if (myIPos[0] > tarIPos[0] && posInfo[1] == myIPos[1] && myIPos[0] > posInfo[0])
            {
                path.Add(item);
                FindPathRec(item, tarData, path);
            }
            else if (myIPos[1] < tarIPos[1] && posInfo[0] == myIPos[0] && myIPos[1] < posInfo[1])
            {
                path.Add(item);
                FindPathRec(item, tarData, path);
            }
            else if (myIPos[1] > tarIPos[1] && posInfo[0] == myIPos[0] && myIPos[1] > posInfo[1])
            {
                path.Add(item);
                FindPathRec(item, tarData, path);
            }
        }
    }
    private void FindDirInMid(ChessPosLogic ogl, int tp, int wb)
    {
        ChessPosLogic tep = FindPosWithPos("MD" + (tp - 1) + "-" + wb);

        if (tep != null)
        {
            ogl.AddNodes(tep);
        }
        tep = FindPosWithPos("MD" + tp + "-" + (wb + 1));
        if (tep != null)
        {
            ogl.AddNodes(tep);
        }
        tep = FindPosWithPos("MD" + (tp + 1) + "-" + wb);
        if (tep != null)
        {
            ogl.AddNodes(tep);
        }
        tep = FindPosWithPos("MD" + tp + "-" + (wb - 1));
        if (tep != null)
        {
            ogl.AddNodes(tep);
        }
    }
 public void Init()
 {
     PosLogic = ChessBoardLogicData.Instance.FindPosWithPos(name);
 }
 private void FindDir(ChessPosLogic ogl, int tp, int wb)
 {
     if (ogl.CsType == ChessBoardPosType.XY)
     {
         ChessPosLogic tep = FindPosWithPos((tp - 1) + "-" + (wb - 1));
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
         tep = FindPosWithPos((tp - 1) + "-" + wb);
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
         tep = FindPosWithPos((tp - 1) + "-" + (wb + 1));
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
         tep = FindPosWithPos(tp + "-" + (wb + 1));
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
         tep = FindPosWithPos((tp + 1) + "-" + (wb + 1));
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
         tep = FindPosWithPos((tp + 1) + "-" + wb);
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
         tep = FindPosWithPos((tp + 1) + "-" + (wb - 1));
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
         tep = FindPosWithPos(tp + "-" + (wb - 1));
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
     }
     else
     {
         ChessPosLogic tep = FindPosWithPos((tp - 1) + "-" + (wb - 1));
         if (tep != null)
         {
             if (tep.CsType == ChessBoardPosType.XY)
             {
                 ogl.AddNodes(tep);
             }
         }
         tep = FindPosWithPos((tp - 1) + "-" + wb);
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
         tep = FindPosWithPos((tp - 1) + "-" + (wb + 1));
         if (tep != null)
         {
             if (tep.CsType == ChessBoardPosType.XY)
             {
                 ogl.AddNodes(tep);
             }
         }
         tep = FindPosWithPos(tp + "-" + (wb + 1));
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
         tep = FindPosWithPos((tp + 1) + "-" + (wb + 1));
         if (tep != null)
         {
             if (tep.CsType == ChessBoardPosType.XY)
             {
                 ogl.AddNodes(tep);
             }
         }
         tep = FindPosWithPos((tp + 1) + "-" + wb);
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
         tep = FindPosWithPos((tp + 1) + "-" + (wb - 1));
         if (tep != null)
         {
             if (tep.CsType == ChessBoardPosType.XY)
             {
                 ogl.AddNodes(tep);
             }
         }
         tep = FindPosWithPos(tp + "-" + (wb - 1));
         if (tep != null)
         {
             ogl.AddNodes(tep);
         }
     }
 }
    private void SetPosType(ChessBoardPosType Type, string Pos)
    {
        ChessPosLogic tp = FindPosWithPos(Pos);

        tp.CsType = Type;
    }
Exemple #10
0
 public void AddNodes(ChessPosLogic pos)
 {
     nodes.Add(pos);
 }
    public int OnFightClick(string pos = "")
    {
        StateFight se = StateManager.Instance.GetState() as StateFight;

        if (pos == "")
        {
            if (GetChessType() == ChessType.DiLei || transform.parent.GetComponent <ChessPosViewBehaviour>().PosLogic.CsType == ChessBoardPosType.BY)
            {
                if (se.PrevSeletedChess != null)
                {
                    se.PrevSeletedChess.GetComponent <ChessViewBehaviour>().StopAllCoroutines();
                    se.PrevSeletedChess.GetComponent <SpriteRenderer>().enabled = true;
                }
                return(-1);
            }
            if (se.PrevSeletedChess != null)
            {
                se.PrevSeletedChess.GetComponent <ChessViewBehaviour>().StopAllCoroutines();
                se.PrevSeletedChess.GetComponent <SpriteRenderer>().enabled = true;
            }
            StartCoroutine(Flash());
            AudioManager.Instance.PlayEffect(Selected);
            return(0);
        }
        else  //棋子移动
        {
            ChessPosLogic        mData      = transform.parent.GetComponent <ChessPosViewBehaviour>().PosLogic;
            ChessPosLogic        tarData    = ChessBoardLogicData.Instance.FindPosWithPos(pos);
            List <ChessPosLogic> path       = FindPath(mData, tarData);
            List <Path>          pathString = new List <Path>();
            foreach (var item in path)
            {
                Path pt = new Path();
                pt.pt = item.LocalPos;
                pathString.Add(pt);
            }
            if (path.Count <= 0)
            {
                return(0);
            }
            if (path[path.Count - 1] != tarData)
            {
                path.RemoveAll(data => { return(true); });
                return(0);
            }
            StopAllCoroutines();
            GetComponent <SpriteRenderer>().enabled = true;
            //object[] obj = new object[1];
            //AnimParameter am = new AnimParameter();
            //am.Path = path;
            //am.Start = gameObject;
            //obj[0] = am;
            //ModuleManager.Instance.Invoke("PlayChessMove", obj);
            //tarData.IsOccupied = true;
            MovResReq req = new MovResReq(pathString);
            req.start     = mData.LocalPos;
            req.targetpos = tarData.LocalPos;
            NetWorkManagement.Instance.SendProtobufCmd((int)Stype.game_server, (int)Cmd.eMovResReq, req);
            StateManager.Instance.ChangeState(StateManager.Instance.SA);
            return(1);
        }
    }
 private bool IsNeedTurn(ChessPosLogic nowPos, ChessPosLogic tarPos)
 {
     int[] myIPos  = GetGlobalTB(nowPos.GlobalPos);
     int[] tarIPos = GetGlobalTB(tarPos.GlobalPos);
     if (myIPos[1] == 6 && (myIPos[0] >= 11 && myIPos[0] <= 15))
     {
         if (tarIPos[0] == 10 && (tarIPos[1] >= 1 && tarIPos[1] <= 5))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else if (myIPos[0] == 10 && (myIPos[1] >= 1 && myIPos[1] <= 5))
     {
         if (tarIPos[1] == 6 && (tarIPos[0] >= 11 && tarIPos[0] <= 15))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else if (myIPos[0] == 6 && (myIPos[1] >= 1 && myIPos[1] <= 5))
     {
         if (tarIPos[1] == 6 && (tarIPos[0] >= 1 && tarIPos[0] <= 5))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else if (myIPos[1] == 6 && (myIPos[0] >= 1 && myIPos[0] <= 5))
     {
         if (tarIPos[0] == 6 && (tarIPos[1] >= 1 && tarIPos[1] <= 5))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else if (myIPos[0] == 6 && (myIPos[1] >= 11 && myIPos[1] <= 15))
     {
         if (tarIPos[1] == 10 && (tarIPos[0] >= 1 && tarIPos[0] <= 5))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else if (myIPos[1] == 10 && (myIPos[0] >= 1 && myIPos[0] <= 5))
     {
         if (tarIPos[0] == 6 && (tarIPos[1] >= 11 && tarIPos[1] <= 15))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else if (myIPos[0] == 10 && (myIPos[1] >= 11 && myIPos[1] <= 15))
     {
         if (tarIPos[1] == 10 && (tarIPos[0] >= 11 && tarIPos[0] <= 15))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else if (myIPos[1] == 10 && (myIPos[0] >= 11 && myIPos[0] <= 15))
     {
         if (tarIPos[0] == 10 && (tarIPos[1] >= 11 && tarIPos[1] <= 15))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
Exemple #13
0
 private static int HeuristicsFoo(ChessPosLogic start, ChessPosLogic end)
 {
     int[] posStart = ConvertPos(start.GlobalPos.Split('-'));
     int[] posEnd   = ConvertPos(end.GlobalPos.Split('-'));
     return(Math.Abs(posStart[0] - posEnd[0]) + Math.Abs(posStart[1] - posEnd[1]));
 }