コード例 #1
0
 private bool ProcessTurnsAndAskToPlayAgain()
 {
     while (true)
     {
         NextPlayer();
         _board.Display();
         if (_currentPlayer.AI != null) //it's an AI player
         {
             _board.AddMark(_currentPlayer.Mark, _currentPlayer.AI.TakeTurn(_board.BoardArray));
             Console.Clear();
             _board.Display();
             Console.ForegroundColor = _currentPlayer.TextColor;
             Console.Write("              " + _currentPlayer.Name + " has spoken!");
             Console.ResetColor();
             Thread.Sleep(1000);
         }
         else
         {
             PromptUser();
         }
         //check for victory or tie and ask to play again
         if (_board.IsVictory(_currentPlayer))
         {
             _board.Display();
             Console.ForegroundColor = _currentPlayer.TextColor;
             TextEffects.CenterAlignWriteLine(_currentPlayer.Name + " wins!");
             Console.ResetColor();
             return(AskToPlayAgain());
         }
         else if (_board.IsCatGame())
         {
             _board.Display();
             Console.ForegroundColor = TextEffects.NeutralColor;
             Console.WriteLine(TextEffects.offsetString + "Cats game!");
             Console.ResetColor();
             return(AskToPlayAgain());
         }
     }
 }
コード例 #2
0
        private Player PromptToCreatePlayer(string mark = null)
        {
            TextEffects.DisplayLogo();
            int    currentCursorTop = Console.CursorTop;
            string userInput        = null;

            if (mark == null)
            {
                bool validInput = false;
                //prompt for their choice of mark
                while (!validInput)
                {
                    Console.WriteLine("                   X goes first.                   ");
                    Console.WriteLine();
                    Console.Write("          Would you like to be ");
                    Console.ForegroundColor = TextEffects.XMarkColor;
                    Console.Write("Xs ");
                    Console.ResetColor();
                    Console.Write("or ");
                    Console.ForegroundColor = TextEffects.OMarkColor;
                    Console.Write("Os");
                    Console.ResetColor();
                    Console.Write("? ");

                    Console.ForegroundColor = TextEffects.NeutralColor;
                    userInput = Console.ReadLine().Trim().ToUpper();
                    Console.ResetColor();
                    switch (userInput)
                    {
                    case "X":
                    case "XS":
                        mark = "X";
                        Console.SetCursorPosition(0, currentCursorTop);
                        Console.Write("                                                   ");
                        Console.Write("                                                   ");
                        Console.Write("                                                   ");
                        Console.Write("                                                   ");
                        Console.SetCursorPosition(0, currentCursorTop);
                        validInput = true;
                        break;

                    case "O":
                    case "OS":
                        mark = "O";
                        Console.SetCursorPosition(0, currentCursorTop);
                        Console.Write("                                                   ");
                        Console.Write("                                                   ");
                        Console.Write("                                                   ");
                        Console.Write("                                                   ");
                        Console.SetCursorPosition(0, currentCursorTop);
                        validInput = true;
                        break;

                    default:
                        Console.SetCursorPosition(0, currentCursorTop);
                        Console.WriteLine();
                        Console.WriteLine("                                                   ");
                        Console.WriteLine("                                                   ");
                        Console.WriteLine("                                                   ");
                        Console.SetCursorPosition(0, currentCursorTop);
                        Console.ForegroundColor = TextEffects.ErrorColor;
                        Console.WriteLine("                  Enter X or O...                  ");
                        Console.ResetColor();
                        Thread.Sleep(1000);
                        //erase the error message and reset cursor
                        Console.SetCursorPosition(0, currentCursorTop);
                        Console.Write(" ", Console.WindowWidth);
                        Console.SetCursorPosition(0, currentCursorTop);
                        continue;
                    }
                }
            }

            ConsoleColor playerTextColor = mark == "X" ? TextEffects.XMarkColor : TextEffects.OMarkColor;

            while (true)
            {
                //space it over
                Console.Write("   ");
                Console.Write("Player \"");
                Console.ForegroundColor = playerTextColor;
                Console.Write(mark);
                Console.ResetColor();
                Console.Write("\" please enter your name: ");
                Console.ForegroundColor = playerTextColor;
                userInput = Console.ReadLine();
                Console.ResetColor();
                if (userInput.Length < 1)
                {
                    Console.SetCursorPosition(0, currentCursorTop);
                    Console.ForegroundColor = TextEffects.ErrorColor;
                    Console.WriteLine("                Please try again...                ");
                    Console.ResetColor();
                    Thread.Sleep(800);
                    //erase the error message and reset cursor
                    Console.SetCursorPosition(0, currentCursorTop);
                    Console.Write("                   ");
                    Console.SetCursorPosition(0, currentCursorTop);
                    continue;
                }
                else if (userInput.Length > 12)
                {
                    Console.SetCursorPosition(0, currentCursorTop);
                    Console.ForegroundColor = TextEffects.ErrorColor;
                    Console.WriteLine(" Sorry, your name can only be 12 characters long.                                  ");
                    Console.ResetColor();
                    Thread.Sleep(1500);
                    //erase the error message and reset cursor
                    Console.SetCursorPosition(0, currentCursorTop);
                    Console.Write("                                                                            ");
                    Console.SetCursorPosition(0, currentCursorTop);
                    continue;
                }
                Console.SetCursorPosition(0, currentCursorTop);
                Console.Write("                                                                            ");
                Console.SetCursorPosition(0, currentCursorTop);
                break;
            }

            //say hello to the player in their color, then erase message
            Console.ForegroundColor = playerTextColor;
            //space it over
            Console.Write("                    ");
            Console.Write("Hello, " + userInput + "!");
            Thread.Sleep(1000);
            Console.Clear();
            Console.ResetColor();

            //return new Player(userInput, mark, false, playerTextColor);
            return(new Player()
            {
                Mark = mark, Name = userInput, TextColor = playerTextColor
            });
        }