Ejemplo n.º 1
0
    private bool CalculateSwappedGems()
    {
        //var dirctions = (
        //    left:{-1, 0},// left
        //    right: {1, 0},// right
        //    up: {0, -1},// up
        //    down: {0, 1},// down
        //);
        var minRowIndex    = 0;
        var maxRowIndex    = GameConfig.Instance.gemRow - 1;
        var minColumnIndex = 0;
        var maxColumnIndex = GameConfig.Instance.gemColumn - 1;

        var gemToCheckList = new List <GemController>();

        gemToCheckList.Add(_firstSelectedGemControler);
        gemToCheckList.Add(_secondSelectedGemControler);

        var hasMatchedGems = false;

        foreach (var gem in gemToCheckList)
        {
            // Horizontal
            var leftMostColumnIndex  = gem.Column;
            var rightMostColumnIndex = gem.Column;
            // Left
            for (int col = gem.Column - 1; col >= minColumnIndex; col--)
            {
                var leftGem    = _currentGemControllerMatrix[gem.Row, col];
                var currentGem = _currentGemControllerMatrix[gem.Row, leftMostColumnIndex];
                if (GemController.IsMatched(leftGem, currentGem))
                {
                    leftMostColumnIndex = col;
                }
                else
                {
                    break;
                }
            }
            // Right
            for (int col = gem.Column + 1; col <= maxColumnIndex; col++)
            {
                var rightGem   = _currentGemControllerMatrix[gem.Row, col];
                var currentGem = _currentGemControllerMatrix[gem.Row, rightMostColumnIndex];
                if (GemController.IsMatched(rightGem, currentGem))
                {
                    rightMostColumnIndex = col;
                }
                else
                {
                    break;
                }
            }

            // Vertical
            var upperMostRowIndex = gem.Row;
            var lowestRowIndex    = gem.Row;
            // Up
            for (int row = gem.Row - 1; row >= minRowIndex; row--)
            {
                var upperGem   = _currentGemControllerMatrix[row, gem.Column];
                var currentGem = _currentGemControllerMatrix[upperMostRowIndex, gem.Column];
                if (GemController.IsMatched(upperGem, currentGem))
                {
                    upperMostRowIndex = row;
                }
                else
                {
                    break;
                }
            }
            // Low
            for (int row = gem.Row + 1; row <= maxRowIndex; row++)
            {
                var lowerGem   = _currentGemControllerMatrix[row, gem.Column];
                var currentGem = _currentGemControllerMatrix[lowestRowIndex, gem.Column];
                if (GemController.IsMatched(lowerGem, currentGem))
                {
                    lowestRowIndex = row;
                }
                else
                {
                    break;
                }
            }

            if (rightMostColumnIndex - leftMostColumnIndex >= 2)
            {
                hasMatchedGems = true;
                for (int i = leftMostColumnIndex; i <= rightMostColumnIndex; i++)
                {
                    var current = _currentGemControllerMatrix[gem.Row, i];
                    current.IsTagged = true;
                }
            }
            if (lowestRowIndex - upperMostRowIndex >= 2)
            {
                hasMatchedGems = true;
                for (int i = upperMostRowIndex; i <= lowestRowIndex; i++)
                {
                    var current = _currentGemControllerMatrix[i, gem.Column];
                    current.IsTagged = true;
                }
            }
        }

        return(hasMatchedGems);
    }