Exemplo n.º 1
0
        public SolutionDetails SolvePuzzle(Board board, IPieceFeeder pieceFeeder)
        {
            if (!SanityCheck(board, pieceFeeder))
                throw new InvalidOperationException("Failed sanity check!");

            var solved = false;
            var iterations = 0;
            IEnumerable<RotatedPiece> solution = null;

            foreach (var permutation in pieceFeeder.Permutations) {

                iterations++;

                board.Reset();

                // ReSharper disable PossibleMultipleEnumeration
                if (permutation.All(board.LayoutNextPiece))
                {
                    solved = true;
                    solution = permutation;
                    break;
                }
                // ReSharper restore PossibleMultipleEnumeration
            }

            return new SolutionDetails(solved, iterations, solution);
        }
Exemplo n.º 2
0
        public Board PopulateBoardWithSolution(int[] solutionRowIndexes)
        {
            var board = new Board(_board.BoardSize);

            for (var i = 0; i < solutionRowIndexes.Length; i++)
            {
                var solutionRowIndex = solutionRowIndexes[i];
                var tuple = _dictionary[solutionRowIndex];
                var rotatedPiece = tuple.Item1;
                var x = tuple.Item2;
                var y = tuple.Item3;
                board.PlacePieceAt(rotatedPiece, x, y);
            }

            return board;
        }
Exemplo n.º 3
0
        private static void SolveBoard(int boardSize, Piece[] pieces)
        {
            var board = new Board(boardSize);
            var solver = new Solver();
            var pieceFeeder = new PieceFeeder(pieces);
            var solutionDetails = solver.SolvePuzzle(board, pieceFeeder);

            if (solutionDetails.Solved) {
                var consolePrintTarget = new ConsolePrintTarget();
                var boardPrinter = new BoardPrinter(consolePrintTarget);
                Console.WriteLine("Solution found after {0} iterations.", solutionDetails.Iterations);
                PrintPermutation(solutionDetails.Solution);
                Console.WriteLine();
                boardPrinter.Print(board);
            }
            else
            {
                Console.WriteLine("Failed to find a solution after {0} iterations.", solutionDetails.Iterations);
            }

            Console.WriteLine();
        }
Exemplo n.º 4
0
        public SolverUsingDlx(IEnumerable<Piece> pieces)
        {
            _pieces = pieces.ToArray();
            _dictionary = new Dictionary<int, Tuple<RotatedPiece, int, int>>();

            var numSquares = 0;

            foreach (var piece in _pieces)
            {
                for (var x = 0; x < piece.Width; x++)
                {
                    for (var y = 0; y < piece.Height; y++)
                    {
                        if (piece.SquareAt(x, y) != null)
                            numSquares++;
                    }
                }
            }

            var boardSize = Convert.ToInt32(Math.Sqrt(numSquares));
            _board = new Board(boardSize);
            _board.ForceColourOfSquareZeroZeroToBeBlack();
        }
Exemplo n.º 5
0
        public bool SanityCheck(Board board, IPieceFeeder pieceFeeder)
        {
            var totalSquaresExpected = board.BoardSize * board.BoardSize;
            var totalWhiteSquaresExpected = totalSquaresExpected / 2;
            var totalBlackSquaresExpected = totalSquaresExpected / 2;

            var actualSquares = 0;
            var actualWhiteSquares = 0;
            var actualBlackSquares = 0;

            foreach (var piece in pieceFeeder.Pieces)
            {
                for (var x = 0; x < piece.Width; x++)
                {
                    for (var y = 0; y < piece.Height; y++)
                    {
                        var square = piece.SquareAt(x, y);
                        if (square != null)
                        {
                            actualSquares++;

                            if (square.Colour == Colour.White)
                                actualWhiteSquares++;

                            if (square.Colour == Colour.Black)
                                actualBlackSquares++;
                        }
                    }
                }
            }

            return (
                actualSquares == totalSquaresExpected &&
                actualWhiteSquares == totalWhiteSquaresExpected &&
                actualBlackSquares == totalBlackSquaresExpected);
        }