using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace BlackjackClasses { public class Round { public enum GameStatus { UnfinishedGame, DealerWin, PlayerWin, Draw} private List _deck = new List(); private Player _house; private Player _player; private GameStatus _gameStatus = GameStatus.UnfinishedGame; public GameStatus status { get { return isGameOver(); } set { _gameStatus = value; } } private int numOfDecks = 1; public List Deck { get { return _deck; } } public int PlayerDummyScore { get { return _player.dummyScore; } set { _player.dummyScore = value; } } public int PlayerScore { get { return _player.Score; } } public int HouseDummyScore { get { return _house.dummyScore; } set { _house.dummyScore = value; } } public int HouseScore { get { return _house.Score; } } public Round(Player paraHouse, Player paraPlayer) { _house = paraHouse; _player = paraPlayer; for( int i = 0; i < numOfDecks; i++) _deck = PopulateDeckWith52Cards(); } public Round() { _house = new Player(); _player = new Player(); } public void firstRound() { //deal to cards at random to each player. _house.Hand.Add(dealCard()); _house.Hand.Add(dealCard()); _player.Hand.Add(dealCard()); _player.Hand.Add(dealCard()); } public void CheatRound(List paramHouseCards, List paramPlayerCards) { //pick card straight from deck if (paramHouseCards.Count() != 0) { _house.Hand.Add(pickCard(paramHouseCards[0])); _house.Hand.Add(pickCard(paramHouseCards[1])); } else //no cards selected for the house {//deal to cards at random instead _house.Hand.Add(dealCard()); _house.Hand.Add(dealCard()); } //choose which cards are dealt to this player if (paramPlayerCards.Count() != 0) { _player.Hand.Add(pickCard(paramPlayerCards[0])); _player.Hand.Add(pickCard(paramPlayerCards[1])); } else //no cards selected for the player {//deal to cards at random instead _player.Hand.Add(dealCard()); _player.Hand.Add(dealCard()); } //check for winner } public void hit() { _player.Hand.Add(dealCard()); if (_house.Score < 17) _house.Hand.Add(dealCard()); } public void stay() { while (isGameOver().Equals(GameStatus.UnfinishedGame)) { if (_house.Score < 17) _house.Hand.Add(dealCard()); else { determineWinner(); } } } private void determineWinner() { int i = _house.Score.CompareTo(_player.Score); if (i == 0) _gameStatus = GameStatus.Draw; else if (i == -1) _gameStatus = GameStatus.PlayerWin; else if (i == 1) _gameStatus = GameStatus.DealerWin; } public void cheatHitPlayer(Card paraCard) { _player.Hand.Add(paraCard); } public void cheatHitDealer(Card paraCard) { _house.Hand.Add(paraCard); } public Card dealCard() { Random randy = new Random(); Thread.Sleep(20); int ranNum = randy.Next((numOfDecks * _deck.Count)); Card card = _deck[ranNum]; _deck.RemoveAt(ranNum); return card; } /// /// Selects a Card to remove from the Deck. If the Card is not in the deck and Exception is thrown /// /// Card to remove. Must be in the deck, or an Exception is thrown /// The selected Card public Card pickCard(Card c) { int cardsRemoved = _deck.RemoveAll(delegate(Card theCard) { return theCard.CompareTo(c) == 0; }); if (cardsRemoved == 1) return c; else { throw new Exception("Card not Removed, was not in the deck"); return c; } } public static List PopulateDeckWith52Cards() { List deck = new List(); Card card; foreach (Card.CardSuits suit in Enum.GetValues(typeof(Card.CardSuits))) { foreach (Card.CardValues name in Enum.GetValues(typeof(Card.CardValues))) { card = new Card(suit, name); deck.Add(card); } } return deck; } private GameStatus isGameOver() { if (_player.Score == 21 && _house.Score == 21) { _gameStatus = GameStatus.Draw; } else if (_player.Score == 21) { _gameStatus = GameStatus.PlayerWin; } else if (_house.Score == 21) { _gameStatus = GameStatus.DealerWin; } if (_player.isBust) { _gameStatus = GameStatus.DealerWin; } if (_house.isBust) { _gameStatus = GameStatus.PlayerWin; } return _gameStatus; } public override string ToString() { var sb = new StringBuilder(); sb.AppendLine("House Score: " + _house.CalculateScore()); foreach (Card c in _house.Hand) sb.AppendLine(c.ToString()); sb.AppendLine("Player Score: " + _player.CalculateScore()); foreach (Card c in _player.Hand) sb.AppendLine(c.ToString()); return sb.ToString(); } } }