예제 #1
0
        /// <summary>
        /// Visualises the <paramref name="game"/>.
        /// </summary>
        private void VisualiseGame(IFourInARowGame game, bool isDone = false)
        {
            PrintGameHeader(clearScreen: true);

            var winMethodLabel = "Win method";
            var winMethodValue = game.GetWinState().Method == WinMethod.None ? "The game is still on!" : game.GetWinState().Method.ToString();

            PrintTopInfoBox(winMethodLabel, winMethodValue, HeaderSize.Height, HeaderSize.Width - 6 - winMethodLabel.Length - winMethodValue.Length);

            var currentPlayerLabel = isDone ? "Winner" : "Current player";
            var currentPlayerValue = isDone
                ? game.GetWinState().Winner.HasValue
                    ? game.GetWinState().Winner.Value == BoardSlotValue.P1
                      ? game.PlayerOne.Name
                      : game.PlayerTwo.Name
                    : "No winner due to 'Draw Game'"
                : game.CurrentPlayer.Name;

            var playerColor = ConsoleColor.White;

            if (game.GetWinState().HasWinner)
            {
                playerColor = GetColorBasedOnSlotValue(game.GetWinState().Winner.Value);
            }
            if (game.GetWinState().Method == WinMethod.Draw)
            {
                playerColor = ConsoleColor.DarkMagenta;
            }
            if (game.GetWinState().Method == WinMethod.None)
            {
                playerColor = GetColorBasedOnSlotValue(game.CurrentPlayer.Type);
            }

            PrintTopInfoBox(currentPlayerLabel, currentPlayerValue, HeaderSize.Height, 0, HeaderPaddingLeft + 2, playerColor);

            if (!isDone)
            {
                PrintAskPlayerForColumnIndex();
            }
            else
            {
                Console.WriteLine();
            }

            Console.WriteLine();
            Console.WriteLine();
            PrintBoard(game.Board, HeaderPaddingLeft + 2, isDone);
        }
예제 #2
0
        /// <summary>
        /// Starts the game.
        /// </summary>
        private void Run(string[] args)
        {
            var boardSize = new BoardSize(7, 6);

            if (args.Length == 2)
            {
                boardSize = new BoardSize(Int32.Parse(args[0]), Int32.Parse(args[1]));
            }

            Console.BackgroundColor = ConsoleColor.White;

            try
            {
                Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
                ShowWindow(ThisConsole, MAXIMIZE);
            }
            catch (Exception ex)
            {
            }

            Console.BackgroundColor = ConsoleColor.Black;

            Game = new FourInARowGame(AskPlayerName("one"), AskPlayerName("two"), boardSize);

            try
            {
                while (Game.GetWinState().Method == WinMethod.None)
                {
                    VisualiseGame(Game, isDone: false);
                    Game.PlayValueInColumn(AskPlayerForColumnIndex());
                }

                VisualiseGame(Game, isDone: true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fatal error occured: " + ex);
            }

            Console.ReadLine();
        }