Example #1
0
    /// <summary>
    /// 清除规则里物品的方法
    /// </summary>
    /// <returns></returns>
    private bool ClearAllMatchedSweet()
    {
        bool needRefill = false;         //是否需要填充

        for (int y = 0; y < yHang; y++)
        {
            for (int x = 0; x < xLie; x++)
            {
                if (sweets[x, y].CanClear())                 //如果可以清除
                {
                    List <GameSweet> matchList = MatchSweets(sweets[x, y], x, y);

                    if (matchList != null)                                                     //需要消除
                    {
                        SweetsType specialSweetsType = SweetsType.COUNT;                       //定义一个枚举类型:COUNT——是否产生特殊甜品:默认是Count类型

                        GameSweet randomSweet   = matchList[Random.Range(0, matchList.Count)]; //随机产生位置
                        int       specialSweetX = randomSweet.X;
                        int       specialSweetY = randomSweet.Y;


                        if (matchList.Count == 4)                                                                                    //消除的4个物品
                        {
                            specialSweetsType = (SweetsType)Random.Range((int)SweetsType.HANG_CLEAR, (int)SweetsType.LIE_CLEAR + 1); //特殊类型赋值:取左不取右,所以+1
                        }
                        else if (matchList.Count >= 5)
                        {
                            specialSweetsType = SweetsType.RAINBOWCANDY;
                        }
                        //5个

                        for (int i = 0; i < matchList.Count; i++)                         //遍历数组
                        {
                            if (ClearSweet(matchList[i].X, matchList[i].Y))
                            {
                                needRefill = true;                                 //填充
                            }
                        }

                        if (specialSweetsType != SweetsType.COUNT)                                                //有特殊类型
                        {
                            Destroy(sweets[specialSweetX, specialSweetY]);                                        //删除空白物品
                            GameSweet newSweet = CreateNewSweet(specialSweetX, specialSweetY, specialSweetsType); //生成特殊甜品
                            if (specialSweetsType == SweetsType.HANG_CLEAR || specialSweetsType == SweetsType.LIE_CLEAR &&
                                newSweet.CanColor() &&
                                matchList[0].CanColor())                                                  //种类的确定
                            {
                                newSweet.ColorComponet.SetColor(matchList[0].ColorComponet.Color);        //给特殊物品,着色:第一个物品的颜色
                            }
                            else if (specialSweetsType == SweetsType.RAINBOWCANDY && newSweet.CanColor()) //如果是彩虹堂
                            {
                                newSweet.ColorComponet.SetColor(ColorSweet.ColorType.ANY);
                            }
                        }
                    }
                }
            }
        }
        return(needRefill);
    }
Example #2
0
    private bool ClearAllMatchedSweet()
    {
        bool needRefill = false;//默认不需要重新填充

        for (int y = 0; y < yRow; y++)
        {
            for (int x = 0; x < xColumn; x++)
            {
                if (sweets[x, y].CanClear())
                {
                    List <GameSweet> matchList = MatchSweets(sweets[x, y], x, y);
                    if (matchList != null)
                    {
                        SweetsType specialSweetsType = SweetsType.COUNT;//决定是否产生特殊甜品

                        GameSweet randomSweet   = matchList[Random.Range(0, matchList.Count)];
                        int       specialSweetX = randomSweet.X;
                        int       specialSweetY = randomSweet.Y;
                        //四个产生行列消除
                        if (matchList.Count == 4)
                        {
                            specialSweetsType = (SweetsType)Random.Range((int)SweetsType.ROW_CLEAR, (int)SweetsType.COLUMN_CLEAR);
                        }
                        //五个产生彩虹糖
                        else if (matchList.Count >= 5)
                        {
                            specialSweetsType = SweetsType.RAINBOWCANDY;
                        }

                        for (int i = 0; i < matchList.Count; i++)
                        {
                            if (ClearSweet(matchList[i].X, matchList[i].Y))
                            {
                                needRefill = true;
                            }
                        }

                        if (specialSweetsType != SweetsType.COUNT)
                        {
                            Destroy(sweets[specialSweetX, specialSweetY]);
                            GameSweet newSweet = CreatNewSweet(specialSweetX, specialSweetY, specialSweetsType);

                            if ((specialSweetsType == SweetsType.ROW_CLEAR || specialSweetsType == SweetsType.COLUMN_CLEAR) &&
                                newSweet.CanColor() && matchList[0].CanColor())
                            {
                                newSweet.ColoredComponent.SetColor(matchList[0].ColoredComponent.Color);
                            }
                            //加上彩虹糖特殊类型的产生
                            else if (specialSweetsType == SweetsType.RAINBOWCANDY && newSweet.CanColor())
                            {
                                newSweet.ColoredComponent.SetColor(ColorSweet.ColorType.ANY);
                            }
                        }
                    }
                }
            }
        }
        return(needRefill);
    }
Example #3
0
    private bool ClearAllMatchedSweet()//清除所有块方法
    {
        if (isGameOver)
        {
            return(false);
        }
        print("清除所有块方法");
        bool needRefill = false;

        for (int y = 0; y < yRows; y++)
        {
            for (int x = 0; x < xColumns; x++)
            {
                if (sweetsarray[y, x].CanClear())
                {
                    List <GameSweet> matchlist = MatchSweets(sweetsarray[y, x], x, y);
                    if (matchlist != null)
                    {
                        SweetsType specialSweetsType = SweetsType.COUNT;//特殊类甜品产生flag;
                        GameSweet  randomSweet       = matchlist[Random.Range(0, matchlist.Count)];
                        int        specialSweetX     = randomSweet.X;
                        int        specialSweetY     = randomSweet.Y;
                        if (matchlist.Count == 4)
                        {
                            specialSweetsType = (SweetsType)Random.Range((int)SweetsType.ROW_CLEAR, (int)SweetsType.COLUMN_CLEAR);
                        }
                        //5个同样块消除时判定是行还是列  RainbowSweet产生条件
                        else if (matchlist.Count > 4)
                        {
                            specialSweetsType = SweetsType.RAINBOWCANDY;
                        }
                        for (int i = 0; i < matchlist.Count; i++)//---清除符合条件的所有块
                        {
                            if (ClearSweet(matchlist[i].X, matchlist[i].Y))
                            {
                                needRefill = true;
                            }
                        }

                        if (specialSweetsType != SweetsType.COUNT)
                        {
                            Destroy(sweetsarray[specialSweetY, specialSweetX]);
                            GameSweet newsweet = CreateSweet(specialSweetX, specialSweetY, specialSweetsType);
                            if (newsweet.CanColor() && specialSweetsType == SweetsType.ROW_CLEAR || specialSweetsType == SweetsType.COLUMN_CLEAR)
                            {
                                newsweet.ColorComponent.SetColor(matchlist[0].ColorComponent.Color);
                            }
                            else if (specialSweetsType == SweetsType.RAINBOWCANDY && newsweet.CanColor())
                            {
                                newsweet.ColorComponent.SetColor(ColorSweet.ColorEnum.ANYCOLORS);
                            }
                        }
                    }
                }
            }
        }
        return(needRefill);
    }
