Ejemplo n.º 1
0
    public int DeleteMatched()
    {
        int count = 0;

        List <Character[, ]> allDirectionsForMatching = new List <Character[, ]>()
        {
            HorizontalLines(),
            VerticalLines(),
            UpDiagonalLines(),
            DownDiagonalLines()
        };

        List <IMatchable> lineMatched = new List <IMatchable>();

        foreach (var item in allDirectionsForMatching)
        {
            for (int n = 0; n < item.GetLength(0); n++)
            {
                lineMatched.Clear();

                for (int i = 0; i < item.GetLength(1); i++)
                {
                    IMatchable matchable = item[n, i];
                    if (lineMatched.Count < minMatchesCount)
                    {
                        if (matchable == null)
                        {
                            lineMatched.Clear();
                            continue;
                        }

                        if (lineMatched.Count == 0 || IsMatched(lineMatched[0], matchable) == false)
                        {
                            lineMatched.Clear();
                        }

                        lineMatched.Add(matchable);
                    }
                    else
                    {
                        if (item == null || IsMatched(lineMatched[0], matchable) == false)
                        {
                            break;
                        }

                        lineMatched.Add(matchable);
                    }
                }

                if (lineMatched.Count >= minMatchesCount)
                {
                    foreach (var matchable in lineMatched)
                    {
                        var destroyed = matchable as Character;

                        if (destroyed != null)
                        {
                            characterProvider.Destroy(destroyed);
                        }
                    }

                    count += lineMatched.Count;
                }
            }
        }

        return(count);
    }