public void AddPlayer(BlackJackPlayer player)
 {
     Players.Add(player);
 }
Example #2
0
        static void Main()
        {
            Console.WriteLine("*********************");
            Console.WriteLine("Welcome to the Console Black Jack Game");

            string numOfPlayers = "";
            Int16  h;

            while (!Int16.TryParse(numOfPlayers, out h))
            {
                Console.Write("How many players will play this game? ");
                try
                {
                    numOfPlayers = Console.ReadLine();
                    if (!Int16.TryParse(numOfPlayers, out h))
                    {
                        throw new ArgumentException($"{numOfPlayers} is invalid! Must specify a number");
                    }
                }
                catch (ArgumentException ax)
                {
                    Console.WriteLine(ax.Message);
                }
            }

            Players players = new Players();

            // adds new players, skips dealer at index 0
            for (int i = 1; i < Int16.Parse(numOfPlayers) + 1; i++)
            {
                Player player = new Player();
                Console.Write($"Player {i}'s name:");
                player.Name = Console.ReadLine();
                players.Add(player);
            }

            var answer = new ConsoleKeyInfo();

            while (answer.KeyChar != '0')
            {
                Console.WriteLine("\r\n0 - Exit");
                Console.WriteLine("1 - Play");
                Console.Write("Your choice: ");

                try
                {
                    answer = Console.ReadKey();
                    Console.WriteLine();

                    if (answer.KeyChar != '1' && answer.KeyChar != '0' && answer.Key != ConsoleKey.Escape)
                    {
                        throw new Exception($"\r\n{answer.KeyChar} is invalid. Please enter a valid selection");
                    }

                    switch (answer.KeyChar)
                    {
                    case '0':
                        return;

                    case (char)ConsoleKey.Escape:
                        return;

                    case '1':
                        Game game = new Game(players);

                        //if someone has blackjack game is over
                        if (game.Players.Any(player => player.HasBlackJack()))
                        {
                            DeclareWinners(game);
                            break;
                        }
                        //everyone gets a turn now
                        foreach (Player p in game.Players)
                        {
                            //dealer plays last
                            if (p.IsDealer)
                            {
                                continue;
                            }
                            Console.WriteLine($"\r\n\r\nHi {p.Name}. Your cards: ");
                            Console.WriteLine(p.Hand.ToString());
                            //gives the sum of the hand for all players except dealer
                            Console.WriteLine($"{p.Name}, your total is: {p.GetSumOfAllCards()}\r\n");
                            Console.WriteLine($"Dealer's first card: {game.Dealer.Hand.ElementAt(0).Face}, {game.Dealer.Hand.ElementAt(0).Value}\r\n");

                            //time to hit or stay

                            var answ = new ConsoleKeyInfo();

                            while (answ.KeyChar != 's' && answ.Key != ConsoleKey.Escape && !p.IsBusted())
                            {
                                Console.Write($"\r\n{p.Name}, Hit or stay? h/s: ");
                                try
                                {
                                    answ = Console.ReadKey();
                                    if (answ.KeyChar != 's' && answ.Key != ConsoleKey.Escape && answ.KeyChar != 's')
                                    {
                                        throw new Exception($"\r\n{answ.KeyChar} is invalid. Please enter a valid selection");
                                    }
                                    switch (answ.KeyChar)
                                    {
                                    case 'h':
                                        game.HitPlayer(p);
                                        if (p.IsBusted())
                                        {
                                            Console.WriteLine($"{p.Name} busted.");
                                            p.PlayerStands = true;
                                            break;
                                        }
                                        Console.WriteLine($"\r\nYou got: {p.Hand.ToString()}. Your sum: {p.GetSumOfAllCards()}");
                                        break;

                                    case 's':
                                        break;

                                    default:
                                        break;
                                    }
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.Message);
                                }
                            }
                        }

                        //now dealer's turn
                        if (!game.CheckAnyoneLeft())
                        {
                            while (game.Dealer.GetSumOfAllCards() <= 17)
                            {
                                game.HitDealer();
                                if (game.Dealer.IsBusted())
                                {
                                    Console.WriteLine($"{game.Dealer.Name} busted.");
                                    game.Dealer.PlayerStands = true;
                                    break;
                                }
                            }
                        }


                        Console.WriteLine("\r\nGame over!\r\n");

                        //uf no one busted or had blackjack, winner is the player with the
                        //highest score

                        DeclareWinners(game);
                        //All done. Time to show cards and sum all up game scores.
                        foreach (Player player in game.Players)
                        {
                            Console.WriteLine($"{player.Name}: Sum: {player.GetSumOfAllCards()}. Score: {player.Score}");
                        }


                        break;

                    default:
                        Console.WriteLine($"{answer} is an invalid selection");
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
Example #3
0
		/// <summary>
		/// Creates a new deck and calls InitializeNewGame with already defined players
		/// </summary>
		/// <param name="players">A predefined list of players</param>
		public Game(Players players)
		{
			GameDeck = new Deck();
			InitializeNewGame(players);
		}