Exemple #1
0
        private static void StackSolver(BlockPuzzle blockPuzzle, BlockLine.Orientation nextOrientation)
        {
            if (!blockPuzzle.isFinished)
            {
                if (blockPuzzle.AddBlockLine(nextOrientation))
                {
                    StackSolver(blockPuzzle, BlockLine.Orientation.Left);
                    StackSolver(blockPuzzle, BlockLine.Orientation.Right);
                    StackSolver(blockPuzzle, BlockLine.Orientation.Up);
                    StackSolver(blockPuzzle, BlockLine.Orientation.Down);
                    StackSolver(blockPuzzle, BlockLine.Orientation.Front);
                    StackSolver(blockPuzzle, BlockLine.Orientation.Back);

                    blockPuzzle.RemoveLastBlockLine();
                }
            }
        }
Exemple #2
0
        public bool AddBlockLine(BlockLine.Orientation orientation)
        {
            BlockLine.Orientation lastBlockOrientation;
            bool additionSuccess = false;

            int       numBlocks  = this.blockLineOrder[this.blockLineIndex];
            BlockLine addedBlock = new BlockLine(numBlocks, orientation);

            if (this.listOfBlocks.Count == 0)
            {
                // This is the first block, so we need to add one block
                addedBlock.numBlocks++;
                addedBlock.startPoint = new Point(0, 0, 0);
                addedBlock.endPoint   = new Point(0, 0, addedBlock.numBlocks - 1);
                this.SpaceCheck(addedBlock, new Point(0, 0, 1));
                additionSuccess = true;
            }
            else
            {
                lastBlockOrientation = listOfBlocks[listOfBlocks.Count - 1].orientation;

                switch (orientation)
                {
                case BlockLine.Orientation.Back:
                    if (lastBlockOrientation != BlockLine.Orientation.Back && lastBlockOrientation != BlockLine.Orientation.Front)
                    {
                        addedBlock.startPoint = new Point(this.endingPoint.xPos, this.endingPoint.yPos + 1, this.endingPoint.zPos);
                        addedBlock.endPoint   = new Point(this.endingPoint.xPos, this.endingPoint.yPos + numBlocks, this.endingPoint.zPos);
                        if (this.BoundsCheck(addedBlock))
                        {
                            if (this.SpaceCheck(addedBlock, new Point(0, 1, 0)))
                            {
                                additionSuccess = true;
                            }
                        }
                    }
                    break;

                case BlockLine.Orientation.Front:
                    if (lastBlockOrientation != BlockLine.Orientation.Back && lastBlockOrientation != BlockLine.Orientation.Front)
                    {
                        addedBlock.startPoint = new Point(this.endingPoint.xPos, this.endingPoint.yPos - 1, this.endingPoint.zPos);
                        addedBlock.endPoint   = new Point(this.endingPoint.xPos, this.endingPoint.yPos - numBlocks, this.endingPoint.zPos);
                        if (this.BoundsCheck(addedBlock))
                        {
                            if (this.SpaceCheck(addedBlock, new Point(0, -1, 0)))
                            {
                                additionSuccess = true;
                            }
                        }
                    }
                    break;

                case BlockLine.Orientation.Left:
                    if (lastBlockOrientation != BlockLine.Orientation.Left && lastBlockOrientation != BlockLine.Orientation.Right)
                    {
                        addedBlock.startPoint = new Point(this.endingPoint.xPos - 1, this.endingPoint.yPos, this.endingPoint.zPos);
                        addedBlock.endPoint   = new Point(this.endingPoint.xPos - numBlocks, this.endingPoint.yPos, this.endingPoint.zPos);
                        if (this.BoundsCheck(addedBlock))
                        {
                            if (this.SpaceCheck(addedBlock, new Point(-1, 0, 0)))
                            {
                                additionSuccess = true;
                            }
                        }
                    }
                    break;

                case BlockLine.Orientation.Right:
                    if (lastBlockOrientation != BlockLine.Orientation.Left && lastBlockOrientation != BlockLine.Orientation.Right)
                    {
                        addedBlock.startPoint = new Point(this.endingPoint.xPos + 1, this.endingPoint.yPos, this.endingPoint.zPos);
                        addedBlock.endPoint   = new Point(this.endingPoint.xPos + numBlocks, this.endingPoint.yPos, this.endingPoint.zPos);
                        if (this.BoundsCheck(addedBlock))
                        {
                            if (this.SpaceCheck(addedBlock, new Point(1, 0, 0)))
                            {
                                additionSuccess = true;
                            }
                        }
                    }
                    break;

                case BlockLine.Orientation.Up:
                    if (lastBlockOrientation != BlockLine.Orientation.Up && lastBlockOrientation != BlockLine.Orientation.Down)
                    {
                        addedBlock.startPoint = new Point(this.endingPoint.xPos, this.endingPoint.yPos, this.endingPoint.zPos + 1);
                        addedBlock.endPoint   = new Point(this.endingPoint.xPos, this.endingPoint.yPos, this.endingPoint.zPos + numBlocks);
                        if (this.BoundsCheck(addedBlock))
                        {
                            if (this.SpaceCheck(addedBlock, new Point(0, 0, 1)))
                            {
                                additionSuccess = true;
                            }
                        }
                    }
                    break;

                case BlockLine.Orientation.Down:
                    if (lastBlockOrientation != BlockLine.Orientation.Up && lastBlockOrientation != BlockLine.Orientation.Down)
                    {
                        addedBlock.startPoint = new Point(this.endingPoint.xPos, this.endingPoint.yPos, this.endingPoint.zPos - 1);
                        addedBlock.endPoint   = new Point(this.endingPoint.xPos, this.endingPoint.yPos, this.endingPoint.zPos - numBlocks);
                        if (this.BoundsCheck(addedBlock))
                        {
                            if (this.SpaceCheck(addedBlock, new Point(0, 0, -1)))
                            {
                                additionSuccess = true;
                            }
                        }
                    }
                    break;
                }
            }

            if (additionSuccess)
            {
                this.listOfBlocks.Add(addedBlock);
                this.endingPoint = addedBlock.endPoint;

                //Console.WriteLine("Just added block {0} with orientation {1}.", this.blockLineIndex, addedBlock.orientation);

                this.blockLineIndex++;

                if (this.blockLineOrder[this.blockLineIndex] == 0)
                {
                    this.isFinished = true;
                    Console.WriteLine("==Solution==================================================================================");
                    this.PrintPuzzle();
                }
            }

            return(additionSuccess);
        }