예제 #1
0
        /// <summary>
        /// Finds the farthest block in dir direction, that has a same color block in check direction
        /// </summary>
        /// <param name="block"></param>
        /// <param name="dir"></param>
        /// <param name="check"></param>
        private Block GetEndBlock(Block block, Direction dir, Direction checkDir)
        {
            Block current = null;
            Block next;
            Block nextAdj;

            //Set current block
            current = block;

            //Iterate left
            next    = current.SameAdjacent(dir);
            nextAdj = next?.SameAdjacent(checkDir);

            while (next != null && nextAdj != null && next.IsGrouped() == false && nextAdj.IsGrouped() == false)
            {
                current = next;
                next    = current.SameAdjacent(dir);
                nextAdj = next?.SameAdjacent(checkDir);
            }

            return(current);
        }
예제 #2
0
 private Block SameLeft(Block block)
 {
     return(block.SameAdjacent(Direction.left));
 }
예제 #3
0
 private Block SameDown(Block block)
 {
     return(block.SameAdjacent(Direction.down));
 }
예제 #4
0
 private Block SameRight(Block block)
 {
     return(block.SameAdjacent(Direction.right));
 }
예제 #5
0
 private Block SameUp(Block block)
 {
     return(block.SameAdjacent(Direction.up));
 }
예제 #6
0
        private bool SameColorLine(Block block, Direction dir, int length)
        {
            if (block == null || block.IsGrouped() == true)
            {
                return(false);
            }

            switch (dir)
            {
            case Direction.up:
                for (int i = 0; i < length; i++)
                {
                    block = block.SameAdjacent(Direction.up);

                    if (block == null || block.IsGrouped() == true)
                    {
                        return(false);
                    }
                }

                break;

            case Direction.right:
                for (int i = 0; i < length; i++)
                {
                    block = block.SameAdjacent(Direction.right);
                    if (block == null || block.IsGrouped() == true)
                    {
                        return(false);
                    }
                }
                break;

            case Direction.down:
                for (int i = 0; i < length; i++)
                {
                    block = block.SameAdjacent(Direction.down);
                    if (block == null || block.IsGrouped() == true)
                    {
                        return(false);
                    }
                }
                break;

            case Direction.left:
                for (int i = 0; i < length; i++)
                {
                    block = block.SameAdjacent(Direction.left);
                    if (block == null || block.IsGrouped() == true)
                    {
                        return(false);
                    }
                }
                break;

            default:
                return(false);
            }

            return(true);
        }
예제 #7
0
        public BlockPosition BlockPos(Block block)
        {
            bool up = false, right = false, down = false, left = false;
            bool upperLeft = false, upperRight = false, lowerLeft = false, lowerRight = false;

            int adjacentCount = 0;

            Block other = block.SameAdjacent(Direction.up);

            //Check directly adjacent blocks
            if (other != null && other.IsGrouped() == false)
            {
                adjacentCount++;
                up = true;
            }

            other = block.SameAdjacent(Direction.right);
            if (other != null && other.IsGrouped() == false)
            {
                adjacentCount++;
                right = true;
            }

            other = block.SameAdjacent(Direction.down);
            if (other != null && other.IsGrouped() == false)
            {
                adjacentCount++;
                down = true;
            }

            other = block.SameAdjacent(Direction.left);
            if (other != null && other.IsGrouped() == false)
            {
                adjacentCount++;
                left = true;
            }

            //If 3 or more adjacent, check diagonals too
            if (adjacentCount >= 3)
            {
                //check upper diagonals
                other = GetBlock(block.X - 1, block.Y + 1);
                if (block.SameColor(other) && other.IsGrouped() == false)
                {
                    upperLeft = true;
                }

                other = GetBlock(block.X + 1, block.Y + 1);
                if (block.SameColor(other) && other.IsGrouped() == false)
                {
                    upperRight = true;
                }

                //Check lower diagonals
                other = GetBlock(block.X - 1, block.Y - 1);
                if (block.SameColor(other) && other.IsGrouped() == false)
                {
                    lowerLeft = true;
                }

                other = GetBlock(block.X + 1, block.Y - 1);
                if (block.SameColor(other) && other.IsGrouped() == false)
                {
                    lowerRight = true;
                }
            }

            if (adjacentCount == 2)
            {
                //Corner block
                if (up && right)
                {
                    //bottom left
                    return(BlockPosition.bottomLeft);
                }
                else if (right && down)
                {
                    //top left
                    return(BlockPosition.topLeft);
                }
                else if (down && left)
                {
                    // top right
                    return(BlockPosition.topRight);
                }
                else if (left && up)
                {
                    //bottom right
                    return(BlockPosition.bottomRight);
                }

                //3 blocks in a row
            }
            else if (adjacentCount == 3)
            {
                //T shape

                //Edge of rect
                if (up && right && down)
                {
                    //left edge
                    if (upperRight && lowerRight)
                    {
                        return(BlockPosition.left);
                    }

                    //bottom left
                    if (upperRight)
                    {
                        return(BlockPosition.bottomLeft);
                    }

                    //top right
                    if (lowerRight)
                    {
                        return(BlockPosition.topLeft);
                    }
                }
                else if (right && down && left)
                {
                    //top edge
                    if (lowerLeft && lowerRight)
                    {
                        return(BlockPosition.top);
                    }

                    //top right
                    if (lowerLeft)
                    {
                        return(BlockPosition.topRight);
                    }

                    //top left
                    if (lowerRight)
                    {
                        return(BlockPosition.topLeft);
                    }
                }
                else if (down && left && up)
                {
                    //right edge
                    if (upperLeft && lowerLeft)
                    {
                        return(BlockPosition.right);
                    }

                    //bottom right
                    if (upperLeft)
                    {
                        return(BlockPosition.bottomRight);
                    }

                    //top right
                    if (lowerLeft)
                    {
                        return(BlockPosition.topRight);
                    }
                }
                else if (left && up && right)
                {
                    //bottom edge
                    if (upperLeft && upperRight)
                    {
                        return(BlockPosition.bottom);
                    }

                    //bottom left corner
                    if (upperRight)
                    {
                        return(BlockPosition.bottomLeft);
                    }

                    //bottom right corner
                    if (upperLeft)
                    {
                        return(BlockPosition.bottomRight);
                    }
                }
            }
            else if (adjacentCount == 4)
            {
                //+ shape
                //+ shape + 1 corner = group
                if (upperLeft || upperRight || lowerLeft || lowerRight)
                {
                    return(BlockPosition.middle);
                }


                //Middle block of group
                if (upperLeft && upperRight && lowerLeft && lowerRight)
                {
                    return(BlockPosition.middle);
                }
            }

            return(BlockPosition.original);
        }