static SudokuPuzzleTest()
        {
            try
            {
                if (File.Exists("vars.txt"))
                {
                    SudokuPuzzleTest.ParseFile("vars.txt");
                }

                else if (File.Exists("Variables.txt"))
                {
                    SudokuPuzzleTest.ParseFile("Variables.txt");
                }

                else
                {
                    throw new Exception();
                }
            }

            catch
            {
                SudokuPuzzleTest.Difficulty = SudokuPuzzleTest.DefaultDifficulty;
                SudokuPuzzleTest.Size       = SudokuPuzzleTest.DefaultSize;
            }

            Assert.AreNotEqual(SudokuPuzzleTest.Difficulty, SudokuDifficulty.None, "Difficulty must not be None");
            Assert.IsTrue(SudokuPuzzle.VerifySize(SudokuPuzzleTest.Size), "Size must be a positive, square integer");
        }
Beispiel #2
0
        private static bool ParseArguments(String[] args, out int command, out int size, out SudokuDifficulty difficulty, out String filename, out String outfile)
        {
            command    = size = 0;
            filename   = outfile = null;
            difficulty = SudokuDifficulty.None;

            try
            {
                switch (args[0].ToLower())
                {
                case "c":
                case "comp":
                case "compare":
                    command = Program.CompareCommand;
                    break;

                case "e":
                case "enum":
                case "enumerate":
                    command = Program.EnumerateCommand;
                    break;

                case "g":
                case "gen":
                case "generate":
                    command = Program.GenerateCommand;
                    break;

                case "i":
                case "int":
                case "inter":
                case "interactive":
                    command = Program.InteractiveCommand;
                    break;

                case "p":
                case "print":
                    command = Program.PrintCommand;
                    break;

                case "s":
                case "solve":
                    command = Program.SolveCommand;
                    break;

                default:
                    return(false);
                }

                for (int i = 1; i < args.Length; i++)
                {
                    if (String.IsNullOrWhiteSpace(args[i]))
                    {
                        continue;
                    }

                    String arg = Regex.Replace(args[i].ToLower(), @"\s+", "");

                    if (arg[0] == '-')
                    {
                        if (arg.Length > 2)
                        {
                            return(false);
                        }

                        //String param = Regex.Replace(args[i + 1].ToLower(), @"\s+", "");
                        String param = args[i + 1];

                        switch (arg[1])
                        {
                        case 'd':
                            if (command != Program.GenerateCommand || difficulty != SudokuDifficulty.None)
                            {
                                return(false);
                            }

                            try
                            {
                                difficulty = (SudokuDifficulty)Enum.Parse(typeof(SudokuDifficulty), param, true);
                            }

                            catch
                            {
                                difficulty = (SudokuDifficulty)Int32.Parse(param);
                            }

                            if (difficulty == SudokuDifficulty.None)
                            {
                                return(false);
                            }

                            break;

                        case 'f':
                            if (command == Program.GenerateCommand || !(filename is null) || String.IsNullOrWhiteSpace(param) || command == Program.InteractiveCommand && size != 0)
                            {
                                return(false);
                            }

                            filename = param;
                            break;

                        case 'o':
                            if (command == Program.PrintCommand || !(outfile is null) || String.IsNullOrWhiteSpace(param))
                            {
                                return(false);
                            }

                            outfile = param;
                            break;

                        case 's':
                            if (!(command == Program.GenerateCommand || command == Program.InteractiveCommand) || size != 0 || command == Program.InteractiveCommand && !(filename is null))
                            {
                                return(false);
                            }

                            size = Int32.Parse(param);

                            if (!SudokuPuzzle.VerifySize(size))
                            {
                                return(false);
                            }

                            break;

                        default:
                            return(false);
                        }

                        i++;
                    }

                    else
                    {
                        return(false);
                    }
                }
            }

            catch
            {
                return(false);
            }

            switch (command)
            {
            case Program.CompareCommand:
                if (String.IsNullOrWhiteSpace(filename) || String.IsNullOrWhiteSpace(outfile))
                {
                    return(false);
                }

                break;

            case Program.EnumerateCommand:
                if (filename is null)
                {
                    return(false);
                }

                break;

            case Program.GenerateCommand:
                if (!SudokuPuzzle.VerifySize(size) || difficulty == SudokuDifficulty.None)
                {
                    return(false);
                }

                break;

            case Program.InteractiveCommand:
                if (filename is null && !SudokuPuzzle.VerifySize(size) || !(filename is null || size == 0))
                {
                    return(false);
                }

                break;

            case Program.PrintCommand:
                if (String.IsNullOrWhiteSpace(filename))
                {
                    return(false);
                }

                break;

            case Program.SolveCommand:
                if (String.IsNullOrWhiteSpace(filename) || !(outfile is null) && String.IsNullOrWhiteSpace(outfile))
                {
                    return(false);
                }

                break;

            default:
                return(false);
            }

            return(true);
        }