public static void Main(string[] args) { Console.WriteLine("Homework 2 Output"); // instantiate some cards Card c1 = new Card(Suit.HEARTS, Rank.TWO); Card c2 = new Card(Suit.HEARTS, Rank.TWO); Card c3 = new Card(Suit.CLUBS, Rank.FOUR); Card c4 = new Card(Suit.CLUBS, Rank.JACK); Card c5 = new Card(Suit.DIAMONDS, Rank.ACE); Card c6 = new Card(Suit.HEARTS, Rank.QUEEN); // testing ToString functionality Console.WriteLine(c1.GetRank()); Console.WriteLine(c1.GetSuit()); Console.WriteLine(c1); // printing all ranks foreach (Rank r in Rank.VALUES) { Console.WriteLine(r.GetSymbol() + " " + r.GetName() + " " + r); } Console.WriteLine("Number of ranks: " + Rank.VALUES.Count); // printing all suits foreach (Suit s in Suit.VALUES) { Console.WriteLine(s.GetSymbol()); } Console.WriteLine("Number of suits: " + Suit.VALUES.Count); // testing CompareTo functionality Console.WriteLine("Comparing {0} and {1}: ---> {2}", c1, c2, c1.CompareTo(c2)); Console.WriteLine("Comparing {0} and {1}: ---> {2}", c1, c3, c1.CompareTo(c3)); Console.WriteLine("Comparing {0} and {1}: ---> {2}", c3, c1, c3.CompareTo(c1)); Console.WriteLine("Comparing {0} and {1}: ---> {2}", c1, c4, c1.CompareTo(c4)); Console.WriteLine("Comparing {0} and {1}: ---> {2}", c4, c5, c4.CompareTo(c5)); // play a CardCountGame CardCountGame.PlayGame(); BlackJackGame.PlayGame(); }
//Assumption: BlackJack is usually played in casinos between the dealer and //5-7 users, hence, allowing max 7 players. Also, assuming one hand per player private static void PlayBlackJack(CardGameLibrary.Interfaces.IDeckOfCards deckOfCards) { var blackJackGame = new BlackJackGame(deckOfCards); //Ask for user input for number of players Console.WriteLine("Enter number of players not greater than 7:"); int numPlayers = 0; if (!int.TryParse(Console.ReadLine(), out numPlayers) || numPlayers > 7) { Console.WriteLine("Number of players needs to be an integer lesser than 7."); return; } blackJackGame.Play(numPlayers); foreach (var player in blackJackGame.PlayerOfCardsInGame) { if (!player.IsDealer) { while (player.PlayerScore < 21 || (player.HasSoftScore && (player.PlayerScore - 10) < 21)) { ConsoleWriteLinePlayerScore(player.PlayerNumber, player.HasSoftScore, player.PlayerScore, player.PlayerCards, true); var toHitOrNotToHit = Console.ReadLine(); if (toHitOrNotToHit.Trim().ToUpper() == "Y") { blackJackGame.DealForPlayer(player.PlayerNumber); } else { break; } } ConsoleWriteLinePlayerScore(player.PlayerNumber, player.HasSoftScore, player.PlayerScore, player.PlayerCards); } else { while (player.PlayerScore < 17) { ConsoleWriteLineDealerScore(player.HasSoftScore, player.PlayerScore, player.PlayerCards); blackJackGame.DealForPlayer(player.PlayerNumber); } ConsoleWriteLineDealerScore(player.HasSoftScore, player.PlayerScore, player.PlayerCards); } } var winners = blackJackGame.EvaluateWinners(); var sb = new System.Text.StringBuilder(); sb.Append("Result of the game is as follows: \n"); foreach (var winner in winners) { if (winner.whoWon == CardGameLibrary.DealerOrPlayerOrNone.Dealer) { sb.Append("Dealer won against " + winner.playerNumber + ".\n"); } else if (winner.whoWon == CardGameLibrary.DealerOrPlayerOrNone.Player) { sb.Append("Player#" + winner.playerNumber + " won against dealer.\n"); } else { sb.Append("Player#" + winner.playerNumber + " and dealer reached a push; no one won.\n"); } } Console.WriteLine(sb); }