diff --git a/TwentyOne/Deck.cs b/TwentyOne/Deck.cs index 2935e4d..0ebd1c5 100644 --- a/TwentyOne/Deck.cs +++ b/TwentyOne/Deck.cs @@ -1,8 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace TwentyOne { diff --git a/TwentyOne/Program.cs b/TwentyOne/Program.cs index 0b35db9..2bba00a 100644 --- a/TwentyOne/Program.cs +++ b/TwentyOne/Program.cs @@ -2,19 +2,28 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace TwentyOne { class Program { - private static bool PlayerHit() + private static string PlayerChoice(List hand, int handTotal, int bet, int chips) { - Console.WriteLine("Do you want to hit or stay? Type 'Y' to hit, 'N' to stay"); - var key = Console.ReadKey(); - Console.WriteLine(""); - return (key.KeyChar == 'Y' - || key.KeyChar == 'y'); + int[] values = { 9, 10, 11 }; + var key = ""; + if (bet <= (chips/2) + && (hand.Count() == 2 + || values.Contains(handTotal))) + { + Console.WriteLine("\nDo you want to hit, double-down, or stay?\n\t'H' to hit\n\t'D' to double-down\n\t'S' to stay"); + key = Console.ReadLine(); + } + else + { + Console.WriteLine("\nDo you want to hit or stay?\n\t'H' to hit\n\t'S' to stay"); + key = Console.ReadLine(); + } + return key; } private static bool DealerHit(int dealerTotal) @@ -29,9 +38,9 @@ private static void PlayerHandDisplay(List hand, int playerTotal) { handList.AppendFormat("{0},", card.Name); } - Console.WriteLine("Your hand is {0}: {1}", playerTotal, handList.ToString().Substring(0, handList.ToString().Length - 1)); - if (playerTotal == 21) - Console.WriteLine("You have blackjack!"); + Console.WriteLine("Your hand: {0} Total: {1}", handList.ToString().Substring(0, handList.ToString().Length - 1), playerTotal); + if (hand.Count() == 2 && playerTotal == 21) + Console.WriteLine("BLACKJACK!!!"); } private static void DealerHandDisplay(List hand, int dealerTotal) @@ -41,21 +50,32 @@ private static void DealerHandDisplay(List hand, int dealerTotal) { handList.AppendFormat("{0},", card.Name); } - Console.WriteLine("Dealer's hand is {0}: {1}", dealerTotal, handList.ToString().Substring(0, handList.ToString().Length - 1)); - if (dealerTotal == 21) - Console.WriteLine("Dealer has blackjack! :("); + Console.WriteLine("Dealer's hand: {0} Total: {1}\n", handList.ToString().Substring(0, handList.ToString().Length - 1), dealerTotal); + if (hand.Count() == 2 && dealerTotal == 21) + Console.WriteLine("Bummer... Dealer has blackjack :("); } static void Main(string[] args) { + var gameChips = 1000; var playAgain = false; + do { - Console.Clear(); - var gameDeck = new Deck(); - var gamePlay = new TwentyOne(); + var bet = 0; + + Console.Clear(); + + Console.WriteLine("Your chip count currently is: " + gameChips); + do + { + Console.WriteLine("How much do you want to bet?"); + var input = Console.ReadLine(); + var validation = String.IsNullOrWhiteSpace(input) ? "0" : input; + bet = Convert.ToInt32(validation); + } while (bet > gameChips); gamePlay.PlayerHand.Add(gameDeck.GetCard()); gamePlay.PlayerHand.Add(gameDeck.GetCard()); @@ -63,21 +83,63 @@ static void Main(string[] args) gamePlay.DealerHand.Add(gameDeck.GetCard()); gamePlay.DealerHand.Add(gameDeck.GetCard()); - Console.WriteLine("Welcome to our Casino's Blackjack Table!"); + Console.WriteLine("Welcome to our Casino's Blackjack Table!\n"); PlayerHandDisplay(gamePlay.PlayerHand, gamePlay.PlayerTotal()); - Console.WriteLine("The dealer's visible hand is {0}", gamePlay.DealerHand.Skip(1).First().Name); + var DealerFaceUpCardValue = gamePlay.DealerVisibleHand(gamePlay.DealerHand.Skip(1).First().Value); + + Console.WriteLine("The dealer's visible hand is {0} Total: {1}", gamePlay.DealerHand.Skip(1).First().Name, DealerFaceUpCardValue); + + if (DealerFaceUpCardValue == 11) + { + Console.WriteLine("Do you want insurance? [Y/N]"); + var insurance = Console.ReadKey().Key == ConsoleKey.Y; + var insuranceBet = Convert.ToInt32(bet / 2); + + if (insurance && gamePlay.DealerBlackjack()) + { + Console.WriteLine("\nInsurance bet won, gain {0} chips.", insuranceBet); + gameChips = gameChips + insuranceBet; + } + else if (insurance && !gamePlay.DealerBlackjack()) + { + Console.WriteLine("\nInsurance bet lost, lose {0} chips.", insuranceBet); + gameChips = gameChips - insuranceBet; + } + } var playerBust = false; + var Choice = ""; while (!playerBust + && !(gamePlay.DealerBlackjack()) && !(gamePlay.PlayerTotal() == 21) - && PlayerHit()) + && (Choice != "s" && Choice != "S")) { - gamePlay.PlayerHand.Add(gameDeck.GetCard()); - playerBust = gamePlay.PlayerBusted(); - if (playerBust) - Console.WriteLine("Your busted!"); - PlayerHandDisplay(gamePlay.PlayerHand, gamePlay.PlayerTotal()); + Choice = PlayerChoice(gamePlay.PlayerHand, gamePlay.PlayerTotal(), bet, gameChips); + if (Choice == "H" + || Choice == "h") + { + gamePlay.PlayerHand.Add(gameDeck.GetCard()); + playerBust = gamePlay.PlayerBusted(); + if (playerBust) + Console.WriteLine("\nYou're busted!"); + PlayerHandDisplay(gamePlay.PlayerHand, gamePlay.PlayerTotal()); + } + else if (Choice == "D" + || Choice == "d") + { + bet = bet * 2; + gamePlay.PlayerHand.Add(gameDeck.GetCard()); + playerBust = gamePlay.PlayerBusted(); + if (playerBust) + Console.WriteLine("\nYou're busted!"); + PlayerHandDisplay(gamePlay.PlayerHand, gamePlay.PlayerTotal()); + break; + } + else + { + PlayerHandDisplay(gamePlay.PlayerHand, gamePlay.PlayerTotal()); + } } var dealerBust = false; @@ -87,14 +149,46 @@ static void Main(string[] args) gamePlay.DealerHand.Add(gameDeck.GetCard()); dealerBust = gamePlay.DealerBusted(); if (dealerBust) - Console.WriteLine("Dealer busted!"); + Console.WriteLine("\nDealer busted!"); } DealerHandDisplay(gamePlay.DealerHand, gamePlay.DealerTotal()); - Console.WriteLine(string.Format("You {0}!", gamePlay.GetRoundResult())); + if (gamePlay.PlayerBlackjack() + && !gamePlay.DealerBlackjack()) + Console.WriteLine(string.Format("You won with a {0}!", gamePlay.GetRoundResult())); + else + Console.WriteLine(string.Format("You {0}!", gamePlay.GetRoundResult())); + + switch (gamePlay.GetRoundResult().ToString()) + { + case "BLACKJACK": + var blackjackWinnings = Convert.ToInt32(bet * 1.5); + Console.WriteLine("You gained " + blackjackWinnings + " chips!!!"); + gameChips = gamePlay.addChips(blackjackWinnings, gameChips); + break; + case "Win": + Console.WriteLine("You gained " + bet + " chips!!!"); + gameChips = gamePlay.addChips(bet, gameChips); + break; + case "Lose": + Console.WriteLine("You lost " + bet + " chips :("); + gameChips = gamePlay.subtractChips(bet, gameChips); + break; + default: + break; + } - Console.WriteLine("Play again (Y/N)?"); - playAgain = Console.ReadKey().Key == ConsoleKey.Y; + if (gameChips > 0) + { + Console.WriteLine("Play again? [Y/N]"); + playAgain = Console.ReadKey().Key == ConsoleKey.Y; + } + else + { + Console.WriteLine("You have lost all of your money, hit the ATM and come back."); + Console.Read(); + playAgain = false; + } } while (playAgain); } } diff --git a/TwentyOne/TwentyOne.cs b/TwentyOne/TwentyOne.cs index df65191..dabff9b 100644 --- a/TwentyOne/TwentyOne.cs +++ b/TwentyOne/TwentyOne.cs @@ -1,16 +1,14 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace TwentyOne { public enum GameResult { + BLACKJACK, Win, Lose, - Tie + Push } public class TwentyOne @@ -26,6 +24,16 @@ public TwentyOne() PlayerHand = new List(); } + private bool BlackJack(List hand) + { + var value = HandTotal(hand); + var blackjack = false; + + if (hand.Count() == 2 && value == MAX_VALID_CARD_VALUE) + blackjack = true; + return blackjack; + } + private bool Busted(int cardTotal) { return cardTotal > MAX_VALID_CARD_VALUE; @@ -34,14 +42,26 @@ private bool Busted(int cardTotal) private int HandTotal(List hand) { var initialSum = hand.Sum(x => x.Value); - var aceExists = hand.Any(x => x.Value == 1); + if (!aceExists) return initialSum; - else if (initialSum >= 21) + else if (initialSum >= MAX_VALID_CARD_VALUE) return initialSum; else - return initialSum + 10; + { + initialSum = initialSum + 10; + if (initialSum > MAX_VALID_CARD_VALUE) + initialSum = initialSum - 10; + return initialSum; + } + } + + public int DealerVisibleHand(int value) + { + if (value == 1) + value = value + 10; + return value; } public int PlayerTotal() @@ -54,16 +74,14 @@ public int DealerTotal() return HandTotal(DealerHand); } - public GameResult GetRoundResult() + public bool PlayerBlackjack() { - if (!PlayerBusted() - && (PlayerTotal() > DealerTotal() - || DealerBusted())) - return GameResult.Win; - else if (PlayerTotal() == DealerTotal()) - return GameResult.Tie; - else - return GameResult.Lose; + return BlackJack(PlayerHand); + } + + public bool DealerBlackjack() + { + return BlackJack(DealerHand); } public bool PlayerBusted() @@ -75,5 +93,33 @@ public bool DealerBusted() { return Busted(DealerTotal()); } + + public int addChips(int bet, int chips) + { + var total = chips + bet; + return total; + } + + public int subtractChips(int bet, int chips) + { + var total = chips - bet; + return total; + } + + public GameResult GetRoundResult() + { + if (PlayerBlackjack() + && !DealerBlackjack()) + return GameResult.BLACKJACK; + else if (!PlayerBusted() + && (PlayerTotal() > DealerTotal() + || DealerBusted())) + return GameResult.Win; + else if (PlayerTotal() == DealerTotal() + || (PlayerBusted() && DealerBusted())) + return GameResult.Push; + else + return GameResult.Lose; + } } } diff --git a/TwentyOne/TwentyOne.v12.suo b/TwentyOne/TwentyOne.v12.suo new file mode 100644 index 0000000..b336890 Binary files /dev/null and b/TwentyOne/TwentyOne.v12.suo differ diff --git a/TwentyOne/bin/Debug/TwentyOne.exe b/TwentyOne/bin/Debug/TwentyOne.exe index c2090bd..9e45e4b 100644 Binary files a/TwentyOne/bin/Debug/TwentyOne.exe and b/TwentyOne/bin/Debug/TwentyOne.exe differ diff --git a/TwentyOne/bin/Debug/TwentyOne.pdb b/TwentyOne/bin/Debug/TwentyOne.pdb index 132f85a..34b7eb9 100644 Binary files a/TwentyOne/bin/Debug/TwentyOne.pdb and b/TwentyOne/bin/Debug/TwentyOne.pdb differ diff --git a/TwentyOne/bin/Debug/TwentyOne.vshost.exe b/TwentyOne/bin/Debug/TwentyOne.vshost.exe index 8c84517..666c0af 100644 Binary files a/TwentyOne/bin/Debug/TwentyOne.vshost.exe and b/TwentyOne/bin/Debug/TwentyOne.vshost.exe differ diff --git a/TwentyOne/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/TwentyOne/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index 2377c1a..b05a599 100644 Binary files a/TwentyOne/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/TwentyOne/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/TwentyOne/obj/Debug/TwentyOne.csproj.FileListAbsolute.txt b/TwentyOne/obj/Debug/TwentyOne.csproj.FileListAbsolute.txt index a93df86..6338ee1 100644 --- a/TwentyOne/obj/Debug/TwentyOne.csproj.FileListAbsolute.txt +++ b/TwentyOne/obj/Debug/TwentyOne.csproj.FileListAbsolute.txt @@ -10,3 +10,15 @@ C:\Users\javelinco.OLS\Documents\visual studio 2012\Projects\TwentyOne\TwentyOne C:\Users\javelinco.OLS\Documents\visual studio 2012\Projects\TwentyOne\TwentyOne\bin\Debug\TwentyOne.exe C:\Users\javelinco.OLS\Documents\visual studio 2012\Projects\TwentyOne\TwentyOne\bin\Debug\TwentyOne.pdb C:\Users\javelinco.OLS\Documents\visual studio 2012\Projects\TwentyOne\TwentyOne\obj\Debug\TwentyOne.csprojResolveAssemblyReference.cache +C:\Users\Mario Vivanco\Documents\UofP\PRG_409\Team\TwentyOne\TwentyOne\bin\Debug\TwentyOne.exe.config +C:\Users\Mario Vivanco\Documents\UofP\PRG_409\Team\TwentyOne\TwentyOne\obj\Debug\TwentyOne.exe +C:\Users\Mario Vivanco\Documents\UofP\PRG_409\Team\TwentyOne\TwentyOne\obj\Debug\TwentyOne.pdb +C:\Users\Mario Vivanco\Documents\UofP\PRG_409\Team\TwentyOne\TwentyOne\bin\Debug\TwentyOne.exe +C:\Users\Mario Vivanco\Documents\UofP\PRG_409\Team\TwentyOne\TwentyOne\bin\Debug\TwentyOne.pdb +C:\Users\Mario Vivanco\Documents\UofP\PRG_409\Team\TwentyOne\TwentyOne\obj\Debug\TwentyOne.csprojResolveAssemblyReference.cache +C:\Users\Mario Vivanco\Documents\GitHub\TwentyOne\TwentyOne\bin\Debug\TwentyOne.exe.config +C:\Users\Mario Vivanco\Documents\GitHub\TwentyOne\TwentyOne\obj\Debug\TwentyOne.exe +C:\Users\Mario Vivanco\Documents\GitHub\TwentyOne\TwentyOne\obj\Debug\TwentyOne.pdb +C:\Users\Mario Vivanco\Documents\GitHub\TwentyOne\TwentyOne\bin\Debug\TwentyOne.exe +C:\Users\Mario Vivanco\Documents\GitHub\TwentyOne\TwentyOne\bin\Debug\TwentyOne.pdb +C:\Users\Mario Vivanco\Documents\GitHub\TwentyOne\TwentyOne\obj\Debug\TwentyOne.csprojResolveAssemblyReference.cache diff --git a/TwentyOne/obj/Debug/TwentyOne.csprojResolveAssemblyReference.cache b/TwentyOne/obj/Debug/TwentyOne.csprojResolveAssemblyReference.cache index 22b48ea..ae0343b 100644 Binary files a/TwentyOne/obj/Debug/TwentyOne.csprojResolveAssemblyReference.cache and b/TwentyOne/obj/Debug/TwentyOne.csprojResolveAssemblyReference.cache differ diff --git a/TwentyOne/obj/Debug/TwentyOne.exe b/TwentyOne/obj/Debug/TwentyOne.exe index c2090bd..9e45e4b 100644 Binary files a/TwentyOne/obj/Debug/TwentyOne.exe and b/TwentyOne/obj/Debug/TwentyOne.exe differ diff --git a/TwentyOne/obj/Debug/TwentyOne.pdb b/TwentyOne/obj/Debug/TwentyOne.pdb index 132f85a..34b7eb9 100644 Binary files a/TwentyOne/obj/Debug/TwentyOne.pdb and b/TwentyOne/obj/Debug/TwentyOne.pdb differ diff --git a/TwentyOneTest/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/TwentyOneTest/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index 6dd8a07..2bd3b75 100644 Binary files a/TwentyOneTest/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/TwentyOneTest/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