Exemple #1
0
        private void IsDocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
        {
            var browser  = (System.Windows.Forms.WebBrowser)sender;
            var document = browser.Document;

            var counter = 0;

            for (int i = 1; i <= 81; i++)
            {
                var element = this.FindName("TextBox" + counter) as TextBox;
                int number;
                var isParsed = int.TryParse(element.Text, out number);
                if (isParsed)
                {
                    document.GetElementById("textBox" + i).InnerText = element.Text;
                }
                counter++;
            }

            var sudokuAsString = string.Empty;

            var rnd = new Random();

            for (int start = 100; start < 600; start += 100)
            {
                try
                {
                    connection.Open();
                    var           queryString = string.Format(string.Format("SELECT TOP 1 SudokuValues,Difficulty FROM Sudoku where Difficulty = {0} ORDER BY NEWID()", lastSudokuDifficulty));
                    SqlCommand    query       = new SqlCommand(queryString, connection);
                    SqlDataReader reader      = query.ExecuteReader();
                    reader.Read();
                    sudokuAsString = reader[0] as string;
                }
                catch (Exception)
                {
                    if (lastSudokuDifficulty == 1)
                    {
                        sudokuAsString = PredefinedEasySudokus.GetPredefinedSudokuEasy(rnd.Next(0, MAX_PREDEFINED_SUDOKUS));
                    }
                    if (lastSudokuDifficulty == 2)
                    {
                        sudokuAsString = PredefinedMediumSudokus.GetPredefinedSudokuMedium(rnd.Next(0, MAX_PREDEFINED_SUDOKUS));
                    }
                    if (lastSudokuDifficulty == 4)
                    {
                        sudokuAsString = PredefinedHardSudokus.GetPredefinedSudokuHard(rnd.Next(0, MAX_PREDEFINED_SUDOKUS));
                    }
                    if (lastSudokuDifficulty == 8)
                    {
                        sudokuAsString = PredefinedVeryHardSudokus.GetPredefinedSudokuVeryHard(rnd.Next(0, 100));
                    }
                }
                finally
                {
                    connection.Close();
                }


                for (int i = 1; i <= 81; i++)
                {
                    if (sudokuAsString[i - 1] != '0')
                    {
                        document.GetElementById("textBox" + (i + start)).InnerText = sudokuAsString[i - 1].ToString();
                    }
                }
            }

            if (browser.ReadyState.Equals(System.Windows.Forms.WebBrowserReadyState.Complete))
            {
                browser.ShowPrintDialog();
            }
        }
Exemple #2
0
        private void GetSudoku(object sender, RoutedEventArgs e)
        {
            if (lastSudokuDifficulty == 0)
            {
                ShowDisabledItems();
            }

            var sudokuAsString = string.Empty;
            var difficulty     = 0;

            switch (((Button)sender).Name[6])
            {
            case 'E':
                difficulty           = 1;
                lastSudokuDifficulty = 1;
                break;

            case 'M':
                difficulty           = 2;
                lastSudokuDifficulty = 2;
                break;

            case 'H':
                difficulty           = 4;
                lastSudokuDifficulty = 4;
                break;

            case 'V':
                difficulty           = 8;
                lastSudokuDifficulty = 8;
                break;

            default:
                break;
            }

            try
            {
                connection.Open();
                var           queryString = string.Format(string.Format("SELECT TOP 1 SudokuValues,Difficulty FROM Sudoku where Difficulty = {0} ORDER BY NEWID()", difficulty));
                SqlCommand    query       = new SqlCommand(queryString, connection);
                SqlDataReader reader      = query.ExecuteReader();
                reader.Read();
                sudokuAsString = reader[0] as string;
            }
            catch (Exception)
            {
                MessageBox.Show("Could not connect to the server! An offline sudoku will be generated for you!", "Warning!");
                var rnd = new Random();
                if (difficulty == 1)
                {
                    sudokuAsString = PredefinedEasySudokus.GetPredefinedSudokuEasy(rnd.Next(0, MAX_PREDEFINED_SUDOKUS));
                }
                if (difficulty == 2)
                {
                    sudokuAsString = PredefinedMediumSudokus.GetPredefinedSudokuMedium(rnd.Next(0, MAX_PREDEFINED_SUDOKUS));
                }
                if (difficulty == 4)
                {
                    sudokuAsString = PredefinedHardSudokus.GetPredefinedSudokuHard(rnd.Next(0, MAX_PREDEFINED_SUDOKUS));
                }
                if (difficulty == 8)
                {
                    sudokuAsString = PredefinedVeryHardSudokus.GetPredefinedSudokuVeryHard(rnd.Next(0, 100));
                }
            }
            finally
            {
                connection.Close();
            }

            var counter = 0;

            for (int index = 0; index < 81; index++)
            {
                var textBox = this.FindName("TextBox" + counter) as TextBox;
                textBox.Text       = string.Empty;
                textBox.IsReadOnly = false;
                textBox.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
                textBox.IsEnabled  = true;
                if (sudokuAsString[index] != '0')
                {
                    textBox.IsReadOnly = true;
                    textBox.Background = new SolidColorBrush(Color.FromRgb(222, 227, 229));
                    textBox.Text       = sudokuAsString[index].ToString();
                }
                counter++;
            }

            StartDispatchTimer();

            stopWatch.Restart();
        }