예제 #1
0
        public void RenderToOutput()
        {
            var gameLines = new List <List <Glyph> >();

            var gameTitle = $"{_Output.ConvertGameSquareToGlyphValue(Enums.SquareStatus.Mine)} Welcome to {_GameTitle} {_Output.ConvertGameSquareToGlyphValue(Enums.SquareStatus.Mine)}";
            var padding   = (_Output.WindowWidth - gameTitle.Length) / 2;

            //insert the main heading
            gameLines.Insert(0, new List <Glyph>());
            gameLines.Insert(1, Utils.StringToLine(new StringBuilder().Append(' ', padding).Append(gameTitle).ToString(), ConsoleColor.Red));
            gameLines.Insert(2, Utils.StringToLine(new StringBuilder().Append(' ', padding - 1).Append('═', gameTitle.Length + 2).ToString(), ConsoleColor.Red));
            gameLines.Insert(3, new List <Glyph>());

            //generate the board
            var boardLines = _Board.Render(State == Enums.GameState.Loser || State == Enums.GameState.Winner);

            //generate the status/info lines to sit beside the board
            var info = new List <string>()
            {
                "Navigate from West to East using the arrow keys",
                "Watch out for the mines!",
                "",
                State == Enums.GameState.DifficultySelect ? "Select difficulty: [E]asy / [M]edium / [H]ard"
                    : State == Enums.GameState.StartPositionSelect ? "Enter start row number and press ENTER:"
                    : State == Enums.GameState.Loser || State == Enums.GameState.Winner ? "Press R to reset, ESC to quit"
                    : "",
                "",
                State == Enums.GameState.StartPositionSelect ? _DesiredStartPosition : "",
                State == Enums.GameState.Loser ? "GAME OVER"
                    : State == Enums.GameState.Winner ? $"CONGRATULATIONS, YOU MADE IT!"
                    : State == Enums.GameState.FreeMovement ? $"Position: {Utils.GetXLegendValue(_Player.PositionX)}{_Player.PositionY + 1}" : "",
                State != Enums.GameState.Loser && State != Enums.GameState.Winner ? $"Lives remaining: {LivesRemaining}" : "",
                State != Enums.GameState.DifficultySelect ? $"Moves made: {MoveCounter}" : ""
            };

            //board should be positioned to the right of the screen so the lhs has space for status and instructions
            var boardWidth = boardLines.First().Count;

            padding = (_Output.WindowWidth - boardWidth) - 10; //10 cols of safety margin

            //create the default padding used for board lines without any info/status text
            var defaultPadder = new StringBuilder().Append(' ', padding).ToString();
            var infoStartLine = (boardLines.Count - info.Count) / 2; //positions the info/status block roughly in the middle of the board height

            //merge our status/info lines with the board lines
            for (int i = 0; i < boardLines.Count - 1; i++)
            {
                var linePrepend = defaultPadder;

                //check we're in an appropriate place in the board lines array and build the status/info padded string
                if (i >= infoStartLine && i - infoStartLine < info.Count)
                {
                    linePrepend = BuildBoardLinePrependString(info[i - infoStartLine], padding);
                }

                //insert the status/info line into the list of chars for this board line
                boardLines[i].InsertRange(0, Utils.StringToLine(linePrepend));
            }

            //add the board into the overall output array
            gameLines.AddRange(boardLines);


            //clear the display and print out each line of the game
            _Output.Clear();

            foreach (var line in gameLines)
            {
                foreach (var chr in line)
                {
                    _Output.RenderSingle(chr);
                }

                _Output.Flush();
            }

            _Output.SetGameDefaultColors();
        }
예제 #2
0
        /// <summary>
        /// Represents the main game loop
        /// </summary>
        public void Run()
        {
            //init the game engine
            _GameEngine.Init(TITLE, DIFFICULTYFACTOR, STARTLIVES);

            //listen for some user input
            var keyPress = _Output.ReadInput;

            //escape always quits no matter the game state
            while (keyPress != ConsoleKey.Escape)
            {
                switch (keyPress)
                {
                //difficulty selection
                case ConsoleKey.E:
                    _GameEngine.SetDifficulty(Enums.Difficulty.Easy);
                    break;

                case ConsoleKey.M:
                    _GameEngine.SetDifficulty(Enums.Difficulty.Medium);
                    break;

                case ConsoleKey.H:
                    _GameEngine.SetDifficulty(Enums.Difficulty.Hard);
                    break;

                //start position
                case ConsoleKey.D0:
                case ConsoleKey.D1:
                case ConsoleKey.D2:
                case ConsoleKey.D3:
                case ConsoleKey.D4:
                case ConsoleKey.D5:
                case ConsoleKey.D6:
                case ConsoleKey.D7:
                case ConsoleKey.D8:
                case ConsoleKey.D9:
                    _GameEngine.SetDesiredStartPosition((int)keyPress - (int)ConsoleKey.D0);
                    break;

                case ConsoleKey.Enter:
                    _GameEngine.ConfirmDesiredStartPosition();
                    break;

                case ConsoleKey.Backspace:
                    _GameEngine.SetDesiredStartPosition(null);
                    break;

                //player movement
                case ConsoleKey.UpArrow:
                    _GameEngine.TryMove(Enums.Direction.North);
                    break;

                case ConsoleKey.RightArrow:
                    _GameEngine.TryMove(Enums.Direction.East);
                    break;

                case ConsoleKey.DownArrow:
                    _GameEngine.TryMove(Enums.Direction.South);
                    break;

                case ConsoleKey.LeftArrow:
                    _GameEngine.TryMove(Enums.Direction.West);
                    break;

                //misc
                case ConsoleKey.R:
                    _GameEngine.Reset();
                    break;

                default:
                    //_Output.RenderGroupWithFlush("Press ESC to quit or R to reset");
                    break;
                }

                keyPress = _Output.ReadInput;
            }

            _Output.ResetColors();
            _Output.ShowCursor = true;
            _Output.Clear();
        }