Exemple #1
0
        static void Main(string[] args)
        {
            Sudoku s = new Sudoku();
            s.solveIt();

            System.Threading.Thread.Sleep(500);
            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }
        public SudokuGridViewModel(Sudoku _sudoku)
        {
            sudoku = _sudoku;

            editableValues = new bool[sudoku.size, sudoku.size];

            for (int i = 0; i < sudoku.size; i++) {
                for (int j = 0; j < sudoku.size; j++) {

                    if (sudoku.sudoku[i, j] == '.') {
                        editableValues[i, j] = true;
                    } else {
                        editableValues[i, j] = false;
                    }
                }
            }
        }
 public static void Main()
 {
     Console.WriteLine("Enter Suduko here:");
     var input = Console.ReadLine();
     var sudoku = new Sudoku(input);
     var displayer = new Displayer();
     displayer.Show(sudoku);
     Console.WriteLine();
     Console.WriteLine("Show solve? (y/n)");
     var showSolve = Console.ReadLine() == "y";
     var solver = new Solver(sudoku, showSolve);
     Console.WriteLine();
     Console.WriteLine("***SOLVING***");
     Console.WriteLine();
     solver.Solve();
     Console.WriteLine("SOLUTION:");
     Console.WriteLine();
     displayer.Show(sudoku);
     Console.ReadLine();
 }
 public void Show(Sudoku sudoku)
 {
     for (int i = 0; i < 9; i++)
     {
         if (i % 3 == 0)
         {
             Console.WriteLine("_________________________");
             Console.WriteLine();
         }
         for (int j = 0; j < 9; j++)
         {
             if (j % 3 == 0)
             {
                 Console.Write("| ");
             }
             Console.Write(sudoku[i, j] == 0 ? " " : sudoku[i, j].ToString());
             Console.Write(" ");
         }
         Console.WriteLine("|");
     }
     Console.WriteLine("_________________________");
 }
        private static void DoSudoku()
        {
            var chill = new[,]

            {
                {0, 0, 0, 3, 9, 0, 0, 1, 0},
                {5, 0, 1, 0, 0, 0, 0, 4, 0},
                {9, 0, 0, 7, 0, 0, 5, 0, 0},
                {6, 0, 2, 5, 3, 0, 0, 7, 0},
                {0, 0, 0, 0, 7, 0, 0, 0, 8},
                {7, 0, 0, 8, 0, 0, 9, 0, 3},
                {8, 0, 3, 0, 1, 0, 0, 9, 0},
                {0, 9, 0, 2, 0, 6, 0, 0, 7},
                {4, 0, 0, 0, 0, 3, 0, 6, 1}
                /*{0, 2, 3, 4, 5, 6, 7, 8, 9},
                {9, 1, 2, 3, 4, 5, 6, 7, 8},
                {8, 9, 1, 2, 3, 4, 5, 6, 7},
                {7, 8, 9, 1, 2, 3, 4, 5, 6},
                {6, 7, 8, 9, 1, 2, 3, 4, 5},
                {5, 6, 7, 8, 9, 1, 2, 3, 4},
                {4, 5, 6, 7, 8, 9, 1, 2, 3},
                {3, 4, 5, 6, 7, 8, 9, 1, 2},
                {2, 3, 4, 5, 6, 7, 8, 9, 1}*/
            };
            var sudoku = new Sudoku(chill);
            var exit = false;
            //sudoku.PrintBoard();

            while (!exit)
            {
                Console.WriteLine("\n--------------------\nSUDOKU CONTROL PANEL\n--------------------\n");
                Console.WriteLine("1 Solve it!");
                Console.WriteLine("2 Try placing a number");
                Console.WriteLine("3 View current board");
                Console.WriteLine("4 Enter a new board");
                Console.WriteLine("5 Exit");

                Console.Write("Enter your menu choice: ");
                var userInput = Console.ReadKey().KeyChar.ToString();

                switch (userInput)
                {
                    case "1":
                        DoSolveIt(sudoku);
                        break;
                    case "2":
                        DoNumberPlacer(sudoku);
                        break;
                    case "3":
                        Console.WriteLine("\nYou have selected View current board");
                        break;
                    case "4":
                        Console.WriteLine("\nYou have selected Enter a new board");
                        break;
                    case "5":
                        Console.WriteLine("\n**EXITING SUDOKU**");
                        Thread.Sleep(500);
                        Console.WriteLine("Cleaning Board");
                        Thread.Sleep(250);
                        Console.WriteLine("Replacing Board");
                        Thread.Sleep(500);
                        Console.WriteLine("Wasting Time...");
                        Thread.Sleep(100);
                        Console.WriteLine("\n**EXITING**\n");
                        exit = true;
                        break;
                    default:
                        Console.WriteLine("\n***INVALID ENTRY***");
                        break;
                }
            }
        }
 private static void DoSolveIt(Sudoku sudoku)
 {
     Console.Write("\nShould I solve it the fast way?: ");
     var heuristic = Console.ReadKey().KeyChar.ToString().ToUpper().Equals("Y");
     Console.WriteLine("\n**SOLVING**");
     Thread.Sleep(1000);
     sudoku.BruteForce(heuristic);
     Console.WriteLine("**SOLVED**");
     sudoku.PrintBoard();
 }
 private static void DoNumberPlacer(Sudoku sudoku)
 {
     Console.WriteLine("\n*********************\n*********************");
     var done = false;
     while (!done)
     {
         sudoku.PrintBoard();
         Console.Write("\nPlease insert number to try (1-9): ");
         var userInput = Console.ReadKey().KeyChar.ToString();
         int num;
         if (!int.TryParse(userInput, out num) || num == 0)
         {
             Console.WriteLine("\n***INVALID ENTRY***");
             continue;
         }
         Console.Write("\nPlease insert x location to place (1-9): ");
         userInput = Console.ReadKey().KeyChar.ToString();
         int x;
         if (int.TryParse(userInput, out x) && x != 0)
         {
             Console.Write("\nPlease insert y location to place (1-9): ");
             userInput = Console.ReadKey().KeyChar.ToString();
             int y;
             if (int.TryParse(userInput, out y) && y != 0)
             {
                 Console.WriteLine("\n{0} can be placed at ({1}, {2}): {3}", num, x, y,
                     sudoku.NumberAt(x - 1, y - 1, num));
                 done = true;
             }
             else
             {
                 Console.WriteLine("\n***INVALID ENTRY***");
             }
         }
         else
         {
             Console.WriteLine("\n***INVALID ENTRY***");
         }
     }
 }
