public void DisplayMenu()
        {
            bool loop = true;

            do
            {
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine($"{this.ToString()} menu:");
                Console.WriteLine(" 1) Play");
                Console.WriteLine(" 2) Show rules");
                Console.WriteLine(" 3) Show statistics");
                Console.WriteLine();
                Console.WriteLine(" Q) Return to main menu");
                Console.WriteLine();
                var pressedKey = AskUserForInput("Selection:");
                switch (pressedKey.ToUpper())
                {
                case "1":
                    this.Play();
                    break;

                case "2":
                    this.DisplayRules();
                    break;

                case "3":
                    this.DisplayStatistics();
                    break;

                case "Q":
                    loop = false;
                    break;

                default:
                    PlayingCardGame.DisplayErrorMessage("Invalid menu option.");
                    Console.ReadKey();
                    break;
                }
                Console.Clear();
            } while (loop);
        }
Exemple #2
0
        public override void Play()
        {
            Console.Clear();
            if (this.Players.Count == 0)
            {
                var playerName = string.Empty;
                do
                {
                    playerName = PlayingCardGame.AskUserForInput("What is your name?");
                    if (!string.IsNullOrWhiteSpace(playerName))
                    {
                        break;
                    }
                } while (true);
                this.Players = new List <Player> {
                    new Player {
                        Name = playerName, IsAi = false, CardsOnHand = this.CardDeck.GetCardsFromTop(2)
                    }
                };
                this.Players[0].CardsOnHand[0].Visible = true;
            }
            else
            {
                this.Players[0].CardsOnHand            = this.CardDeck.GetCardsFromTop(2);
                this.Players[0].CardsOnHand[0].Visible = true;
            }

            Console.Clear();
            Console.WriteLine($"Player {this.Players[0].Name}");
            Console.WriteLine("Your cards:");

            foreach (var card in this.Players[0].CardsOnHand)
            {
                Console.Write($"{card.ToString()}\t");
            }
            Console.WriteLine();
            do
            {
                Console.WriteLine();
                string userGuess = AskUserForInput("Is the hidden card higher or lower?: ");

                // User guessed higher
                if (userGuess.Equals("higher", StringComparison.CurrentCultureIgnoreCase) || userGuess.Equals("h", StringComparison.CurrentCultureIgnoreCase))
                {
                    DisplayResult(this.Players[0], HighOrLow.Higher);
                    break;
                }
                // User guessed lower
                else if (userGuess.Equals("lower", StringComparison.CurrentCultureIgnoreCase) || userGuess.Equals("l", StringComparison.CurrentCultureIgnoreCase))
                {
                    DisplayResult(this.Players[0], HighOrLow.Lower);
                    break;
                }
                // Invalid guess
                else
                {
                    DisplayErrorMessage("Invalid guess");
                }
            } while (true);
            var userInput = string.Empty;

            do
            {
                userInput = AskUserForInput("Play again? (Y/N):");
                if (userInput.Equals("Y", StringComparison.CurrentCultureIgnoreCase) || userInput.Equals("N", StringComparison.CurrentCultureIgnoreCase))
                {
                    break;
                }
                else
                {
                    DisplayErrorMessage("Invalid input");
                    Console.ReadKey();
                }
            } while (true);

            // Return cards on hand to deck
            this.CardDeck.PutCardLast(this.Players[0].CardsOnHand);
            this.Players[0].CardsOnHand = null;

            if (userInput.Equals("Y", StringComparison.CurrentCultureIgnoreCase))
            {
                this.Play();
            }
            else if (userInput.Equals("N", StringComparison.CurrentCultureIgnoreCase))
            {
                this.Players = new List <Player>();
            }
        }