Exemple #1
0
    public bool CheckPossibleSwapBlocks()
    {
        for (int y = (mapModel.LengthY - 1); y >= 0; --y)
        {
            for (int x = 0; x < mapModel.LengthX; ++x)
            {
                if (!mapModel.CheckShowBlock(y, x))
                {
                    continue;
                }
                if (mapModel.CheckEmptyBlock(y, x))
                {
                    continue;
                }
                if (mapModel.CheckFixedBlock(y, x))
                {
                    continue;
                }

                int directionIndex = (x % 2);
                for (int j = 0; j < mapModel.DirectionLengthY; ++j)
                {
                    for (int k = 0; k < mapModel.DirectionLengthX; ++k)
                    {
                        Vector2Int swapDirection = mapModel.GetDirection(directionIndex, j, k);
                        int        swapY         = y + swapDirection.y;
                        int        swapX         = x + swapDirection.x;

                        if (mapModel.CheckRangeOver(swapY, swapX))
                        {
                            continue;
                        }
                        if (mapModel.CheckEmptyBlock(swapY, swapX))
                        {
                            continue;
                        }
                        if (mapModel.CheckFixedBlock(swapY, swapX))
                        {
                            continue;
                        }

                        mapModel.SwapBlocks(mapModel.GetBlock(y, x), mapModel.GetBlock(swapY, swapX));
                        if (mapModel.GetBlock(y, x).BlockType > BlockType.Red)
                        {
                            mapModel.SwapBlocks(mapModel.GetBlock(y, x), mapModel.GetBlock(swapY, swapX));
                            continue;
                        }

                        for (int n = 0; n < mapModel.DirectionLengthY; ++n)
                        {
                            Queue <Block> block = new Queue <Block>();
                            block.Enqueue(mapModel.GetBlock(y, x));

                            for (int m = 0; m < mapModel.DirectionLengthX; ++m)
                            {
                                Vector2Int direction = mapModel.GetDirection(directionIndex, n, m);
                                int        nextY     = y + direction.y;
                                int        nextX     = x + direction.x;
                                DFSMatchingBlock(nextY, nextX, n, m, block);
                            }

                            if (block.Count >= MATCH_COUNT_MIN)
                            {
                                mapModel.SwapBlocks(mapModel.GetBlock(y, x), mapModel.GetBlock(swapY, swapX));
                                return(true);
                            }
                        }

                        mapModel.SwapBlocks(mapModel.GetBlock(y, x), mapModel.GetBlock(swapY, swapX));
                    }
                }
            }
        }

        return(false);
    }