Exemple #8
0
        /// <summary>
        /// Initializes and renders the form, using the gridSize to determine how to dynamically add and
        /// style form controls.
        /// </summary>
        /// <param name="gridSize">The 'gridSize' equals the amount of cells in a block, row or column of the sudoku.</param>
        public ViewerForm(int gridSize)
        {
            InitializeComponent();

            Sudoku = new Sudoku(gridSize);

            // sudokuColumnCount equals the amount of columns needed in the top-level TableLayoutPanel
            // to be able to show all blocks in the sudoku.
            int sudokuColumnCount = Sudoku.GridSize / Sudoku.BlockSize.Horizontal;

            // The width of a column in a TableLayoutPanel is defined as a percentage, so we divide 100 by sudokuColumnCount.
            float sudokuColumnWidth = 100 / sudokuColumnCount;


            // sudokuRowCount equals the amount of rows needed in the top-level TableLayoutPanel
            // to be able to show all blocks in the sudoku.
            int sudokuRowCount = Sudoku.GridSize / Sudoku.BlockSize.Vertical;

            // The height of a row in a TableLayoutPanel is defined as a percentage, so we divide 100 by sudokuRowCount.
            float sudokuRowHeight = 100 / sudokuRowCount;


            // 'Width' and 'Height' are builtin properties of the 'Form' class, representing the width and height of the form.
            // The formulas for them are defined as follows:
            // Width = (int)(((width of numericUpDown) + (extra required width per digit) * (Math.Floor(Math.Log10(Sudoku.GridSize)) + 1)) * Sudoku.GridSize +
            //      sudokuColumnCount * ((total horizontal margin along the outside of all numericUpDowns in a single block) +
            //      (horizontal margin between two numericUpDowns in the same block) * (Sudoku.BlockSize.Horizontal - 1)) + 22);
            //
            // Height = ((height of numericUpDown) * Sudoku.GridSize + sudokuRowCount * ((total vertical margin along the outside of all numericUpDowns in a single block) +
            //      (vertical margin between two numericUpDowns in the same block) * (Sudoku.BlockSize.Vertical - 1))) + (height of progressBar);
            Width  = (int)((22 + 25 * (Math.Floor(Math.Log10(Sudoku.GridSize)) + 1)) * Sudoku.GridSize + sudokuColumnCount * (12 + 6 * (Sudoku.BlockSize.Horizontal - 1)) + 22);
            Height = (41 * Sudoku.GridSize + sudokuRowCount * (15 + 9 * (Sudoku.BlockSize.Vertical - 1))) + 71;


            // Create the top-level TableLayoutPanel with the specified attributes.
            TableLayoutPanel mainSudokuContainer = new TableLayoutPanel()
            {
                Dock            = DockStyle.Fill,
                CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble,

                ColumnCount = sudokuColumnCount,
                RowCount    = sudokuRowCount
            };

            // Set the width for all columns in the top-level TableLayoutPanel.
            for (int i = 0; i < sudokuColumnCount; i++)
            {
                mainSudokuContainer.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, sudokuColumnWidth));
            }

            // Set the height for all rows in the top-level TableLayoutPanel.
            for (int i = 0; i < sudokuRowCount; i++)
            {
                mainSudokuContainer.RowStyles.Add(new RowStyle(SizeType.Percent, sudokuRowHeight));
            }

            // Each cell in the top-level TableLayoutPanel makes up one block of the sudoku;
            // here we fill each of these blocks with another TableLayoutPanel, which will contain the actual cells of the sudoku.
            for (int i = 0; i < Sudoku.GridSize; i++)
            {
                mainSudokuContainer.Controls.Add(GenerateSudokuBlockContainer(i + 1));
            }

            // Add the top-level TableLayoutPanel with all of it's child controls to the ViewerForm.
            sudokuContainerPanel.Controls.Add(mainSudokuContainer);
        }
        public static (Sudoku solution, int functionCallCount, double elapsedTimeInMs) Solve(Sudoku initial)
        {
            if (!SudokuValidator.Validate(initial))
            {
                return(null, 0, 0);
            }

            var stopwatch         = Stopwatch.StartNew();
            var functionCallCount = 0;
            var solution          = Solve(initial, ref functionCallCount);

            return(solution, functionCallCount, stopwatch.Elapsed.TotalMilliseconds);
        }
 // create a new puzzle to solve
 private void btnGenerate_Click(object sender, System.EventArgs e)
 {
     _currentPuzzle = new Sudoku();
     _currentPuzzle.Data = new byte[9,9];
     _currentPuzzle.Generate(20);
     PopulateFromData();
 }
