From 5bfb8a40885b60f8c1b30a3055f607f1ccd6dbae Mon Sep 17 00:00:00 2001 From: Mitch Peck Date: Wed, 9 Mar 2022 20:25:10 -0600 Subject: [PATCH] Implement evaluateRound with tests --- 10_Blackjack/java/src/Game.java | 53 +++++++++++++++++--- 10_Blackjack/java/src/Player.java | 33 +++---------- 10_Blackjack/java/test/GameTest.java | 73 ++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 33 deletions(-) diff --git a/10_Blackjack/java/src/Game.java b/10_Blackjack/java/src/Game.java index 24334fe0..e0a415b7 100644 --- a/10_Blackjack/java/src/Game.java +++ b/10_Blackjack/java/src/Game.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.text.DecimalFormat; /** * This is the primary class that runs the game itself. @@ -95,11 +96,11 @@ public class Game { // TODO only play the dealer if at least one player has not busted or gotten a natural blackjack (21 in the first two cards) // otherwise, just print the dealer's concealed card - dealerHand = playDealer(dealerHand, deck); + dealerHand = playDealer(dealerHand, deck); } - evaluateRound(players, dealerHand); - } + evaluateRound(players, dealer.getHand());//TODO: User dealerHand once playHeader implemented + } } protected void collectInsurance(Iterable players) { @@ -173,7 +174,7 @@ public class Game { Card c = deck.deal(); player.dealCard(c, handNumber); if(ScoringUtils.scoreHand(player.getHand(handNumber)) > 21){ - userIo.println("...BUSTED"); + userIo.println("RECEIVED " + c.toProseString() + " ...BUSTED"); break; } action = userIo.prompt("RECEIVED " + c.toProseString() + " HIT"); @@ -183,7 +184,7 @@ public class Game { Card c = deck.deal(); player.doubleDown(c, handNumber); if(ScoringUtils.scoreHand(player.getHand(handNumber)) > 21){ - userIo.println("...BUSTED"); + userIo.println("RECEIVED " + c.toProseString() + " ...BUSTED"); break; } userIo.println("RECEIVED " + c.toProseString()); @@ -241,7 +242,7 @@ public class Game { * @param players * @param dealerHand */ - private void evaluateRound(List players, LinkedList dealerHand) { + protected void evaluateRound(List players, List dealerHand) { // TODO implement evaluateRound // print something like: /* @@ -254,6 +255,46 @@ public class Game { // this should probably take in a "Dealer" instance instead of just the dealer hand so we can update the dealer's total. // currentBets of each player are added/subtracted from the dealer total depending on whether they win/lose (accounting for doubling down, insurance etc.) // remember to handle a "PUSH" when the dealer ties and the bet is returned. + + for(Player player : players){ + int result = ScoringUtils.compareHands(player.getHand(), dealerHand); + double totalBet = 0; + if(result > 0){ + totalBet += player.getCurrentBet(); + } else if(result < 0){ + totalBet -= player.getCurrentBet(); + } + if(player.isSplit()) { + int splitResult = ScoringUtils.compareHands(player.getHand(2), dealerHand); + if(splitResult > 0){ + totalBet += player.getSplitBet(); + } else if(splitResult < 0){ + totalBet -= player.getSplitBet(); + } + } + if(player.getInsuranceBet() != 0){ + int dealerResult = ScoringUtils.scoreHand(dealerHand); + if(dealerResult == 21 && dealerHand.size() == 2){ + totalBet += (player.getInsuranceBet() * 2); + } else { + totalBet -= player.getInsuranceBet(); + } + } + + userIo.print("PLAYER " + player.getPlayerNumber()); + if(totalBet < 0) { + userIo.print(" LOSES "); + } else if(totalBet > 0) { + userIo.print(" WINS "); + } else { + userIo.print(" PUSHES"); + } + player.recordRound(totalBet); + DecimalFormat formatter = new DecimalFormat("0.#"); //Removes trailing zeros + userIo.println(String.format("%6s", formatter.format(Math.abs(totalBet))) + " TOTAL= " + formatter.format(player.getTotal())); + player.resetHand(); + } + } /** diff --git a/10_Blackjack/java/src/Player.java b/10_Blackjack/java/src/Player.java index 0416ae8e..731cb37f 100644 --- a/10_Blackjack/java/src/Player.java +++ b/10_Blackjack/java/src/Player.java @@ -53,35 +53,14 @@ public class Player { } /** - * RecordWin adds 'currentBet' to 'total' and then sets 'currentBet' to zero + * RecordRound adds input paramater 'totalBet' to 'total' and then + * sets 'currentBet', 'splitBet', and 'insuranceBet' to zero */ - public void recordWin() { - this.total = this.total + this.currentBet; + public void recordRound(double totalBet) { + this.total = this.total + totalBet; this.currentBet = 0; - } - - /** - * RecordLoss subtracts 'currentBet' to 'total' and then sets 'currentBet' to zero - */ - public void recordLoss() { - total = total - currentBet; - currentBet = 0; - } - - /** - * Adds 2x the insurance bet to the players total and resets the insurance bet to zero. - */ - public void recordInsuranceWin() { - total = total + (insuranceBet * 2); - insuranceBet = 0; - } - - /** - * Subtracts the insurance bet from the players total and resets the insurance bet to zero. - */ - public void recordInsuranceLoss() { - total = total - insuranceBet; - insuranceBet = 0; + this.splitBet = 0; + this.insuranceBet = 0; } /** diff --git a/10_Blackjack/java/test/GameTest.java b/10_Blackjack/java/test/GameTest.java index 360c897f..d1445b6e 100644 --- a/10_Blackjack/java/test/GameTest.java +++ b/10_Blackjack/java/test/GameTest.java @@ -411,4 +411,77 @@ public class GameTest { // Then assertTrue(out.toString().contains("TYPE H, S OR D, PLEASE")); } + + @Test + @DisplayName("evaluateRound() should total both hands when split") + public void evaluateRoundWithSplitHands(){ + // Given + Player dealer = new Player(0); //Dealer + dealer.dealCard(new Card(1, Card.Suit.HEARTS)); + dealer.dealCard(new Card(1, Card.Suit.SPADES)); + + Player player = new Player(1); + player.recordRound(200);//Set starting total + player.setCurrentBet(50); + player.dealCard(new Card(1, Card.Suit.HEARTS)); + player.dealCard(new Card(1, Card.Suit.SPADES)); + + playerSays("/"); + playerGets(13, Card.Suit.CLUBS); // First hand + playerSays("S"); + playerGets(13, Card.Suit.SPADES); // Second hand + playerSays("S"); + initGame(); + + // When + game.play(player); + game.evaluateRound(Arrays.asList(player), dealer.getHand()); + + // Then + assertTrue(out.toString().contains("PLAYER 1 WINS 100 TOTAL= 300")); + } + + @Test + @DisplayName("evaluateRound() should total add twice insurance bet") + public void evaluateRoundWithInsurance(){ + // Given + Player dealer = new Player(0); //Dealer + dealer.dealCard(new Card(10, Card.Suit.HEARTS)); + dealer.dealCard(new Card(1, Card.Suit.SPADES)); + + Player player = new Player(1); + player.setCurrentBet(50); + player.setInsuranceBet(10); + player.dealCard(new Card(2, Card.Suit.HEARTS)); + player.dealCard(new Card(1, Card.Suit.SPADES)); + initGame(); + + // When + game.evaluateRound(Arrays.asList(player), dealer.getHand()); + + // Then + // Loses current bet (50) and wins 2*10 for total -30 + assertTrue(out.toString().contains("PLAYER 1 LOSES 30 TOTAL= -30")); + } + + @Test + @DisplayName("evaluateRound() should push with no total change") + public void evaluateRoundWithPush(){ + // Given + Player dealer = new Player(0); + dealer.dealCard(new Card(10, Card.Suit.HEARTS)); + dealer.dealCard(new Card(8, Card.Suit.SPADES)); + + Player player = new Player(1); + player.setCurrentBet(10); + player.dealCard(new Card(9, Card.Suit.HEARTS)); + player.dealCard(new Card(9, Card.Suit.SPADES)); + initGame(); + + // When (Dealer and Player both have 19) + game.evaluateRound(Arrays.asList(player), dealer.getHand()); + + // Then + assertTrue(out.toString().contains("PLAYER 1 PUSHES 0 TOTAL= 0")); + } }