Пример #1
0
        private static int Generate(int size, SudokuDifficulty difficulty, String outfile)
        {
            SudokuPuzzle sudoku = SudokuGenerator.Generate(size, difficulty);

            if (outfile is null)
            {
                Console.WriteLine(sudoku);
            }

            else
            {
                if (Program.WriteToFile(sudoku.ToString(), outfile))
                {
                    Console.WriteLine($"Generated sudoku written to {outfile}");
                }

                else
                {
                    Console.WriteLine(Program.FileOutputErrorMessage);
                    return(Program.FileOutputError);
                }
            }

            return(0);
        }
        public void ToStringTest()
        {
            string newLine = Environment.NewLine;

            var sudokuPuzzle        = new SudokuPuzzle(new int[9, 9]);
            var toString            = sudokuPuzzle.ToString();
            var emptyPuzzleToString = string.Concat(Enumerable.Repeat($"0 0 0 0 0 0 0 0 0{newLine}", 9));

            Assert.That(toString, Is.EqualTo(emptyPuzzleToString));

            sudokuPuzzle.Cells[1, 2].Value = 5;
            toString = sudokuPuzzle.ToString();
            var expectedStringRepresentation = emptyPuzzleToString.Remove(23, 1).Insert(23, "5");

            Assert.That(toString, Is.EqualTo(expectedStringRepresentation));
        }
Пример #3
0
        private void OnSaveGame(SudokuPuzzle sudokuPuzzle)
        {
            var saveFileDialog = new SaveFileDialog
            {
                Filter = DialogFilter
            };

            if (saveFileDialog.ShowDialog() == true)
            {
                File.WriteAllText(saveFileDialog.FileName, sudokuPuzzle.ToString());
            }
        }
Пример #4
0
        private void btnSaveFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string puzzleStr = puzzle.ToString().Replace(",", "");
                    using (StreamWriter sr = new StreamWriter(saveFileDialog1.FileName))
                    {
                        sr.Write(puzzleStr);
                        sr.Flush();
                        sr.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not write file to disk. Original error: " + ex.Message);
                }
            }
        }