private static void TrySolve(Definition.Sudoku playingSudoku, bool showGUI)
        {
            SudokuConsole.Print(playingSudoku);

            Console.WriteLine();

            //show sudoku with GUI
            if (showGUI)
            {
                playerGUIController = new GUI.SudokuPlayerController(null, playingSudoku);
            }

            bool     solved;
            TimeSpan interval;

            using (var solver = new Core.SudokuSolver(playingSudoku))
            {
                DateTime beforeNow = DateTime.Now;
                solved   = solver.TrySolve();
                interval = DateTime.Now - beforeNow;
            }

            string resultInfo = solved ?
                                "Solved" :
                                "Cannot solve this sudoku with current knowledge";

            Console.WriteLine("{0} in {1:0} ms", resultInfo, interval.TotalMilliseconds);

            if (!solved)
            {
                Console.WriteLine("{0} seats remainder",
                                  new SudokuElementEnumerable(playingSudoku).SeatCount());
            }

            bool reallySolved = solved && playingSudoku.Validate();

            if (!reallySolved)
            {
                if (solved)
                {
                    Console.WriteLine("じゃないよ!");
                }
            }
            SudokuConsole.Print(playingSudoku);

            Console.WriteLine();

            if (showGUI)
            {
                playerGUIController.Show();
            }
        }
Пример #2
0
        public static bool ValueEquals(this Definition.Sudoku first, Definition.Sudoku second)
        {
            if (first == null)
            {
                throw new ArgumentNullException("first");
            }
            if (second == null)
            {
                throw new ArgumentNullException("second");
            }

            return(new SudokuElementEnumerable(first).SequenceEqual(
                       new SudokuElementEnumerable(second),
                       new ElementValueComparer()));
        }
Пример #3
0
 private IEnumerable <Definition.IElementCluster> Iterator(Definition.Sudoku sudoku)
 {
     foreach (var grid in sudoku.Grids)
     {
         yield return(grid);
     }
     foreach (var row in sudoku.Rows)
     {
         yield return(row);
     }
     foreach (var column in sudoku.Columns)
     {
         yield return(column);
     }
 }
Пример #4
0
            public void Print(Definition.Sudoku sudoku)
            {
                if (sudoku == null)
                {
                    throw new ArgumentNullException("sudoku");
                }

                int gridIndex = 0;
                var cursorTop = Console.CursorTop;

                foreach (var grid in sudoku.Grids)
                {
                    printGrid(grid, gridIndex++, cursorTop);
                }
                Console.WriteLine();
            }
