コード例 #1
0
        public override void Play()
        {
            Dealer = new TwentyOneDealer();

            //Set up Players
            foreach (Player player in Players)
            {
                player.Hand = new List <Card>();
                player.Stay = false;
            }

            //Set up Dealer
            Dealer.Hand = new List <Card>();
            Dealer.Stay = false;
            Dealer.Deck = new Deck();
            Dealer.Deck.Shuffle();



            //Place Bet
            foreach (Player player in Players)
            {
                bool validAnswer = false;
                int  bet         = 0;
                while (!validAnswer)
                {
                    Console.Write("Place your Bet!");
                    validAnswer = int.TryParse(Console.ReadLine(), out bet);
                    if (!validAnswer)
                    {
                        Console.WriteLine("please enter digits only, no decimals.");
                    }
                }
                if (bet < 0)
                {
                    throw new FraudException();
                }

                bool successfullybet = player.Bet(bet);
                if (!successfullybet)
                {
                    return;
                }
                Bets[player] = bet;
            }

            // Shuffle


            //  Deal Round
            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 = TwentyOneRules.CheckForBlackJack(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.WriteLine("Dealer");
                Dealer.Deal(Dealer.Hand);
                if (i == 1)
                {
                    bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand);
                    if (blackJack)
                    {
                        Console.WriteLine("Dealer has BlackJack! Everyone loses!");
                        foreach (KeyValuePair <Player, int> entry in Bets)
                        {
                            Dealer.Balance -= entry.Value;
                        }
                    }
                }
            }
            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 = TwentyOneRules.isBusted(player.Hand);
                    if (busted)
                    {
                        Dealer.Balance += Bets[player];
                        Console.WriteLine("{0} Busted! You lose your bet of {1}. Your balance is now {2}.", player.Name, Bets[player], player.Balance);
                        Console.WriteLine("Do you want to play again?");
                        answer = Console.ReadLine().ToLower();
                        if (answer == "yes" || answer == "yeah")
                        {
                            player.isActivelyPlaying = true;
                        }
                        else
                        {
                            player.isActivelyPlaying = false;
                        }
                    }
                }
            }
            Dealer.isBusted = TwentyOneRules.isBusted(Dealer.Hand);
            Dealer.Stay     = TwentyOneRules.shouldDealerStay(Dealer.Hand);
            while (!Dealer.Stay && !Dealer.isBusted)
            {
                Console.WriteLine("dealer is hitting..");
                Dealer.Deal(Dealer.Hand);
                Dealer.isBusted = TwentyOneRules.isBusted(Dealer.Hand);
                Dealer.Stay     = TwentyOneRules.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);
                }
                return;
            }
            foreach (Player player in Players)
            {
                bool?playerWon = TwentyOneRules.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("Play again?");
                string answer = Console.ReadLine().ToLower();
                if (answer == "yes" || answer == "yeah")
                {
                    player.isActivelyPlaying = true;
                }
                else
                {
                    player.isActivelyPlaying = false;
                }
                return;
            }
        }
