} //twentyonegame has it's own dealer property public override void Play() //use overide keyword to "satisfy contract to define this method() from abract class" //implement Play() method { Dealer = new TwentyOneDealer(); //instantiate that dealer object as a new TwentyOneDealer foreach (player player in Players) //Players is a property of the game class { player.Hand = new List <Card>(); //want hand to be blank for each player a the start of a game player.Stay = false; //set at false for new game } Dealer.Hand = new List <Card>(); // want hand to be blank for dealer at the start of game Dealer.Stay = false; Dealer.Deck = new Deck(); //refresh deck every single round Dealer.Deck.Shuffle(); foreach (player player in Players) //loop through each player and have them place a bet { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("Place your bet!"); //next step in game validAnswer = int.TryParse(Console.ReadLine(), out bet); if (!validAnswer) { Console.WriteLine("Please enter digits only, without decimals"); } } if (bet < 0) { throw new FraudException("Security! Kick this person out."); //that way the user cant enter in a negative amount of money } bool successfullyBet = player.Bet(bet); //passes in user's bet into Bet() method and if they have enough it'll be true, if not false if (!successfullyBet) //if false { return; //in void method, what return does is ends method } Bets[player] = bet; //if bet was successful, create a dictionary entry for player which will add player object with their bet amount to dictionary } for (int i = 0; i < 2; i++) //next step is to deal, will have cards face up for simplicity with for each loop 2x { Console.WriteLine("Dealing..."); foreach (player player in Players) //loop through players, deal each player { Console.Write("{0}: ", player.Name); //use of string shorthand to write players names so we know whos getting dealt. Writing something to console without pressing enter Dealer.Deal(player.Hand); //passing in players hand and is given card and printed to console to see what you're given if (i == 1) //check firsthand to see if you were dealt blackjack { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); //passing in players hand if (blackJack) { Console.WriteLine("Blackjack! {0} wins {1}", player.Name, Bets[player]); //blackjack! will return value of players bet player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]); //player wins bet * 1.5 + original bet return; //round is over } } } Console.Write("Dealer: "); Dealer.Deal(Dealer.Hand); //dealer deals dealers hand if (1 == 1) //check for blackjack { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); if (blackJack) { Console.WriteLine("Dealer has Blackjack! Everyone loses!"); foreach (KeyValuePair <player, int> entry in Bets) //iterate through dictionary { Dealer.Balance += entry.Value; } } } } foreach (player player in Players) { while (!player.Stay) //while player is not staying... { Console.WriteLine("Your cards"); foreach (Card card in player.Hand) { Console.Write("{0} ", card.ToString()); //tostring method so we can show player their cards } Console.WriteLine("\n\nHit or stay?"); string answer = Console.ReadLine().ToLower(); //easy to compare users answer if (answer == "stay") { player.Stay = true; break; //end while loop, break, and check condition } else if (answer == "hit") { Dealer.Deal(player.Hand); //passing in hand and dealing player a card that will print to console } bool busted = TwentyOneRules.IsBusted(player.Hand); //did player go over 21, will return true or false if (busted) { Dealer.Balance += Bets[player]; //if player busts, have to give dealer that money, access that through Bets table (dictionary) 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 can wait to see if other players bust, highly unlikely but we will still put in the code for it Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); //checks for sum between 16 and 22 while (!Dealer.Stay && !Dealer.isBusted) //as long as dealer isn't busted and as long as dealer is staying... { Console.WriteLine("Dealer is hitting..."); Dealer.Deal(Dealer.Hand); //deal dealer a card Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); //checks again Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); //checks again } if (Dealer.Stay) { Console.WriteLine("Dealer is staying"); } if (Dealer.isBusted) { Console.WriteLine("Dealer busted"); //if dealer busted, give players their winnings foreach (KeyValuePair <player, int> entry in Bets) //loop through dictionary again { Console.WriteLine("{0} won {1}", entry.Key.Name, entry.Value); //player won x amount, access values in keyvaluepair with entry. Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); //use LAMBDA expression to get list of players where their name = name in the dictionary, giving them their money back + their winnings Dealer.Balance -= entry.Value; } return; //ends round } foreach (player player in Players) //if no one busts, next, you want to compare the dealers hand to the players hand, whoever has the closest to 21 wins. or if they tie they push { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); //bool? can actually be set to null even though its a struct, useful when there are three options (dealer wins, player wins, they tie if (playerWon == null) { Console.WriteLine("Push! No one wins."); player.Balance += Bets[player]; //gets bet money back } else if (playerWon == true) { Console.WriteLine("{0} won {1}!", player.Name, Bets[player]); player.Balance += (Bets[player] * 2); //if player wins, they earn back their bets + their winnings Dealer.Balance -= Bets[player]; //subtract winnings from dealers balance } 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; } } }
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(); 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; i < 2; i++) { Console.WriteLine("Dealing..."); foreach (Player player in Players) { Console.WriteLine("{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.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.Write("{0} ", card.ToString()); } Console.WriteLine("\n\n Hit 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; return; } else { player.isActivelyPlaying = false; return; } } } } Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); while (!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]; } 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; } } }
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(); Console.WriteLine("Place your bet!"); foreach (Player player in Players) { int bet = Convert.ToInt32(Console.ReadLine()); bool successfullyBet = player.Bet(bet); if (!successfullyBet) //same as successfullyBet == false { return; //this ends the method } //if the bet fails it ends the method, but if the bet is successful, we don't need code for that. //tracking bets - dictionary is a collection of key value pairs (key = player and the value = their bet) //so if we create a dictionary object of all the bets, so when amount is needed, look up name and see what was bet //dictionary object will be created in Game.cs as all games have bets 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) { //business logic layer - checking for blackjack on deal 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.Write("Dealer: "); Dealer.Deal(Dealer.Hand); if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); if (blackJack) { Console.WriteLine("Dealer has BlackJack! All players lose!"); 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 = TwentyOneRules.IsBusted(player.Hand); if (busted) { Dealer.Balance += Bets[player]; Console.WriteLine("{0} Busted! You ose 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 == "y" || answer == "yep" || answer == "yeah" || answer == "ya") { player.isActivelyPlaying = true; return; } else { player.isActivelyPlaying = false; return; } } } } // DEALERS CODE 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; } //Compare hands code 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 == "y" || answer == "yep" || answer == "yeah" || answer == "ya") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
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(); Console.WriteLine("Place your bet! Big money!"); 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; 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("Black Jack! {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 Black Jack! Ya'll lost! Suck on that!"); 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\nWould you prefer to hit or stay?"); string answer = Console.ReadLine().ToLower(); if (answer == "stay" || answer == "s" || answer == "saty") { player.Stay = true; break; } else if (answer == "hit" || answer == "h" || answer == "hti" || answer == "iht") { 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("Wanna go again?"); answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || 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("Dealer HIT!"); Dealer.Deal(Dealer.Hand); Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); } if (Dealer.Stay) { Console.WriteLine("Dealer STAY!"); } if (Dealer.isBusted) { Console.WriteLine("Dealer BUST!"); foreach (KeyValuePair <Player, int> entry in Bets) { Console.WriteLine("{0} wont {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 winner."); 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("Wanna go again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || answer == "yup" || answer == "ya") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
public override void Play() /* have to use the override keyword because it is an inherited * abstract method from the Game Class */ { Dealer = new TwentyOneDealer(); foreach (Player player in Players) { player.Hand = new List <Card>(); player.Stay = false; /* because if it was true at the end of a previous game we would * not want that value to carry over into the beginning of the next * game and effect game play */ } Dealer.Hand = new List <Card>(); Dealer.Stay = false; Dealer.Deck = new Deck(); Dealer.Deck.Shuffle(); Dictionary <Player, int> Bets = new Dictionary <Player, int>(); foreach (Player player in Players) { Console.WriteLine("Place your bet:"); int bet = Convert.ToInt32(Console.ReadLine()); bool successfullyBet = player.Bet(bet); //passing the "bet"(amount) into the "Bet" method (found in Player Class) if (!successfullyBet) // this says if the bet is not successful (or false instead of true) { return; // this just ends the method if it is not successful } // no need for an else statement here just move on to the next step in dealing with the bets for each player Bets.Add(player, bet); player.PlayerBet = bet; // added Entry into our Bets Dictionary ( in Game class) to keep track of each players bet per hand of TwentyOne ListPlayers(); } for (int i = 0; i < 2; i++) { Console.WriteLine("Dealing..."); foreach (Player player in Players) { Console.Write("{0}: ", player.Name); // using Console.Write (instead of WriteLine because we don't want a new line) Dealer.Deal(player.Hand); // to say each players name as they are getting delt their cards if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); if (blackJack) { Console.WriteLine("Blackjack! {0} wins {1}", player.Name, Bets[player]); // announce winner player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]); //pay winning players the amount they bet PLUS the bet amount times 1.5 return; //ends the round of play } } 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]; 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); /* Lambda Expression: get the List of Players..WHERE(where produces a list) that player's name == the * entry Name in the Dictionary and the balance amount that goes with that name. * The list we are grabbing is the First() List then we add .Balance to get the * amount and we are adding to the balance amount the entry amount times 2 */ Dealer.Balance -= entry.Value; Dealer.Balance -= entry.Value; return; } } } foreach (Player player in Players) //booleans are structs : they can be true , false , or null. { 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; } } } }
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.Shuffle(Dealer.Deck); Console.WriteLine("Place Your Bet"); foreach (Player player in Players) { int bet = Convert.ToInt32(Console.ReadLine()); if (bet < 0) { throw new FraudException("Security Kick This Person out"); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { return; } Bets[player] = bet; player.Balance -= 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 && TwentyOneRules.CheckForBlackJack(player.Hand)) { 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 && TwentyOneRules.CheckForBlackJack(Dealer.Hand)) { Console.WriteLine("Dealer got blackjack everyone loses"); foreach (KeyValuePair <Player, int> entry in Bets) { Dealer.Balance += entry.Value; } } } foreach (Player player in Players) { while (!player.stay) { Console.WriteLine("\n\nDealer's Cards"); foreach (Card card in Dealer.Hand) { Console.WriteLine(card.ToString()); } Console.WriteLine("\n\nYour Cards"); foreach (Card card in player.Hand) { Console.WriteLine(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 continue playing?"); answer = Console.ReadLine(); if (answer == "yes") { player.isActivelyPlaying = true; return; } else { player.isActivelyPlaying = false; return; } } } } Dealer.IsBusted = TwentyOneRules.isBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldStay(Dealer.Hand); while (!Dealer.IsBusted && !Dealer.Stay) { Console.WriteLine("Dealer is hitting"); Dealer.Deal(Dealer.Hand); Console.WriteLine("\n\nDealer's Cards"); foreach (Card card in Dealer.Hand) { Console.WriteLine(card.ToString()); } Dealer.IsBusted = TwentyOneRules.isBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldStay(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("No one wins. Its a push"); player.Balance += Bets[player]; } else if (playerWon == true) { Console.WriteLine("{0} won {1}", player.Name, Bets[player]); player.Balance += Bets[player] * 2; } else { Console.WriteLine("Dealer Wins"); Dealer.Balance += Bets[player]; } Console.WriteLine("Your new balance is {0}", player.Balance); Console.WriteLine("Play again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
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(); Console.WriteLine("Place your bet."); foreach (Player player in Players) { int bet = Convert.ToInt32(Console.ReadLine()); bool successfullyBet = player.Bet(bet); if (!successfullyBet) { return; //end this method - it will go back to the while loop in main method & go back to the play method } Bets[player] = bet; } for (int i = 0; i < 2; i++) { Console.WriteLine("Dealing..."); foreach (Player player in Players) { //.write = the thing that comes after this won't be on a new line Console.Write("{0}: ", player.Name); Dealer.Deal(player.Hand); if (i == 1) { //twentyonerules = business logic layer exists without knowledge of other classes. storage class for helper methods 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]); //ending round - once the player gets blackjack, the round is over and we return to the beginning 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.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; 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("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) { //turn boolean into nullable value win/lose/tie 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; } } }