Exemple #11
0
 public void DisplaySudoku(OutputStream stream)
 {
     Sudoku.DisplaySudoku(this.sudoku, stream);
 }
Exemple #12
0
 public static IEnumerable <int> GetPossibilities(Sudoku sudoku, int fila, int columna)
 {
     var(f, c, q) = sudoku.Get(fila, columna);
     return(Enumerable.Range(1, 9).Except(f.Union(c).Union(q)));
 }
Exemple #13
0
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
            case 1:

            #line 8 "..\..\MainWindow.xaml"
                ((SudokuSolver.MainWindow)(target)).KeyDown += new System.Windows.Input.KeyEventHandler(this.Window_KeyDown);

            #line default
            #line hidden
                return;

            case 2:
                this.Sudoku = ((SudokuSolver.Sudoku)(target));
                return;

            case 3:
                this.buttSolve = ((System.Windows.Controls.Button)(target));

            #line 12 "..\..\MainWindow.xaml"
                this.buttSolve.Click += new System.Windows.RoutedEventHandler(this.buttSolve_Click);

            #line default
            #line hidden
                return;

            case 4:
                this.buttClean = ((System.Windows.Controls.Button)(target));

            #line 13 "..\..\MainWindow.xaml"
                this.buttClean.Click += new System.Windows.RoutedEventHandler(this.buttClean_Click);

            #line default
            #line hidden
                return;

            case 5:
                this.buttFill = ((System.Windows.Controls.Button)(target));

            #line 14 "..\..\MainWindow.xaml"
                this.buttFill.Click += new System.Windows.RoutedEventHandler(this.buttFill_Click);

            #line default
            #line hidden
                return;

            case 6:
                this.buttSave = ((System.Windows.Controls.Button)(target));

            #line 15 "..\..\MainWindow.xaml"
                this.buttSave.Click += new System.Windows.RoutedEventHandler(this.buttSave_Click);

            #line default
            #line hidden
                return;

            case 7:
                this.stateComboBox = ((System.Windows.Controls.ComboBox)(target));

            #line 16 "..\..\MainWindow.xaml"
                this.stateComboBox.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(this.stateComboBox_SelectionChanged);

            #line default
            #line hidden
                return;

            case 8:
                this.txtState = ((System.Windows.Controls.TextBox)(target));

            #line 17 "..\..\MainWindow.xaml"
                this.txtState.MouseDown += new System.Windows.Input.MouseButtonEventHandler(this.txtState_MouseDown);

            #line default
            #line hidden
                return;

            case 9:
                this.buttUnfix = ((System.Windows.Controls.Button)(target));

            #line 18 "..\..\MainWindow.xaml"
                this.buttUnfix.Click += new System.Windows.RoutedEventHandler(this.buttUnfix_Click);

            #line default
            #line hidden
                return;
            }
            this._contentLoaded = true;
        }
 public MainWindow()
 {
     InitializeComponent();
     _sudoku          = new Sudoku();
     this.DataContext = _sudoku;
 }
 public void AddGrid(int size, int difficulty)
 {
     String dictionary = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     Sudoku generatedSudoku = new Sudoku("Sudoku " + (SudokuList.Count + 1), DateTime.Now, dictionary.Substring(0, size), difficulty);
     generatedSudoku.Validate();
     SudokuList.Add(new SudokuGridViewModel(generatedSudoku));
 }
 public bool IsValid(Sudoku sudoku)
 {
     return sudoku.Rows.AllAreValidSudokuSets()
         && sudoku.Columns.AllAreValidSudokuSets()
         && sudoku.Squares.AllAreValidSudokuSets();
 }