コード例 #2
0
        public override void Play()
        {
            Dealer = new TwentyOneDealer();
            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();
            Console.WriteLine("Place your bet!");

            foreach (Player player in Players)
            {
                int  bet             = Convert.ToInt32(Console.ReadLine());
                bool successfullyBet = player.Bet(bet);
                if (!successfullyBet)
                {
                    return;
                }
                Bets[player] = bet;
            }
            for (int i = 0; 1 < 2; i++)
            {
                Console.WriteLine("Dealing....");
                foreach (Player player in Players)
                {
                    Console.Write("{0}: ", player.Name);
                    Dealer.Deal(player.Hand);
                    if (i == 1)
                    {
                        bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand);
                        if (blackJack)
                        {
                            Console.WriteLine("Blackjack! {0} wins {1}", player.Name, Bets[player]);
                            player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]);
                            Bets.Remove(player);
                            return;
                        }
                    }
                }
                Console.Write("Dealer: ");
                Dealer.Deal(Dealer.Hand);
                if (i == 1)
                {
                    bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand);
                    if (blackJack)
                    {
                        Console.WriteLine("Dealer has BlackJack! Everyone loses!");
                        foreach (KeyValuePair <Player, int> entry in Bets)
                        {
                            Dealer.Balance += entry.Value;
                        }
                    }
                }
                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("\nHit or stay?\n");
                        string answer = Console.ReadLine().ToLower();
                        if (answer == "stay")
                        {
                            player.Stay = true;
                            break;
                        }
                        else if (answer == "hit")
                        {
                            Dealer.Deal(player.Hand);
                        }
                        bool busted = TwentyOneRules.isBusted(player.Hand);
                        if (busted)
                        {
                            Dealer.Balance += Bets[player];
                            Console.WriteLine("{0} Busted! You lose bet of {1}. Your balance is now  {2}.", player.Name, Bets[player], player.Balance);
                            Console.WriteLine("Do you want to play again?");
                            answer = Console.ReadLine().ToLower();
                            if (answer == "yes" || answer == "yeah")
                            {
                                player.isActivelyPlaying = true;
                            }
                        }
                    }
                }
                Dealer.isBusted = TwentyOneRules.isBusted(Dealer.Hand);
                Dealer.Stay     = TwentyOneRules.ShouldDealerStay(Dealer.Hand);
                while (!Dealer.Stay && !Dealer.isBusted)
                {
                    Console.WriteLine("Dealer is hitting...");
                    Dealer.Deal(Dealer.Hand);
                    Dealer.isBusted = TwentyOneRules.isBusted(Dealer.Hand);
                    Dealer.Stay     = TwentyOneRules.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;
                    }
                    return;
                }
                foreach (Player player in Players)
                {
                    bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand);
                    if (playerWon == null)
                    {
                        Console.WriteLine("Push! No one wins.");
                        player.Balance += Bets[player];
                        Bets.Remove(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("Play again?");
                    string answer = Console.ReadLine().ToLower();
                    if (answer == "yes" || answer == "yeah")
                    {
                        player.isActivelyPlaying = true;
                    }
                    else
                    {
                        player.isActivelyPlaying = false;
                    }
                }
            }
        }
