static void Main(string[] args) { try { SudokuMapper sudokuMapper = new SudokuMapper(); SudokuBoard sudokuBoard = new SudokuBoard(); SudokuEngine sudokuEngine = new SudokuEngine(sudokuBoard, sudokuMapper); SudokuReader sudokuReader = new SudokuReader(); Console.WriteLine("Please enter the Sudoku file name:"); var filename = Console.ReadLine(); var sudoku = sudokuReader.ReadFile(filename); sudokuBoard.Show(sudoku); bool isSudoKuSolved = sudokuEngine.Solve(sudoku); sudokuBoard.Show(sudoku); Console.WriteLine(isSudoKuSolved ? "Sudoku is solved." : "Unfortunately, current algorithm(s) were not enough to solve the Sudoku."); } catch (Exception e) { Console.WriteLine("{0}:{1}", "Something go wrong! ", e.Message); } }
static void Main(string[] args) { #if PROGRESS_TRACE Trace.Listeners.Add(new ConsoleTraceListener()); #endif for (int i = 0; i < 10; i++) { //runSimpleSolver(); //SudokuEngine.Run(); SudokuEngine.RunSudoku1(); // SudokuEngine.RunNotFunSudoku(); SudokuEngine.RunMisaSudoku(); //solveMoreFormulas(); //solveHard250Sample().Wait(); } Console.ReadLine(); }
static void Main(string[] args) { SudokuBoardStateManager sudokuBoardStateManager = new SudokuBoardStateManager(); SudokuMapper sudokuMapper = new SudokuMapper(); Displayer displayer = new Displayer(); SudokuEngine sudokuEngine = new SudokuEngine(sudokuBoardStateManager, sudokuMapper); SudokuFileReader sudokuFileReader = new SudokuFileReader(); string fileName = "SudokuEasy.txt"; var sudokuBoard = sudokuFileReader.FileReader(fileName); displayer.Display("initial", sudokuBoard); sudokuEngine.SolveSudoku(sudokuBoard); displayer.Display("final", sudokuBoard); Console.ReadKey(); }
public void NewGameTest() { int[,] testBoard = new int[9, 9]; int[] testBoard1 = new int[9]; bool flag = true; SudokuEngine.NewGame(testBoard, testBoard1); foreach (var i in testBoard) { if (i != 0) { flag = false; break; } else { flag = true; } } Assert.IsFalse(flag); }
private static void verifyBoard( SudokuEngine.Board board ) { // Check rows and columns for duplicates for ( int idx1 = 0; idx1 < board.Rows; ++idx1 ) { bool[] rowSymbols = new bool[ board.Rows ]; bool[] colSymbols = new bool[ board.Columns ]; for ( int idx2 = 0; idx2 < board.Columns; ++idx2 ) { // Check row for duplicates int symbol = board[ idx1, idx2 ].Entry; if ( rowSymbols[ symbol - 1 ] ) { Debug.WriteLine( "FAILURE: symbol " + symbol + " occurs multiple times on row " + idx1 + " at " ); Debug.WriteLine( board ); Debug.Assert( false ); } else { rowSymbols[ symbol - 1 ] = true; } // Check column for duplicates symbol = board[ idx2, idx1 ].Entry; if ( colSymbols[ symbol - 1 ] ) { Debug.WriteLine( "FAILURE: symbol " + symbol + " occurs multiple times on column " + idx1 ); Debug.WriteLine( board ); Debug.Assert( false ); } else { colSymbols[ symbol - 1 ] = true; } } } // Check blocks for duplicates for ( int idx1 = 0; idx1 < ( board.Rows / board.SubRows ); ++idx1 ) { for ( int idx2 = 0; idx2 < ( board.Columns / board.SubColumns ); ++idx2 ) { bool[] symbols = new bool[ board.Rows ]; for ( int row = ( idx1 * board.SubRows ); row < ( idx1 * board.SubRows ) + board.SubRows; ++row ) { for ( int col = ( idx2 * board.SubColumns ); col < ( idx2 * board.SubColumns ) + board.SubColumns; ++col ) { int symbol = board[ row, col ].Entry; if ( symbols[ symbol - 1 ] ) { Debug.WriteLine( "FAILURE: symbol " + symbol + " occurs multiple times in block[ " + idx1 + ", " + idx2 + " ]" ); Debug.WriteLine( board ); Debug.Assert( false ); } else { symbols[ symbol - 1 ] = true; } } } } } }
private void Button_Click(object sender, RoutedEventArgs e) { //Ukrywa label z informacja końca gry EndGameLabel.Visibility = Visibility.Hidden; //Zeruje zegar tick = 0; //Startuje zegar timer.Start(); isGenerated = false; //Kopiuje wszystkie dzieci glownego Grida SudokuGrid.Children.CopyTo(UIs, 0); //Uruchamia losowanie SudokuEngine.NewGame(SudokuBoard, Array); //Przypisuje wszystkie bordery do tablicy borderów for (int i = 0; i < borders.Length; i++) { borders[i] = UIs[i] as Border; } int z = 0; //szuka z wszystkich borderów textboxy for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (z > 81) { break; } if (borders[z] != null) { texts[i, j] = borders[z].Child as TextBox; } z++; } } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (texts[i, j] != null) { texts[i, j].Text = SudokuBoard[i, j].ToString(); } if (texts[i, j].Text == "0") { texts[i, j].Text = ""; } } } //Zdarzenie ktore ma sie wykonac po zmianie w TextBox foreach (var i in texts) { if (i != null) { i.TextChanged += CheckAfter; } } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { texts[i, j].Name = String.Format("_{0}_{1}", i, j); } } isGenerated = true; }
/// <summary> /// Metoda ktora sie wykonuje gdy zajdzie zdarzenie /// </summary> /// <param name="o"></param> /// <param name="e"></param> public void CheckAfter(object o, RoutedEventArgs e) { if (isGenerated) { flag = false; //pobiera informacje w ktorym textbox zostala zmieniona liczba l = (TextBox)e.OriginalSource; //pobiera nazwe textboxa gdzie byla zmieniona liczba string[] NameArray = new string[2]; NameArray = l.Name.Split('_'); if (texts[Convert.ToInt32(NameArray[1]), Convert.ToInt32(NameArray[2])] != null) { texts[Convert.ToInt32(NameArray[1]), Convert.ToInt32(NameArray[2])].Text = l.Text; } //sprawdza czy jest poprawny format if (l.Text != "1" && l.Text != "2" && l.Text != "3" && l.Text != "4" && l.Text != "5" && l.Text != "6" && l.Text != "7" && l.Text != "8" && l.Text != "9" && l.Text != "") { ErrorWindow.Visibility = Visibility.Visible; l.Text = ""; } else { //zmienia kolor tła na biały texts[Convert.ToInt32(NameArray[1]), Convert.ToInt32(NameArray[2])].Background = Brushes.White; // Sprawdza Maly kwadrat if (!flag) { SudokuEngine.CheckSmallSquare(NameArray, texts, Array); } flag = SudokuEngine.CheckAfterChangeNumbers(Array); //Sprawdza w pionie if (!flag) { SudokuEngine.CheckVertical(NameArray, texts, Array); } flag = SudokuEngine.CheckAfterChangeNumbers(Array); //Sprawdza w poziomie if (!flag) { SudokuEngine.CheckHorizontal(NameArray, texts, Array); } flag = SudokuEngine.CheckAfterChangeNumbers(Array); //Zmienia kolor tła jeżeli liczba się powtarza if (flag) { texts[Convert.ToInt32(NameArray[1]), Convert.ToInt32(NameArray[2])].Background = Brushes.Red; } //Przypisuje do Boarda wszystkie textBoxy for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (texts[i, j].Text != "") { SudokuBoard[i, j] = Convert.ToInt32(texts[i, j].Text); } else { SudokuBoard[i, j] = 0; } } } //Sprawdza czy koniec gry poprzez sprawdzenie czy żadna liczba sie nie powtarza , //w swoim obrębie i czy nie ma pusych pól for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (texts[i, j].Background == Brushes.Red || texts[i, j].Text == "") { endGame = true; break; } else { endGame = false; } } } //Kończy Gre wyswietlajac YouWin i zatrzymuje czas if (!endGame) { EndGameLabel.Visibility = Visibility.Visible; timer.Stop(); } } } }