예제 #1
0
        /// <summary>
        /// Parse the board starting state from a text file and init the board object
        /// </summary>
        /// <param name="FilePath">The file containing the puzzle</param>
        public Boolean Load(string FilePath)
        {
            //ensure file exists
            try
            {
                if (File.Exists(FilePath))
                {
                    //open file
                    using (StreamReader sr = File.OpenText(FilePath))
                    {
                        string text = "";
                        int    r = 1, c = 1;

                        //while the first 9 rows have data
                        while ((text = sr.ReadLine()) != null && r < 10)
                        {
                            //process the first 9 chars
                            while (c < 10)
                            {
                                foreach (char value in text)
                                {
                                    //for non 0 values
                                    if (value != 'X')
                                    {
                                        //get cell and set value
                                        Cell cell = Cells.Single(x => x.Row == r && x.Col == c);
                                        cell.SetValueFromChar(value);

                                        //Remove value possibility from other cells
                                        PropagateValue(cell);
                                    }

                                    c++;
                                }
                            }

                            c = 1; //reset column number and go to next row
                            r++;
                        }
                    }
                }
                else
                {
                    Console.Write("The input file " + FilePath + " was not found.");
                    return(false);
                }

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("An error has occurred loading the board: " + e.InnerException + ". Please try a different puzzle.");
                return(false);
            }
        }