示例#1
0
        public Engine(IRender render, IStatisticFactory statisticFactory, IStatisticStorage statisticStorage)
        {
            if (render == null)
            {
                throw new ArgumentNullException("render");
            }

            if (statisticFactory == null)
            {
                throw new ArgumentNullException("statisticFactory");
            }

            if (statisticStorage == null)
            {
                throw new ArgumentNullException("statisticStorage");
            }

            this.Render = render;
            this.StatisticFactory = statisticFactory;
            this.StatisticStorage = statisticStorage;

            this.State = new StartState(this);
            this.CommandFactory = new CommandFactory(this);
            this.Statistic = StatisticFactory.CreateStatistic();
        }
        public Command SetCommand(CommandFactory commandFactory)
        {
            Console.WriteLine();
            Console.Write("Enter row and column: ");
            string input = Console.ReadLine().Trim();

            switch (input)
            {
                case "top":
                    return commandFactory.CreateShowStatistiCommand();
                case "exit":
                    return commandFactory.CreateExitCommand();
                case "restart":
                    return commandFactory.CreateRestartCommand();
            }

            if (input.Length != 3)
            {
                this.ShowInvalidMoveMessage();

                return this.SetCommand(commandFactory);
            }

            if (input[1] != ' ')
            {
                this.ShowInvalidMoveMessage();

                return this.SetCommand(commandFactory);
            }

            bool isSuccessParse;
            int rowInput;
            isSuccessParse = int.TryParse(input[0].ToString(), out rowInput);

            if (!isSuccessParse)
            {
                this.ShowInvalidMoveMessage();
                return this.SetCommand(commandFactory);
            }

            int columnInput;
            isSuccessParse =int.TryParse(input[2].ToString(), out columnInput);

            if (!isSuccessParse)
            {
                this.ShowInvalidMoveMessage();
                return this.SetCommand(commandFactory);
            }

            return commandFactory.CreateMoveCommand(rowInput, columnInput);
        }