//Executes a unit test of class Hand. static void testHand() { Console.WriteLine("\nTest Hand:"); Deck deck = new Deck(); Hand currentHand = new Hand(); for (int i = 0; i < 2; i++) { currentHand.add(deck.deal()); } Console.WriteLine(currentHand.ToString()); //Test the hand score. Console.WriteLine("\nTest Hand Score:"); Console.WriteLine(currentHand.BJscore()); //Test for giving cards back to dealer. Console.WriteLine("\nTest Giving cards to dealer (end of round action): "); foreach (Card c in currentHand.surrenderCards()) { Console.WriteLine(c.ToString()); } Console.WriteLine("(The above cards have been sucsessfully returned to the dealer.)"); }
/// <summary> /// This method handles the guest players turn, prompting them for more cards, and doing /// basic error handling should there be invalid input. It will return immediatly should the /// guest player reach 21. It determines whether or not the player busted. /// </summary> /// <param name="guestHand">The guest's hand.</param> /// <param name="houseHand">The house players hand.</param> /// <param name="d">The deck in play for the current game session.</param> public static void guestTurn(Hand guestHand, Hand houseHand, Deck d) { Console.Write("Do you want another card (y or n)? "); string answer = Console.ReadLine(); if (answer.CompareTo("y") != 0 && answer.CompareTo("n") != 0) { Console.WriteLine("Invalid Input, please type 'y' or 'n'."); guestTurn(guestHand, houseHand, d); } else { if (answer.CompareTo("y") == 0) { guestHand.add(d.deal()); Console.WriteLine("Your hand is: "); Console.Write(guestHand.ToString()); Console.WriteLine("The score is: " + guestHand.BJscore()); if (guestHand.BJscore() == 21) { Console.WriteLine(); return; } else if (guestHand.BJscore() > 21) { Console.WriteLine("A bust!\n\nYou lose!"); playersGiveTheirCardsBack(guestHand, houseHand, d); printStats(); anotherRound(); guestBusted = true; } else { guestTurn(guestHand, houseHand, d); } } else { Console.WriteLine(); } } }
/// <summary> /// This method handles the house player's turn (which occurs after the guest player's turn). /// The house player will continue to accept cards if the current value of the hand is less /// than 17. It will keep track of whether or not the house player busts. /// </summary> /// <param name="houseHand">The house player's hand.</param> /// <param name="guestHand">The guest player's hand.</param> /// <param name="d">The deck in play for the current play session.</param> public static void housesTurn(Hand houseHand, Hand guestHand, Deck d) { while (houseHand.BJscore() <= 21) { Console.WriteLine("The house player's hand is: "); Console.WriteLine(houseHand.ToString() + "The score is " + houseHand.BJscore()); if (houseHand.BJscore() < 17) { houseHand.add(d.deal()); } else { return; } } Console.WriteLine("The house player's hand is: "); Console.WriteLine(houseHand.ToString() + "The score is " + houseHand.BJscore()); Console.WriteLine("A bust!\n\nYou win!"); playersGiveTheirCardsBack(guestHand, houseHand, d); wins++; printStats(); anotherRound(); houseBusted = true; }
/// <summary> /// A method containing commands necessary to complete one round of blackjack. /// This method, along with the methods that it calls, determine the win/loss/tie /// state of the player. /// </summary> /// <param name="d">The deck being used for the current game.</param> public static void RoundOfBlackjack(Deck d) { //A new round begins. Neither player has busted, as no cards have been dealt. Console.WriteLine("\nNew Round:"); round++; guestBusted = false; houseBusted = false; //Creates Hands for the guest and the member of the house. Hand guestHand = new Hand(); Hand houseHand = new Hand(); //Each Player is dealt two cards from the deck into their hands. guestHand.add(d.deal()); guestHand.add(d.deal()); houseHand.add(d.deal()); houseHand.add(d.deal()); //Reveal the top cards of each player. Console.WriteLine("Your top card is: " + guestHand.firstCardDealt().ToString()); Console.WriteLine("The house player's top card is: " + houseHand.firstCardDealt().ToString()); //Check to see if the house player was dealt a blackjack. if (houseHand.BJscore() == 21) { Console.WriteLine("The house player holds a blackjack! The round is over.\n"); Console.WriteLine("You lose!"); Console.WriteLine("After " + round + " rounds, you have " + wins + " wins."); playersGiveTheirCardsBack(guestHand, houseHand, d); anotherRound(); return; } else { Console.WriteLine("The house player does NOT hold a blackjack, so play continues:\n"); } //Look at the player's hand. Console.WriteLine("Your hand is:"); Console.Write(guestHand.ToString()); Console.WriteLine("The score is: " + guestHand.BJscore()); //Check to see if the player was dealt blackjack... if (guestHand.BJscore() == 21) { Console.WriteLine("Wowzers! You were dealt blackjack, lucky you!\nYou win!\n"); playersGiveTheirCardsBack(guestHand, houseHand, d); wins++; printStats(); anotherRound(); return; } //At this point, neither player has gotten blackjack. Thus cards are now dealt out //to the guest player at theier discretion. The round ends if the human player busts. guestTurn(guestHand, houseHand, d); if (guestBusted == true) { return; } //Once the human player is done, the computerized "house" hand will aquire cards. Will end the round if the computer busts. housesTurn(houseHand, guestHand, d); if (houseBusted == true) { return; } //Check for a tie. if (guestHand.BJscore() == houseHand.BJscore()) { Console.WriteLine("\nThe round ends in a tie!"); playersGiveTheirCardsBack(guestHand, houseHand, d); printStats(); anotherRound(); } //Otherwise, check for a win. else if (guestHand.BJscore() > houseHand.BJscore()) { Console.WriteLine("\nYou Win!"); playersGiveTheirCardsBack(guestHand ,houseHand, d); wins++; printStats(); anotherRound(); } //Otherwise check for a loss. else { Console.WriteLine("\nYou Lose!"); printStats(); playersGiveTheirCardsBack(guestHand, houseHand, d); anotherRound(); } }
//Excecutes a unit test of class Hand. static void testHand() { Console.WriteLine("\nTest Hand:"); Deck deck = new Deck(); Hand currentHand = new Hand(); for (int i = 0; i < 2; i++) { currentHand.add(deck.deal()); } Console.WriteLine(currentHand.ToString()); //Test the hand score. Console.WriteLine("\nTest Hand Score:"); Console.WriteLine(currentHand.BJscore()); //Test for the first card Dealt. Console.WriteLine("\nTest first card dealt:"); Console.WriteLine(currentHand.firstCardDealt().ToString()); //Test for giving cards back to dealer. Console.WriteLine("\nTest Giving cards to dealer (end of round action): "); foreach (Card c in currentHand.giveCardsBackToDealer()) { Console.WriteLine(c.ToString()); } Console.WriteLine("(The above cards have been sucsessfully returned to the dealer.)"); //NO TEST FOR THE REMOVE METHOD, AS IT IS NOT USED IN MY PROGRAM }