Пример #5
0
        public static bool Validate(this Definition.Sudoku sudoku)
        {
            if (sudoku == null)
            {
                throw new ArgumentNullException("sudoku");
            }

            foreach (var elements in new SudokuBlockEnumerable(sudoku))
            {
                if (!Validate(elements))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #6
0
        private IEnumerable <Definition.Line> Iterator(Definition.Sudoku sudoku, Definition.LineType lineType)
        {
            switch (lineType)
            {
            case Definition.LineType.Row:
                return(sudoku.Rows);

            case Definition.LineType.Column:
                return(sudoku.Columns);

            case Definition.LineType.Both:
                return(sudoku.Rows.Concat(sudoku.Columns));

            default: throw new NotImplementedException();
            }
        }
Пример #7
0
        public static bool TryParse(string source, out Definition.Sudoku sudoku)
        {
            sudoku = null;

            if (string.IsNullOrEmpty(source))
            {
                return(false);
            }

            int size = SUDOKU_SPLIT_LEN;

            string[] lines = source.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            if (lines.Length != size)
            {
                return(false);
            }

            int[] elementValues = new int[size * size];
            int   valueIndex    = 0;

            foreach (var line in lines)
            {
                int[] values;
                if (!TryParse(line, out values))
                {
                    return(false);
                }
                Array.Copy(values, 0, elementValues, valueIndex++ *size, size);
            }

            sudoku = new Definition.Sudoku();
            using (var sudokuElementIterator = new SudokuElementEnumerable(sudoku, SudokuElementTravelType.Row).GetEnumerator())
                using (var valueIterator = elementValues.AsEnumerable().GetEnumerator())
                {
                    while (sudokuElementIterator.MoveNext() && valueIterator.MoveNext())
                    {
                        var element = sudokuElementIterator.Current;
                        int value   = valueIterator.Current;
                        if (value > 0)
                        {
                            element.SetValue(value);
                        }
                    }
                }

            return(true);
        }
Пример #8
0
        /// <summary>
        /// build sudoku from completed sudoku then try to resolve
        /// </summary>
        /// <param name="seed">known seeds to generate a completed sudoku</param>
        /// <param name="showGUI">whether to show GUI. turned on this will make solve time more longer</param>
        public static void BuildAndResolve(int seed, bool showGUI)
        {
            //create the only one instance of Sudoku
            var sudoku = new Definition.Sudoku();

            //build completed Sudoku with one of known seeds
            if (new Core.CompletedSudokuBuilder(seed).Build(sudoku))
            {
                Console.WriteLine("Built");
            }
            else
            {
                SudokuConsole.WaitLine();
                return;
            }

            SudokuConsole.Print(sudoku);

            var builder = new Core.SudokuBuilder(sudoku.Copy(),
                                                     //DateTime.Now.Millisecond);
                                                 0); //use fixed seed so each level build same sudoku

            Console.WriteLine("press Enter to start");
            SudokuConsole.WaitLine();
            Console.Clear();

            int startLevel = 125;
            int lastLevel  = 53;            //last 53

            for (int i = startLevel; i < lastLevel; i++)
            {
                BuildSudokuAndTrySolve(builder, i, showGUI);

                Console.WriteLine("press Enter to next");
                SudokuConsole.WaitLine();
                Console.Clear();
            }

            BuildSudokuAndTrySolve(builder, lastLevel, showGUI);
        }
Пример #9
0
 private static readonly SudokuPlayer consolePlayer = new SudokuPlayer();         //use only one instance
 public static void Print(Definition.Sudoku sudoku)
 {
     consolePlayer.Print(sudoku);
 }
        static bool InputSudoku(out Definition.Sudoku sudoku)
        {
            sudoku = null;

            Console.WriteLine("input sudoku data:");
            Console.WriteLine();
            Console.WriteLine("(whitespace to separate elements");
            Console.WriteLine(" _ to indicate empty element");
            Console.WriteLine(" new line to separete rows");
            Console.WriteLine(" esc to cancel)");
            Console.WriteLine();

            bool cancelled = false;
            int  size      = SudokuHelper.SUDOKU_SPLIT_LEN;

            int[] values = new int[size * size];

            for (int i = 0; i < size;)
            {
                Console.WriteLine("line " + (i + 1));
                StringBuilder lineBuilder = new StringBuilder();
                bool          endOfLine   = false;
                while (!endOfLine && !cancelled)
                {
                    var key = Console.ReadKey();
                    switch (key.Key)
                    {
                    case ConsoleKey.Enter:
                        endOfLine = true;
                        break;

                    case ConsoleKey.Escape:
                        cancelled = true;
                        break;

                    case ConsoleKey.Backspace:
                        if (lineBuilder.Length > 0)
                        {
                            lineBuilder.Remove(lineBuilder.Length - 1, 1);
                        }
                        break;
                    }

                    switch (key.KeyChar)
                    {
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                    case '_':
                    case ' ':
                        lineBuilder.Append(key.KeyChar);
                        break;

                    default: break;
                    }
                }

                if (cancelled)
                {
                    Console.WriteLine("cancelled");
                    return(false);
                }

                int[] lineValues;
                if (!SudokuHelper.TryParse(lineBuilder.ToString(), out lineValues))
                {
                    Console.WriteLine("wrong line data");
                    i--;
                    continue;
                }

                Array.Copy(lineValues, 0, values, i++ *size, size);

                Console.WriteLine();
            }

            sudoku = new Definition.Sudoku();
            using (var sudokuElementIterator = new SudokuElementEnumerable(sudoku, SudokuElementTravelType.Row).GetEnumerator())
                using (var valueIterator = values.AsEnumerable().GetEnumerator())
                {
                    while (sudokuElementIterator.MoveNext() && valueIterator.MoveNext())
                    {
                        var element = sudokuElementIterator.Current;
                        int value   = valueIterator.Current;
                        if (value > 0)
                        {
                            element.SetValue(value);
                        }
                    }
                }

            if (!sudoku.Validate())
            {
                sudoku = null;
                Console.WriteLine("invalid sudoku data");
                return(false);
            }

            return(true);
        }
Пример #11
0
 public SudokuElementEnumerable(Definition.Sudoku sudoku)
     : this(sudoku, SudokuElementTravelType.Grid)
 {
 }