Beispiel #1
0
        private void ProcessMatches(List <BlockRemoval> removedBlocks, List <MatchSet> matchSets)
        {
            for (int i = 0; i < matchSets.Count; i++)
            {
                MatchSet matchSet = matchSets[i];

                // удаляем блоки или активируем, если это "активные" блоки
                for (int j = 0; j < matchSet.length; j++)
                {
                    int row, column;
                    if (matchSet.alignment == MatchSet.Alignment.Horizontal)
                    {
                        row    = matchSet.startRow;
                        column = matchSet.startColumn + j;
                    }
                    else
                    {
                        row    = matchSet.startRow + j;
                        column = matchSet.startColumn;
                    }

                    Block block = blocks[row, column];
                    if (block.type != BlockType.Normal)
                    {
                        activatedBlocks.Add(new BlockAndPosition(block, row, column));
                    }
                    else if (block.color != BlockColor.Empty)
                    {
                        blocks[row, column] = EmptyBlock;
                        removedBlocks.Add(new BlockRemoval(block, BlockRemovalReason.DestroyedByMatching, row, column));
                    }
                }
            }
        }
Beispiel #2
0
        private void GenerateNewBlocks(List <BlockAndPosition> createdBlocks, List <MatchSet> matchSets)
        {
            for (int i = 0; i < matchSets.Count; i++)
            {
                MatchSet matchSet = matchSets[i];

                bool      shouldGenerateNewBlock = false;
                BlockType generatedBlockType     = BlockType.Normal;

                if (matchSet.length > 4)
                {
                    shouldGenerateNewBlock = true;
                    generatedBlockType     = BlockType.Mega;
                }
                else if (matchSet.length == 4)
                {
                    shouldGenerateNewBlock = true;
                    generatedBlockType     = BlockType.Bomb;
                }

                if (shouldGenerateNewBlock)
                {
                    int row;
                    int column;
                    if (FindBestPositionForNewBlock(matchSet, out row, out column)) // добавляем только если есть пустое место
                    {
                        var newBlock = CreateNewBlock(generatedBlockType == BlockType.Mega ? BlockColor.Rainbow : matchSet.color, generatedBlockType);
                        blocks[row, column] = newBlock;
                        createdBlocks.Add(new BlockAndPosition(newBlock, row, column));
                    }
                }
            }
        }
Beispiel #3
0
 public bool Contains(MatchSet matchSet)
 {
     return(horizontal.Equals(matchSet) || vertical.Equals(matchSet));
 }
Beispiel #4
0
 public CrossingMatches(MatchSet horizontal, MatchSet vertical)
 {
     this.horizontal = horizontal;
     this.vertical   = vertical;
 }
Beispiel #5
0
        private bool FindBestPositionForNewBlock(MatchSet matchSet, out int bestRow, out int bestColumn)
        {
            if (matchSet.alignment == MatchSet.Alignment.Horizontal)
            {
                int row         = matchSet.startRow;
                int leftColumn  = matchSet.startColumn;
                int rightColumn = matchSet.startColumn + matchSet.length - 1;

                if (swapRow1 == row && swapColumn1 >= leftColumn && swapColumn1 <= rightColumn)
                {
                    if (blocks[swapRow1, swapColumn1].type == BlockType.Normal)
                    {
                        bestRow    = swapRow1;
                        bestColumn = swapColumn1;
                        return(true);
                    }
                }

                if (swapRow2 == row && swapColumn2 >= leftColumn && swapColumn2 <= rightColumn)
                {
                    if (blocks[swapRow2, swapColumn2].type == BlockType.Normal)
                    {
                        bestRow    = swapRow2;
                        bestColumn = swapColumn2;
                        return(true);
                    }
                }


                float centerColumn = matchSet.startColumn + (matchSet.length - 1) / 2f;
                for (int i = 0; i < (matchSet.length + 1) / 2; i++)
                {
                    int left  = (int)(centerColumn - i);
                    int right = (int)(centerColumn + i);
                    if (left >= leftColumn)
                    {
                        if (blocks[row, left].type == BlockType.Normal)
                        {
                            bestRow    = row;
                            bestColumn = left;
                            return(true);
                        }
                    }
                    if (right <= rightColumn)
                    {
                        if (blocks[row, right].type == BlockType.Normal)
                        {
                            bestRow    = row;
                            bestColumn = right;
                            return(true);
                        }
                    }
                }

                bestRow    = 0;
                bestColumn = 0;
                return(false);
            }
            else
            {
                int column    = matchSet.startColumn;
                int topRow    = matchSet.startRow;
                int bottomRow = matchSet.startRow + matchSet.length - 1;

                if (swapColumn1 == column && swapRow1 >= topRow && swapRow1 <= bottomRow)
                {
                    if (blocks[swapRow1, swapColumn1].type == BlockType.Normal)
                    {
                        bestRow    = swapRow1;
                        bestColumn = swapColumn1;
                        return(true);
                    }
                }

                if (swapColumn2 == column && swapRow2 >= topRow && swapRow2 <= bottomRow)
                {
                    if (blocks[swapRow2, swapColumn2].type == BlockType.Normal)
                    {
                        bestRow    = swapRow2;
                        bestColumn = swapColumn2;
                        return(true);
                    }
                }


                float centerRow = matchSet.startRow + (matchSet.length - 1) / 2f;

                for (int i = 0; i < (matchSet.length + 1) / 2; i++)
                {
                    int top    = (int)(centerRow - i);
                    int bottom = (int)(centerRow + i);
                    if (top >= topRow)
                    {
                        if (blocks[top, column].type == BlockType.Normal)
                        {
                            bestRow    = top;
                            bestColumn = column;
                            return(true);
                        }
                    }
                    if (bottom <= bottomRow)
                    {
                        if (blocks[bottom, column].type == BlockType.Normal)
                        {
                            bestRow    = bottom;
                            bestColumn = column;
                            return(true);
                        }
                    }
                }

                bestRow    = 0;
                bestColumn = 0;
                return(false);
            }
        }
Beispiel #6
0
 public bool Equals(MatchSet other)
 {
     return(startRow == other.startRow && startColumn == other.startColumn && length == other.length && alignment == other.alignment);
 }