Example #1
0
        public void Start()
        {
            string nextPlayer = "X";

            _view.WriteLine("Welcome to Tic-Tac-Toe Game!");
            _view.WriteLine("Player X will go first...");

            while (string.IsNullOrEmpty(_game.Winner))
            {
                _view.Write($"Player {nextPlayer} move: ");

                var playerEntry = _view.ReadLine();

                bool isValid = int.TryParse(playerEntry.ToString(), out int nextMove);

                if (isValid && _game.Play(nextMove))
                {
                    nextPlayer = nextPlayer == "X" ? "O" : "X";
                }
                else
                {
                    _view.WriteLine("Your move is not valid.");
                }

                DisplayBoard();
            }

            _view.WriteLine(Environment.NewLine);
            _view.WriteLine(_game.Winner == "-" ? "Cat's Game!" : $"Player {_game.Winner} Won!");
            _view.WriteLine(Environment.NewLine);
            _view.WriteLine("Press any key to exit..");
            _view.ReadKey();
        }
        public void Run()
        {
            bool finished = false;

            // Stretch 2: Process entries until Ctrl + C
            do
            {
                _consoleInterface.Write("Welcome to String Calculator ");
                _consoleInterface.Write("Enter Operation: Add, Multiply ");

                string command = _consoleInterface.ReadLine().ToLower();

                if (String.IsNullOrWhiteSpace(command))
                {
                    _consoleInterface.Write("Goodbye");
                    finished = true;
                }
                else if (command == "add")
                {
                    // read input string
                    _consoleInterface.Write("Enter numbers: ");
                    var input = _consoleInterface.ReadLine();

                    // execute and print
                    var addServiceOutput = _addService.Execute(input);
                    _consoleInterface.WriteLine(addServiceOutput);
                }
                else if (command == "multiply")
                {
                    // read input string
                    _consoleInterface.Write("Enter numbers: ");
                    var input = _consoleInterface.ReadLine();

                    // execute and print
                    var multiplyServiceOutput = _multiplyService.Execute(input);
                    _consoleInterface.WriteLine(multiplyServiceOutput);
                }
                else
                {
                    _consoleInterface.WriteLine("Invalid Input, please try again");
                }
            }while (!finished);
        }
Example #3
0
        //TODO: remove "required" validation from the GetPromptedData method
        private string GetPromptedData(string prompt)//, bool inputRequired = false)
        {
            consoleInterface.WriteLine(prompt);

            var input = consoleInterface.ReadLine();

            //if (inputRequired && string.IsNullOrWhiteSpace(input))
            //{
            //    throw new ArgumentException("Required argument must be entered");
            //}

            return(input);
        }
 public static string Prompt(this IConsoleInterface con, string message)
 {
     con.WriteLine(message);
     return(con.ReadLine());
 }