Exemple #17
0
        static void Main(string[] args)
        {
            Sudoku game = new Sudoku(medium1);

            game.Solve();
        }
Exemple #18
0
 public override string ToString()
 {
     return(Sudoku.ToString(Sudoku.To2dArray(this.cells)));
 }
 public Sudoku Solve(Sudoku sudoku) => solve(sudoku, 0);
 public static bool Validate(Sudoku sudoku) =>
 NoRowContainsDuplicates(sudoku) &&
 NoColumnContainsDuplicates(sudoku) &&
 NoBoxContainsDuplicates(sudoku);
Exemple #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SudokuFitnessCalculator"/> class.
 /// </summary>
 /// <param name="sudoku">The sudoku we want to solve.</param>
 public SudokuFitnessCalculator(Sudoku sudoku)
 {
     _sudoku = sudoku;
 }
Exemple #22
0
 private static void PrintSudoku(Sudoku sudoku)
 {
     Console.WriteLine();
     Console.WriteLine(SudokuStringifier.Stringify(sudoku, HeaderLineLength));
     Console.WriteLine();
 }
Exemple #23
0
        public MainWindow()
        {
            InitializeComponent();

            #region initializing Sudoku
            Sudoku.Initialize(new SudokuPiece[]
            {
                new SudokuPiece(textBox1),
                new SudokuPiece(textBox2),
                new SudokuPiece(textBox3),
                new SudokuPiece(textBox4),
                new SudokuPiece(textBox5),
                new SudokuPiece(textBox6),
                new SudokuPiece(textBox7),
                new SudokuPiece(textBox8),
                new SudokuPiece(textBox9),
                new SudokuPiece(textBox10),
                new SudokuPiece(textBox11),
                new SudokuPiece(textBox12),
                new SudokuPiece(textBox13),
                new SudokuPiece(textBox14),
                new SudokuPiece(textBox15),
                new SudokuPiece(textBox16),
                new SudokuPiece(textBox17),
                new SudokuPiece(textBox18),
                new SudokuPiece(textBox19),
                new SudokuPiece(textBox20),
                new SudokuPiece(textBox21),
                new SudokuPiece(textBox22),
                new SudokuPiece(textBox23),
                new SudokuPiece(textBox24),
                new SudokuPiece(textBox25),
                new SudokuPiece(textBox26),
                new SudokuPiece(textBox27),
                new SudokuPiece(textBox28),
                new SudokuPiece(textBox29),
                new SudokuPiece(textBox30),
                new SudokuPiece(textBox31),
                new SudokuPiece(textBox32),
                new SudokuPiece(textBox33),
                new SudokuPiece(textBox34),
                new SudokuPiece(textBox35),
                new SudokuPiece(textBox36),
                new SudokuPiece(textBox37),
                new SudokuPiece(textBox38),
                new SudokuPiece(textBox39),
                new SudokuPiece(textBox40),
                new SudokuPiece(textBox41),
                new SudokuPiece(textBox42),
                new SudokuPiece(textBox43),
                new SudokuPiece(textBox44),
                new SudokuPiece(textBox45),
                new SudokuPiece(textBox46),
                new SudokuPiece(textBox47),
                new SudokuPiece(textBox48),
                new SudokuPiece(textBox49),
                new SudokuPiece(textBox50),
                new SudokuPiece(textBox51),
                new SudokuPiece(textBox52),
                new SudokuPiece(textBox53),
                new SudokuPiece(textBox54),
                new SudokuPiece(textBox55),
                new SudokuPiece(textBox56),
                new SudokuPiece(textBox57),
                new SudokuPiece(textBox58),
                new SudokuPiece(textBox59),
                new SudokuPiece(textBox60),
                new SudokuPiece(textBox61),
                new SudokuPiece(textBox62),
                new SudokuPiece(textBox63),
                new SudokuPiece(textBox64),
                new SudokuPiece(textBox65),
                new SudokuPiece(textBox66),
                new SudokuPiece(textBox67),
                new SudokuPiece(textBox68),
                new SudokuPiece(textBox69),
                new SudokuPiece(textBox70),
                new SudokuPiece(textBox71),
                new SudokuPiece(textBox72),
                new SudokuPiece(textBox73),
                new SudokuPiece(textBox74),
                new SudokuPiece(textBox75),
                new SudokuPiece(textBox76),
                new SudokuPiece(textBox77),
                new SudokuPiece(textBox78),
                new SudokuPiece(textBox79),
                new SudokuPiece(textBox80),
                new SudokuPiece(textBox81)
            });
            #endregion
        }