public IActionResult DealCardToPlayer(CasinoTable table) { try { if (table == null) { return(BadRequest("CasinoTable cannot be null.")); } if (table.Player == null) { return(BadRequest("A player is needed in order to deal a card.")); } table.Player.Cards.Add(_gameManager.DealNewCard(table)); table.Player.Cards = _gameManager.HandleAces(table.Player); table.Player.Score = _gameManager.CalculateScore(table.Player.Cards); if (table.Player.Score > 21) { return(Ok(_gameManager.EndGameFromUserBust(table))); } return(Ok(table)); } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failed when retrieving Players data.")); } }
public ActionResult StartBlackjackGame(PlayerBetViewModel model) { try { if (model == null) { return(BadRequest("Error starting new game. ViewModel cannot be null.")); } if (model.Player == null) { return(BadRequest("Error starting new game. Player cannot be null.")); } if (model.Bet > model.Player.CurrentMoney || model.Bet <= 0) { return(BadRequest( "Invalid bet. Please choose a bet greater than zero and less than the player's available money." )); } CasinoTable table = _gameManager.StartNewGame(model); table.Player.Cards = _gameManager.HandleAces(table.Player); return(Ok(table)); } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failed when retrieving Players data.")); } }
public CasinoTable FinishGame(CasinoTable table) { table.Dealer = PlayDealersTurn(table); table = GetGameResults(table); table = ActOnGameResults(table); return(table); }
private CasinoTable ActOnGameResults(CasinoTable table) { table.Player = table.Result == Result.PlayerWins.ToString() ? AwardPlayerForWin(table.Player) : table.Result == Result.Tie.ToString() ? ReturnPlayerBet(table.Player) : SubtractPlayerBetFromTotalLost(table.Player); return(table); }
protected Task <HttpResponseMessage> FinishGame(CasinoTable table) { var requestPath = $"{TestingServer.BaseAddress}{BlackjackRouteConstants.BaseRoute}{BlackjackRouteConstants.PlayerStayRoute}"; var postRequest = new HttpRequestMessage(HttpMethod.Post, requestPath); postRequest.Content = new StringContent(JsonConvert.SerializeObject(table), Encoding.UTF8, "application/json"); return(HttpClient.SendAsync(postRequest)); }
private List <Card> GetCardsInPlay(CasinoTable table) { // Use attribute for players from table here as well var cardsInPlay = new List <Card>(); cardsInPlay.AddRange(table.Player.Cards); cardsInPlay.AddRange(table.Dealer.Cards); return(cardsInPlay); }
public CasinoTable EndGameFromUserBust(CasinoTable table) { if (table == null) { throw new ArgumentNullException(nameof(table)); } table.Result = Result.Bust.ToString(); table.Dealer.Score = CalculateScore(table.Dealer.Cards); table = ActOnGameResults(table); return(table); }
public Card DealNewCard(CasinoTable table) { var cardsOnTable = GetCardsInPlay(table); Card newCard = new Card(); while (cardsOnTable.Contains(newCard)) { newCard = new Card(); } return(newCard); }
private CasinoTable DealCardsToBeginGame(CasinoTable table) { for (int i = 0; i < 2; i++) { table.Player.Cards.Add(DealNewCard(table)); } table.Player.Score = CalculateScore(table.Player.Cards); table.Dealer.Cards.Add(DealNewCard(table)); table.Dealer.Score = CalculateScore(table.Dealer.Cards); return(table); }
private Dealer PlayDealersTurn(CasinoTable table) { table.Dealer.Cards = HandleAces(table.Dealer); table.Dealer.Score = CalculateScore(table.Dealer.Cards); while (table.Dealer.Score < 17 && table.Dealer.Score <= table.Player.Score) { table.Dealer.Cards.Add(DealNewCard(table)); table.Dealer.Cards = HandleAces(table.Dealer); table.Dealer.Score = CalculateScore(table.Dealer.Cards); } return(table.Dealer); }
public BlackjackControllerTests(TestFixture testFixture) : base(testFixture) { TestPlayerModel = new PlayerModel() { Username = "******", CurrentMoney = 100, TotalEarned = 0, TotalLost = 0, Active = true }; TestPlayerBetViewModel = new PlayerBetViewModel { Bet = 5, Player = TestPlayerModel }; TestCasinoTable = new CasinoTable(TestPlayerBetViewModel); }
public GameManagerTests() { _gameManager = new GameManager(); TestPlayerModel = new PlayerModel() { Username = "******", CurrentMoney = 100, TotalEarned = 0, TotalLost = 0, Active = true }; TestPlayerBetViewModel = new PlayerBetViewModel { Bet = 5, Player = TestPlayerModel }; TestCasinoTable = new CasinoTable(TestPlayerBetViewModel); }
private CasinoTable GetGameResults(CasinoTable table) { if (table.Player.Score > table.Dealer.Score || table.Dealer.Score > 21) { table.Result = Result.PlayerWins.ToString(); } else if (table.Dealer.Score > table.Player.Score) { table.Result = Result.DealerWins.ToString(); } else if (table.Player.Score == table.Dealer.Score) { table.Result = Result.Tie.ToString(); } return(table); }
public ActionResult FinishGame(CasinoTable table) { try { if (table == null) { return(BadRequest("Casino Table cannot be null.")); } table = _gameManager.FinishGame(table); return(Ok(table)); } catch (Exception) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failed when retrieving Players data.")); } }
public void CasinoTable_ShouldCreateTableIfGivenPlayerBetViewModel() { var playerBetViewModel = new PlayerBetViewModel() { Bet = 5, Player = new PlayerModel() { Username = "******", CurrentMoney = 100, TotalEarned = 0, TotalLost = 0, Active = true } }; var myTable = new CasinoTable(playerBetViewModel); Assert.Equal(myTable.Player.Username, playerBetViewModel.Player.Username); myTable.Dealer.Should().NotBeNull(); myTable.Player.CurrentBet.Should().Be(5); myTable.Player.Score.Should().Be(0); }