Пример #1
0
        public override void Play()
        {
            Dealer = new BlackjackDealer();
            foreach (Player player in Players)
            {
                player.Hand = new List <Card>();
                player.Stay = false;
            }
            Dealer.Hand = new List <Card>();
            Dealer.Stay = false;
            Dealer.Deck = new Deck();
            Dealer.Deck.Shuffle();

            foreach (Player player in Players)
            {
                bool valid = false;
                int  bet   = 0;
                while (!valid)
                {
                    Console.WriteLine("Place your bet!");
                    valid = int.TryParse(Console.ReadLine(), out bet);
                    if (!valid)
                    {
                        Console.WriteLine("Please enter a whole number using digits only.");
                    }
                }
                if (bet < 0)
                {
                    throw new FraudException("Security! Kick this person out!");
                }
                bool successfullyBet = player.Bet(bet);
                if (!successfullyBet)
                {
                    return; //exits out of the method and returns to the while loop in the main program, restarting the play method.
                }
                Bets[player] = bet;
            }
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("Dealing...");
                foreach (Player player in Players)
                {
                    Console.Write("{0}: ", player.Name);
                    Dealer.Deal(player.Hand);
                    if (i == 1)
                    {
                        bool blackJack = BlackjackRules.CheckForBlackjack(player.Hand);
                        if (blackJack)
                        {
                            Console.WriteLine("Blackjack! {0} wins {1}", player.Name, Bets[player] * 1.5);
                            player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]);
                            return;
                        }
                    }
                }
                Console.Write("Dealer: ");
                Dealer.Deal(Dealer.Hand);
                if (i == 1)
                {
                    bool blackJack = BlackjackRules.CheckForBlackjack(Dealer.Hand);
                    if (blackJack)
                    {
                        Console.WriteLine("Dealer has Blackjack! Everyone Loses!");
                        foreach (KeyValuePair <Player, int> entry in Bets)
                        {
                            Dealer.Balance += entry.Value;
                        }
                        return;
                    }
                }
            }
            foreach (Player player in Players)
            {
                while (!player.Stay)
                {
                    Console.WriteLine("Your Cards are: ");
                    foreach (Card card in player.Hand)
                    {
                        Console.Write("{0} ", card.ToString());
                    }
                    Console.WriteLine("\n\nHit or stay?");
                    string answer = Console.ReadLine().ToLower();
                    if (answer == "stay")
                    {
                        player.Stay = true;
                        break;
                    }
                    else if (answer == "hit")
                    {
                        Dealer.Deal(player.Hand);
                    }
                    bool busted = BlackjackRules.IsBusted(player.Hand);
                    if (busted)
                    {
                        Dealer.Balance += Bets[player];
                        Console.WriteLine("{0} Busted! You lose you bet of {1}.  Your balance is now {2}.", player.Name, Bets[player], player.Balance);
                        Console.WriteLine("Would you like to continue?");
                        answer = Console.ReadLine().ToLower();
                        if (answer == "yes")
                        {
                            player.isActivelyPlaying = true;
                            return;
                        }
                        else
                        {
                            player.isActivelyPlaying = false;
                            return;
                        }
                    }
                }
            }
            Dealer.isBusted = BlackjackRules.IsBusted(Dealer.Hand);
            Dealer.Stay     = BlackjackRules.ShouldDealerStay(Dealer.Hand);
            while (!Dealer.isBusted && !Dealer.Stay)
            {
                Console.WriteLine("Dealer is Hitting...");
                Dealer.Deal(Dealer.Hand);
                Dealer.isBusted = BlackjackRules.IsBusted(Dealer.Hand);
                Dealer.Stay     = BlackjackRules.ShouldDealerStay(Dealer.Hand);
            }
            if (Dealer.Stay)
            {
                Console.WriteLine("Dealer is staying.");
            }
            if (Dealer.isBusted)
            {
                Console.WriteLine("Dealer busted!");
                foreach (KeyValuePair <Player, int> entry in Bets)
                {
                    Console.WriteLine("{0} won {1}!", entry.Key.Name, entry.Value);
                    Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2);
                    Dealer.Balance -= entry.Value;
                }
            }

            foreach (Player player in Players)
            {
                bool?playerWon = BlackjackRules.CompareHands(player.Hand, Dealer.Hand);
                if (playerWon == null)
                {
                    Console.WriteLine("Push! No one Wins.");
                    player.Balance += Bets[player];
                }
                else if (playerWon == true)
                {
                    Console.WriteLine("{0} won {1}!", player.Name, Bets[player]);
                    player.Balance += Bets[player] * 2;
                    Dealer.Balance -= Bets[player];
                }
                else
                {
                    Console.WriteLine("Dealer wins {0}!", Bets[player]);
                    Dealer.Balance += Bets[player];
                }
                Console.WriteLine("Would you like to play again?");
                string answer = Console.ReadLine().ToLower();
                if (answer == "yes")
                {
                    player.isActivelyPlaying = true;
                    return;
                }
                else
                {
                    player.isActivelyPlaying = false;
                    return;
                }
            }
        }
 //===== CONSTRUCTOR
 public BlackjackGame() : base()
 {
     // using constructor from base Class "Game"
     // creates new instances of Players, Bets
     Dealer = new BlackjackDealer("", 0);
 }
        //===== PLAY
        public override void Play() //override satisfies the requirement for this child to have the parent method
        {
            //----- START set Dealer and declare variables at the beginning of game
            if (Dealer.Name == "")
            {
                Dealer = new BlackjackDealer(name: "Doc Holliday", bank: int.MaxValue);
                ListPlayers();
            }
            int  bet;
            bool isValid, winner;

            //----- RESET each round
            Dealer.Stay = Dealer.Blackjack = winner = false;
            Dealer.Hand = new List <Card>();
            Dealer.Deck = new Deck(deckCount: Players.Count);
            foreach (Player p in Players)
            {
                p.Hand = new List <Card>();
                p.Stay = false;
            }

            //----- BET for each player
            Console.WriteLine("\n=== Place Bets...");
            foreach (Player p in Players)
            {
                isValid = false;
                while (!isValid)
                {
                    try
                    {
                        Console.WriteLine("{0} your bet:", p.Name);
                        bet = Convert.ToInt32(Console.ReadLine());
                        if (p.Bet(bet))
                        {
                            isValid = true;
                            Bets[p] = bet;
                        }
                    }
                    catch (FraudException)
                    {
                        p.ActivelyPlaying = false;
                        throw new FraudException(string.Format("Security! {0} has tried to commit fraud", p.Name));
                        // Console.WriteLine("SECURITY! Throw this person out.");
                        // Console.ReadLine();
                        // return;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Oops...something went wrong:");
                        Console.WriteLine(e.Message);
                    }
                }
            }

            //----- DEAL START
            Console.WriteLine("\n=== Dealing...");
            for (int i = 0; i < 2; i++)
            {
                Dealer.Deal(Dealer.Hand, Dealer.Name);
                foreach (Player p in Players)
                {
                    Dealer.Deal(p.Hand, p.Name);
                }
            }

            //----- CHECK BLACKJACK - If dealer has Blackjack all players lose except those that also have Blackjack (Draw)
            Dealer.Blackjack = BlackjackRules.CheckBlackjack(Dealer.Hand);
            if (Dealer.Blackjack)
            {
                //--- Dealer Blackjack, all payouts, return
                Console.WriteLine("\n=== Dealer gets Blackjack!");
                foreach (Player p in Players)
                {
                    if (BlackjackRules.CheckBlackjack(p.Hand))
                    {
                        //--- DRAW player blackjack
                        Dealer.Payout(Dealer, p, Bets, condition: "draw");
                    }
                    else
                    {
                        //--- LOSE no blackjack
                        Dealer.Payout(Dealer, p, Bets, condition: "lose");
                    }
                }
                return;
            }
            else
            {
                //--- Dealer no Blackjack, check player Blackjack, payout if winner
                winner = Players.Any(p => BlackjackRules.CheckBlackjack(p.Hand));
                if (winner)
                {
                    foreach (Player p in Players)
                    {
                        if (BlackjackRules.CheckBlackjack(p.Hand))
                        {
                            Dealer.Payout(Dealer, p, Bets, condition: "blackjack");
                        }
                    }
                }
            }

            //----- CONTINUE PLAY
            Console.WriteLine("\n=== Continue Play...");
            foreach (Player p in Players)
            {
                while (!p.Stay)
                {
                    //--- Hand display
                    Console.WriteLine("\nDealer hand:");
                    foreach (Card c in Dealer.Hand)
                    {
                        Console.WriteLine("- {0}", c.ToString());
                    }
                    Console.WriteLine("\n{0} your cards are:", p.Name);
                    foreach (Card c in p.Hand)
                    {
                        Console.WriteLine("- {0}", c.ToString());
                    }

                    //--- Hit or Stay
                    Console.WriteLine("\n Hit or Stay?");
                    if (Console.ReadLine().ToLower().Contains("s"))
                    {
                        p.Stay = true;
                        break;
                    }
                    else
                    {
                        Dealer.Deal(p.Hand, p.Name);
                    }

                    //--- Check Busted
                    if (BlackjackRules.CheckBusted(p.Hand))
                    {
                        Dealer.Payout(Dealer, p, Bets, condition: "bust");
                    }
                }
            }

            //----- DEALER PLAY
            if (Bets.Count() == 0)
            {
                return;                    // check if all players are out
            }
            Dealer.Stay = BlackjackRules.CheckDealerStay(Dealer.Hand);
            while (!Dealer.Stay && !Dealer.Bust)
            {
                Console.WriteLine("\n=== Dealer is hitting...");
                Dealer.Deal(Dealer.Hand, Dealer.Name);
                Dealer.Bust = BlackjackRules.CheckBusted(Dealer.Hand);
                Dealer.Stay = BlackjackRules.CheckDealerStay(Dealer.Hand);
            }

            //----- DEALER BUST
            if (Dealer.Bust)
            {
                Console.WriteLine("\n=== Dealer Busted!");
                //--- PAYOUT function - pay only thoses players that still have bets
                foreach (Player p in Players.Where(x => Bets.ContainsKey(x)))
                {
                    Dealer.Payout(Dealer, p, Bets, condition: "win");
                }

                ////--- ALTERNATIVE - Lambda function method
                //foreach (KeyValuePair<Player, int> entry in Bets)
                //{
                //    Players.Where(p => p == entry.Key).First().Balance += (entry.Value * 2);
                //}
            }
            //----- DEALER STAY - check only thoses players that still have bets
            else if (Dealer.Stay)
            {
                Console.WriteLine("\n=== Dealer is staying.");
                foreach (Player p in Players.Where(x => Bets.ContainsKey(x)))
                {
                    switch (BlackjackRules.CheckWin(Dealer.Hand, p.Hand))
                    {
                    case true:     //--- player win
                        Dealer.Payout(Dealer, p, Bets, condition: "win");
                        break;

                    case false:     //--- dealer win
                        Dealer.Payout(Dealer, p, Bets, condition: "lose");
                        break;

                    default:     //--- draw
                        Dealer.Payout(Dealer, p, Bets, condition: "draw");
                        break;
                    }
                }
            }
            return;
        }
