Beispiel #1
0
        //Form Methods
        private bool OpenSudokuFile()
        {
            //Read a .txt file containing the sudoku puzzle.

            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "Text |*.txt";
            DialogResult result = dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                FileStream   fs        = (FileStream)dialog.OpenFile();
                StreamReader reader    = new StreamReader(fs);
                string       boardSize = reader.ReadLine();
                int          size;
                switch (boardSize)
                {
                case "4x4":
                    size = 4;
                    break;

                case "6x6":
                    size = 6;
                    break;

                case "9x9":
                    size = 9;
                    break;

                default:
                    //Crazy size. We can't handle that.
                    size                  = 0;
                    StatusLabel.Text      = String.Format("Error, can't make a {0} board.", boardSize);
                    StatusLabel.ForeColor = Color.Red;
                    StatusLabel.Visible   = true;
                    return(false);
                }


                _board = new GameBoard(size);

                while (!reader.EndOfStream)
                {
                    //Read the 'locked' tiles from the file.

                    string   tileLine    = reader.ReadLine();
                    string[] tileDetails = tileLine.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    try {
                        _board.SetUpTile(Convert.ToInt32(tileDetails[0]), Convert.ToInt32(tileDetails[1]), Convert.ToInt32(tileDetails[2]), true);
                    } catch {
                        //The file was not correct.
                        StatusLabel.Text      = "Error: The File Could Not Be Read";
                        StatusLabel.Visible   = true;
                        StatusLabel.ForeColor = Color.Red;
                        reader.Close();
                        return(false);
                    }
                }

                reader.Close();
            }
            else
            {
                //User cancelled finding a file, bad user.
                return(false);
            }
            return(true);
        }