Last active
November 3, 2015 04:08
-
-
Save CasonBarnhill/931e591cce528686bfb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Week2Lab | |
{ | |
public class Card | |
{ | |
public Rank Rank { get; set; } | |
public Suit Suit { get; set; } | |
public int Value | |
{ | |
get | |
{ | |
switch (this.Rank) | |
{ | |
case Rank.Jack: | |
case Rank.Queen: | |
case Rank.King: | |
return 10; | |
default: | |
return (int)this.Rank; | |
} | |
} | |
} | |
public Card(Suit suit, Rank rank) | |
{ | |
this.Suit = suit; | |
this.Rank = rank; | |
} | |
public override string ToString() | |
{ | |
switch (this.Suit) | |
{ | |
case Suit.Spades: | |
return $"{Rank} of spades"; | |
case Suit.Diamonds: | |
return $"{Rank} of diamonds"; | |
case Suit.Clubs: | |
return $"{Rank} of clubs"; | |
case Suit.Hearts: | |
default: | |
return $"{Rank} of hearts"; | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Week2Lab | |
{ | |
internal class Deck: Stack<Card> | |
{ | |
public Deck(IEnumerable<Card> cards) : base(cards) | |
{ | |
} | |
public void DealCard(Hand hand) | |
{ | |
hand.Add(this.Pop()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
namespace Week2Lab | |
{ | |
public class Hand : List<Card> | |
{ | |
public bool IsBusted { get { return Value > 21; } } | |
public int Value { get { return this.Sum(c => c.Value); } } | |
public override string ToString() | |
{ | |
var result = ""; | |
var cards = this; | |
foreach (var card in cards) | |
{ | |
result += $"{card} "; | |
} | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Week2Lab | |
{ | |
internal class Player | |
{ | |
public Player() | |
{ | |
Hand = new Hand(); | |
} | |
public Hand Hand { get; internal set; } | |
public bool IsDealer { get; internal set; } | |
public string Name { get; internal set; } | |
} | |
public enum HandChoice | |
{ | |
Hit, | |
Stay | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Week2Lab | |
{ | |
public class Program | |
{ | |
static List<Player> players; | |
static Deck deck; | |
static void Main(string[] args) | |
{ | |
Console.WriteLine($"Welcome to BlackJack!!"); | |
Console.WriteLine($"Press enter to play"); | |
Console.ReadLine(); | |
players = CreatePlayerList(); | |
deck = CreateAndShuffleDeck(); | |
DealCards(); | |
foreach (var p in players) | |
{ | |
while (true) | |
{ | |
RenderTable(); | |
if (p.Hand.Value >= 21) | |
{ | |
break; | |
} | |
HandChoice result = AskHitOrStay(p); | |
if (result == HandChoice.Hit) | |
{ | |
deck.DealCard(p.Hand); | |
continue; | |
} | |
else | |
{ | |
break; | |
} | |
} | |
} | |
CheckIfWon(); | |
Console.ReadLine(); | |
} | |
private static void CheckIfWon() | |
{ | |
Player dealer = players.First(p => p.IsDealer); | |
foreach (var p in players.Where(p => !p.IsDealer)) | |
{ | |
Console.Write($"{p.Name}: "); | |
if (p.Hand.IsBusted) | |
{ | |
Console.WriteLine("Player lost"); | |
continue; | |
} | |
if (dealer.Hand.IsBusted) | |
{ | |
Console.WriteLine("Player won"); | |
continue; | |
} | |
if (p.Hand.Value == dealer.Hand.Value) | |
{ | |
Console.WriteLine("Tie game"); | |
continue; | |
} | |
if (p.Hand.Value < dealer.Hand.Value) | |
{ | |
Console.WriteLine("Dealer won"); | |
continue; | |
} | |
if (p.Hand.Value > dealer.Hand.Value) | |
{ | |
Console.WriteLine("Player won"); | |
continue; | |
} | |
} | |
} | |
private static HandChoice AskHitOrStay(Player p) | |
{ | |
if (p.IsDealer) | |
{ | |
if (p.Hand.Value < 17) | |
{ | |
return HandChoice.Hit; | |
} | |
else | |
{ | |
return HandChoice.Stay; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("Please enter (h)it or (s)tay"); | |
string result = Console.ReadLine(); | |
if (result == "h") | |
{ | |
return HandChoice.Hit; | |
} | |
else | |
{ | |
return HandChoice.Stay; | |
} | |
} | |
} | |
private static void RenderTable() | |
{ | |
foreach (var p in players) | |
{ | |
Console.WriteLine($"{p.Name}:({p.Hand.Value}) {p.Hand}"); | |
} | |
} | |
private static void DealCards() | |
{ | |
foreach (var p in players) | |
{ | |
deck.DealCard(p.Hand); | |
deck.DealCard(p.Hand); | |
} | |
} | |
private static Deck CreateAndShuffleDeck() | |
{ | |
List<Card> cards = new List<Card>(); | |
foreach (Rank rank in Enum.GetValues(typeof(Rank))) | |
{ | |
foreach (Suit suit in Enum.GetValues(typeof(Suit))) | |
{ | |
cards.Add(new Card(suit, rank)); | |
} | |
} | |
cards = cards.OrderBy(x => Guid.NewGuid()).ToList(); | |
return new Deck(cards); | |
} | |
private static List<Player> CreatePlayerList() | |
{ | |
var dealer = new Player() | |
{ | |
Name = "dealer", | |
IsDealer = true | |
}; | |
var player = new Player() | |
{ | |
Name = "player" | |
}; | |
return new List<Player>() { player, dealer }; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Week2Lab | |
{ | |
public enum Rank | |
{ | |
Ace = 11, | |
Duece = 2, | |
Three = 3, | |
Four = 4, | |
Five = 5, | |
Six = 6, | |
Seven = 7, | |
Eight = 8, | |
Nine = 9, | |
Ten = 10, | |
Jack = 12, | |
Queen = 13, | |
King = 14 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Week2Lab | |
{ | |
public enum Suit | |
{ | |
Hearts, | |
Diamonds, | |
Spades, | |
Clubs | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment