Example #1
0
        /// <summary>
        /// 在指定的范围内获取一个Cell
        /// </summary>
        /// <param name="range">0是代表只选取center,小于0是代表全范围,只是计算center这点,size大于一时可能会超出Range的范围</param>
        ///<param name="random">是否全部随机,如果不是就从近到远获取</param>
        ///<param name="exceptRange">排除的范围,0表示排除center,只是排除center这点,并不会影响到全范围的cell</param>
        public HexagonsWithCenter FindClearCellsInRange(int size, HexCell center, int range = -1, int exceptRange = -1, bool random = false)
        {
            if (size <= 1)
            {
                HexCell cell = FindClearCellInRange(center, range, exceptRange, random);
                if (cell != null)
                {
                    return(new HexagonsWithCenter(cell));
                }
                else
                {
                    return(null);
                }
            }

            if (range == 0)
            {
                if (center.isTaken)
                {
                    return(GetSpiralRingsWithSameHeight(center, size - 1));
                }
                else
                {
                    return(null);
                }
            }

            if (random)
            {
                //从全范围内随机
                List <HexCell> tempList = null;
                if (exceptRange >= 0)
                {
                    //被除队的范围
                    List <HexCell> exceptTempList = center.GetSpiralRings(exceptRange);
                    if (range < 0)
                    {
                        tempList = cells.Except(exceptTempList).ToList <HexCell>();
                    }
                    else
                    {
                        tempList = center.GetSpiralRings(range).Except(exceptTempList).ToList <HexCell>();
                    }
                }
                else
                {
                    if (range < 0)
                    {
                        tempList = new List <HexCell>(cells);
                    }
                    else
                    {
                        tempList = center.GetSpiralRings(range);
                    }
                }

                Untility.Tool.RandSortList <HexCell>(ref tempList);
                foreach (HexCell tempCell in tempList)
                {
                    if (!tempCell.isTaken)
                    {
                        HexagonsWithCenter result = GetSpiralRingsWithSameHeight(tempCell, size - 1);
                        if (result != null)
                        {
                            return(result);
                        }
                    }
                }
                return(null);
            }
            else
            {
                //从近到远
                int i        = exceptRange >= 0 ? exceptRange + 1 : 0;
                int maxRange = range > 0 ? range : int.MaxValue;
                for (; i <= maxRange; i++)
                {
                    List <HexCell> tempList = center.GetRings(i);
                    if (tempList.Count <= 0)
                    {
                        break;
                    }
                    Untility.Tool.RandSortList <HexCell>(ref tempList);
                    foreach (HexCell tempCell in tempList)
                    {
                        if (!tempCell.isTaken)
                        {
                            HexagonsWithCenter result = GetSpiralRingsWithSameHeight(tempCell, size - 1);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                }
                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// 在指定的范围内获取一个Cell
        /// </summary>
        /// <param name="range">0是代表只选取center</param>
        ///<param name="random">是否全部随机,如果不是就从近到远获取</param>
        ///<param name="exceptRange">排除的范围,0表示排除center</param>
        public HexCell FindClearCellInRange(HexCell center, int range = -1, int exceptRange = -1, bool random = false)
        {
            if (range == 0)
            {
                if (center.isTaken)
                {
                    return(center);
                }
                else
                {
                    return(null);
                }
            }

            if (random)
            {
                //从全范围内随机
                List <HexCell> tempList = null;
                if (exceptRange >= 0)
                {
                    //被除队的范围
                    List <HexCell> exceptTempList = center.GetSpiralRings(exceptRange);
                    if (range < 0)
                    {
                        tempList = cells.Except(exceptTempList).ToList <HexCell>();
                    }
                    else
                    {
                        tempList = center.GetSpiralRings(range).Except(exceptTempList).ToList <HexCell>();
                    }
                }
                else
                {
                    if (range < 0)
                    {
                        tempList = new List <HexCell>(cells);
                    }
                    else
                    {
                        tempList = center.GetSpiralRings(range);
                    }
                }

                Untility.Tool.RandSortList <HexCell>(ref tempList);
                foreach (HexCell tempCell in tempList)
                {
                    if (!tempCell.isTaken)
                    {
                        return(tempCell);
                    }
                }
                return(null);
            }
            else
            {
                //从近到远
                int i        = exceptRange >= 0 ? exceptRange + 1: 0;
                int maxRange = range > 0 ? range : int.MaxValue;
                for (; i <= maxRange; i++)
                {
                    List <HexCell> tempList = center.GetRings(i);
                    if (tempList.Count < 0)
                    {
                        break;
                    }
                    Untility.Tool.RandSortList <HexCell>(ref tempList);
                    foreach (HexCell tempCell in tempList)
                    {
                        if (!tempCell.isTaken)
                        {
                            return(tempCell);
                        }
                    }
                }
                return(null);
            }
        }