Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions TwentyOne/Deck.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TwentyOne
{
Expand Down
150 changes: 122 additions & 28 deletions TwentyOne/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Card> 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)
Expand All @@ -29,9 +38,9 @@ private static void PlayerHandDisplay(List<Card> 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<Card> hand, int dealerTotal)
Expand All @@ -41,43 +50,96 @@ private static void DealerHandDisplay(List<Card> 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());

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;
Expand All @@ -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);
}
}
Expand Down
80 changes: 63 additions & 17 deletions TwentyOne/TwentyOne.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,6 +24,16 @@ public TwentyOne()
PlayerHand = new List<Card>();
}

private bool BlackJack(List<Card> 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;
Expand All @@ -34,14 +42,26 @@ private bool Busted(int cardTotal)
private int HandTotal(List<Card> 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()
Expand All @@ -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()
Expand All @@ -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;
}
}
}
Binary file added TwentyOne/TwentyOne.v12.suo
Binary file not shown.
Binary file modified TwentyOne/bin/Debug/TwentyOne.exe
Binary file not shown.
Binary file modified TwentyOne/bin/Debug/TwentyOne.pdb
Binary file not shown.
Binary file modified TwentyOne/bin/Debug/TwentyOne.vshost.exe
Binary file not shown.
Binary file not shown.
12 changes: 12 additions & 0 deletions TwentyOne/obj/Debug/TwentyOne.csproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file modified TwentyOne/obj/Debug/TwentyOne.csprojResolveAssemblyReference.cache
Binary file not shown.
Binary file modified TwentyOne/obj/Debug/TwentyOne.exe
Binary file not shown.
Binary file modified TwentyOne/obj/Debug/TwentyOne.pdb
Binary file not shown.
Binary file not shown.