コード例 #3
0
 //Implementing the abstract method "play" from the Game class.
 public override void Play()
 {
     Dealer = new TwentyOneDealer();
     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(3);
     Console.WriteLine("\nThe game is 21 and you have a balance of {0}.", Players.First().Balance);
     foreach (Player player in Players)
     {
         bool validAnswer = false;
         int  bet         = 0;
         while (!validAnswer)
         {
             Console.WriteLine("Please place your bets!");
             validAnswer = int.TryParse(Console.ReadLine(), out bet);
             if (!validAnswer)
             {
                 Console.WriteLine("Please enter digits only, no decimals.");
             }
         }
         if (bet <= 0)
         {
             throw new FraudException("Security! Remove this person immediately for attempting to defraud this casino!");
         }
         bool successfullyBet = player.Bet(bet);
         if (!successfullyBet)
         {
             return;
         }
         Bets[player] = bet;
     }
     for (int i = 0; i < 2; i++)
     {
         Console.WriteLine("\nDealing...");
         foreach (Player player in Players)
         {
             Console.Write("{0}: ", player.Name);
             Dealer.Deal(player.Hand);
             if (i == 1)
             {
                 bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand);
                 if (blackJack)
                 {
                     //Error with this balance
                     //Now this balance works
                     player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]);
                     Console.WriteLine("BlackJack! {0} wins {1}. Your balance is {2}.", player.Name, Bets[player], player.Balance);
                     return;
                 }
             }
         }
         Console.Write("Dealer: ");
         Dealer.Deal(Dealer.Hand);
         if (i == 1)
         {
             bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand);
             if (blackJack)
             {
                 foreach (KeyValuePair <Player, int> entry in Bets)
                 {
                     Dealer.Balance += entry.Value;
                 }
                 Console.WriteLine("Dealer has BlackJack! Everyone loses. Your balance is {0}", Players.First().Balance);
                 return;
             }
         }
     }
     foreach (Player player in Players)
     {
         while (!player.Stay)
         {
             Console.WriteLine("\nYour cards are: ");
             foreach (Card card in player.Hand)
             {
                 Console.WriteLine("{0}. ", card.ToString());
             }
             foreach (int handPossibilities in TwentyOneRules.GetAllPossibleHandValues(player.Hand))
             {
                 Console.WriteLine("\nYour hand's value is: {0}", handPossibilities);
             }
             foreach (int handPossibilities in TwentyOneRules.GetAllPossibleHandValues(Dealer.Hand))
             {
                 Console.WriteLine("The dealer's hand is worth: {0}", handPossibilities);
             }
             Console.WriteLine("\nHit or stay?");
             string answer = Console.ReadLine().ToLower();
             if (answer == "stay")
             {
                 player.Stay = true;
                 break;
             }
             else if (answer == "hit")
             {
                 Dealer.Deal(player.Hand);
             }
             else
             {
                 Console.WriteLine("\nI didn't understand. Do you want to hit or stay?");
             }
             bool busted = TwentyOneRules.isBusted(player.Hand);
             if (busted)
             {
                 Dealer.Balance += Bets[player];
                 Console.WriteLine("\n{0} busted! You lost your bet of {1}. Your balance is now {2}.", player.Name, Bets[player], player.Balance);
                 Console.WriteLine("Do you want to play again?");
                 answer = Console.ReadLine().ToLower();
                 if (answer == "yes" || answer == "ok" || answer == "okay" || answer == "sure" || answer == "yeah" || answer == "y" || answer == "yep" || answer == "yup" || answer == "ya")
                 {
                     player.isActivelyPlaying = true;
                     return;
                 }
                 else
                 {
                     player.isActivelyPlaying = false;
                     return;
                 }
             }
         }
     }
     Dealer.isBusted = TwentyOneRules.isBusted(Dealer.Hand);
     Dealer.Stay     = TwentyOneRules.ShouldDealerStay(Dealer.Hand);
     while (!Dealer.Stay && !Dealer.isBusted)
     {
         Console.WriteLine("\nDealer is hitting...");
         Dealer.Deal(Dealer.Hand);
         Dealer.isBusted = TwentyOneRules.isBusted(Dealer.Hand);
         Dealer.Stay     = TwentyOneRules.ShouldDealerStay(Dealer.Hand);
     }
     if (Dealer.Stay)
     {
         Console.WriteLine("\nDealer is staying.");
         foreach (int handPossibilities in TwentyOneRules.GetAllPossibleHandValues(Dealer.Hand))
         {
             Console.WriteLine("The dealer's hand is worth: {0}", handPossibilities);
         }
     }
     if (Dealer.isBusted)
     {
         Console.WriteLine("Dealer busted!");
         foreach (KeyValuePair <Player, int> entry in Bets)
         {
             Dealer.Balance -= entry.Value;
             Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2);
             Console.WriteLine("{0} won {1}! Your balance is {2}", entry.Key.Name, entry.Value, Players.First().Balance);
         }
         return;
     }
     foreach (Player player in Players)
     {
         bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand);
         if (playerWon == null)
         {
             Console.WriteLine("Push! No one wins. Your balance is {0}", player.Balance);
             player.Balance = +Bets[player];
             Bets.Remove(player);
         }
         else if (playerWon == true)
         {
             player.Balance += (Bets[player] * 2);
             Dealer.Balance -= Bets[player];
             Console.WriteLine("{0} won {1}! Your balance is {2}", player.Name, Bets[player], player.Balance);
         }
         else
         {
             Dealer.Balance += Bets[player];
             Console.WriteLine("I'm sorry, dealer wins {0}! Your balance is {1}", Bets[player], player.Balance);
         }
         Console.WriteLine("Play again?");
         string answer = Console.ReadLine().ToLower();
         if (answer == "yes" || answer == "ok" || answer == "okay" || answer == "sure" || answer == "yeah" || answer == "y" || answer == "yep" || answer == "yup" || answer == "ya")
         {
             player.isActivelyPlaying = true;
         }
         else
         {
             player.isActivelyPlaying = false;
         }
     }
 }
