コード例 #1
0
        /*
         * IntersectionStrategy takes it this far:

              9 8
              3 2 7  1 9      8
                5 4    8 2

              7 4    6        9
              2 9
                1 3      7

             >4<       5    2
              5 6      1 4  8 7 3
                            5   4

         * Take it further! (resolve that >4<)
         */
        public Cell Solve(Board board)
        {
            // iterate over all numbers
            foreach (var cellValue in Cell.All)
            {
                // for each sub-grid
                for (var x = 0; x < Board.GridSize; x++)
                {
                    for (var y = 0; y < Board.GridSize; y++)
                    {
                        // and search
                        var values = board.GetGridCells (x,y);
                        if (values.Contains(cellValue))
                        {
                            continue;
                        }

                        // attempt solve this
                        var availablePositions = board
                            .GetGridCells(x, y)
                            .Where(i => !i.IsAssigned())
                            .ToArray();

                        // check columns
                        var adjacentColumnPositions = GetColumnPositions (board, cellValue, x);
                        var adjacentRowPositions =    GetRowPositions (board, cellValue, y);

                        var candidates =
                            availablePositions
                                .ExcludeOn(adjacentColumnPositions, i => i.X)
                                .ExcludeOn(adjacentRowPositions, i => i.Y)
                                .ToArray();

                        if (candidates.Length == 1)
                        {
                            var target = candidates[0];
                            var move = target.Apply(cellValue);
                            Statistics.NeighbouringNumbersStrategyMoves++;
                            return move;
                        }
                    }
                }
            }

            // give up
            return null;
        }
コード例 #2
0
        public static Cell[] GetValueCandidates(Board board, Cell cell)
        {
            var columnValues = board.GetAssignedColumnCells(cell);
            var rowValues =    board.GetAssignedRowCells(cell);
            var gridValues = board.GetGridCells(cell)
                .Where(i => i.IsAssigned())
                .ToArray();

            var all = Cell.All;
            // BUG: "all" keeps bad positional value, corrupting board.

            var missingColumnValues = all.Except(columnValues).ToArray();
            var missingRowValues =    all.Except(rowValues)   .ToArray();
            var missingGridValues =   all.Except(gridValues)  .ToArray();

            var candidates = missingColumnValues
                .Intersect(missingRowValues)
                .Intersect(missingGridValues)
                .ToArray();

            return candidates;
        }