Example #1
0
        public void Start(BoardOptions boardOptions)
        {
            _gameFinished = false;
            _board        = _boardGenerator.GenerateBoard(boardOptions);
            _user.ReadUserName();

            var gameStart = DateTime.UtcNow;

            while (!_gameFinished)
            {
                try
                {
                    // Clear the screen
                    MineSweeperConsole.Clear();

                    // Print the board
                    _boardPrinter.PrintBoardWithCoords(_board);

                    // Print the available options
                    MineSweeperConsole.WriteLine(_menuOptions);

                    // Get the user input
                    var input = _user.ReadUserAction();
                    MineSweeperConsole.WriteLine(input);

                    // Parse the action
                    var action = _actionParser.ParseAction(input);

                    // Process the action
                    ProcessAction(action);
                }
                catch (MineFoundException)
                {
                    _gameFinished = true;

                    // Clear the screen
                    MineSweeperConsole.Clear();

                    // Print the board
                    _boardPrinter.PrintBoardWithCoords(_board);
                    MineSweeperConsole.WriteLine("YOU LOOSE");
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            var elapsedTime = DateTime.UtcNow - gameStart;
            var score       = Score.GenerateScore(TimeSpan.FromSeconds(elapsedTime.TotalSeconds), UserWon(), _user, _board);

            _scoreManager.AddRow(score);
            _scoreManager.PrintScore();
        }