Ejemplo n.º 1
0
 public SudokuBoard(int width, int height, int maxValue)
 {
     _maxValue = maxValue;
     tiles     = new SudokuTile[width, height];
     CreateTiles();
     if (_maxValue == width || _maxValue == height) // If maxValue is not width or height, then adding line rules would be stupid
     {
         SetupLineRules();
     }
 }
Ejemplo n.º 2
0
        public IEnumerable <SudokuBoard> Solve()
        {
            ResetSolutions();
            SudokuProgress simplify = SudokuProgress.PROGRESS;

            while (simplify == SudokuProgress.PROGRESS)
            {
                simplify = Simplify();
            }

            if (simplify == SudokuProgress.FAILED)
            {
                yield break;
            }

            // Find one of the values with the least number of alternatives, but that still has at least 2 alternatives
            var query = from rule in rules
                        from tile in rule
                        where tile.PossibleCount > 1
                        orderby tile.PossibleCount ascending
                        select tile;

            SudokuTile chosen = query.FirstOrDefault();

            if (chosen == null)
            {
                // The board has been completed, we're done!
                yield return(this);

                yield break;
            }

            Console.WriteLine("SudokuTile: " + chosen.ToString());

            foreach (var value in Enumerable.Range(1, _maxValue))
            {
                // Iterate through all the valid possibles on the chosen square and pick a number for it
                if (!chosen.IsValuePossible(value))
                {
                    continue;
                }
                var copy = new SudokuBoard(this);
                copy.Tile(chosen.X, chosen.Y).Fix(value, "Trial and error");
                foreach (var innerSolution in copy.Solve())
                {
                    yield return(innerSolution);
                }
            }
            yield break;
        }
Ejemplo n.º 3
0
        internal SudokuProgress Simplify()
        {
            SudokuProgress result = SudokuProgress.NO_PROGRESS;
            bool           valid  = CheckValid();

            if (!valid)
            {
                return(SudokuProgress.FAILED);
            }

            foreach (SudokuRule rule in rules)
            {
                result = SudokuTile.CombineSolvedState(result, rule.Solve());
            }

            return(result);
        }
Ejemplo n.º 4
0
        public SudokuBoard(SudokuBoard copy)
        {
            _maxValue = copy._maxValue;
            tiles     = new SudokuTile[copy.Width, copy.Height];
            CreateTiles();
            // Copy the tile values
            foreach (var pos in SudokuFactory.box(Width, Height))
            {
                tiles[pos.Item1, pos.Item2]       = new SudokuTile(pos.Item1, pos.Item2, _maxValue);
                tiles[pos.Item1, pos.Item2].Value = copy.tiles[pos.Item1, pos.Item2].Value;
            }

            // Copy the rules
            foreach (SudokuRule rule in copy.rules)
            {
                var ruleTiles = new HashSet <SudokuTile>();
                foreach (SudokuTile tile in rule)
                {
                    ruleTiles.Add(tiles[tile.X, tile.Y]);
                }
                rules.Add(new SudokuRule(ruleTiles, rule.Description));
            }
        }