Exemple #1
0
 private static bool CheckExecutionTime(int palette, int size)
 {
     if (Math.Pow(palette, size) > 1_000_000)
     {
         return(Shell.PromptBool("It can take some time to solve. Continue?", true));
     }
     return(true);
 }
Exemple #2
0
        private static void Main()
        {
            Shell.Initialize(new ShellOptions
            {
                DefaultForegroundColor = ConsoleColor.DarkGray,
                DefaultBackgroundColor = ConsoleColor.Gray,
                PromptColor            = ConsoleColor.Black,
                PromptQuestionColor    = ConsoleColor.Red,
                PromptOptionColor      = ConsoleColor.Magenta,
            });

            Board lastBoard = null;

            do
            {
                Shell.Clear();
                try
                {
                    var options = CreateGameOptions(lastBoard?.Options);
                    if (options == null)
                    {
                        return;
                    }

                    var codemaker   = CreateCodemaker(options, lastBoard?.Codemaker);
                    var codebreaker = CreateCodebreaker(options, lastBoard?.Codebreaker);
                    var board       = new Board(options, codemaker, codebreaker);

                    board.Run();

                    lastBoard = board;
                }
                catch (Exception e)
                {
                    using (ShellColorizer.Set(ConsoleColor.White, ConsoleColor.Red))
                    {
                        Shell.WriteLine($"Error: {e.Message}");
                    }
                    return;
                }
            } while (Shell.PromptBool("Play again?"));
        }
Exemple #3
0
        private static GameOptions CreateGameOptions(GameOptions lastOptions)
        {
            var options = new GameOptions();

            options.Palette = new Palette(Shell.PromptInt("Number of peg color", lastOptions?.Palette.PegCount ?? 6, (1, 10)));
            ShellEx.WriteKeyValue("Peg colors", options.Palette.PegCount);

            options.AllowDuplicates = Shell.PromptBool("Allow duplicates", lastOptions?.AllowDuplicates ?? true);
            ShellEx.WriteKeyValue("Duplicates", options.AllowDuplicates ? "yes" : "no");

            var maxSize = options.AllowDuplicates ? 10 : options.Palette.PegCount;

            options.Size = Shell.PromptInt("Length of secret code", Math.Min(maxSize, lastOptions?.Size ?? 4), (1, maxSize));
            ShellEx.WriteKeyValue("Code length", options.Size);

            if (!CheckExecutionTime(options.Palette.PegCount, options.Size))
            {
                return(null);
            }
            return(options);
        }