Example #1
0
        private static void ProceedCommand(string command)
        {
            var randomNumberGenerator = new RandomNumberGenerator();
            var reader   = new ConsoleReader();
            var renderer = new ConsoleRenderer();
            var factory  = new PlayfieldFactory();

            switch (command)
            {
            case "New game":
                Playfield playfield;
                Console.Clear();
                Console.WriteLine(ChooseSizeMessage);

                string size = Console.ReadLine();

                playfield = factory.CreatePlayfield(size);
                playfield.FillPlayfield(randomNumberGenerator);

                if (playfield == null)
                {
                    Console.Clear();
                    Console.WriteLine(ChooseSizeMessage);
                    ProceedCommand("New game");
                }
                else
                {
                    var game = new GameEngine(reader, renderer, playfield);
                    game.Run();
                }

                break;

            case "Load game":
                try
                {
                    var fileSerializer = new FileSerializer();
                    var memento        = fileSerializer.DeserializeObject(GameLocation);
                    playfield = factory.CreatePlayfield(ConvertNumberToString(memento.Grid.GetLength(0)));
                    playfield.RestoreMemento(memento);

                    File.Delete(GameLocation);

                    var game = new GameEngine(reader, renderer, playfield);
                    game.Run();
                }
                catch (Exception ex)
                {
                    Console.Clear();
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(WelcomeMessage);
                    ProceedCommand(Console.ReadLine());
                }

                break;

            case "Exit":
                Environment.Exit(0);
                break;

            default:
                Console.Clear();
                Console.WriteLine(WelcomeMessage);
                ProceedCommand(Console.ReadLine());
                break;
            }
        }
        private static void ProceedCommand(string command)
        {
            var randomNumberGenerator = new RandomNumberGenerator();
            var reader = new ConsoleReader();
            var renderer = new ConsoleRenderer();
            var factory = new PlayfieldFactory();

            switch (command)
            {
                case "New game":
                    Playfield playfield;
                    Console.Clear();
                    Console.WriteLine(ChooseSizeMessage);

                    string size = Console.ReadLine();

                    playfield = factory.CreatePlayfield(size);
                    playfield.FillPlayfield(randomNumberGenerator);

                    if (playfield == null)
                    {
                        Console.Clear();
                        Console.WriteLine(ChooseSizeMessage);
                        ProceedCommand("New game");
                    }
                    else
                    {
                        var game = new GameEngine(reader, renderer, playfield);
                        game.Run();
                    }

                    break;
                case "Load game":
                    try
                    {
                        var fileSerializer = new FileSerializer();
                        var memento = fileSerializer.DeserializeObject(GameLocation);
                        playfield = factory.CreatePlayfield(ConvertNumberToString(memento.Grid.GetLength(0)));
                        playfield.RestoreMemento(memento);

                        File.Delete(GameLocation);

                        var game = new GameEngine(reader, renderer, playfield);
                        game.Run();
                    }
                    catch (Exception ex)
                    {
                        Console.Clear();
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(WelcomeMessage);
                        ProceedCommand(Console.ReadLine());
                    }

                    break;
                case "Exit":
                    Environment.Exit(0);
                    break;
                default:
                    Console.Clear();
                    Console.WriteLine(WelcomeMessage);
                    ProceedCommand(Console.ReadLine());
                    break;
            }
        }