コード例 #4
0
        public override void Play() //use the override keyword which says we will use this abstract method that
        {                           //was inherited from the parent class Game
            Dealer = new TwentyOneDealer();
            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(); //here we refreshed everyones hands
            Dealer.Deck.Shuffle();

            foreach (Player player in Players) // for each player, make them bet
            {
                bool validAnswer = false;
                int  bet         = 0;
                while (!validAnswer)
                {
                    Console.WriteLine("Place your bet!");
                    //tryparse will convert the readline string to an integer and return it as var bank. If it succeeds valid answer is true if nto its false.
                    validAnswer = int.TryParse(Console.ReadLine(), out bet);
                    if (!validAnswer)
                    {
                        Console.WriteLine("Please enter digits only, no decimals.");
                    }
                }
                if (bet < 0)
                {
                    throw new FraudException();         //if they enter a neg number it is cheating. call this exception
                }
                bool successfullyBet = player.Bet(bet); //passing in our bet value to the Bet function
                if (!successfullyBet)
                {
                    return;         //if player dosent have enough for their bet the return brings them back to having to place a bet
                }
                Bets[player] = bet; //adding to the bets dictionary thats in the Game class
            }
            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 = TwentyOneRules.CheckForBlackJack(player.Hand); //check if player got black jack
                        if (blackJack)
                        {
                            Console.WriteLine("BlackJack! {0} wins {1}", player.Name, Bets[player]);
                            player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]); // if they do pay them 1.5x of their bet
                            return;
                        }
                    }
                }
                Console.Write("Dealer: ");
                Dealer.Deal(Dealer.Hand);
                if (i == 1)
                {
                    bool blackJack = TwentyOneRules.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.WriteLine("{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 = TwentyOneRules.isBusted(player.Hand);
                    if (busted)
                    {
                        Dealer.Balance += Bets[player]; //if you bust dealer gets your bet
                        Console.WriteLine("{0} Busted! You lose you bet of {1}. Your balance is now {2}.", player.Name, Bets[player], player.Balance);
                        Console.WriteLine("Do you want to play again?");
                        answer = Console.ReadLine().ToLower();
                        if (answer == "yes" || answer == "yea" || answer == "yeah" || answer == "yep")
                        {
                            player.IsActivelyPlaying = true;
                            return;
                        }
                        else
                        {
                            player.IsActivelyPlaying = false;
                            return;
                        }
                    }
                }
            }
            Dealer.isBusted = TwentyOneRules.isBusted(Dealer.Hand);
            Dealer.Stay     = TwentyOneRules.ShouldDealerStay(Dealer.Hand);
            while (!Dealer.Stay && !Dealer.isBusted) //dealer hits
            {
                Console.WriteLine("Deal is hitting...");
                Dealer.Deal(Dealer.Hand);                                       //add a card to dealers hand
                Dealer.isBusted = TwentyOneRules.isBusted(Dealer.Hand);         //check if dealer busted
                Dealer.Stay     = TwentyOneRules.ShouldDealerStay(Dealer.Hand); //check is dealer stayed (one of these has to be true)
            }
            if (Dealer.Stay)
            {
                Console.WriteLine("Dealer is Staying....");
            }
            if (Dealer.isBusted) // dealer lost so all players win
            {
                Console.WriteLine("Dealer Busted!");
                foreach (KeyValuePair <Player, int> entry in Bets)                                     //for each winner
                {
                    Console.WriteLine("{0} won {1}!", entry.Key.Name, entry.Value);                    //write a winners name ot the console
                    Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); //If they won find them in our dictionary and add their winnings to balance
                    Dealer.Balance -= entry.Value;                                                     //subtract bets from dealers balance
                }
            }
            foreach (Player player in Players)                                          //comparing players hands to dealers to see who wins
            {
                bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); //the ? makes it so that the bool value can be null, that way it can have 3 values
                if (playerWon == null)                                                  //if theres a tie
                {
                    Console.WriteLine("Push! No one wins.");
                    player.Balance += Bets[player];
                }
                else if (playerWon == true) //if player wins
                {
                    Console.WriteLine("{0} wins {1}!", player.Name, Bets[player]);
                    player.Balance += (Bets[player] * 2);
                    Dealer.Balance -= Bets[player];
                }
                else // if dealer wins
                {
                    Console.WriteLine("Dealer wins {0}!", Bets[player]);
                    Dealer.Balance += Bets[player];
                }
                Console.WriteLine("Play again?");
                string answer2 = Console.ReadLine().ToLower();
                if (answer2 == "yes" || answer2 == "yea" || answer2 == "yeah" || answer2 == "yep")
                {
                    player.IsActivelyPlaying = true;
                }
                else
                {
                    player.IsActivelyPlaying = false;
                }
            }
        }