public override void Play() //abstract method inherited from Game { Dealer = new TwentyOneDealer(); foreach (Player player in Players) //Players is a property of the Game class { //at the beginning of the play method, we want to set everything fresh again player.Hand = new List <Card>(); //fresh hand player.Stay = false; //not staying because it's a new game } Dealer.Hand = new List <Card>(); //fresh hand for the dealer Dealer.Stay = false; // not staying because its a new game Dealer.Deck = new Deck(); //fresh deck for the dealer too Dealer.Deck.Shuffle(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security! Kick this person out!"); } bool successfullyBet = player.Bet(bet); //passing in the amount they bet into the Bet method if (!successfullyBet) { return; //this doesn't return anything because Play() is a void method, but it ends the method } 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); //passing in the player's hand, then the hand is given a card, then that card is printed to the console. if (i == 1) //if it's the second turn { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); //pass in the player's hand to check it for blackjack if (blackJack) { Console.WriteLine("Blackjack! {0} wins {1}", player.Name, Bets[player]); player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]); //player gets their bet back plus 1.5 the same amount return; } } } Console.Write("Dealer: "); //lets the user know it's the dealer's turn Dealer.Deal(Dealer.Hand); //deal a hand to the dealer if (i == 1) //if it's the second turn { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); //check the Dealer's hand for blackjack 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); //pass in the player's hand to see if they busted 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; //the Play() method only runs while IsActivelyPlaying is 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); //use entry.Key to access properties of the key Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); //this give everyone their bet back plus the same amount. Where() produced a list, First() grabs the first value Dealer.Balance -= entry.Value; //takes each person's bet away from the dealer } return; } foreach (Player player in Players) //loop through each player and compare their hand to the dealer's { 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" || answer == "y") { 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) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("Place your bet!"); validAnswer = int.TryParse(Console.ReadLine(), out bet); if (!validAnswer) { Console.WriteLine("Please enter digits only, no decimals"); } } if (bet < 10) { throw new FraudExceptions(); } 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); //bc CheckForBackJac is a static method, you have to preface it with TwentyOneRules (where it lives) 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\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 you bet of {1}. Your blance 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.isActivielyPlaying = true; return; } else { player.isActivielyPlaying = 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("Dealder is staying."); } if (Dealer.isBusted) { Console.WriteLine("Dealer is Busted"); foreach (KeyValuePair <Player, int> entry in Bets) { Console.WriteLine("{0} won {1}!", entry.Key.Name, entry.Value); //accessing keyvalue pairs Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); //Lambda. player in the player list. where produces a list Dealer.Balance -= entry.Value; } return; } foreach (Player player in Players) { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); //changes bool data type into a null datatype 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.isActivielyPlaying = true; } else { player.isActivielyPlaying = 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(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security! Kick this person out!"); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) //this is same as (sucessfullyBet == false) { 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("Blackjack! {0} wins {1}", player.Name, Bets[player]); player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]); //Bets.Remove(player); //so if playing game with mult ppl you would need to remove the player from the list so he does not get paid at the end of the round too. For purpose of video they chose to end the play method if someone gets blackjack and thus deleted this line. 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; } return; } } } foreach (Player player in Players) { while (!player.Stay) { Console.WriteLine("Your cards: "); 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.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; Console.WriteLine("Do you want to play again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah") { entry.Key.isActivelyPlaying = true; return; } else { entry.Key.isActivelyPlaying = false; 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 += (2 * Bets[player]); 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() // one hand one 21 { Dealer = new TwentyOneDealer(); //instantiate a new dealer foreach (Player player in Players) { player.Hand = new List <Card>(); // new deck player.Stay = false; } Dealer.Hand = new List <Card>(); // new deck 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("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("Security! Kick this person out!"); } bool successfullyBet = player.Bet(bet); //pass in the amount they bet into the bet method if (!successfullyBet) // if succfully bet is false same as succesfullybet ==false { return; // not returning anything but means end this method and goes back to while loop (play method) } Bets[player] = bet; //Bets, which rep the dictionary with the player is the key, and the 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); //passing in the players hand and given a card and that card then given to the console if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); // since this is a static method, we have to prefance it with class name if (blackJack) { Console.WriteLine("Blackjack! {0} wins {1}", player.Name, Bets[player]); player.Balance += Convert.ToInt32((Bets[player] * 1.5 + Bets[player])); //give bet back plus the same amount if win 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); // will return true or false 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; } } } } 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); // where produes a list and take the playesr balance and add player *2 Dealer.Balance -= entry.Value; } return; } foreach (Player player in Players) { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); // this basically says now this bool can have a value null 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; } } }
// Since Play() is an abstract method from Game, // you must use the override keyword to implement public override void Play() { // With every new game create a dealer Dealer = new TwentyOneDealer(); foreach (Player player in Players) // Players is a prop of Game which holds a list of each Player { player.Hand = new List <Card>(); // Reset players hands to empty player.Stay = false; // Reset stay == false because it could be true at the end of the game } Dealer.Hand = new List <Card>(); // Reset dealer hand to empty Dealer.Stay = false; Dealer.Deck = new Deck(); // Use a brand new deck of cards Dealer.Deck.Shuffle(); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.Write("\nPlace your bet: "); Console.ResetColor(); // Loop through each player and have them place a bet foreach (Player player in Players) // Players is a prop of Game which holds a list of each Player { int bet = Convert.ToInt32(Console.ReadLine()); bool successfullyBet = player.Bet(bet); // Passing in bet amount into the Bet method on Player if (!successfullyBet) { return; // This will end the Play() and start the While loop in Main() and see if player is still active and have enough money } Bets[player] = bet; // Dictionary.. setting the key to player and the value to the amount they bet } for (int i = 0; i < 2; i++) // Only dealing 2 cards to each player { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("\nDEALING..."); Console.ResetColor(); foreach (Player player in Players) { Console.ForegroundColor = ConsoleColor.Cyan; Console.Write($"{player.Name}: "); // Stating who got the card Console.ResetColor(); Dealer.Deal(player.Hand); // Passing in players hand and giving it a card if (i == 1) // This is the second card that's dealt { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); // Passing in players hand to check for blackjack if (blackJack) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"Blackjack! {player.Name} wins {Bets[player]} "); Console.ResetColor(); player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]); // If you win 21, you get your bet * 1.5 plus your original bet amount return; } } } Console.ForegroundColor = ConsoleColor.Magenta; Console.Write("Dealer: "); Console.ResetColor(); Dealer.Deal(Dealer.Hand); if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); if (blackJack) { Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("Dealer has BlackJack! Everyone loses!"); Console.ResetColor(); foreach (KeyValuePair <Player, int> entry in Bets) // If dealer has 21, go through each player and get all their bets { Dealer.Balance += entry.Value; // put bets into dealer balance } return; } } } // Ask each player if they want to hit or stay foreach (Player player in Players) { while (!player.Stay) { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Your cards are:"); Console.ResetColor(); foreach (Card card in player.Hand) { Console.WriteLine(card.ToString()); // This is overriden in Card class } Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("\nHit or Stay? "); Console.ResetColor(); string answer = Console.ReadLine().ToLower(); if (answer == "stay" || answer == "s") { player.Stay = true; break; // If they say stay, everything stops inside the while loop } else if (answer == "hit" || answer == "h") { Dealer.Deal(player.Hand); } bool busted = TwentyOneRules.IsBusted(player.Hand); if (busted) { Console.WriteLine(); Dealer.Balance += Bets[player]; Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine($"{player.Name} Busted!"); Console.ResetColor(); Console.WriteLine($"Dealer had {TwentyOneRules.DealerCardValue(Dealer.Hand)} and {player.Name} had {TwentyOneRules.PlayerCardValue(player.Hand)}"); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"You lose your bet of {Bets[player]}. Your balance is now {player.Balance}"); Console.ResetColor(); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("\n\nDo you want to play again? "); Console.ResetColor(); answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || answer == "ya" || answer == "yea") { 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.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("\nDealer is hitting..."); Console.ResetColor(); Dealer.Deal(Dealer.Hand); Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); } if (Dealer.Stay) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("\nDealer is staying.\n"); Console.ResetColor(); } if (Dealer.isBusted) { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("\nDealer Busted!"); Console.ResetColor(); foreach (Player player in Players) { player.Balance += Convert.ToInt32(Bets[player] * 2); Dealer.Balance -= Bets[player]; Console.WriteLine($"Dealer had {TwentyOneRules.DealerCardValue(Dealer.Hand)} and {player.Name} had {TwentyOneRules.PlayerCardValue(player.Hand)}"); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"{player.Name} won {Bets[player]} and now has a balance of {player.Balance}"); Console.ResetColor(); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("\n\nKeep playing? "); Console.ResetColor(); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || answer == "ya" || answer == "yea") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } return; } foreach (Player player in Players) { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); if (playerWon == null) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Push! No one wins."); Console.ResetColor(); Console.WriteLine($"Dealer had {TwentyOneRules.DealerCardValue(Dealer.Hand)} and {player.Name} had {TwentyOneRules.PlayerCardValue(player.Hand)}"); player.Balance += Bets[player]; // Giving players their money back Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"{player.Name} still has a balance of {player.Balance}\n\n"); Console.ResetColor(); } else if (playerWon == true) { player.Balance += (Bets[player] * 2); Dealer.Balance -= Bets[player]; Console.WriteLine($"Dealer had {TwentyOneRules.DealerCardValue(Dealer.Hand)} and {player.Name} had {TwentyOneRules.PlayerCardValue(player.Hand)}"); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"{player.Name} won {Bets[player]} and now has a balance of {player.Balance}\n\n"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine($"Dealer wins {Bets[player]}"); Console.ResetColor(); Console.WriteLine($"Dealer had {TwentyOneRules.DealerCardValue(Dealer.Hand)} and {player.Name} had {TwentyOneRules.PlayerCardValue(player.Hand)}"); Dealer.Balance += Bets[player]; Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"{player.Name} now has a balance of {player.Balance}\n\n"); Console.ResetColor(); } Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("Play again? "); Console.ResetColor(); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || answer == "ya" || answer == "yea") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
// This fires from the "Main" method. public override void Play() {// This is ran per each individual hand! // Abstantiated a new "dealer" specific to the twenty one type game. Dealer = new TwentyOneDealer(); //Loops through players in "Player" list, as there could be multiple players. foreach (Player player in Players) { player.Hand = new List <Card>(); player.Stay = false; } // Refreshes deck and hand per each round. 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("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("Security! Kick this person out!"); } // Passing in amount entered in "Player" bet method. bool successfullyBet = player.Bet(bet); //if (successfullyBet == false) the same as below, but better to use shorthand option! if (!successfullyBet) { // Even though this is a void method this is telling the program to "end" this particular method & will go back to the top. return; } // If true, instead of putting an "else" statement this will fire. // A data type "dictionary" holds a collection of "key value pairs". // Added dictionary entry! Bets[player] = bet; } for (int i = 0; i < 2; i++) { Console.WriteLine("\n\nDealing..."); foreach (Player player in Players) { Console.Write("{0}: ", player.Name); Dealer.Deal(player.Hand); if (i == 1) // Means second turn. { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); if (blackJack) { Console.WriteLine("\n\n\nBlackjack! {0} wins {1}", player.Name, Bets[player]); // Bets[player] retrieves the original amount bet. player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]); // Adds 1.5x + original bet back! return; } } } Console.Write("Dealer: "); Dealer.Deal(Dealer.Hand); if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); if (blackJack) { Console.WriteLine("\n\n\nDealer has BlackJack! Everyone loses!"); foreach (KeyValuePair <Player, int> entry in Bets) { Dealer.Balance += entry.Value; // Adds entered bet values from everyone and gives it to the dealers balance. } return; } } } // Goes through each player in the list, and ask hit or stay, until they say "stay". foreach (Player player in Players) { while (!player.Stay)// While player "is not" staying { Console.WriteLine("\nYour cards are: "); foreach (Card card in player.Hand) { Console.Write("{0} ", card.ToString()); } Console.WriteLine("\n \nHit or stay?\n"); string answer = Console.ReadLine().ToLower(); if (answer == "stay") { player.Stay = true;// Ends "while" loop. break; } else if (answer == "hit") { Dealer.Deal(player.Hand); } // This portion will check to see whether or not player has "busted". bool busted = TwentyOneRules.IsBusted(player.Hand); // Custom method to check "busted" status. if (busted) // Boolean value, if true the code below is fired off! { Dealer.Balance += Bets[player]; // Adds players money to the dealers balance. Console.WriteLine("\n\n{0} Busted! You lose your bet of {1}. Your balance is now {2}.", player.Name, Bets[player], player.Balance); Console.WriteLine("\nDo you want to play again?"); answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah") // Just in case player were to answer with "yeah", could be implemented above also. { player.isActivelyPlaying = true; return; // Ends void function, must have it or it wont end program, same thing on the below. } else { player.isActivelyPlaying = false; // In the "main" program it will only operate with a "true" value for "isActivelyPlaying". return; //Extra side note, I think it would be advantagous to add a more defined option here, as a player would be upset if they //accidently terminated their game due to an invalid response. } } } } Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); // Sends dealers hand to "rules" class to verify to continue. Returns a bool value. while (!Dealer.Stay && !Dealer.isBusted) // As long as dealer is not busted or not staying the below code will fire. { 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."); } if (Dealer.isBusted) { Console.WriteLine("\nDealer Busted!"); foreach (KeyValuePair <Player, int> entry in Bets) // Pays out users winnings due to dealer bust. For every pair it will print to console. { Console.WriteLine("\n{0} won {1}!", entry.Key.Name, entry.Value); // Accessess "bets" table by using the "entry" keyword. Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); // Lambda expression... For some reason threw an error at first, rewritten exactly the same & fine. // Loops through each key value pair, "where" produces a list, and pays out winner. Dealer.Balance -= entry.Value; } return; } foreach (Player player in Players) { bool?playerWon = TwentyOneRules.CompareHand(player.Hand, Dealer.Hand); //Creates a nullable boolean value, allowing 3 assignable values. if (playerWon == null) { Console.WriteLine("\nPush! No one wins."); player.Balance += Bets[player]; } else if (playerWon == true) { Console.WriteLine("\n{0} won {1}!", player.Name, Bets[player]); player.Balance += (Bets[player] * 2); player.Balance -= Bets[player]; } else { Console.WriteLine("\n\nDealer wins {0}!", Bets[player]); Dealer.Balance += Bets[player]; } Console.WriteLine("\n\nPlay again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
public override void Play() //use override in method when inheriting abstract class { Dealer = new TwentyOneDealer(); //new method foreach (Player player in Players) //Players is a property of game class { player.Hand = new List <Card>(); // each new game, players hand is discarded player.Stay = false; } Dealer.Hand = new List <Card>(); // dealer hand is discarded Dealer.Stay = false; Dealer.Deck = new Deck(); //each new game a new deck will be created to insured there is 52 cards, otherwise there is a partial deck for the next game Dealer.Deck.Shuffle(); //shuffles deck for each game foreach (Player player in Players) //loops through each player placing a bet { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security! Kick this person out."); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { return; } Bets[player] = bet; } for (int i = 0; i < 2; i++) //cards are face up, loops through twice giving each player 2 cards { 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 wins player.Balance += Convert.ToInt32(Bets[player] * 1.5 + Bets[player]); //player wins 1/2 times the amount bet + player's bet Bets.Remove(player); //removes player from game if dealer bust so player doesn't get paid twice return; //round is over when player wins blackjack } } } 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!"); //dealer gains all bets from player when dealer wins foreach (KeyValuePair <Player, int> entry in Bets) //iterates through dictionary { Dealer.Balance += entry.Value; //assigns balance to everything } return; } } } foreach (Player player in Players) //goes through each player to check if they want to stay or hit { while (!player.Stay) //while not staying { 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; //stops while loop } else if (answer == "hit") { Dealer.Deal(player.Hand);//prints card to console to player } bool busted = TwentyOneRules.Isbusted(player.Hand); if (busted) { Dealer.Balance += Bets[player]; // all bets from players goes to dealer balance 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?"); //if player bust, ask to play again answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah") { player.isActivelyPlaying = true; return; //ends void function } else { player.isActivelyPlaying = false; return; //ends void function } } } } //dealer doesn't get any card until all the players get their cards and if they bust before the dealer they lose Dealer.isBusted = TwentyOneRules.Isbusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); while (!Dealer.Stay && !Dealer.isBusted)//continue to keep giving cards to the dealer { Console.WriteLine("Dealer is hitting..."); Dealer.Deal(Dealer.Hand); Dealer.isBusted = TwentyOneRules.Isbusted(Dealer.Hand); //checks if busted then breaks Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); // checks if staying then breaks } if (Dealer.Stay) { Console.WriteLine("Dealer is staying."); } if (Dealer.isBusted) { Console.WriteLine("Dealer busted!"); foreach (KeyValuePair <Player, int> entry in Bets)//if dealer is busted, all player is given their winnings { Console.WriteLine("{0} won {1}!", entry.Key.Name, entry.Value); Players.Where(x => x.Name == entry.Key.Name).First().Balance = +(entry.Value * 2);// where(word) produces a list //list of players . where (their name == the name in the dictionary ). grabs the balance of the person() and += what they bet * 2 //takes their balance and add what they get * 2 //loops through each key value pair and finds the player in the player's list that matches that key value pair Dealer.Balance -= entry.Value; //subtracts the bet from the dealer } return; } //compares player hand to dealer's hand foreach (Player player in Players) { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); //bool now can take the value null if (playerWon == null) { Console.WriteLine("Push! No one wins."); player.Balance += Bets[player];//player gets their bets back if null(tie) Bets.Remove(player); } else if (playerWon == true) { Console.WriteLine("{0} won {1}!", player.Name, Bets[player]); //prints player name and the amount they won player.Balance += (Bets[player] * 2); //player gets their bet back *(times) 2 Dealer.Balance -= Bets[player]; //subtract from dealer balance } else { Console.WriteLine("Dealer wins {0}!", Bets[player]); Dealer.Balance += Bets[player]; //dealer wins all player bets and adds to balance } Console.WriteLine("Play again?"); //loops through if player won and ask for a new game string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
public override void Play() { //implemented Play method, create new dealer and loop players and reset hand 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(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Securtiy get this person information, take their photo and kick them out!!!"); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { //return ends the method if it fails return; } Bets[player] = bet; } //creating for loop to deal 2 cards and foreach loop to deal to each player with name printed. 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} wine {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! Game Over!"); 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 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 == "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("The Dealer Busted, Players win."); foreach (KeyValuePair <Player, int> entry in Bets) { Console.WriteLine("{0} won {1}!", entry.Key.Name, entry.Value); //lambda expression to add the player winning to their balance Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); Dealer.Balance -= entry.Value; } return; } foreach (Player player in Players) { //forced boolean to accept null to compare player and dealer hands 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(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("Place your bet!"); validAnswer = int.TryParse(Console.ReadLine(), out bet); if (!validAnswer) { Console.WriteLine("Please use only use digits, do not use decimals."); // added this while loop on 3/29 (validation) } } if (bet < 0) { throw new FraudException("Security! Kick this person out."); // added on 3/29 to make validation for the game.play(); making sure the amount given isnt less than 0. } //updated on 3/30 to display message so we can use ex.message in main program 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("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.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" || answer == "y") { 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) { 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" || answer == "y") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
public override void Play() { //wipe players who opted out from player list List <Player> removalList = new List <Player>(); removalList = Players.Where(x => !x.IsActive).ToList(); foreach (Player player in removalList) { Players.Remove(player); Bets.Remove(player); } if (Players.Count == 0) { return; } 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); foreach (Player player in Players) { Console.WriteLine("{0}, you have {1} in the bank.", player.Name, player.Balance); bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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; } 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) { int blackJackPayout = Convert.ToInt32(1.5 * Bets[player]); Console.WriteLine("Blackjack! {0} wins {1}", player.Name, blackJackPayout); player.Balance += Convert.ToInt32(((Bets[player]) * 1.5) + Bets[player]); Console.WriteLine("{0} has a balance of: {1}", player.Name, player.Balance); foreach (Player blackjackplayer in Players) { playAgain(blackjackplayer); } 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) { playAgain(player); } return; } } } foreach (Player player in Players) { while (!player.Stay && player.IsActive && !TwentyOneRules.IsBusted(player.Hand)) { Console.WriteLine("{0}, your cards are:", player.Name); foreach (Card card in player.Hand) { Console.Write("{0} ", card.ToString()); } Console.WriteLine("\nHit or Stay?"); string answer = Console.ReadLine().ToLower(); if (answer.StartsWith("s")) { player.Stay = true; break; } else if (answer.StartsWith("h")) { Dealer.Deal(player.Hand); } bool busted = TwentyOneRules.IsBusted(player.Hand); if (busted) { Dealer.Balance += Bets[player]; Console.WriteLine("{0} busted! Lost bet of {1}, balance is now {2}", player.Name, Bets[player], player.Balance); playAgain(player); } } } Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); while (!Dealer.Stay && !Dealer.isBusted) { Console.WriteLine("Dealer hits..."); 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; } foreach (Player player in Players) { playAgain(player); } return; } foreach (Player player in Players) { if (!TwentyOneRules.IsBusted(player.Hand)) { 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); Console.WriteLine("{0} has a balance of: {1}", player.Name, player.Balance); Dealer.Balance -= Bets[player]; } else { Console.WriteLine("Dealer wins {0}.", Bets[player]); Dealer.Balance += Bets[player]; } playAgain(player); } else { Console.WriteLine("Dealer wins {0}.", Bets[player]); Dealer.Balance += Bets[player]; } } return; }
//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; } } }
public override void Play() //going to encompass 1 hand in twenty one, after the hand it will ask the user where he/she wants to continue { Dealer = new TwentyOneDealer(); //we name the twentyonedealer "Dealer". foreach (Player player in Players) //allows for multiple players, Players is a list property from the Game class { player.Hand = new List <Card>(); //we want their hand to be blank (reset) player.Stay = false; } Dealer.Hand = new List <Card>(); //we also want the dealers hand to be a new hand, again we want to reset the dealers hand with a new list to ensure nothing is carried over Dealer.Stay = false; Dealer.Deck = new Deck(); //we want to create a new deck. If we didn't do a new deck then we would get a partial deck every round, so each round will end up having an affect on the next round. Dealer.Deck.Shuffle(); foreach (Player player in Players) // Now we iterate through every player in the list Players for their bet { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security! Kick this person out."); } bool successfullyBet = player.Bet(bet); if (successfullyBet == false) { return; //what return does in void doesn't return anythin. We are just saying to end this method. It will go back to the while loop in the main program. If the user is actively playing still (true), then it will hit the PLay() method again and ask for a bet. } Bets[player] = bet; //here we have entered a new entry into the dictionary } for (int i = 0; i < 2; i++) //This form loop is for the dealing of the cards. We will give everyone a card and then give the dealer a card, and then repeat. For simplicity both cards will be face up. { Console.WriteLine("Dealing..."); foreach (Player player in Players) { Console.Write("{0}: ", player.Name); //Console.Write() is so that the next thing will not be on a new line (doesn't automatically press enter). Dealer.Deal(player.Hand); //the method Deal from the class Dealer, is taking in the player's Hand as a parameter. if (i == 1) //asking if it is the second round { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); //we create a boolean for CheckForBlackJack method if (blackJack) //if the boolean is true essentially { Console.WriteLine("Blackjack! {0} wins {1}", player.Name, Bets[player]); //return the value from the Dictionary Bets player.Balance = player.Balance + Convert.ToInt32((Bets[player] * 1.5) + Bets[player]); //Bets[player] is a dictionary property from Game class that takes in player as a parameter return; } } } Console.Write("Dealer: "); Dealer.Deal(Dealer.Hand); if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); if (blackJack) //checking if dealer has a blackjack hand { Console.WriteLine("Dealer has Blackjack! Everyone loses!"); foreach (KeyValuePair <Player, int> entry in Bets) //here we iterate through a dictionary named "Bets" from the class Game, for each dictionary item (player and integer pair), which we will name entry we do the following: { // btw each item in the dictionary "Bet" is given the name entry Dealer.Balance = Dealer.Balance + entry.Value; //here we assign the dealer's balance to everything in the dictionary pair } return; //without this it will ask to hit or stay even if the dealer got blackjack } } } foreach (Player player in Players) //now we begin designing our hit/stay for our players! (we still have to do it for our dealer after) { while (!player.Stay) // same as player.Stay == false??? { 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(); //take in user output and put it in lower case... if (answer == "stay") //if they choose to stay than player.Stay become true, and the loop is broken { player.Stay = true; break; } else if (answer == "hit") { Dealer.Deal(player.Hand); //here the dealer implements from method Deal() from the class Dealer, and makes you oh the player's hand as a parameter } bool busted = TwentyOneRules.IsBusted(player.Hand); //passes in player.Hand as a parameter if (busted) //if busted has a value of True { Dealer.Balance = Dealer.Balance + Bets[player]; //increase dealer balance with bet Console.WriteLine("{0} Busted! You lose your bet of {1}. Your balance is {2}.", player.Name, Bets[player], player.Balance); Console.WriteLine("Do you want to play again?"); answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || answer == "ya") { player.IsActivelyPlaying = true; return; } else { player.IsActivelyPlaying = false; //ends game! return; //return will end the void function } } } } Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); //now we do everything for the dealers side. Here we check to see whether the Dealer's hand is busted. Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); //ShouldDealerStay is another custome rule/methos we must create that is very specific to Twenty One. Dealer.Stay is a boolean property within the TwentyOneDealerClass the inherits fromt the dealer class while (!Dealer.Stay && !Dealer.isBusted) //this states, while dealer is not staying (false) AND has not busted (fasse) { Console.WriteLine("Dealer is hitting..."); Dealer.Deal(Dealer.Hand); //here the the method "Deal" from the class Dealer is being implemented, taking in the Dealer.Hand as a parameter Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); //with the new Dealer.Hand, because of Dealer.Deal, isBusted is a boolean property that checkts to see if the dealer has busted, and will change to the updated boolean Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); //here we check for the method ShouldDealerStay, and assign to the boolean variable/property "Stay" //if either isbusted or stay is true, then the while loop breaks } if (Dealer.Stay) //if dealer decides to stay (true value) { Console.WriteLine("Dealer is staying."); } if (Dealer.isBusted) // if Dealers.IsBusted == True { Console.WriteLine("Dealer busted!"); foreach (KeyValuePair <Player, int> entry in Bets) //if the dealer busts, all players must get all the winnings, as recorded within the dictionary "Bets" { Console.WriteLine("{0} won {1}", entry.Key.Name, entry.Value); //you access the dictionary values for name and bets by making use of "entry.Key" Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); //Here we make use of a lambda function where x is each element in a list created by "Where". For any elements who's name matches entry,KeyName, the First() method must grab it from the list. Even though there is only one player. It then adds the corresponding value to the balance Dealer.Balance = Dealer.Balance - entry.Value; //this is what happens to the Dealer's balance when the dealer busts } return; } foreach (Player player in Players) //Now we have to build scenerios in which neither the player nor the dealer bust and both have chosen to stay. In this case you have 3 scenerios. Booleans are structs, meaning they are value types and cannot be null. Therefore, you can only have two options with them...(True or false). In this case we make use of a feature in the .Net Framework that allows a boolean to have a null value, and thus have 3 options! { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); if (playerWon == null) { Console.WriteLine("Push! No one wins."); player.Balance = player.Balance + Bets[player]; } else if (playerWon == true) { Console.WriteLine("{0} won {1}!", player.Name, Bets[player]); player.Balance = player.Balance + (Bets[player] * 2); //they get back their bet and winnings Dealer.Balance = Dealer.Balance - Bets[player]; } else { Console.WriteLine("Dealer wins {0}", Bets[player]); //since the dealer win, it gets the player balance added to its balance Dealer.Balance = Dealer.Balance + Bets[player]; } Console.WriteLine("Play again?"); //here the round is over so we check to see if the user wants to stay within the game string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || 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(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("You are a dirty cheat. Security! Feed this lowlife to compys..."); } 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("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! To bad. So sad. For you!"); foreach (KeyValuePair <Player, int> entry in Bets) { Dealer.Balance += entry.Value; } foreach (Player player in Players) { Console.WriteLine("Play again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || answer == "ya") { player.isActivelyPlaying = true; return; } else { player.isActivelyPlaying = false; 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! So sad. 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" || answer == "y" || 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 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; } foreach (Player player in Players) { Console.WriteLine("Play again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || answer == "ya") { player.isActivelyPlaying = true; return; } else { player.isActivelyPlaying = false; return; } } //After dealer busts it does not give the play an option to choose to play again, it goes straight to placing bets. } 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" || answer == "y" || answer == "ya") { player.isActivelyPlaying = true; return; } else { player.isActivelyPlaying = false; return; } } }
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(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security! Kick this person out."); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { // End this loop. return; } // Game has Bets Dictionary player is key and value is int bet Bets[player] = bet; } for (int i = 0; i < 2; i++) { Console.WriteLine("Dealing..."); foreach (Player player in Players) { Console.Write($"{player.Name}: "); Dealer.Deal(player.Hand); if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); if (blackJack) { Console.WriteLine($"Blackjack! {player.Name} wins {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(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($"{player.Name} Busted! You lose your bet of {Bets[player]}. Your balance is now {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($"{entry.Key.Name} won {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($"{player.Name} won {Bets[player]}!"); player.Balance += (Bets[player] * 2); Dealer.Balance -= Bets[player]; } else { Console.WriteLine($"Dealer wins {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() //added override to use inherited abstract method "Play()" from Game class { 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(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security! Kick this person out."); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { return; //in void method(no returns), this only means "end this method" } Bets[player] = bet; //added dictionary info *see property in game class } for (int i = 0; i < 2; i++) { Console.WriteLine("Dealing..."); foreach (Player player in Players) { Console.Write("{0}: ", player.Name); //What comes next won't be on new line(Write();) Enter not automatically entered 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\nHit or Stay"); string answer = Console.ReadLine().ToLower(); if (answer == "stay" || answer == "s") { player.Stay = true; break; } else if (answer == "hit" || answer == "h") { 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 == "y" || 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) { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); //bool? now lets boolean return a third value = null 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 == "yeah") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
public override void Play() //override added for abstract method from 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(); Dealer.Deck.Shuffle(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security! Kick this person out."); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { return; //return in a void method ends method; does not return anything } 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) //second turn check to see if blackjack { 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; } return; } } } foreach (Player player in Players) { while (!player.Stay) { Console.WriteLine("You 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" || answer == "y" || answer == "ya") { player.isActivelyPlaying = true; } 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 is busted!"); foreach (KeyValuePair <Player, int> entry in Bets) { Console.WriteLine("{0} won {1}!", entry.Key.Name, entry.Value); //use entry. to access Key:Value pair 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); //bool? means bool can be null 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" || answer == "y" || 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(); 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.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); 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?"); Console.WriteLine("Play again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } Console.WriteLine("Play again?"); Console.WriteLine("Play again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah") { } }
public override void Play() { Dealer = new TwentyOneDealer(); // Create a dealer foreach (Player player in Players) // initialize players { player.Hand = new List <Card>(); // Give each player a new empty hand player.Stay = false; // and reset stay (false) } Dealer.Hand = new List <Card>(); // Dealer gets an empty hand. Dealer.Stay = false; Dealer.Deck = new Deck(); // Refresh the deck every single round Dealer.Deck.Shuffle(); foreach (Player player in Players) // protects program to add players in the future { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.Write("\nPlace your bet!\n>> "); validAnswer = int.TryParse(Console.ReadLine(), out bet); if (!validAnswer) { Console.WriteLine("Please enter only digits with no decimal"); } } if (bet < 0) { Console.WriteLine(); throw new FraudException("Security! Kick this person out for cheating!"); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) // not enough in bank to cover the bet { return; // end this method. It is over!! } Bets[player] = bet; } // Deal a hand to each player (2 cards) and the Dealer for (int i = 0; i < 2; i++) { Console.WriteLine("\nDealing..."); var BlackJacks = Players.Where(x => TwentyOneRules.CheckForBlackJack(x.Hand)).ToList(); //Players.OrderByDescending(x => x.GetCardValueSum()) foreach (Player player in Players) { Console.Write("{0}: ", player.Name); Dealer.Deal(player.Hand); if (i == 1) // if second card in initial deal { bool blackJack = TwentyOneRules.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; } } } // Deal a card to the Dealer Console.Write("Dealer: "); Dealer.Deal(Dealer.Hand); if (i == 1) // if second card in initial deal { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); if (blackJack) { Console.WriteLine("Dealer has BlackJack! Everyone loses!"); // Add everybody's bet to the Dealer balance foreach (KeyValuePair <Player, int> entry in Bets) { Dealer.Balance += entry.Value; } return; } } } // end of initial deal of two cards foreach (Player player in Players) { while (!player.Stay) { Console.WriteLine("\nYour cards are:"); foreach (Card card in player.Hand) { Console.Write("{0} ", card.ToString()); // using a custom To.String we overrode in Card class } 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); // deal one card } 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("\nDo you want to play again?"); answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "ya" || answer == "y") { player.IsActivelyPlaying = true; return; // to main() } else { player.IsActivelyPlaying = false; return; // to main() } } } } Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); // as long as dealer is not busted and does not want to stay, keep playing while (!Dealer.isBusted && !Dealer.Stay) { 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."); } else if (Dealer.isBusted) { Console.WriteLine("\nDealer is busted."); // Give all the players their winnings // loop through each Player and associated Bet // Find players that match that name(where always returns a list, take the first one (only one is ever // returned), and add the winning plus the original bet amount (i.e., bet * 2) to player's balance 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; } // loop through all the players and compare the players' hands to the dealer's hand // 3 possibilities can occur // 1) player could have hand greater than dealer // 2) dealer could have hand greater than player // 3) could have a tie foreach (Player player in Players) { switch (TwentyOneRules.CompareHands(player.Hand, Dealer.Hand)) { case null: break; case true: break; case false: break; } // nullable boolean to allow for 3 choices bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); if (playerWon == null) { Console.WriteLine("Push! No one wins!"); // return bets to players player.Balance += Bets[player]; //Bets.Remove(player); Bets table is reset at the beginning of loop. No need here. } else if (playerWon == true) { var test = $"Hello {1 + 1}"; var test1 = string.Format("Hello {0}", 1 + 1); Console.WriteLine("{0} won {1}!", player.Name, Bets[player]); player.Balance += Bets[player] * 2; // return original bet plus winning amt (original bet) Dealer.Balance -= Bets[player]; } else { Console.WriteLine("Dealer wins{0} ", Bets[player]); Dealer.Balance += Bets[player]; } Console.WriteLine("\nPlay again?"); string answer = Console.ReadLine().ToLower(); var test2 = new List <string> { "yes", "yeah", "ya", "y" }.Contains(answer); player.IsActivelyPlaying = answer == "yes" || answer == "yeah" || answer == "ya" || answer == "y"; if (answer == "yes" || answer == "yeah" || answer == "ya" || answer == "y") { 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(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security kick this person out."); } 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("Blackjack! {0} wins {1}", player.Name, Convert.ToInt32(Bets[player] * 1.5)); player.Balance = Bets[player] + (Bets[player] * (int)1.5); return; } } } Console.Write("Dealer: "); Dealer.Deal(Dealer.Hand); if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); if (blackJack) { Console.WriteLine("Dealer has BlackJack! Suck mangos losers!"); foreach (KeyValuePair <Player, int> entry in Bets) { Dealer.Balance += entry.Value; } return; } } } foreach (Player player in Players) { player.Stay = true; 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 {1} dollars sucka! you got {2} left in your bank", player.Name, Bets[player], player.Balance); Console.WriteLine("Do you want to play again?"); answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || 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 hits"); Dealer.Deal(Dealer.Hand); Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); } if (Dealer.Stay) { Console.WriteLine("Dealer stays"); } if (Dealer.isBusted) { Console.WriteLine("Dealer Busts"); foreach (KeyValuePair <Player, int> entry in Bets) { Console.WriteLine("{0} won {1} dollars!", 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} dollars!", 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" || answer == "yeah" || answer == "y" || answer == "yup") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
public override void Play() // override is how you implement an inherited abstract method. { Dealer = new TwentyOneDealer(); foreach (Player player in Players) { player.Hand = new List <Card>(); player.Stay = false; Console.WriteLine("{0}, your balance is: ${1}", player.Name, player.Balance); } Dealer.Hand = new List <Card>(); Dealer.Deck = new Deck(); Dealer.Deck.Shuffle(5); using (StreamWriter file = new StreamWriter(@"C:\Users\Roo\Desktop\Logs\log.txt", true)) { file.WriteLine("-- New Hand --"); } Console.WriteLine("Place your bet: "); foreach (Player player in Players) { int bet = AnswerChecker.IsInt(Console.ReadLine()); if (bet < 0) { throw new FraudException("Negative bet attempted"); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { return; // This will kick the program out of this loop. It will go back out to the while loop in main() and see activelyplay is yes and balance is above zero then kick the player back into the play method where they will see "place your bet" } 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); // passing dealer's hand into the deal function using (StreamWriter file = new StreamWriter(@"C:\Users\Roo\Desktop\Logs\log.txt", true)) { file.WriteLine(" was dealt to User {0} GUID {1}", player.Name, player.Id); } if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); if (blackJack) { Console.WriteLine("Blackjack! {0} wins {1}", player.Name, (2 * Bets[player])); player.Balance += Convert.ToInt32((2 * Bets[player])); return; } } } Console.Write("Dealer: "); Dealer.Deal(Dealer.Hand); using (StreamWriter file = new StreamWriter(@"C:\Users\Roo\Desktop\Logs\log.txt", true)) { file.WriteLine(" was dealt to the Dealer."); } 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.Write("Your cards are: \n"); foreach (Card card in player.Hand) { Console.Write("{0} \n", card.ToString()); } Console.Write("Your hand total is: "); TwentyOneRules.ShowHandTotal(player.Hand); Console.Write("The Dealer's hand total is: "); TwentyOneRules.ShowHandTotal(Dealer.Hand); 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); using (StreamWriter file = new StreamWriter(@"C:\Users\Roo\Desktop\Logs\log.txt", true)) { file.WriteLine(" was dealt to User {0} GUID {1}", player.Name, player.Id); } } 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); Bets[player] = 0; // I added this to ensure no bets are payed out to players who busted if the dealer busts. IF there were multiple players. Console.WriteLine("Would you like to play again? "); answer = Console.ReadLine(); if (AnswerChecker.IsYes(answer)) // I added another class to ensure user input to y/n questions won't cause errors. { 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("\nThe Dealer is hitting..."); Dealer.Deal(Dealer.Hand); using (StreamWriter file = new StreamWriter(@"C:\Users\Roo\Desktop\Logs\log.txt", true)) { file.WriteLine(" was dealt to the Dealer."); } Dealer.IsBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); Console.WriteLine("The dealers hand is: "); foreach (Card card in Dealer.Hand) { Console.Write("{0} \n", card.ToString()); } Console.Write("The Dealer's hand total is: "); TwentyOneRules.ShowHandTotal(Dealer.Hand); } if (Dealer.Stay) { Console.WriteLine("\nThe Dealer is staying at {0}.", TwentyOneRules.FinalScore(Dealer.Hand)); } if (Dealer.IsBusted) { Console.WriteLine("\nThe Dealer busts with a hand total of {0}!", TwentyOneRules.FinalScore(Dealer.Hand)); foreach (KeyValuePair <Player, int> entry in Bets) { Console.WriteLine("{0} won ${1}", entry.Key.Name, entry.Value); // See line 90. Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); Dealer.Balance -= entry.Value; } foreach (Player player in Players) { Console.WriteLine("\nYour balance is ${0}", player.Balance); Console.WriteLine("\nWould you like to play again?"); string answer = Console.ReadLine(); if (AnswerChecker.IsYes(answer)) { player.IsActivelyPlaying = true; } else { player.IsActivelyPlaying = false; } } return; } foreach (Player player in Players) { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); if (playerWon == null) { Console.WriteLine("It's a push for {0}!", player.Name); player.Balance += Bets[player]; } else if (playerWon == true) { Console.WriteLine("{0} wins ${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("\nYour balance is now: ${0}", player.Balance); Console.WriteLine("\nWould you like to play again?"); string answer = Console.ReadLine(); bool isYes = AnswerChecker.IsYes(answer); if (isYes) { player.IsActivelyPlaying = true; return; } else { player.IsActivelyPlaying = false; return; } } }
public override void Play() { //initialize dealer Dealer = new TwentyOneDealer(); //reset player state (empty list, yet to stay) foreach (Player player in Players) { player.Hand = new List <Card>(); player.Stay = false; } //reset dealer, consider constructor Dealer.Hand = new List <Card>(); Dealer.Stay = false; Dealer.Deck = new Deck(); Dealer.Deck.Shuffle(3); //show player balances //prompt user to submit bet //each player bets, building a dictionary of players and their bets foreach (Player player in Players) { Console.WriteLine("{0} has a balance of: {1}", player.Name, player.Balance); Console.WriteLine("Place your bet."); bool validAnswer = false; int bet = 0; while (!validAnswer) { validAnswer = int.TryParse(Console.ReadLine(), out bet); if (!validAnswer) { Console.WriteLine("Please enter digits only (no decimals)."); } } if (bet < 0) { throw new FraudException("Fraud Detected. Security has been notified."); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { return; } //dict Bets[player] = bet; } //dealing in two rounds for (int i = 0; i < 2; i++) { Console.WriteLine("Dealing...\n\n"); foreach (Player player in Players) { //put their name before their dealt card Console.Write("{0}: ", player.Name); //writeline baked into Deal() Dealer.Deal(player.Hand); //on second round if (i == 1) { //check for blackjack bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); if (blackJack) { //announce winner, add pot to balance Console.WriteLine("BlackJack! {0} wins {1}", player.Name, Bets[player]); player.Balance += Convert.ToInt32((Bets[player] * 1.5) + Bets[player]); return; } } } //dealer self-deals Console.Write("Dealer: "); //writeline baked into Deal() Dealer.Deal(Dealer.Hand); //on dealers second card if (i == 1) { //check for blackjack bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); if (blackJack) { Console.WriteLine("Dealer had BlackJack! Everyone loses!"); foreach (KeyValuePair <Player, int> entry in Bets) //dealer collects all players' 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) { //pause Console.WriteLine("{0} ", card.ToString()); } Console.WriteLine("\nDealers cards are: "); foreach (Card card in Dealer.Hand) { //pause 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("Would you like to play again?"); answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "ya" || answer == "y") { 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("\n\nDealer 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; } //comparing hands (dealer/player) 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]; } //player wants to play again? Console.WriteLine("Play again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "ya" || answer == "y") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
public override void Play() { //NOTE: Now we are going to work on this 'Play()' method, where pretty much everything happens. //NOTE: Here we creating a new dealer. Dealer = new TwentyOneDealer(); //NOTE: We are building this with a list of players in mind however, there will only be one player. // It is good practice just in case we want to add more players the future. foreach (Player player in Players) { //NOTE: Here we are giving a new hand to each player, every time 'Play()' is called. player.Hand = new List <Card>(); //NOTE: Here we are just making sure that that the 'Stay' property is false // for all players at the start of of the game. player.Stay = false; } //NOTE: We also want the dealers hand to have a new hand, every time 'Play()' is called. Dealer.Hand = new List <Card>(); //NOTE: Here we are making sure the dealer's 'Stay' value is 'false' at the start of the game. Dealer.Stay = false; //NOTE: Here we are giving assigning the dealer a new deck of cards. Dealer.Deck = new Deck(); //NOTE: Here we are shuffling the dealer's deck which is being used for the game. Dealer.Deck.Shuffle(); foreach (Player player in Players) { //NOTE: Here it uses the same validation logic as 'Program.cs' under 'TwentyOne' bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("Place your bet!"); validAnswer = int.TryParse(Console.ReadLine(), out bet); if (!validAnswer) { Console.WriteLine("Please enter digits only, no decimals"); } } //NOTE: Here we are checking if the player's bet is negative, and if it is, the user // is probably trying to commit a fraudulent act. if (bet < 0) { //NOTE: Here we are deliberately 'throwing' an exception we created, 'FraudException' which // inherits from the intergated c# 'Exception' class. //NOTE: If the line below gets executed, the try-catch in 'Program.cs' will 'catch' the exception, // and proceed with specific instructions. throw new FraudException("Security! Kick this person out."); //NOTE: Our FraudException class has a 'Constructor Chain Call' so that if the parameter passes in a string, // for e.g. if we had FraudException object, 'ex', 'ex.Message' boue be equal to string, "Security! Kick this person out." } //NOTE: We created a bet method in the 'Player class' which takes in a 'bet' int and //returns a boolean based on the player's balance. bool successfullyBet = player.Bet(bet); if (!successfullyBet) { //NOTE: Even though this is a void method we can still have it retun nothing. // All the line below, 'return;', is doing is stopping the code from // continuing to run. return; } //NOTE: If no 'if (!successfullyBet)' doesn't get hit, the code below happens. //NOTE: Now we gotta figure out how to organize and keep track of which players made // which bet. One of the best ways to do that is by using a dictionary because // dictionaries are made up of a collection of key-value pairs. The key would be // the player, and the value would be the player's bet. Then whoever wins doesn't lose // on their balance (those who lose, will) and gains additional cash. //NOTE: We created a 'Bets' dictionry property in the 'Game' class, which will hold // take in player objects as keys and an int (the player's bet) for the value. //NOTE: Below we have added the 'player' object as the key and assigned the 'bet' value to to // the 'Bets' dictionary. Bets[player] = bet; } //NOTE: In Most 21 games, the dealers cards are both face down or one of them face up. In // ours, we are going to make both face up (just for simplicity) //NOTE: Here we are giving each player a card twice. So all players end up with two on hand. for (int i = 0; i < 2; i++) { //NOTE: In this 'foreach' loop we are checking any players if the have a have a blackjack. Console.WriteLine("Dealing..."); foreach (Player player in Players) { Console.Write("{0}: ", player.Name); //NOTE: What the 'Deal' method does is take in a players hand (which is empty at this point) and // removes one card out of the dealers 'Deck' property (which is a list of cards) in index 0 and // adds it to the player hand. It also displays which card that is on the console. Dealer.Deal(player.Hand); //NOTE: Now we are cheking for a BlackJack (if the players has a hand of 21 with the first 2 cards). //NOTE: Since we are inside of the for loop, i = 1 which means 'as soon as the player has two cards on hand'. if (i == 1) { //NOTE: Now we will call the 'CheckForBlackJack' method in class, 'TwentyOneRules'. We are able to // call the method this way without instantiating 'TwentyOneRules' because the method, 'CheckForBlackJack' // is static (objects of the method's class can't be instantiated). Fun fact, this method is private thus // only 'TwentyOneRules' has access to it, but this class is able to access 'TwentyOneRules' because // 'TwentyOneRules' is public. //NOTE: 'CheckForBlackJack' returns a bool (true or false) and stores it in bool, 'blackJack'. bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); if (blackJack) { //NOTE: If this code was executed, that means bool, 'blackJack' was true. //NOTE: The current 'player' wins 150% of their bet. double balanceWon = Bets[player] * 1.5; Console.WriteLine("BlackJack! {0} wins {1}", player.Name, balanceWon); //NOTE: 150% of their bet plus their bet gets added to the current player's account. player.Balance += Convert.ToInt32(balanceWon + Bets[player]); //NOTE: The current game finishes. return; } } } //NOTE: This happens when blackjack doesn't happen for the player. Console.Write("Dealer: "); //NOTE: Here the dealer is getting a hand from a deck (two cards), they get displyed on the console. Dealer.Deal(Dealer.Hand); //NOTE: This is a similar logic to the player getting a BlackJack, except its the dealer this time. if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); if (blackJack) { Console.WriteLine("Dealer has BlackJack! Everyone loses!"); //NOTE: 'KeyValuePair' is technically a dictionary type. foreach (KeyValuePair <Player, int> entry in Bets) { //NOTE: if we got all the way to this line of the code that means the dealer // got a blackjack an everyone literally loses. //NOTE: The code below is all of the player bets being added to the dealer's // balance. Dealer.Balance += entry.Value; } //NOTE: The current game finishes. return; } } } //NOTE: Here the playes are deciding whether to stay or hit. foreach (Player player in Players) { //NOTE: Player.Stay was set to false earlier while (!player.Stay) { Console.WriteLine("Your cards are: "); foreach (Card card in player.Hand) { //NOTE: Here 'Console.Write' was used instead of 'Console.WriteLine' So that the players // hand can be shown th the console in the same line, iteration after iterarion. 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 isnow {2}.", player.Name, Bets[player], player.Balance); Console.WriteLine("Do you want to play again?"); answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || answer == "ya") { player.isActivelyPlaying = true; //NOTE: The current game finishes. return; } else { player.isActivelyPlaying = false; //NOTE: The current game finishes. 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 stying"); } if (Dealer.isBusted) { Console.WriteLine("Dealer Busted!"); //NOTE: Here where are iterating through keyValuePairs in 'Bets' foreach (KeyValuePair <Player, int> entry in Bets) { Console.WriteLine("{0} won {1}!", entry.Key.Name, entry.Value); //NOTE: In the comment below, I am explaining what this lambda expression is doing. //NOTE: Now we are looking a the 'Players' which is a property of the 'Game' class and // is a list containig 'player' objects. 'Where' will return a List that meets a condition. // The condition is for each player name in 'Players', if it is the same as the current // iteration of a key (specifically the players name) in the 'Bets' dictionary, then // 'First()' takes the first element in that list (Which is the player matched), takes // the 'Balance' of that player, and that balance gets updated. We take what the // player bet ('entry.Value'), and times it 2 then add that to the players balance. //NOTE: This is gonna happen for each bet in the Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); //NOTE: now we are taking the dealer and minus the bet from the delaer. Dealer.Balance -= entry.Value; } //NOTE: This return will end the round. return; } foreach (Player player in Players) { //NOTE: The question mark after bool enables that boolean to be nullable (can hanve a value // equals to null. The reason we would want to do this is because a tie is possible in a // this game, which means if bool, 'PlayerWon' is neither true or false then it must be 'null', // which we can now program for that scenario to signify a tie. 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" || answer == "y" || answer == "ya") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
//This is an abstract method in the Game class. It plays one hand. public override void Play() { //Game has started, now we instantiate the dealer object as a new Twenty One game dealer Dealer = new TwentyOneDealer(); //for each turn, the start of each round of playing where the dealer deals, we want to //reset the players. We want their hand to be blank. foreach (Player player in Players) //Players is a property of the Game class { player.Hand = new List <Card>(); player.Stay = false; //If it was true then it would carry over from the last hand and mess things up. } Dealer.Hand = new List <Card>(); //don't want old cards to stack up Dealer.Stay = false; //If we don't create a new deck each time we would end up with a partial deck each time. //this refreshes the deck each time. Dealer.Deck = new Deck(); Dealer.Deck.Shuffle(); Console.WriteLine("Place your bet!"); //In placing a bet, we want to loop through each player and them place a bet foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security! Kick this person out."); } bool successfullyBet = player.Bet(bet); //Pass the amount the user entered into the bet method if (!successfullyBet) //Same as "if successfullyBet == false" { //Since we are in a void method and can't return anything, we are saying //"end this method" and it goes back to the "While" method in Program, see that //isActivelyPlaying is still there and balance is still greater than zero, so it //will hit the Play method at the top of this class and say "Place your bet!" again. return; } //if "successfullyBet" is true, it just bypasses the above "if" statement //Bets is the dictionary. "player" is the key. With this we add the player to the dictionary. Bets[player] = bet; //we now have the player's bet //We want to deal 2 cards, 1 at a time. } for (int i = 0; i < 2; i++) { Console.WriteLine("Dealing..."); foreach (Player player in Players) { Console.WriteLine("{0}: ", player.Name); Dealer.Deal(player.Hand); //We pass in the player's hand and it's given a card if (i == 1) //Check for BlackJack! { //Passing in the players hand to check it for BlackJack bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); if (blackJack) { //Notify player is a winner and display his winning from the Bets table Console.WriteLine("BlackJack! {0} wins {1}", player.Name, Bets[player]); //Add the amount he won to the balance. Player wins his bet back plus 1.5x his bet as winnings player.Balance += Convert.ToInt32((Bets[player] * 1.5 + Bets[player])); return; //Ends the round right here } } } //Deal the dealers hand Console.Write("Dealer: "); Dealer.Deal(Dealer.Hand); if (i == 1) //Check for BlackJack { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); if (blackJack) { Console.WriteLine("Dealer has BlackJack! Everyone loses!"); //Give the dealer all the bets by iterating through the dictionary foreach (KeyValuePair <Player, int> entry in Bets) { Dealer.Balance += entry.Value; //Assign to the dealer the balance of everything } return; } } } //Each player now get's asked hit or stay until they stay foreach (Player player in Players) { while (!player.Stay) //While player is not staying { Console.WriteLine("Your cards are: "); foreach (Card card in player.Hand) { Console.Write("{0} ", card.ToString()); } Console.WriteLine("\n\nHit or Stay?"); //Gives us two lines for spacing //Convert response to lowercase to make it easier to work with string answer = Console.ReadLine().ToLower(); if (answer == "stay") { player.Stay = true; break; //Ends the loop and "While" loop stops } else if (answer == "hit") { Dealer.Deal(player.Hand); //Gives them a card (prints card to console) } bool busted = TwentyOneRules.IsBusted(player.Hand); //Returns true or false if (busted) { Dealer.Balance += Bets[player]; Console.WriteLine("{0} Busted! You now 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; //End the void function } else { player.isActivelyPlaying = false; return; //End the void function } } } } Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); //If any of this "While" loop is true, then loop breaks while (!Dealer.Stay && !Dealer.isBusted) //As long as dealer is not busted... { Console.WriteLine("Dealer is hitting..."); Dealer.Deal(Dealer.Hand); // Deal dealer a card Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); //Check again if dealer is busted 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) { //The person is in the key value pair and won this amount. Based on the Bets table. Console.WriteLine("{0} won {1}!", entry.Key.Name, entry.Value); //Loop through each key value pair where their name equals the name in the dictionary //using a lambda expression. Where produces a list. Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); Dealer.Balance -= entry.Value; //Subtract bet amount from the dealer } return; //Just ends the round right here. } //Comparing a player's hand to the dealer's hand. foreach (Player player in Players) { //Turns playerone into a nullible boolean. Now has the value of null //If player lost it returns "false. If player wins it returns "true. //If it's a push it returns "null". bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); if (playerWon == null) { Console.WriteLine("Push! No one wins."); player.Balance += Bets[player]; //Give his bet back } else if (playerWon == true) { Console.WriteLine("{0} won {1}!", player.Name, Bets[player]); player.Balance += (Bets[player] * 2); //Player's bet times 2 Dealer.Balance -= Bets[player]; //Subtract winnings from the dealer's 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; } } //we are currently in a While loop (from the Program class), //but don't know what stage we are at, so we want to reset all the players }
public override void Play() //needs override bc inheriting from Games which has an abstract method { 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(Dealer.Deck, out int timesShuffled); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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 Fraud_Exception(); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { return;//in a void method--this just simply says, "end this method" and loops back through 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 = 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; } 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("\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 {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) { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); //makes the boolean able to take a null value 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; } } }
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(Dealer.Deck, 3); 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 || bet < 0) { Console.WriteLine("Please enter Valid Digits without loose change or negative amount"); if (bet < 0) { validAnswer = false; } } } bool sucessfullyBet = player.Bet(bet); if (!sucessfullyBet) { 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("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\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 agian?"); answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yea" || answer == "yeah") { player.IsActive = true; return; } else { player.IsActive = false; return; } } } } Dealer.istBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); while (!Dealer.Stay && !Dealer.istBusted) { Console.WriteLine("Dealer is Hitting..."); Dealer.Deal(Dealer.Hand); Dealer.istBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); } if (Dealer.Stay) { Console.WriteLine("Dealer is staying."); if (Dealer.istBusted) { 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.Write("{0} won {1}!", player.Name, Bets[player]); player.Balance += (Bets[player] * 2); Console.WriteLine(" Your balance is {0}", player.Balance); 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.IsActive = true; } else { player.IsActive = 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(); foreach (Player player in Players) { // Handles Format Exception Console.WriteLine("Current total: {0}", player.Balance); bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security! Kick this person out!"); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { return; // Will end the current Play method, and restart it so the player can place another bet. } Bets[player] = bet; // for each player, record their bets in this Dictionary. } for (int i = 0; i < 2; i++) // Do two times, so each player gets two cards. { Console.WriteLine("Dealing..."); foreach (Player player in Players) { Console.Write("{0}: ", player.Name); Dealer.Deal(player.Hand); if (i == 1) // After player receives second card... { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); // Check for blackjack. 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) // After dealer receives second card... { bool blackJack = TwentyOneRules.CheckForBlackJack(Dealer.Hand); // Check for blackjack. 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: "); // After receiveing two cards, and after hitting, write cards to the console. foreach (Card card in player.Hand) { Console.Write("{0} ", card.ToString()); } Console.WriteLine("\n\nDealer's cards are: "); // Also write the dealer's cards for quick comparison. foreach (Card card in Dealer.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); // Determine if player busted. 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); if (player.Balance == 0) // If new balance is 0, game ends. { player.IsActivelyPlaying = false; return; } Console.WriteLine("Do you want to play again?"); answer = Console.ReadLine().ToLower(); //Case insensitive if (answer == "yes" || answer == "yeah" || answer == "sure" || answer == "ok") { player.IsActivelyPlaying = true; return; } else // any response other than the few above will end the game. { player.IsActivelyPlaying = false; return; } } } } Dealer.isBusted = TwentyOneRules.IsBusted(Dealer.Hand); Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); while (!Dealer.Stay && !Dealer.isBusted) // Dealer only hits if 16 or less. { 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); //? allows structs to have Null value 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]; if (player.Balance == 0) { Console.WriteLine("Your balance is 0."); player.IsActivelyPlaying = false; return; } } 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) //new hand for player { player.Hand = new List <Card>(); //reset players to have a new hand player.Stay = false; //if true the cards could carry over } Dealer.Hand = new List <Card>(); Dealer.Stay = false; Dealer.Deck = new Deck();//refreshes the deck every single round Dealer.Deck.Shuffle(); Console.WriteLine("Place your bet!"); foreach (Player player in Players)//placing bet { 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)//checking for 21 at second turn/card { 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]);//wins bet back times 1.5 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)//looked through dictionary and gave the dealer all the money { 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}. You 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" || answer == "y" || answer == "ya") { player.isActivelyPlaying = true; return; } 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)//finding the player that matches the player name and adds winnings x 2 to balance { 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;//minus from dealer balance } return; } foreach (Player player in Players) //compare player hand to dealer hand { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); //this bool can be null, not just true or false if (playerWon == null) { Console.WriteLine("Push! No one wins."); player.Balance += Bets[player];//player gets money back } else if (playerWon == true) { Console.WriteLine("{0} won {1}!", player.Name, Bets[player]); player.Balance += (Bets[player] * 2); //player wins money times 2, takes from dealer balance adds to player balance Dealer.Balance -= Bets[player]; //Dealer loses, takes away from dealer balance } else { Console.WriteLine("Dealer wins {0}!", Bets[player]); Dealer.Balance += Bets[player];//dealer wins, takes money from player balance adds to dealer balance } Console.WriteLine("Play again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || answer == "ya") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }
} //makes this Dealer specific to twentyonegame public override void Play() //override satisfies contract that we will define this method, abstract method so we have to have it { Dealer = new TwentyOneDealer(); foreach (Player player in Players) { player.Hand = new List <Card>();//new hand, don't want to carry over old hand player.Stay = false; } Dealer.Hand = new List <Card>(); //dealer's hand is a new hand Dealer.Stay = false; Dealer.Deck = new Deck(); //this makes it so that each round has a new deck, otherwise it would be a partial deck being used Dealer.Deck.Shuffle(); //Console.WriteLine("Place your bet!"); 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!"); validAnswer = int.TryParse(Console.ReadLine(), out bet); if (!validAnswer) { Console.WriteLine("Please enter digits only, no decimals."); } } //int bet = Convert.ToInt32(Console.ReadLine());//replaced with code above if (bet < 0) { throw new FraudException("Security! Kick this person out."); } bool successfullyBet = player.Bet(bet); //passing in the player's bet, will return true or false into successfullyBet if (!successfullyBet) //(successfullyBet == false) is another way to write it { return; //end this method, won't return anything, goes back to while loop to repeat play method } Bets[player] = bet; //Bets represent dictionary, player is the key, added players name/object and bet to dictionary } for (int i = 0; i < 2; i++) { Console.WriteLine("Dealing..."); foreach (Player player in Players) { Console.Write("{0}: ", player.Name); //console.write writes to console but does not automatically press enter Dealer.Deal(player.Hand); if (i == 1) //second turn, 2nd card passed out { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); if (blackJack) { Console.WriteLine("Blackjack! {0} wins {1}", player.Name, Bets[player]);//grabs the players bet from the bets table, player is the key to get the bet value 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 Backjack! Everyone loses!"); foreach (KeyValuePair <Player, int> entry in Bets) { Dealer.Balance += entry.Value; }//iterates through dictionary and assign the dealer the balance of everything 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); //check if busted Dealer.Stay = TwentyOneRules.ShouldDealerStay(Dealer.Hand); //check for stay, either of these would break out of the while loop } 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); //based off of bets table Players.Where(x => x.Name == entry.Key.Name).First().Balance += (entry.Value * 2); //List of players where their name equals the name in the dictionary //looping through each key value pair, finding the player in the players list that matches that key value pair //where the name equals the name we're loking for //where produces a list //First grabs the player in that list, only one player //take players balance and add what they bet times 2 Dealer.Balance -= entry.Value; } return; } //loop through and compare all the players hands to the dealers hand foreach (Player player in Players) { //bool? turns boolean data type into nullable boolean, can now have value 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];//Bets[player] is just a key for the table, evaluates to an integer } Console.WriteLine("Play again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } //throw new NotImplementedException(); //catch for accidentally calling that method until it is actually defined, not needed now that Play() is defined }
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(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("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("Security, Kick this person out!"); } 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); 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!"); foreach (KeyValuePair <Player, int> entry in Bets) { Dealer.Balance += entry.Value; } return; } } //throw new NotImplementedException(); } 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") { 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) { bool?playerWon = TwentyOneRules.CompareHands(player.Hand, Dealer.Hand); if (playerWon == null) { Console.WriteLine("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") { player.isActivelyPlaying = false; } } }
//PLAYING THE GAME public override void Play() //Use override keyword that says we will eventually add this method //This method is ONE HAND { 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(); foreach (Player player in Players) { bool validAnswer = false; int bet = 0; while (!validAnswer) { Console.WriteLine("Place your bet"); validAnswer = int.TryParse(Console.ReadLine(), out bet); if (!validAnswer) { Console.WriteLine("Please enter only digits, no decimal"); } } if (bet < 0) { throw new FraudException("Security! Kick this person out!"); } bool successfullyBet = player.Bet(bet); if (!successfullyBet) { return; //Just plain return means (End this method) This hits the Play() Loop Again } 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); //Checking for Black Jack if (i == 1) { bool blackJack = TwentyOneRules.CheckForBlackJack(player.Hand); if (blackJack) { Console.WriteLine("Blackjack! {0} wins {1}. Your balance is now{2}", player.Name, Bets[player], player.Balance); 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; } return; } } } //GO THROUGH EACH PLAYER AND ASK IF THEY WANT TO HIT OR STAY 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; //Ends loop, checks condition } 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" || answer == "y" || answer == "sure" || 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 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); //Just gonna be one person in the list, grabs it by the to first, multiplies it Dealer.Balance -= entry.Value; } return; } foreach (Player player in Players) { //You can have a booliean with three possibilities //This turns bool into a nullable datatype (Cause we want to assign null to this value 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}! Your balance is now {2}", player.Name, Bets[player], player.Balance); player.Balance += (Bets[player] * 2); Dealer.Balance -= Bets[player]; } else { Console.WriteLine("Dealer wins {0}! Your balance is now {1}", Bets[player], player.Balance); Dealer.Balance += (Bets[player]); } Console.WriteLine("Play again?"); string answer = Console.ReadLine().ToLower(); if (answer == "yes" || answer == "yeah" || answer == "y" || answer == "sure" || answer == "ya") { player.isActivelyPlaying = true; } else { player.isActivelyPlaying = false; } } }