public UserInputData ParseUserInputData(string[] args)
        {
            var        compressDecompress = args.Length == 0 ? _inOut.ReadLine(MessageConstants.EnterCompressionMethodMessage) : args[0];
            UserAction action             = compressDecompress switch
            {
                "compress" => UserAction.Compress,
                "decompress" => UserAction.Decompress,
                _ => throw new InvalidOperationException($"Unknown command '{compressDecompress}' entered.")
            };

            var sourcePath = args.Length < 2 ? _inOut.ReadLine(MessageConstants.EnterSourceMessage) : args[1];
            var targetPath = args.Length < 3 ? _inOut.ReadLine(MessageConstants.EnterTargetMessage) : args[2];

            return(new UserInputData(action, sourcePath, targetPath));
        }
    }
Ejemplo n.º 2
0
        public ushort GetNumericInput(string message, ushort min, ushort max)
        {
            while (true)
            {
                _inputOutput.Write(message);

                ushort userInput;

                ushort.TryParse(_inputOutput.ReadLine(), out userInput);

                bool validUserInput = userInput >= min && userInput <= max;

                if (validUserInput)
                {
                    return(userInput);
                }
            }
        }
        public static void RunFactorial(IInputOutput io, IMathOperations math)
        {
            io.WriteLine("Michael Wright");
              io.WriteLine("CS 2450\n");

              io.Write("Enter a number to get a factorial: ");
              int factorialNumber = 0;
              bool parseSucceeded = int.TryParse(io.ReadLine(), out factorialNumber);
              if (!parseSucceeded)
              {
            return;
              }

              int sum = math.Factorial(factorialNumber);

              io.WriteLine("Factorial Value is {0}", sum);
              io.ReadLine();
        }
Ejemplo n.º 4
0
        private Either <IValidResult, IInvalidResult> Run()
        {
            const char DELIMITER = ',';

            _io.WriteLine(Settings.Default.ConfigurationQuestion);
            var stallConfig = _io.ReadLine();

            if (string.IsNullOrWhiteSpace(stallConfig))
            {
                return(new EmptyInputResult());
            }

            var tokens = stallConfig.Trim(DELIMITER).Split(DELIMITER);
            var stalls = Array.ConvertAll(tokens, int.Parse);

            if (stalls.Any(s => !VALID_STALL_ENTRIES.Contains(s)))
            {
                return(new InvalidInputResult());
            }

            return(new ValidInputResult(stalls));
        }
Ejemplo n.º 5
0
        public void Run(CancellationToken cancellation)
        {
            string[] inputs;
            var      gridSizeInput = _consoleInputOutput.ReadLine();

            inputs = gridSizeInput.Split(' ');
            int width  = int.Parse(inputs[0]); // size of the grid
            int height = int.Parse(inputs[1]); // top left corner is (x=0, y=0)

            var grid = new GameGrid();

            grid.StoreGrid(ReadGrid(height));

            var strategy = _actionStrategyFactory(grid);

            // game loop
            var loop = new GameLoop(_consoleInputOutput,
                                    cancellation,
                                    strategy,
                                    grid);

            loop.Run();
        }
Ejemplo n.º 6
0
        public IApplicationState PlayAgain()
        {
            _io.WriteLine(Settings.Default.PlayAgainQuestion);
            var result = PlayAgain(_io.ReadLine());

            _io.Clear();

            if (result)
            {
                return(new ApplicationStatePlay(_dockingSystem, _io, _printOptions));
            }

            return(new ApplicationStateStop());
        }
Ejemplo n.º 7
0
        private string Guess()
        {
            _tries = _tries + 1;

            _inout.Write("Take a guess: ");
            var guess = _inout.ReadLine();

            if (guess.Length == 4)
            {
                return(guess.ToUpper());
            }

            // Password guess was wrong size - Error Message
            _inout.WriteLine("Password length is 4.");
            return(Guess());
        }
Ejemplo n.º 8
0
        public void Run()
        {
            while (true)
            {
                if (_cancellation.IsCancellationRequested)
                {
                    break;
                }

                try
                {
                    string[]      inputs          = _inputOutput.ReadLine().Split(' ');
                    int           myScore         = int.Parse(inputs[0]);
                    int           opponentScore   = int.Parse(inputs[1]);
                    int           visiblePacCount = int.Parse(_inputOutput.ReadLine()); // all your pacs and enemy pacs in sight
                    List <PacKey> seenKeys        = new List <PacKey>();
                    for (int i = 0; i < visiblePacCount; i++)
                    {
                        var line = _inputOutput.ReadLine();
                        Console.Error.WriteLine($"Pac line {line}");
                        inputs = line.Split(' ');
                        int    pacId           = int.Parse(inputs[0]);   // pac number (unique within a team)
                        bool   mine            = inputs[1] != "0";       // true if this pac is yours
                        short  x               = short.Parse(inputs[2]); // position in the grid
                        short  y               = short.Parse(inputs[3]); // position in the grid
                        string typeId          = inputs[4];              // unused in wood leagues
                        short  speedTurnsLeft  = short.Parse(inputs[5]); // unused in wood leagues
                        short  abilityCooldown = short.Parse(inputs[6]); // unused in wood leagues
                        var    location        = new Location(x, y);

                        Pac pac;

                        var key = new PacKey(pacId, mine);
                        seenKeys.Add(key);
                        if (!_pacs.ContainsKey(key))
                        {
                            _pacs.Add(key, new Pac(pacId, mine, _actionStrategy, new GiveWayMovementStrategy(_gameGrid)));
                        }

                        pac = _pacs[key];

                        pac.AddLocation(location);
                        pac.AbilityCooldown = abilityCooldown;
                        pac.SpeedTurnsLeft  = speedTurnsLeft;
                        pac.Type            = typeId;

                        //_gameGrid.VisiblePelletsFrom(pac.Location);
                    }

                    var deletion = _pacs.Select(p => p.Key).Where(p => !seenKeys.Contains(p));
                    foreach (var d in deletion)
                    {
                        _pacs.Remove(d);
                    }

                    int visiblePelletCount = int.Parse(_inputOutput.ReadLine()); // all pellets in sight
                    _gameGrid.SetPellets(ParsePellets(visiblePelletCount));
                    _gameGrid.SetEnemies(_pacs.Values.Where(p => !p.Mine));
                    _gameGrid.SetMyPacs(_pacs.Values.Where(p => p.Mine && p.Type != PacType.Dead));
                    //Console.Error.WriteLine(_gameGrid.ToString());

                    // Write an action using Console.WriteLine()
                    // To debug: Console.Error.WriteLine("Debug messages...");

                    //_inputOutput.WriteLine("MOVE 0 15 10"); // MOVE <pacId> <x> <y>

                    var myPacs = _pacs.Values.Where(p => p.Mine);

                    var nextActions = myPacs.Select(pac => pac.NextAction(_gameGrid, _cancellation)).ToDictionary(p => p.Pac.Key);

                    var collisions =
                        nextActions.Values.Where(n => n is MoveAction).GroupBy(g => ((MoveAction)g).Location).Where(g => g.Count() > 1);
                    foreach (var collision in collisions)
                    {
                        var giveWayer = collision.First().Pac;
                        nextActions[giveWayer.Key] = giveWayer.GiveWay(_cancellation);
                    }


                    var moves = string.Join("|", nextActions.Values);
                    _inputOutput.WriteLine(moves);
                }
                catch (OperationCanceledException)
                {
                    break;
                }
            }
        }