Example #4
0
    //清除全部完成匹配的甜品
    private bool ClearAllMatchedSweet()
    {
        bool needRefill = false;//是否重新填充

        //遍历场景是否存在可清除的甜品
        for (int y = 0; y < yRow; y++)
        {
            for (int x = 0; x < xColumn; x++)
            {
                if (sweets[x, y].CanClear())
                {
                    List <GameSweet> matchList = MatchSweets(sweets[x, y], x, y);                  //用一个列表去接收匹配成功的返回来的甜品列表
                    if (matchList != null)
                    {                                                                              //如果不为空则遍历列表中的每一个甜品对象进行清除
                        SweetType specialSweetsType = SweetType.COUNT;                             //定义一个特殊类型的甜品
                        GameSweet randomSweet       = matchList[Random.Range(0, matchList.Count)]; //随机一个匹配成功之后的糖果选定一个,即3个选一个
                        int       specialSweetX     = randomSweet.X;
                        int       specialSweetY     = randomSweet.Y;


                        if (matchList.Count == 4)
                        {
                            //随机出一个(生成)行消除或者列消除的特殊甜品
                            specialSweetsType = (SweetType)Random.Range((int)SweetType.ROW_CLEAR, (int)SweetType.COLUMN_CLEAR + 1);
                        }
                        else if (matchList.Count >= 5)//大于4就产生彩虹糖
                        {
                            //随机出一个(生成)行消除或者列消除的特殊甜品
                            specialSweetsType = SweetType.RAINBOWCANDY;
                        }


                        for (int i = 0; i < matchList.Count; i++)
                        {
                            if (ClearSweet(matchList[i].X, matchList[i].Y))
                            {
                                needRefill = true;//是否重新填充
                            }
                        }

                        if (specialSweetsType != SweetType.COUNT)
                        {                                                                                         //有问题,需要填充,没有为true,还是昨晚想到的那个
                            //Destroy(sweets[specialSweetX, specialSweetY]);
                            GameSweet newSweet = CreateNewSweet(specialSweetX, specialSweetY, specialSweetsType); //生成特殊甜品对象

                            if (specialSweetsType == SweetType.ROW_CLEAR || specialSweetsType == SweetType.COLUMN_CLEAR && matchList[0].CanColor() && newSweet.CanColor())
                            {
                                newSweet.ColoredComponent.SetColor(matchList[0].ColoredComponent.Color);
                            }
                            if (specialSweetsType == SweetType.RAINBOWCANDY && newSweet.CanColor())
                            {
                                newSweet.ColoredComponent.SetColor(ColorType.COLORS);
                            }
                        }
                    }
                }
            }
        }
        return(needRefill);
    }
