Skip to content

Commit

Permalink
intermittent commit: working through some dealer play bugs
Browse files Browse the repository at this point in the history
intermittent commit: working through some dealer play bugs
  • Loading branch information
jmisabella committed Jul 4, 2023
1 parent 2d996f9 commit d2b605c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/scala/behaviors/play/BlackjackPlay.scala
Expand Up @@ -172,14 +172,18 @@ trait BlackjackPlay {
deck = newDeck,
history = game.history ++ newHistory,
dealerHand = game.dealerHand.copy(hand = newDealerCards))

// if dealer's 21 or busted or is Standing, then game is over and bets should be settled
val gameOver: Boolean = eval(newDealerCards) >= 21 || action == Stand || nextState.history.reverse.head.action == ShowCards
val gameOverHistory: Seq[Action[BlackjackAction]] = (eval(newDealerCards), action, nextState.history.reverse.head.action) match {
case (n, _, _) if (n > 21) => Seq(Action("Dealer", Bust, newDealerCards))
case (_, Stand, _) => Seq(Action("Dealer", ShowCards, newDealerCards))
case (_, _, ShowCards) => Seq(Action("Dealer", ShowCards, newDealerCards))
case (_, _, _) => Nil // game not yet over
}
val gameOver: Boolean = gameOverHistory != Nil
gameOver match {
case false => nextState
case true => {
val dealerShowCards = Action("Dealer", ShowCards, newDealerCards)
evaluation.outcomes(nextState.copy(history = nextState.history ++ Seq(dealerShowCards))) // game over: evaluate each hand against dealer's to prepare to settleBets
evaluation.outcomes(nextState.copy(history = nextState.history ++ gameOverHistory)) // game over: evaluate each hand against dealer's to prepare to settleBets
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/test/scala/behaviors/controller/BlackjackControllerSpec.scala
Expand Up @@ -419,4 +419,39 @@ class BlackjackControllerSpec extends AnyFlatSpec with GivenWhenThen {
playerHistory.reverse.head.action should equal (Stand)
}

it should "have history reflect when dealer has busted or achieve 21 for a hand of 3 cards" in {
Given("a game with 1 player who Stands on a 3-card hand with score of 18 and it's dealer's turn and dealer has a hand of 16")
val player1 = BlackjackPlayerState(
"Jeffrey",
20,
Seq(
Hand(Seq(Card(Two, Hearts), Card(Eight, Diamonds), Card(Eight, Clubs)),
bets = Map("Jeffrey" -> 5))))
val playerHistory: Seq[Action[BlackjackAction]] = Seq(Action("Jeffrey", Stand))
val dealerCards: Hand = Hand(Seq(Card(Six, Diamonds), Card(Ten, Spades)), Map(), Nil, None)
val game = BlackjackGameState(
dealerHand = dealerCards,
players = Seq(player1),
currentPlayerIndex = None,
currentHandIndex = None)
module.play.isTimeForDealerToPlay(game) shouldBe (true)
var result = module.next(game, iterations = 1, purgeHistoryAfterRound = false)
var dealerScore: Long = module.play.evaluation.eval(result.dealerHand.hand)
if (dealerScore > 21) {
When("dealer's hand exceeds 21")
Then("history should show dealer has Busted")
result.history.count(a => a.playerId.toLowerCase == "dealer" && a.action == Bust) shouldBe > (0)
} else if (dealerScore == 21) {
When("dealer's hand reaches 21")
Then("history should show dealer hitting")
result.history.count(a => a.playerId.toLowerCase == "dealer" && a.action == Hit) shouldBe > (0)
Then("history should show dealer showing cards")
result.history.count(a => a.playerId.toLowerCase == "dealer" && a.action == ShowCards) shouldBe > (0)
Then("history should not show dealer standing")
result.history.count(a => a.playerId.toLowerCase == "dealer" && a.action == Stand) should equal (0)
// TODO: get this to pass
}

}

}

0 comments on commit d2b605c

Please sign in to comment.