public BlackjackResponse( BlackjackTable table, BlackjackResult result, string message = "") { Table = table; Result = result; Message = message; }
public BlackjackRequest( Player player, BlackjackAction action, BlackjackTable table, string message = "") { Player = player; Action = action; Table = table; Message = message; }
/// <summary> /// Finishes out the dealers hand after all /// the players have completed. /// </summary> /// <param name="table"></param> public void CompleteHand(BlackjackTable table) { List <TableHistory> dealerHistory = new List <TableHistory>(10); while (Hand.Value < 17) { Card card = table.CurrentDeck.Deal(); Hand.AddCard(card); table.AddHistory( String.Format("Dealer takes a {0} ", card.ToString())); } }
/// <summary> /// Processes a stay request. /// </summary> /// <param name="request"></param> /// <returns></returns> private BlackjackResponse processStay(BlackjackRequest request) { BlackjackTable table = (BlackjackTable)_tables.Get(request.Table); if (null == table) { throw new Exception( "Cannot perform action. Table does not exist."); } BlackjackPlayer player = (BlackjackPlayer)table.Get(request.Player); if (null == player) { throw new Exception( "Cannot perform action. Player does not exist at table."); } if (table.State != TableState.Started) { throw new Exception( "Cannot perform action. Table has not started."); } if (player.State != PlayerStateEnum.Turn) { throw new Exception("It is not " + player.Name + " turn."); } table.AddHistory( "Player " + player.Name + " stays."); BlackjackResponse response = null; if (true == table.IsLastPlayer) { response = processEvaluate(request); } else { table.NextPlayer(); response = new BlackjackResponse( table, BlackjackResult.Success); } return(response); }
/// <summary> /// Evaluates all the current hands against the dealer. /// </summary> /// <param name="request"></param> /// <returns></returns> private BlackjackResponse processEvaluate(BlackjackRequest request) { BlackjackTable table = (BlackjackTable)_tables.Get(request.Table); if (null == table) { throw new Exception( "Cannot perform action. Table does not exist."); } if (table.State != TableState.Started) { throw new Exception( "Cannot perform action. Table has not started."); } table.Stop(); return(new BlackjackResponse( table, BlackjackResult.Success)); }
/// <summary> /// Returns all the current table information. /// </summary> /// <param name="request"></param> /// <returns></returns> private BlackjackResponse processGet(BlackjackRequest request) { BlackjackTable table = (BlackjackTable)_tables.Get(request.Table.ID); if (null == table) { throw new Exception( "Cannot perform action. Table does not exist."); } BlackjackPlayer player = (BlackjackPlayer)table.Get(request.Player); if (null == player) { throw new Exception( "Cannot perform action. Player does not exist at table."); } return(new BlackjackResponse( table, BlackjackResult.Success)); }
private BlackjackResponse processLeaveTable(BlackjackRequest request) { BlackjackTable table = (BlackjackTable)_tables.Get(request.Table); if (null == table) { throw new Exception( "Cannot perform action. Table does not exist."); } BlackjackPlayer player = (BlackjackPlayer)table.Get(request.Player); if (null == player) { throw new Exception( "Cannot perform action. Player does not exist at table."); } if (table.State == TableState.Started) { throw new Exception( "Cannot perform action. Game has started. Finish the game first."); } table.Remove(player); table.AddHistory(player + " has left the table."); if (null != table.Owner) { table.AddHistory(table.Owner + " is the new owner of the table."); } else { _tables.Remove(table); } return(new BlackjackResponse( table, BlackjackResult.Success)); }
/// <summary> /// Processes a start action /// </summary> /// <param name="request"></param> /// <returns></returns> private BlackjackResponse processStart(BlackjackRequest request) { BlackjackTable table = (BlackjackTable)_tables.Get(request.Table); if (null == table) { throw new Exception( "Cannot start table. Table does not exist."); } if (table.State == TableState.Started) { throw new Exception( "Cannot start table. Table is already started."); } table.Start(); table.AddHistory("Starting game."); return(new BlackjackResponse( table, BlackjackResult.Success)); }
/// <summary> /// Processes a join action for the engine. /// </summary> /// <param name="request"></param> /// <returns></returns> private BlackjackResponse processJoin(BlackjackRequest request) { BlackjackTable table = (BlackjackTable)_tables.Get(request.Table); // If the table is not there. Create it. if (null == table) { table = new BlackjackTable( request.Table.ID, request.Table.Name, // TODO: Future enhancement would be to deal with // multiple decks. Currently we will just re-shuffle // after every hand. Deck.Create(), request.Player); } // If the table already contains that player // they cannot come through again. if (true == table.Contains(request.Player)) { throw new Exception( "Player " + request.Player.Name + " already exists at the table"); } table.Add(request.Player); table.AddHistory("Player " + request.Player.Name + " added to table."); _tables.AddOrUpdate(table); return(new BlackjackResponse( table, BlackjackResult.Success)); }
/// <summary> /// Processes a hit request. /// </summary> /// <param name="request"></param> /// <returns></returns> private BlackjackResponse processHit(BlackjackRequest request) { BlackjackTable table = (BlackjackTable)_tables.Get(request.Table); if (null == table) { throw new Exception( "Cannot perform action. Table does not exist."); } BlackjackPlayer player = (BlackjackPlayer)table.Get(request.Player); if (null == player) { throw new Exception( "Cannot perform action. Player does not exist at table."); } if (table.State != TableState.Started) { throw new Exception( "Cannot perform action. Table has not started."); } if (player.State != PlayerStateEnum.Turn) { throw new Exception("It is not " + player.Name + " turn."); } Card card = table.CurrentDeck.Deal(); player.Hand.AddCard(card); table.AddHistory( "Player " + player.Name + " drew a " + card); BlackjackResponse response = null; if (true == player.Hand.IsBusted) { table.AddHistory( "Player " + player.Name + " busted."); if (true == table.IsLastPlayer) { response = processEvaluate(request); } else { table.NextPlayer(); response = new BlackjackResponse( table, BlackjackResult.Busted); } } else { response = new BlackjackResponse( table, BlackjackResult.Success); } return(response); }