Example #5
0
    /// <summary>
    /// 匹配方法
    /// </summary>
    /// <param name="sweet">当前选中甜品</param>
    /// <param name="newX">位置,x坐标</param>
    /// <param name="newY">位置,Y坐标</param>
    /// <returns></returns>
    public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY)
    {
        if (sweet.CanColor())
        {
            ColorSweet.ColorType color                  = sweet.ColoredComponent.Color;
            List <GameSweet>     matchRowSweets         = new List <GameSweet>();
            List <GameSweet>     matchLineSweets        = new List <GameSweet>();
            List <GameSweet>     finishedMatchingSweets = new List <GameSweet>();

            //行匹配
            matchRowSweets.Add(sweet);

            //i=0代表往左,i=1代表往右
            for (int i = 0; i <= 1; i++)
            {
                for (int xDistance = 0; xDistance < xColumn; xDistance++)
                {
                    int x;
                    if (i == 0)
                    {
                        x = newX - xDistance;
                    }
                    else
                    {
                        x = newX + xDistance;
                    }
                    if (x < 0 || x >= xColumn)
                    {
                        break;
                    }

                    if (sweets[x, newY].CanColor() && sweets[x, newY].ColoredComponent.Color == color)
                    {
                        matchRowSweets.Add(sweets[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //检查一下当前行遍历列表中的元素数量是否大于3
            if (matchRowSweets.Count >= 3)
            {
                for (int i = 0; i < matchRowSweets.Count; i++)
                {
                    finishedMatchingSweets.Add(matchRowSweets[i]);
                }
            }

            if (finishedMatchingSweets.Count >= 3)
            {
                return(finishedMatchingSweets);
            }
        }

        return(null);
    }
Example #6
0
    private bool ClearMatchedSweet()
    {
        bool isNeedFill = false;

        for (int x = 0; x < xColumn; x++)
        {
            for (int y = 0; y < yRow; y++)
            {
                if (sweets[x, y].CanClear())
                {
                    List <GameSweet> matchList = MatchSweets(sweets[x, y], x, y);
                    if (matchList != null)
                    {
                        SweetsType specialType = SweetsType.COUNT;

                        GameSweet randomSweet = matchList[Random.Range(0, matchList.Count)];
                        int       specialX    = randomSweet.X;
                        int       specialY    = randomSweet.Y;

                        if (matchList.Count == 4)
                        {
                            specialType = (SweetsType)Random.Range((int)SweetsType.ROW_CLEAR, (int)SweetsType.COLUMN_CLEAR);
                        }

                        if (matchList.Count == 5)
                        {
                            specialType = SweetsType.RAINBOWCANDY;
                        }

                        for (int i = 0; i < matchList.Count; i++)
                        {
                            ClearSweet(matchList[i].X, matchList[i].Y);
                            isNeedFill = true;
                        }

                        if (specialType != SweetsType.COUNT)
                        {
                            Destroy(sweets[specialX, specialY]);
                            GameSweet gameSweet = CreateNewSweet(specialX, specialY, specialType);
                            if ((gameSweet.Type == SweetsType.ROW_CLEAR || gameSweet.Type == SweetsType.COLUMN_CLEAR) &&
                                gameSweet.CanColor() && matchList[0].CanColor())
                            {
                                gameSweet.ColorCommpont.SetColor(matchList[0].ColorCommpont.Color);
                            }
                            else if (gameSweet.Type == SweetsType.RAINBOWCANDY)
                            {
                                gameSweet.ColorCommpont.SetColor(ColorSweet.ColorType.ANY);
                            }
                        }
                    }
                }
            }
        }

        return(isNeedFill);
    }
Example #7
0
    /// <summary>
    /// 清除全部完成匹配的甜品
    /// </summary>
    /// <returns></returns>
    private bool ClearAllMatchedSweet()
    {
        bool needRefill = false;

        for (int y = 0; y < yRow; y++)
        {
            for (int x = 0; x < xCol; x++)
            {
                if (sweetArr[x, y].CanClear())
                {
                    List <GameSweet> matchList = MatchSweets(sweetArr[x, y], x, y);

                    if (matchList != null)
                    {
                        //确认是否产生特殊甜品
                        SweetsType sweetsType = SweetsType.Count;
                        //随机获取消除列表中的坐标
                        GameSweet randomSweet = matchList[Random.Range(0, matchList.Count)];
                        //消除4个生成行或列消除甜品
                        if (matchList.Count == 4)
                        {
                            sweetsType = (SweetsType)Random.Range((int)SweetsType.Row_Clear, (int)SweetsType.Column_Clear);
                        }
                        //消除5个生成彩虹糖
                        else if (matchList.Count >= 5)
                        {
                            sweetsType = SweetsType.RainbowCandy;
                        }

                        for (int i = 0; i < matchList.Count; i++)
                        {
                            if (ClearSweet(matchList[i].X, matchList[i].Y))
                            {
                                needRefill = true;
                            }
                        }
                        //创建行列消除特殊甜品
                        if (sweetsType != SweetsType.Count)
                        {
                            Destroy(sweetArr[randomSweet.X, randomSweet.Y]);
                            GameSweet newSweet = CreateNewSweet(randomSweet.X, randomSweet.Y, sweetsType);
                            if (sweetsType == SweetsType.Row_Clear || sweetsType == SweetsType.Column_Clear && newSweet.CanColor() && matchList[0].CanColor())
                            {
                                newSweet.colorSweet.SetSprite(matchList[0].colorSweet.colorType);
                            }
                            else if (sweetsType == SweetsType.RainbowCandy & newSweet.CanColor())
                            {
                                newSweet.colorSweet.SetSprite(ColorType.Any);
                            }
                        }
                    }
                }
            }
        }
        return(needRefill);
    }
Example #8
0
    /// <summary>
    /// 清除游戏中所有的符合清除条件的甜品 以及在清除时判断是否能生成特殊功能的甜品
    /// </summary>
    /// <returns></returns>
    public bool ClearAllMatchSweet()
    {
        bool needReFill = false;//是否需要再次填充

        for (int y = 0; y < yRow; y++)
        {
            for (int x = 0; x < xColumn; x++)
            {
                if (sweets[x, y].CanClear())//如果该位置可以被消除
                {
                    List <GameSweet> matchedSweets = MatchSweets(sweets[x, y], x, y);
                    if (matchedSweets != null)
                    {
                        SweetType specialSweetype = SweetType.COUNT;//该类型表示特殊甜品的类型:行消除列消除彩虹糖等甜品(匹配到的甜品数量大于4时才生成特殊甜品)
                        GameSweet speciaSweet     = matchedSweets[Random.Range(0, matchedSweets.Count)];
                        int       specialSweetX   = speciaSweet.X;
                        int       specialSweetY   = speciaSweet.Y;
                        if (matchedSweets.Count == 4)//如果为4个,则随机生成行消除甜品或列消除甜品
                        {
                            specialSweetype = (SweetType)Random.Range((int)SweetType.ROW_CLEAR, (int)SweetType.COLUMN_CLEAR);
                        }
                        else if (matchedSweets.Count >= 5)//如股票大于等于5个,则生成彩虹糖
                        {
                            specialSweetype = SweetType.RAINBOWCANDY;
                        }
                        for (int i = 0; i < matchedSweets.Count; i++)
                        {
                            if (ClearSweet(matchedSweets[i].X, matchedSweets[i].Y))
                            {
                                needReFill = true;
                            }
                        }
                        if (specialSweetype != SweetType.COUNT)
                        {
                            Destroy(sweets[specialSweetX, specialSweetY]);
                            GameSweet newSweet = CreatNewSweet(specialSweetX, specialSweetY, specialSweetype);
                            if (specialSweetype == SweetType.COLUMN_CLEAR || specialSweetype == SweetType.ROW_CLEAR && newSweet.CanColor() && matchedSweets[0].CanColor())
                            {
                                newSweet.ColorSweetComponent.SetColor(matchedSweets[0].ColorSweetComponent.Color);
                            }
                            //彩虹糖的产生
                            else if (specialSweetype == SweetType.RAINBOWCANDY && newSweet.CanColor())
                            {
                                newSweet.ColorSweetComponent.SetColor(ColorSweet.ColorType.ANY);
                            }
                        }
                    }
                }
            }
        }
        return(needReFill);
    }
Example #9
0
    /// <summary>
    /// 判断是否能进行消除甜品
    /// </summary>
    /// <param name="sweet"></param>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <returns></returns>
    public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY)
    {
        if (sweet.CanMove() && sweet.CanColor())//如果当前甜品是能正常更换精灵的甜品(即不是空甜品或者不是障碍物等)
        {
            ColorSweet.ColorType color = sweet.ColorSweetComponent.Color;
            List <GameSweet>     matchRowSweetsList   = new List <GameSweet>(); //用来做横向匹配的链表
            List <GameSweet>     matchColumnSweetList = new List <GameSweet>(); // 用来做纵向匹配的链表
            List <GameSweet>     finishedMatchList    = new List <GameSweet>(); //匹配结束后的链表,同时返回该链表
            //行匹配
            matchRowSweetsList.Add(sweet);
            for (int i = 0; i <= 1; i++)//0代表往左匹配,1代表往右匹配
            {
                for (int xDistance = 1; xDistance < xColumn; xDistance++)
                {
                    int x;//和当前甜品要进行匹配的甜品的x坐标
                    if (i == 0)
                    {
                        x = newX - xDistance;
                    }
                    else
                    {
                        x = newX + xDistance;
                    }
                    if (x < 0 || x >= xColumn)//如果匹配完成则直接退出
                    {
                        break;
                    }
                    if (sweets[x, newY].CanColor() && color == sweets[x, newY].ColorSweetComponent.Color)//如果类型相同,则可以添加进链表
                    {
                        matchRowSweetsList.Add(sweets[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (matchRowSweetsList.Count >= 3)
            {
                for (int m = 0; m < matchRowSweetsList.Count; m++)
                {
                    finishedMatchList.Add(matchRowSweetsList[m]);
                }
            }

            if (matchRowSweetsList.Count >= 3)
            {
                for (int i = 0; i < matchRowSweetsList.Count; i++)
                {
                    // finishedMatchList.Add(matchRowSweetsList[i]);
                    //行匹配完成后,再进行L,T型匹配,即对每一个二元素的列在进行匹配
                    for (int j = 0; j <= 1; j++)//j=0代表向上匹配,j=1代表向下匹配
                    {
                        for (int yDistance = 1; yDistance < yRow; yDistance++)
                        {
                            int y;
                            if (j == 0)
                            {
                                y = newY - yDistance;
                            }
                            else
                            {
                                y = newY + yDistance;
                            }
                            if (y < 0 || y >= yRow)
                            {
                                break;
                            }
                            if (sweets[matchRowSweetsList[i].X, y].CanColor() && sweets[matchRowSweetsList[i].X, y].ColorSweetComponent.Color == color)
                            {
                                matchColumnSweetList.Add(sweets[matchRowSweetsList[i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    if (matchColumnSweetList.Count >= 2)//如果又在列方向找到两个或以上甜品,则添加到完成列表中
                    {
                        for (int k = 0; k < matchColumnSweetList.Count; k++)
                        {
                            finishedMatchList.Add(matchColumnSweetList[k]);
                        }
                        break;
                    }
                    else//否则进行清空
                    {
                        matchColumnSweetList.Clear();
                    }
                }
            }
            if (finishedMatchList.Count >= 3)
            {
                return(finishedMatchList);
            }
            matchRowSweetsList.Clear();
            matchColumnSweetList.Clear();
            // finishedMatchList.Clear();



            //清空后再进行列匹配
            //否则进行列匹配
            matchColumnSweetList.Add(sweet);
            for (int i = 0; i <= 1; i++)//0代表往上匹配,1代表往下匹配
            {
                for (int YDistance = 1; YDistance < yRow; YDistance++)
                {
                    int y;//和当前甜品要进行匹配的甜品的x坐标
                    if (i == 0)
                    {
                        y = newY - YDistance;
                    }
                    else
                    {
                        y = newY + YDistance;
                    }
                    if (y < 0 || y >= yRow)//如果匹配完成则直接退出
                    {
                        break;
                    }
                    if (sweets[newX, y].CanColor() && color == sweets[newX, y].ColorSweetComponent.Color)//如果类型相同,则可以添加进链表
                    {
                        matchColumnSweetList.Add(sweets[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (matchColumnSweetList.Count >= 3)
            {
                for (int m = 0; m < matchColumnSweetList.Count; m++)
                {
                    finishedMatchList.Add(matchColumnSweetList[m]);
                }
            }

            if (matchColumnSweetList.Count >= 3)//列匹配完成后再进行L,T,型匹配,即再对每一个已经确定的甜品进行行遍历
            {
                for (int i = 0; i < matchColumnSweetList.Count; i++)
                {
                    //finishedMatchList.Add(matchColumnSweetList[i]);

                    for (int j = 0; j <= 1; j++)//j=0代表向左匹配,j=1代表向右匹配
                    {
                        for (int xDistance = 1; xDistance < xColumn; xDistance++)
                        {
                            int x;
                            if (j == 0)
                            {
                                x = newX - xDistance;
                            }
                            else
                            {
                                x = newX + xDistance;
                            }
                            if (x < 0 || x >= yRow)
                            {
                                break;
                            }
                            if (sweets[x, matchColumnSweetList[i].Y].CanColor() && sweets[x, matchColumnSweetList[i].Y].ColorSweetComponent.Color == color)
                            {
                                matchRowSweetsList.Add(sweets[x, matchColumnSweetList[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    if (matchRowSweetsList.Count >= 2)//如果又在行方向找到两个或以上甜品,则添加到完成列表中
                    {
                        for (int k = 0; k < matchRowSweetsList.Count; k++)
                        {
                            finishedMatchList.Add(matchRowSweetsList[k]);
                        }
                        break;
                    }
                    else//否则进行清空
                    {
                        matchRowSweetsList.Clear();
                    }
                }
            }
            if (finishedMatchList.Count >= 3)
            {
                return(finishedMatchList);
            }
        }
        return(null);
    }
Example #10
0
    /// <summary>
    /// 匹配方法
    /// </summary>
    /// <param name="sweet"></param>
    /// <param name="newX"></param>
    /// <param name="newY"></param>
    /// <returns></returns>
    public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY)
    {
        if (sweet.CanColor())
        {
            ColorSweet.ColorType color                  = sweet.ColoredComponent.Color;
            List <GameSweet>     matchRowSweets         = new List <GameSweet>();
            List <GameSweet>     matchLineSweets        = new List <GameSweet>();
            List <GameSweet>     finishedMatchingSweets = new List <GameSweet>();

            //行匹配
            matchRowSweets.Add(sweet);

            //i=0代表往左,i=1代表往右
            for (int i = 0; i <= 1; i++)
            {
                for (int xDistance = 1; xDistance < xColumn; xDistance++)
                {
                    int x;
                    if (i == 0)
                    {
                        x = newX - xDistance;
                    }
                    else
                    {
                        x = newX + xDistance;
                    }
                    if (x < 0 || x >= xColumn)
                    {
                        break;
                    }

                    if (sweets[x, newY].CanColor() && sweets[x, newY].ColoredComponent.Color == color)
                    {
                        matchRowSweets.Add(sweets[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (matchRowSweets.Count >= 3)
            {
                for (int i = 0; i < matchRowSweets.Count; i++)
                {
                    finishedMatchingSweets.Add(matchRowSweets[i]);
                }
            }

            //L T型匹配
            //检查一下当前行遍历列表中的元素数量是否大于3
            if (matchRowSweets.Count >= 3)
            {
                for (int i = 0; i < matchRowSweets.Count; i++)
                {
                    //行匹配列表中满足匹配条件的每个元素上下依次进行列遍历
                    // 0代表上方 1代表下方
                    for (int j = 0; j <= 1; j++)
                    {
                        for (int yDistance = 1; yDistance < yRow; yDistance++)
                        {
                            int y;
                            if (j == 0)
                            {
                                y = newY - yDistance;
                            }
                            else
                            {
                                y = newY + yDistance;
                            }
                            if (y < 0 || y >= yRow)
                            {
                                break;
                            }

                            if (sweets[matchRowSweets[i].X, y].CanColor() && sweets[matchRowSweets[i].X, y].ColoredComponent.Color == color)
                            {
                                matchLineSweets.Add(sweets[matchRowSweets[i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (matchLineSweets.Count < 2)
                    {
                        matchLineSweets.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < matchLineSweets.Count; j++)
                        {
                            finishedMatchingSweets.Add(matchLineSweets[j]);
                        }
                        break;
                    }
                }
            }

            if (finishedMatchingSweets.Count >= 3)
            {
                return(finishedMatchingSweets);
            }

            matchRowSweets.Clear();
            matchLineSweets.Clear();

            matchLineSweets.Add(sweet);

            //列匹配

            //i=0代表往左,i=1代表往右
            for (int i = 0; i <= 1; i++)
            {
                for (int yDistance = 1; yDistance < yRow; yDistance++)
                {
                    int y;
                    if (i == 0)
                    {
                        y = newY - yDistance;
                    }
                    else
                    {
                        y = newY + yDistance;
                    }
                    if (y < 0 || y >= yRow)
                    {
                        break;
                    }

                    if (sweets[newX, y].CanColor() && sweets[newX, y].ColoredComponent.Color == color)
                    {
                        matchLineSweets.Add(sweets[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (matchLineSweets.Count >= 3)
            {
                for (int i = 0; i < matchLineSweets.Count; i++)
                {
                    finishedMatchingSweets.Add(matchLineSweets[i]);
                }
            }

            //L T型匹配
            //检查一下当前行遍历列表中的元素数量是否大于3
            if (matchLineSweets.Count >= 3)
            {
                for (int i = 0; i < matchLineSweets.Count; i++)
                {
                    //行匹配列表中满足匹配条件的每个元素上下依次进行列遍历
                    // 0代表上方 1代表下方
                    for (int j = 0; j <= 1; j++)
                    {
                        for (int xDistance = 1; xDistance < xColumn; xDistance++)
                        {
                            int x;
                            if (j == 0)
                            {
                                x = newY - xDistance;
                            }
                            else
                            {
                                x = newY + xDistance;
                            }
                            if (x < 0 || x >= xColumn)
                            {
                                break;
                            }

                            if (sweets[x, matchLineSweets[i].Y].CanColor() && sweets[x, matchLineSweets[i].Y].ColoredComponent.Color == color)
                            {
                                matchRowSweets.Add(sweets[x, matchLineSweets[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (matchRowSweets.Count < 2)
                    {
                        matchRowSweets.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < matchRowSweets.Count; j++)
                        {
                            finishedMatchingSweets.Add(matchRowSweets[j]);
                        }
                        break;
                    }
                }
            }

            if (finishedMatchingSweets.Count >= 3)
            {
                return(finishedMatchingSweets);
            }
        }

        return(null);
    }
Example #11
0
    //匹配方法
    public List <GameSweet> MatchSweet(GameSweet sweet, int newX, int newY)
    {
        if (sweet.CanColor())
        {
            ColorSweet.ColorType color                  = sweet.ColorComponent.Color;
            List <GameSweet>     matchRowSweets         = new List <GameSweet>();
            List <GameSweet>     matchColumnSweets      = new List <GameSweet>();
            List <GameSweet>     finishedMatchingSweets = new List <GameSweet>();

            //行匹配
            matchRowSweets.Add(sweet);
            matchColumnSweets.Add(sweet);

            for (int i = 0; i <= 1; i++)
            {
                //i=0是左边遍历,i=1是右遍历。左右各遍历一次
                for (int xDistance = 1; xDistance < xColumn; xDistance++)
                {
                    //遍历的新位置
                    int x;
                    if (i == 0)
                    {
                        x = newX - xDistance;
                    }
                    else
                    {
                        x = newX + xDistance;
                    }

                    //避免越界
                    if (x < 0 || x >= xColumn)
                    {
                        break;
                    }

                    if (sweets[x, newY].CanColor() && sweets[x, newY].ColorComponent.Color == color)
                    {
                        matchRowSweets.Add(sweets[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }

                //i=0是下边遍历,i=1是上边遍历。上下各遍历一次
                for (int yDistance = 1; yDistance < yRow; yDistance++)
                {
                    //遍历的新位置
                    int y;
                    if (i == 0)
                    {
                        //加是向下遍历
                        y = newY + yDistance;
                    }
                    else
                    {
                        y = newY - yDistance;
                    }

                    //避免越界
                    if (y < 0 || y >= yRow)
                    {
                        break;
                    }

                    if (sweets[newX, y].CanColor() && sweets[newX, y].ColorComponent.Color == color)
                    {
                        matchColumnSweets.Add(sweets[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //如果大于3,就添加到完成的数组里
            if (matchColumnSweets.Count >= 3)
            {
                matchRowSweets.Clear();

                for (int i = 0; i < matchColumnSweets.Count; i++)
                {
                    finishedMatchingSweets.Add(matchColumnSweets[i]);

                    //和上面一样,两个方向
                    for (int j = 0; j <= 1; j++)
                    {
                        int x;
                        for (int xDistance = 1; xDistance < xColumn; xDistance++)
                        {
                            if (j == 0)
                            {
                                x = newX - xDistance;
                            }
                            else
                            {
                                x = newX + xDistance;
                            }

                            if (x < 0 || x >= xColumn)
                            {
                                break;
                            }

                            if (sweets[x, matchColumnSweets[i].Y].CanColor() && sweets[x, matchColumnSweets[i].Y].ColorComponent.Color == color)
                            {
                                matchRowSweets.Add(sweets[x, matchColumnSweets[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

                //L、T型匹配
                if (matchRowSweets.Count >= 3)
                {
                    for (int i = 0; i < matchRowSweets.Count; i++)
                    {
                        finishedMatchingSweets.Add(matchRowSweets[i]);
                    }
                }

                //匹配成功
                if (finishedMatchingSweets.Count >= 3)
                {
                    return(finishedMatchingSweets);
                }
            }
            else if (matchRowSweets.Count >= 3)
            {
                matchColumnSweets.Clear();

                for (int i = 0; i < matchRowSweets.Count; i++)
                {
                    finishedMatchingSweets.Add(matchRowSweets[i]);

                    for (int j = 0; j <= 1; j++)
                    {
                        int y;
                        for (int yDistance = 1; yDistance < yRow; yDistance++)
                        {
                            if (j == 0)
                            {
                                y = newX - yDistance;
                            }
                            else
                            {
                                y = newX + yDistance;
                            }

                            if (y < 0 || y >= yDistance)
                            {
                                break;
                            }

                            if (sweets[matchColumnSweets[i].X, y].CanColor() && sweets[matchColumnSweets[i].X, y].ColorComponent.Color == color)
                            {
                                matchRowSweets.Add(sweets[matchColumnSweets[i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

                //L、T型匹配
                if (matchColumnSweets.Count >= 3)
                {
                    for (int i = 0; i < matchColumnSweets.Count; i++)
                    {
                        finishedMatchingSweets.Add(matchColumnSweets[i]);
                    }
                }

                //匹配成功
                if (finishedMatchingSweets.Count >= 3)
                {
                    return(finishedMatchingSweets);
                }
            }
        }

        return(null);
    }
    public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY)
    {
        if (sweet.CanColor())
        {
            ColorSweet.ColorType color                  = sweet.ColoredComponent.Color;
            List <GameSweet>     matchRowSweets         = new List <GameSweet>();
            List <GameSweet>     matchLineSweets        = new List <GameSweet>();
            List <GameSweet>     finishedMatchingSweets = new List <GameSweet>();

            matchRowSweets.Add(sweet);

            for (int i = 0; i <= 1; i++)
            {
                for (int xDistance = 1; xDistance < xColumn; xDistance++)
                {
                    int x;

                    if (i == 0)
                    {
                        x = newX - xDistance;
                    }
                    else
                    {
                        x = newX + xDistance;
                    }
                    if (x < 0 || x >= xColumn)
                    {
                        break;
                    }

                    if (sweets[x, newY].CanColor() && sweets[x, newY].ColoredComponent.Color == color)
                    {
                        matchRowSweets.Add(sweets[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (matchRowSweets.Count >= 3)
            {
                for (int i = 0; i < matchRowSweets.Count; i++)
                {
                    finishedMatchingSweets.Add(matchRowSweets[i]);
                }
            }

            if (matchRowSweets.Count >= 3)
            {
                for (int i = 0; i < matchRowSweets.Count; i++)
                {
                    for (int j = 0; j <= 1; j++)
                    {
                        for (int yDistance = 1; yDistance < yRow; yDistance++)
                        {
                            int y;

                            if (j == 0)
                            {
                                y = newY - yDistance;
                            }
                            else
                            {
                                y = newY + yDistance;
                            }
                            if (y < 0 || y >= yRow)
                            {
                                break;
                            }

                            if (sweets[matchRowSweets[i].X, y].CanColor() && sweets[matchRowSweets[i].X, y].ColoredComponent.Color == color)
                            {
                                matchLineSweets.Add(sweets[matchRowSweets[i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (matchLineSweets.Count < 2)
                    {
                        matchLineSweets.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < matchLineSweets.Count; j++)
                        {
                            finishedMatchingSweets.Add(matchLineSweets[j]);
                        }
                        break;
                    }
                }
            }

            if (finishedMatchingSweets.Count >= 3)
            {
                return(finishedMatchingSweets);
            }

            matchRowSweets.Clear();
            matchLineSweets.Clear();
            matchLineSweets.Add(sweet);

            for (int i = 0; i <= 1; i++)
            {
                for (int yDistance = 1; yDistance < yRow; yDistance++)
                {
                    int y;
                    if (i == 0)
                    {
                        y = newY - yDistance;
                    }
                    else
                    {
                        y = newY + yDistance;
                    }
                    if (y < 0 || y >= yRow)
                    {
                        break;
                    }

                    if (sweets[newX, y].CanColor() && sweets[newX, y].ColoredComponent.Color == color)
                    {
                        matchLineSweets.Add(sweets[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (matchLineSweets.Count >= 3)
            {
                for (int i = 0; i < matchLineSweets.Count; i++)
                {
                    finishedMatchingSweets.Add(matchLineSweets[i]);
                }
            }

            if (matchLineSweets.Count >= 3)
            {
                for (int i = 0; i < matchLineSweets.Count; i++)
                {
                    for (int j = 0; j <= 1; j++)
                    {
                        for (int xDistance = 1; xDistance < xColumn; xDistance++)
                        {
                            int x;

                            if (j == 0)
                            {
                                x = newY - xDistance;
                            }
                            else
                            {
                                x = newY + xDistance;
                            }
                            if (x < 0 || x >= xColumn)
                            {
                                break;
                            }

                            if (sweets[x, matchLineSweets[i].Y].CanColor() && sweets[x, matchLineSweets[i].Y].ColoredComponent.Color == color)
                            {
                                matchRowSweets.Add(sweets[x, matchLineSweets[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (matchRowSweets.Count < 2)
                    {
                        matchRowSweets.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < matchRowSweets.Count; j++)
                        {
                            finishedMatchingSweets.Add(matchRowSweets[j]);
                        }
                        break;
                    }
                }
            }

            if (finishedMatchingSweets.Count >= 3)
            {
                return(finishedMatchingSweets);
            }
        }

        return(null);
    }
Example #13
0
    /// <summary>
    /// 匹配消除方法
    /// </summary>
    /// <param name="sweet"></param>
    /// <param name="newX"></param>
    /// <param name="newY"></param>
    /// <returns></returns>
    public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY)
    {
        if (sweet.CanColor())                                                     //如果可以着色
        {
            ColorSweet.ColorType color               = sweet.ColorComponet.Color; //上色
            List <GameSweet>     matchHangSweets     = new List <GameSweet>();    //物品行 列表
            List <GameSweet>     matchLieSweets      = new List <GameSweet>();    //物品列 列表
            List <GameSweet>     finishedMatchSweets = new List <GameSweet>();    //完成待删物品 列表

            //检查行消除匹配
            matchHangSweets.Add(sweet);
            for (int i = 0; i <= 1; i++)             //i等于0,往左,1往右
            {
                for (int xDistance = 1; xDistance < xLie; xDistance++)
                {
                    int x;                     //偏移后的 x 坐标
                    if (i == 0)
                    {
                        x = newX - xDistance;
                    }
                    else
                    {
                        x = newX + xDistance;
                    }
                    if (x < 0 || x >= xLie)
                    {
                        break;                         //限定边界
                    }


                    if (sweets[x, newY].CanColor() && sweets[x, newY].ColorComponet.Color == color)                     //如果物品颜色一样
                    {
                        matchHangSweets.Add(sweets[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (matchHangSweets.Count >= 3)             //行列表元素的添加
            {
                for (int i = 0; i < matchHangSweets.Count; i++)
                {
                    finishedMatchSweets.Add(matchHangSweets[i]);
                }
            }


            //L T形状匹配
            //遍历后,检测当前行遍历元素数量是否大于3
            if (matchHangSweets.Count >= 3)
            {
                for (int i = 0; i < matchHangSweets.Count; i++)
                {
                    //行检查后,检测L和T形状。 检查元素上下元素,是否可以消除:0是上,1是下
                    for (int j = 0; j <= 1; j++)
                    {
                        for (int yDistance = 1; yDistance < yHang; yDistance++)
                        {
                            int y;                                  //被检测物体的,Y轴偏移坐标
                            if (j == 0)                             //如果是上方
                            {
                                y = newY - yDistance;               //每次向上递增(物体Y轴坐标,自上而下是0--10)
                            }
                            else
                            {
                                y = newY + yDistance;                                 //每次向下递增()
                            }
                            if (y < 0 || y >= yHang)
                            {
                                break;                                 //限定边界
                            }

                            if (sweets[matchHangSweets[i].X, y].CanColor() &&
                                sweets[matchHangSweets[i].X, y].ColorComponet.Color == color)                        //如果列方向,颜色一致
                            {
                                matchLieSweets.Add(sweets[matchHangSweets[i].X, y]);                                 //添加甜品对象到 列表中
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (matchLieSweets.Count < 2) //如果在 行列表中的,垂直方向列 数组中,相同元素小于2
                    {
                        matchLieSweets.Clear();   //清除
                    }
                    else                          //满足条件就加到完成列表
                    {
                        for (int j = 0; j < matchLieSweets.Count; j++)
                        {
                            finishedMatchSweets.Add(matchLieSweets[j]);
                        }
                        break;
                    }
                }
            }

            if (finishedMatchSweets.Count >= 3)
            {
                return(finishedMatchSweets);                //返回  行LT 列表
            }

            matchHangSweets.Clear();             //开始列检查之前:清除列表
            matchLieSweets.Clear();


            //列消除匹配
            matchLieSweets.Add(sweet);
            for (int i = 0; i <= 1; i++)             //i等于0,往左,1往右
            {
                for (int yDistance = 1; yDistance < yHang; yDistance++)
                {
                    int y;                     //偏移后的 y 坐标
                    if (i == 0)
                    {
                        y = newY - yDistance;
                    }
                    else
                    {
                        y = newY + yDistance;
                    }
                    if (y < 0 || y >= yHang)
                    {
                        break;                         //限定边界
                    }


                    if (sweets[newX, y].CanColor() && sweets[newX, y].ColorComponet.Color == color)                     //如果物品颜色一样
                    {
                        matchLieSweets.Add(sweets[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (matchLieSweets.Count >= 3)             //LIE 列表元素的添加
            {
                for (int i = 0; i < matchLieSweets.Count; i++)
                {
                    finishedMatchSweets.Add(matchLieSweets[i]);
                }
            }


            //垂直列表中,横向L T形状匹配
            //遍历后,检测当前行遍历元素数量是否大于3
            if (matchLieSweets.Count >= 3)
            {
                for (int i = 0; i < matchLieSweets.Count; i++)
                {
                    //行检查后,检测L和T形状。 检查元素上下元素,是否可以消除:0是上,1是下
                    for (int j = 0; j <= 1; j++)
                    {
                        for (int xDistance = 1; xDistance < xLie; xDistance++)
                        {
                            int x;                                  //被检测物体的,Y轴偏移坐标
                            if (j == 0)                             //如果是上方
                            {
                                x = newX - xDistance;               //每次向上递增(物体Y轴坐标,自上而下是0--10)
                            }
                            else
                            {
                                x = newX + xDistance;                                 //每次向下递增()
                            }
                            if (x < 0 || x >= xLie)
                            {
                                break;                                 //限定边界
                            }

                            if (sweets[x, matchLieSweets[i].Y].CanColor() &&
                                sweets[x, matchLieSweets[i].Y].ColorComponet.Color == color)                         //如果列方向,颜色一致
                            {
                                matchHangSweets.Add(sweets[x, matchLieSweets[i].Y]);                                 //添加甜品对象到 列表中
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (matchHangSweets.Count < 2) //如果在 列列表中的,左右方向行 数组中,相同元素小于2
                    {
                        matchHangSweets.Clear();   //清除
                    }
                    else                           //满足条件就加到完成列表
                    {
                        for (int j = 0; j < matchHangSweets.Count; j++)
                        {
                            finishedMatchSweets.Add(matchHangSweets[j]);
                        }
                        break;
                    }
                }
            }

            //这里
            if (finishedMatchSweets.Count >= 3)
            {
                return(finishedMatchSweets);
            }
        }

        return(null);
    }
Example #14
0
    //匹配方法
    public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY)
    {
        if (sweet.CanColor())
        {
            ColorType        color               = sweet.ColoredComponent.Color;
            List <GameSweet> matchRowSweet       = new List <GameSweet>(); //匹配行
            List <GameSweet> matchColumnSweet    = new List <GameSweet>(); //匹配列
            List <GameSweet> finishedMatchSweets = new List <GameSweet>(); //完成匹配

            //行匹配,嵌套列匹配
            matchRowSweet.Add(sweet);//添加基准甜品
            //i=0往左,i=1往右遍历
            for (int i = 0; i <= 1; i++)
            {
                for (int xDistance = 1; xDistance < xColumn; xDistance++)
                {
                    int x;                    //偏移之后的坐标
                    if (i == 0)               //往左
                    {
                        x = newX - xDistance; //向左走
                    }
                    else//i=1时
                    {
                        x = newX + xDistance;  //向右走
                    }
                    if (x < 0 || x >= xColumn) //保护x范围
                    {
                        break;                 //结束循环遍历
                    }

                    if (sweets[x, newY].CanColor() && sweets[x, newY].ColoredComponent.Color == color)//是否一样颜色
                    {
                        matchRowSweet.Add(sweets[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //先添加进完成匹配列表
            if (matchRowSweet.Count >= 3)
            {
                for (int i = 0; i < matchRowSweet.Count; i++)
                {
                    finishedMatchSweets.Add(matchRowSweet[i]);
                }
            }

            //检查当前行遍历列表元素>=3??
            if (matchRowSweet.Count >= 3)
            {
                for (int i = 0; i < matchRowSweet.Count; i++)//遍历逐个添加进finishedMatchSweets列表
                {
                    //已经满足行匹配,看是否还有没有满足列匹配的TL型消除
                    //0代表上   1代表下方
                    for (int j = 0; j <= 1; j++)
                    {
                        for (int yDistance = 1; yDistance < yRow; yDistance++)
                        {
                            int y;
                            if (j == 0)//向上遍历
                            {
                                y = newY - yDistance;
                            }
                            else
                            {
                                y = newY + yDistance;
                            }
                            if (y >= yRow || y < 0)//不超出范围
                            {
                                break;
                            }
                            if (sweets[matchRowSweet[i].X, y].CanColor() && sweets[matchRowSweet[i].X, y].ColoredComponent.Color == color)//行遍历跟列遍历之后是否相同颜色
                            {
                                matchColumnSweet.Add(sweets[matchRowSweet[i].X, y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (matchColumnSweet.Count < 2)
                    {
                        matchColumnSweet.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < matchColumnSweet.Count; j++)
                        {
                            finishedMatchSweets.Add(matchColumnSweet[j]);
                        }
                        break;
                    }
                }
            }
            if (finishedMatchSweets.Count >= 3)//如果完成匹配列表元素大于3,则直接返回整个列表
            {
                return(finishedMatchSweets);
            }

            //清空一下,接收下一次匹配
            matchRowSweet.Clear();
            matchColumnSweet.Clear();



            //列匹配,嵌套行匹配
            matchColumnSweet.Add(sweet); //添加基准甜品
                                         //i=0往左,i=1往右遍历
            for (int i = 0; i <= 1; i++)
            {
                for (int yDistance = 1; yDistance < yRow; yDistance++)
                {
                    int y;                    //偏移之后的坐标
                    if (i == 0)               //往左
                    {
                        y = newY - yDistance; //向左走
                    }
                    else//i=1时
                    {
                        y = newY + yDistance; //向右走
                    }
                    if (y < 0 || y >= yRow)   //保护x范围
                    {
                        break;                //结束循环遍历
                    }

                    if (sweets[newX, y].CanColor() && sweets[newX, y].ColoredComponent.Color == color)//是否一样颜色
                    {
                        matchColumnSweet.Add(sweets[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //先添加进完成匹配列表
            if (matchColumnSweet.Count >= 3)
            {
                for (int i = 0; i < matchColumnSweet.Count; i++)
                {
                    finishedMatchSweets.Add(matchColumnSweet[i]);    //如果超过3个就添加进最后匹配的列表
                }
            }

            //L T型匹配
            //检查当前列遍历列表元素>=3??
            if (matchColumnSweet.Count >= 3)
            {
                for (int i = 0; i < matchColumnSweet.Count; i++)//遍历逐个添加进finishedMatchSweets列表
                {
                    //已经满足列匹配,看是否还有没有满足列匹配的TL型消除
                    //0代表上   1代表下方
                    for (int j = 0; j <= 1; j++)
                    {
                        for (int xDistance = 1; xDistance < xColumn; xDistance++)
                        {
                            int x;
                            if (j == 0)//向上遍历
                            {
                                x = newX - xDistance;
                            }
                            else
                            {
                                x = newX + xDistance;
                            }
                            if (x >= xColumn || x < 0)
                            {
                                break;
                            }
                            if (sweets[x, matchColumnSweet[i].Y].CanColor() && sweets[x, matchColumnSweet[i].Y].ColoredComponent.Color == color)
                            {
                                matchRowSweet.Add(sweets[x, matchColumnSweet[i].Y]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (matchRowSweet.Count < 2)
                    {
                        matchRowSweet.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < matchRowSweet.Count; j++)
                        {
                            finishedMatchSweets.Add(matchRowSweet[j]);
                        }
                    }
                }
            }
            if (finishedMatchSweets.Count >= 3)//如果完成匹配列表元素大于3,则直接返回整个列表
            {
                return(finishedMatchSweets);
            }
        }
        return(null);
    }
Example #15
0
    //匹配方法
    public List <GameSweet> MatchSweet(GameSweet sweet, int newX, int newY)
    {
        if (sweet.CanColor())//如果可以正常的甜品
        {
            ColorSweet.ColorType color = sweet.ColoredComponent.Clolr;
            //行列表
            List <GameSweet> matchRowSweets = new List <GameSweet>();
            //列列表
            List <GameSweet> matchLineSweets = new List <GameSweet>();
            //完成匹配的列表
            List <GameSweet> finishedMatchingSweets = new List <GameSweet>();

            #region 行匹配
            matchRowSweets.Add(sweet);
            //0表示往左,1表示往右
            for (int i = 0; i <= 1; i++)
            {
                for (int xDistance = 1; xDistance < xColumn; xDistance++)
                {
                    int x = 0;
                    if (i == 0)//往左移动
                    {
                        x = newX - xDistance;
                    }
                    else//往右移动
                    {
                        x = newX + xDistance;
                    }
                    if (x < 0 || x >= xColumn)//如果超出范围,退出当前循环
                    {
                        break;
                    }
                    //如果左边的块是标准快,并且与当前块的类型一样
                    if (sweets[x, newY].CanColor() && sweets[x, newY].ColoredComponent.Clolr == color)
                    {
                        matchRowSweets.Add(sweets[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //检查当前行数列的数值是否大于3
            if (matchRowSweets.Count >= 3)
            {
                for (int i = 0; i < matchRowSweets.Count; i++)
                {
                    if (i == 0)
                    {
                        for (int j = 0; j <= 1; j++)
                        {
                            for (int yDistance = 0; yDistance < xColumn; yDistance++)
                            {
                                int y;
                                if (j == 0)//往下移动
                                {
                                    y = newY - yDistance;
                                }
                                else//往下移动
                                {
                                    y = newY + yDistance;
                                }
                                if (y < 0 || y >= yRow)//如果超出范围,推出当前循环
                                {
                                    break;
                                }
                                //如果上下边的块是标准快,并且与当前块的类型一样
                                if (sweets[newX, y].CanColor() && sweets[newX, y].ColoredComponent.Clolr == color)
                                {
                                    matchLineSweets.Add(sweets[newX, y]);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                    finishedMatchingSweets.Add(matchRowSweets[i]);
                }
                if (matchLineSweets.Count >= 3)
                {
                    foreach (GameSweet item in matchLineSweets)
                    {
                        finishedMatchingSweets.Add(item);
                    }
                    return(finishedMatchingSweets);
                }
                foreach (var item in finishedMatchingSweets)
                {
                    Destroy(item.gameObject);
                }
            }
            //如果匹配的个数大于3,返回完成匹配的集合
            if (finishedMatchingSweets.Count >= 3)
            {
                return(finishedMatchingSweets);
            }
            #endregion

            /* #region 列匹配
             * matchLineSweets.Add(sweet);
             * //0表示往上,1表示往下
             * for (int i = 0; i <= 1; i++)
             * {
             *   for (int yDistance = 1; yDistance < yRow; yDistance++)
             *   {
             *       int y ;
             *       if (i == 0)//往下移动
             *       {
             *           y = newY - yDistance;
             *       }
             *       else//往下移动
             *       {
             *           y = newY + yDistance;
             *       }
             *       if (y < 0 || y >= yRow)//如果超出范围,推出当前循环
             *       {
             *           break;
             *       }
             *       //如果上下边的块是标准快,并且与当前块的类型一样
             *       if (sweets[newX, y].CanColor() && sweets[newX, y].ColoredComponent.Clolr == color)
             *       {
             *           matchLineSweets.Add(sweets[newX, y]);
             *       }
             *       else
             *       {
             *           break;
             *       }
             *   }
             * }
             * //检查当前行数列的数值是否大于3
             * if (matchLineSweets.Count >= 3)
             * {
             *   foreach (GameSweet item in matchLineSweets)
             *   {
             *       finishedMatchingSweets.Add(item);
             *   }
             * }
             * //如果匹配的个数大于3,返回完成匹配的集合
             * if (finishedMatchingSweets.Count >= 3)
             * {
             *   return finishedMatchingSweets;
             * }
             #endregion
             */
        }
        return(null);
    }
    private bool ClearAllMatchedSweet()
    {
        bool needRefill = false;

        for (int y = 0; y < yRow; y++)
        {
            for (int x = 0; x < xColumn; x++)
            {
                if (sweets[x, y].CanClear())
                {
                    List <GameSweet> matchList = MatchSweets(sweets[x, y], x, y);

                    if (matchList != null)
                    {
                        SweetsType specialSweetsType = SweetsType.COUNT;
                        GameSweet  randomSweet       = matchList[Random.Range(0, matchList.Count)];
                        int        specialSweetX     = randomSweet.X;
                        int        specialSweetY     = randomSweet.Y;

                        if (matchList.Count == 4)
                        {
                            if (pressedSweet == null || enteredSweet == null)
                            {
                                specialSweetsType = (SweetsType)Random.Range((int)SweetsType.ROW_CLEAR, (int)SweetsType.COLUMN_CLEAR);
                            }
                            else if (pressedSweet.Y == enteredSweet.Y)
                            {
                                specialSweetsType = SweetsType.ROW_CLEAR;
                            }
                            else
                            {
                                specialSweetsType = SweetsType.COLUMN_CLEAR;
                            }
                        }
                        else if (matchList.Count >= 5)
                        {
                            specialSweetsType = SweetsType.RAINBOWCANDY;
                        }

                        for (int i = 0; i < matchList.Count; i++)
                        {
                            if (ClearSweet(matchList[i].X, matchList[i].Y))
                            {
                                needRefill = true;

                                if (matchList[i] == pressedSweet || matchList[i] == enteredSweet)
                                {
                                    specialSweetX = matchList[i].X;
                                    specialSweetY = matchList[i].Y;
                                }
                            }
                        }

                        if (specialSweetsType != SweetsType.COUNT)
                        {
                            Destroy(sweets[specialSweetX, specialSweetY]);
                            GameSweet newSweet = CreateNewSweet(specialSweetX, specialSweetY, specialSweetsType);

                            if (specialSweetsType == SweetsType.ROW_CLEAR || specialSweetsType == SweetsType.COLUMN_CLEAR && newSweet.CanColor() && matchList[0].CanColor())
                            {
                                newSweet.ColoredComponent.SetColor(matchList[0].ColoredComponent.Color);
                            }
                            else if (specialSweetsType == SweetsType.RAINBOWCANDY && newSweet.CanColor())
                            {
                                newSweet.ColoredComponent.SetColor(ColorSweet.ColorType.ANY);
                            }
                        }
                    }
                }
            }
        }
        return(needRefill);
    }
Example #17
0
    public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY)
    {
        if (sweet.CanColor())
        {
            ColorSweet.ColorEnum color              = sweet.ColorComponent.Color;
            List <GameSweet>     matchRowSweets     = new List <GameSweet>();
            List <GameSweet>     matchLineSweets    = new List <GameSweet>();
            List <GameSweet>     finishedMatchweets = new List <GameSweet>();

            //----行匹配
            matchRowSweets.Add(sweet);
            for (int i = 0; i <= 1; i++)//0,1代表方向
            {
                for (int xDistance = 1; xDistance < xColumns; xDistance++)
                {
                    int x;
                    if (i == 0)
                    {
                        x = newX - xDistance;
                    }
                    else
                    {
                        x = newX + xDistance;
                    }
                    if (x < 0 || x >= xColumns)
                    {
                        break;
                    }
                    if (sweetsarray[newY, x].CanColor() &&
                        sweetsarray[newY, x].ColorComponent.Color == color)
                    {
                        matchRowSweets.Add(sweetsarray[newY, x]);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (matchRowSweets.Count >= 3)
            {
                for (int i = 0; i < matchRowSweets.Count; i++)
                {
                    finishedMatchweets.Add(matchRowSweets[i]);
                }
                for (int i = 0; i < matchRowSweets.Count; i++)
                {
                    for (int j = 0; j <= 1; j++)//方向
                    {
                        for (int yDisTance = 1; yDisTance < yRows; yDisTance++)
                        {
                            int y;
                            if (j == 0)
                            {
                                y = newY - yDisTance;
                            }
                            else
                            {
                                y = newY + yDisTance;
                            }
                            if (y < 0 || y >= yRows)
                            {
                                break;
                            }
                            if (sweetsarray[y, matchRowSweets[i].X].CanColor() &&
                                sweetsarray[y, matchRowSweets[i].X].ColorComponent.Color == color)
                            {
                                matchLineSweets.Add(sweetsarray[y, matchRowSweets[i].X]);
                            }
                            else
                            {
                                break;//找到第一个不同颜色时直接跳出
                            }
                        }
                    }
                    if (matchLineSweets.Count < 2)//该列表未包含基准颜色块
                    {
                        matchLineSweets.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < matchLineSweets.Count; j++)
                        {
                            finishedMatchweets.Add(matchLineSweets[j]);
                        }
                        break;
                    }
                    //finishedMatchweets.Add(matchRowSweets[i]);
                }
            }
            if (finishedMatchweets.Count >= 3)
            {
                print("匹配"); return(finishedMatchweets);
            }


            matchRowSweets.Clear();
            matchLineSweets.Clear();

            matchLineSweets.Add(sweet);
            for (int i = 0; i <= 1; i++)//0,1代表方向
            {
                for (int yDistance = 1; yDistance < yRows; yDistance++)
                {
                    int y;
                    if (i == 0)
                    {
                        y = newY - yDistance;
                    }
                    else
                    {
                        y = newY + yDistance;
                    }
                    if (y < 0 || y >= yRows)
                    {
                        break;
                    }
                    if (sweetsarray[y, newX].CanColor() &&
                        sweetsarray[y, newX].ColorComponent.Color == color)
                    {
                        matchLineSweets.Add(sweetsarray[y, newX]);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (matchLineSweets.Count >= 3)
            {
                for (int i = 0; i < matchLineSweets.Count; i++)
                {
                    finishedMatchweets.Add(matchLineSweets[i]);
                }


                for (int i = 0; i < matchLineSweets.Count; i++)
                {
                    for (int j = 0; j <= 1; j++)//方向
                    {
                        for (int xDisTance = 1; xDisTance < xColumns; xDisTance++)
                        {
                            int x;
                            if (j == 0)
                            {
                                x = newX - xDisTance;
                            }
                            else
                            {
                                x = newX + xDisTance;
                            }
                            if (x < 0 || x >= xColumns)
                            {
                                break;
                            }
                            if (sweetsarray[matchLineSweets[i].Y, x].CanColor() &&
                                sweetsarray[matchLineSweets[i].Y, x].ColorComponent.Color == color)
                            {
                                matchRowSweets.Add(sweetsarray[matchLineSweets[i].Y, x]);
                            }
                            else
                            {
                                break;//找到第一个不同颜色时直接跳出
                            }
                        }
                    }
                    if (matchRowSweets.Count < 2)//该列表未包含基准颜色块
                    {
                        matchRowSweets.Clear();
                    }
                    else
                    {
                        for (int j = 0; j < matchRowSweets.Count; j++)
                        {
                            finishedMatchweets.Add(matchRowSweets[j]);
                        }
                        break;
                    }
                    //finishedMatchweets.Add(matchLineSweets[i]);
                }
            }
            if (finishedMatchweets.Count >= 3)
            {
                print("匹配"); return(finishedMatchweets);
            }
        }

        print("匹配失败");
        return(null);
    }