Пример #4
0
        public override void Play()
        {
            Dealer = new BlackjackDealer();
            foreach (Player player in Players)
            {
                player.Hand = new List <Card>();
                player.Stay = false;
            }
            Dealer.Hand = new List <Card>();
            Dealer.Stay = false;
            Dealer.Deck = new Deck();
            Dealer.Deck.Shuffle();

            foreach (Player player in Players)
            {
                bool validAnswer = false;
                int  bet         = 0;
                while (!validAnswer)
                {
                    Console.WriteLine("{0}, please place your bet:", player.Name);
                    Console.Write("$");
                    validAnswer = int.TryParse(Console.ReadLine(), out bet);
                    if (!validAnswer)
                    {
                        Console.WriteLine("Please enter a whole number.");
                    }
                }
                if (bet < 0)
                {
                    throw new FraudException("Suspicious activity detected. Security has been notified.");
                }

                bool HasFunds = player.Bet(bet);
                if (!HasFunds)
                {
                    return;
                }
                Bets[player] = bet;
                Console.WriteLine();
                Console.WriteLine("Dealing cards...\n");
            }

            for (int i = 0; i < 2; i++)
            {
                foreach (Player player in Players)
                {
                    Console.Write("{0} gets: ", player.Name);
                    Dealer.Deal(player.Hand);
                    if (i == 1)
                    {
                        bool blackJack = BlackjackRules.IsBlackjack(player.Hand);
                        if (blackJack)
                        {
                            Console.WriteLine("Blackjack! {0} wins {1}", player.Name, Bets[player]);
                            player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]);
                            return;
                        }
                    }
                }
                Console.Write("Dealer gets: ");
                Dealer.Deal(Dealer.Hand);
                if (i == 1)
                {
                    bool blackJack = BlackjackRules.IsBlackjack(Dealer.Hand);
                    if (blackJack)
                    {
                        Console.WriteLine("Dealer has Blackjack!  Everyone loses.");
                        foreach (KeyValuePair <Player, int> entry in Bets)
                        {
                            Dealer.Balance += entry.Value;
                        }
                        return;
                    }
                }
            }

            foreach (Player player in Players)
            {
                while (!player.Stay)
                {
                    Console.WriteLine("{0}'s cards: ", player.Name);
                    foreach (Card card in player.Hand)
                    {
                        Console.WriteLine("{0}", card.ToString());
                    }
                    Console.WriteLine("\n\nHit or Stay? (h or s)");
                    string decision = Console.ReadLine().ToLower();
                    if (decision == "stay" || decision == "s")
                    {
                        player.Stay = true;
                        break;
                    }
                    else if (decision == "hit" || decision == "h")
                    {
                        Console.WriteLine("Your new card:");
                        Dealer.Deal(player.Hand);
                    }
                    bool busted = BlackjackRules.HasBusted(player.Hand);
                    if (busted)
                    {
                        Dealer.Balance += Bets[player];
                        player.Balance -= Bets[player];
                        Console.WriteLine("You busted!  You lose your bet of ${0}. You have ${1} remaining.", Bets[player], player.Balance);
                        Console.WriteLine("\nWould you like to play again? (y or n)");
                        decision = Console.ReadLine().ToLower();
                        if (decision == "yes" || decision == "y")
                        {
                            player.IsActive = true;
                        }
                        else
                        {
                            player.IsActive = false;
                        }
                    }
                }
            }

            Dealer.HasBusted = BlackjackRules.HasBusted(Dealer.Hand);
            Dealer.Stay      = BlackjackRules.DealerHitOrStay(Dealer.Hand);
            while (!Dealer.Stay && !Dealer.HasBusted)
            {
                Console.WriteLine("Dealer hits.");
                Dealer.Deal(Dealer.Hand);
                Dealer.HasBusted = BlackjackRules.HasBusted(Dealer.Hand);
                Dealer.Stay      = BlackjackRules.DealerHitOrStay(Dealer.Hand);
            }

            if (Dealer.Stay)
            {
                Console.WriteLine("Dealer stays.");
            }
            if (Dealer.HasBusted)
            {
                Console.WriteLine("Dealer has busted!  Everyone wins!");
                foreach (KeyValuePair <Player, int> entry in Bets)
                {
                    Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2);
                    Dealer.Balance -= entry.Value;
                    Console.WriteLine("{0} won ${1}.", entry.Key.Name, entry.Value);
                }
                return;
            }
            foreach (Player player in Players)
            {
                bool?playerWins = BlackjackRules.CompareHands(player.Hand, Dealer.Hand);
                if (playerWins == null)
                {
                    Console.WriteLine("Push.  No one wins.");
                    player.Balance += Bets[player];
                }
                else if (playerWins == true)
                {
                    Console.WriteLine("{0} won ${1}!", player.Name, Bets[player]);
                }
                else
                {
                    Console.WriteLine("Dealer wins.");
                    Dealer.Balance += Bets[player];
                }
                Console.WriteLine("Play Again? (y or n)");
                string again = Console.ReadLine().ToLower();
                if (again == "yes" || again == "y")
                {
                    player.IsActive = true;
                    return;
                }
                else
                {
                    player.IsActive = false;
                    return;
                }
            }
        }