Skip to content

Instantly share code, notes, and snippets.

@200even
Last active August 29, 2015 14:24
Show Gist options
  • Save 200even/724327148dcce9f96206 to your computer and use it in GitHub Desktop.
Save 200even/724327148dcce9f96206 to your computer and use it in GitHub Desktop.
Scott - Week2.Day4 - Blackjack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Blackjack
{
public class Card
{
private Suit s;
private Rank r;
public Card(Suit s, Rank r)
{
this.s = s;
this.r = r;
}
public enum Suit
{
H,
C,
D,
S
}
public enum Rank
{
_A_ = 1,
_2_,
_3_,
_4_,
_5_,
_6_,
_7_,
_8_,
_9_,
_10,
_J_,
_Q_,
_K_
}
public int evaluateCard()
{
switch (this.r)
{
case Card.Rank._A_:
return 11;
case Card.Rank._2_:
return 2;
case Card.Rank._3_:
return 3;
case Card.Rank._4_:
return 4;
case Card.Rank._5_:
return 5;
case Card.Rank._6_:
return 6;
case Card.Rank._7_:
return 7;
case Card.Rank._8_:
return 8;
case Card.Rank._9_:
return 9;
case Card.Rank._10:
return 10;
case Card.Rank._J_:
return 10;
case Card.Rank._Q_:
return 10;
case Card.Rank._K_:
return 10;
default:
return 10;
}
}
public string RankToString()
{
string rank = r.ToString();
return rank;
}
public string SuitToString()
{
string suit = s.ToString();
return suit;
}
}//end Card Class
}//end namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Blackjack
{
public class Deck
{
public static List<Card> getAndShuffleCards()
{
List<Card> deck = getDeck();
var randomDeck = deck.OrderBy(x => Guid.NewGuid()).ToList();
return randomDeck;
}
private static List<Card> getDeck()
{
List<Card> deck = new List<Card>();
foreach (Card.Rank r in Enum.GetValues(typeof(Card.Rank)))
{
foreach (Card.Suit s in Enum.GetValues(typeof(Card.Suit)))
{
deck.Add(new Card(s, r));
}
}
return deck;
}
public static void DealCard(List<Card> randomDeck, out Card newCard, out int newCardValue)
{
newCard = randomDeck.First();
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
if (newCard.SuitToString() == "H")
{
Console.ForegroundColor = ConsoleColor.Red;
}
if (newCard.SuitToString() == "D")
{
Console.ForegroundColor = ConsoleColor.Red;
}
Console.WriteLine(" --------- ");
Console.WriteLine("| {0} |", newCard.SuitToString());
Console.WriteLine("| ___ |");
Console.WriteLine("| | | |");
Console.WriteLine("| |{0}| |", newCard.RankToString());
Console.WriteLine("| |___| |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine(" --------- ");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Gray;
randomDeck.RemoveAt(0);
newCardValue = newCard.evaluateCard();
Thread.Sleep(750);
}
public static void DealerFirstCard(List<Card> randomDeck, out Card newCard, out int newCardValue)
{
newCard = randomDeck.First();
randomDeck.RemoveAt(0);
newCardValue = newCard.evaluateCard();
}
}//End Deck Class
}//End Namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Blackjack
{
public class Program
{
public static void Main(string[] args)
{
bool isGameOver = false;
int totalChips = 200;
Card newCard;
int newCardValue;
Console.WriteLine("Welcome to Blackjack!\r\n");
Console.WriteLine("You are starting with $200.");
Console.WriteLine("Press enter to shuffle and start the next shoe.");
Console.ReadLine();
Console.Clear();
var randomDeck = Deck.getAndShuffleCards();
//Game Loop
while (!isGameOver)
{
if (randomDeck.Count() < 10)
{
Console.WriteLine("\r\nNew Deck!\r\n");
randomDeck = Deck.getAndShuffleCards();
}
Console.WriteLine("How much would you like to wager? 5? 10? more??");
int bet = Convert.ToInt32(Console.ReadLine());
List<int> playerHand = new List<int>();
List<int> dealerHand = new List<int>();
//Player First Card
Deck.DealCard(randomDeck, out newCard, out newCardValue);
playerHand.Add(newCardValue);
//Dealer First Card
Deck.DealerFirstCard(randomDeck, out newCard, out newCardValue);
dealerHand.Add(newCardValue);
//Player Second Card
Deck.DealCard(randomDeck, out newCard, out newCardValue);
playerHand.Add(newCardValue);
//Dealer Second Card
Console.WriteLine("\r\n Dealer shows:\r\n");
Deck.DealCard(randomDeck, out newCard, out newCardValue);
dealerHand.Add(newCardValue);
int choice = 0;
while (true)
{
if (playerHand.GetRange(0, 2).Sum() == 21)
{
Console.BackgroundColor = ConsoleColor.Green;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("|||| || || ||||| || || || || ||||| || || ||");
Console.WriteLine("|| || || |||| || || || || |||| || || || ||");
Console.WriteLine("|||| || || || || ||| || || || || ||| ||");
Console.WriteLine("|| || || |||||||| || || || || || |||||||| || || || ");
Console.WriteLine("|||| ||||| || || ||||| || || ||| || || ||||| || || ||");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Gray;
totalChips = totalChips + bet + bet / 2;
break;
}
//evaluating for Aces before declaring bust
bool hasAce = playerHand.Contains(11);
while (hasAce && playerHand.Sum() > 21)
{
playerHand.Remove(11);
playerHand.Add(1);
hasAce = playerHand.Contains(11);
continue;
}
if (playerHand.Sum() > 21)
{
Console.WriteLine("\r\nYou busted.");
totalChips = totalChips - bet;
break;
}
if (choice != 4)
{
Console.WriteLine("\r\nYour total is {0}. Dealer shows {1}. (1)Hit, (2)Stay, or (3) Double down?", playerHand.Sum(), dealerHand.ElementAt(1));
choice = Convert.ToInt32(Console.ReadLine());
}
if (choice == 1)
{
Deck.DealCard(randomDeck, out newCard, out newCardValue);
playerHand.Add(newCardValue);
continue;
}
if (choice == 3)
{
Deck.DealCard(randomDeck, out newCard, out newCardValue);
playerHand.Add(newCardValue);
choice = 4;
continue;
}
Console.WriteLine("\r\nYour total is {0}", playerHand.Sum());
Thread.Sleep(750);
Console.WriteLine("Dealer down card is {0}", dealerHand.ElementAt(0));
Thread.Sleep(750);
Console.WriteLine("Dealer has {0}", dealerHand.Sum());
while (dealerHand.Sum() < 17)//add feature to evaluate soft 17
{
Deck.DealCard(randomDeck, out newCard, out newCardValue);
dealerHand.Add(newCardValue);
Thread.Sleep(500);
}
Console.WriteLine("\r\nDealer: {0} Player: {1}", dealerHand.Sum(), playerHand.Sum());
Thread.Sleep(500);
//dealer hits on soft 17
hasAce = dealerHand.Contains(11);
if (dealerHand.Sum() == 17 && hasAce)
{
Deck.DealCard(randomDeck, out newCard, out newCardValue);
dealerHand.Add(newCardValue);
Thread.Sleep(500);
}
//evaluating for Aces before declaring bust
while (hasAce && dealerHand.Sum() > 21)
{
dealerHand.Remove(11);
dealerHand.Add(1);
hasAce = dealerHand.Contains(11);
}
if (dealerHand.Sum() > 21)
{
Console.WriteLine("\r\nDealer busted. You win!");
totalChips = totalChips + bet;
break;
}
int compare = dealerHand.Sum().CompareTo(playerHand.Sum());
switch (compare)
{
case -1:
Console.WriteLine("\r\nWin!");
totalChips = totalChips + bet;
break;
case 1:
Console.WriteLine("\r\nDealer wins.");
totalChips = totalChips - bet;
break;
default:
Console.WriteLine("\r\nPush");
break;
}
break;
}
if (totalChips <= 0)
{
isGameOver = true;
Console.WriteLine("\r\nYou're broke! Game over.");
break;
}
Console.WriteLine("You now have ${0}", totalChips);
Console.WriteLine("Play again (y/n)?");
string playAgain = Console.ReadLine();
isGameOver = playAgain == "n";
Console.Clear();
}//End game loop
Console.ReadLine();
}//main
}//program
}//namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment