From 15922632586987c0a080be16e9cb15bef22053d7 Mon Sep 17 00:00:00 2001 From: Mitch Peck Date: Sat, 19 Feb 2022 18:06:20 -0600 Subject: [PATCH] Update player with split fields, stub out playSplit, and playSplit tests --- 10_Blackjack/java/src/Game.java | 19 +++++-- 10_Blackjack/java/src/Player.java | 42 +++++++++++++- 10_Blackjack/java/test/GameTest.java | 85 +++++++++++++++++++++++++++- 3 files changed, 137 insertions(+), 9 deletions(-) diff --git a/10_Blackjack/java/src/Game.java b/10_Blackjack/java/src/Game.java index 01e34d39..f9b7cd6e 100644 --- a/10_Blackjack/java/src/Game.java +++ b/10_Blackjack/java/src/Game.java @@ -136,7 +136,6 @@ public class Game { * actions. On a hit, prints "RECEIVED A [x] HIT? " * * @param player - * @param deck */ protected void play(Player player) { String action = userIo.prompt("PLAYER " + player.getPlayerNumber() + " "); @@ -156,10 +155,8 @@ public class Game { player.dealCard(deck.deal()); return; } else if(player.getHand().size() == 2 && action.equalsIgnoreCase("/")) { // SPLIT - if(player.getHand().get(0).equals(player.getHand().get(1))){ - // TODO split = split into two hands that play separately. only allowed for pairs - // TODO implement player.split that takes one card from 'hand' and adds it to a new 'splitHand' field. - // TODO determine if the original code allowed re-splitting, splitting on aces, or doubling down on a split and if it requires cards + if(player.getHand().get(0).equals(player.getHand().get(1))){ + playSplit(player); } else { userIo.println("SPLITTING NOT ALLOWED"); action = userIo.prompt("PLAYER " + player.getPlayerNumber() + " "); @@ -174,6 +171,18 @@ public class Game { } } + /** + * Splits the players hand and deals a card to each hand then prompts the user to + * hit (H), stay (S), or double down (D), and then performs those actions. + * + * @param player + */ + protected void playSplit(Player player) { + player.split(); + //TODO: Deal cards, and prompt user action + //TODO Uncomment playSplit tests and adjust as needed + } + /** * Calculates the value of a hand. When the hand contains aces, it will diff --git a/10_Blackjack/java/src/Player.java b/10_Blackjack/java/src/Player.java index e664780c..4a277128 100644 --- a/10_Blackjack/java/src/Player.java +++ b/10_Blackjack/java/src/Player.java @@ -4,9 +4,11 @@ public class Player { private int playerNumber; // e.g. playerNumber = 1 means "this is Player 1" private double currentBet; + private double insuranceBet; + private double splitBet; private double total; private LinkedList hand; - // TODO we'll need to decide how to deal with a split hand or doubled down bet. + private LinkedList splitHand; /** * Represents a player in the game with cards, bets, total and a playerNumber. @@ -14,11 +16,14 @@ public class Player { public Player(int playerNumber) { this.playerNumber = playerNumber; currentBet = 0; + insuranceBet = 0; + splitBet = 0; total = 0; hand = new LinkedList<>(); + splitHand = new LinkedList<>(); } - public void setPlayerNumber(int playerNumber) { //TODO: Is this needed if set in constructor? + public void setPlayerNumber(int playerNumber) { this.playerNumber = playerNumber; } @@ -34,6 +39,22 @@ public class Player { return this.currentBet; } + public double getSplitBet() { + return splitBet; + } + + public void setSplitBet(double splitBet) { + this.splitBet = splitBet; + } + + public double getInsuranceBet() { + return insuranceBet; + } + + public void setInsuranceBet(double insuranceBet) { + this.insuranceBet = insuranceBet; + } + /** * RecordWin adds 'currentBet' to 'total' and then sets 'currentBet' to zero */ @@ -60,13 +81,28 @@ public class Player { public void dealCard(Card card) { hand.add(card); } + + public void dealSplitHandCard(Card card) { + splitHand.add(card); + } + /** + * Removes first card from hand to adds it to split hand + */ + public void split() { + splitHand.add(hand.pop()); + } - // resetHand resets 'hand' to an empty list + // resetHand resets 'hand' & 'splitHand' to empty lists public void resetHand() { this.hand = new LinkedList<>(); + this.splitHand = new LinkedList<>(); } public LinkedList getHand() { return this.hand; } + + public LinkedList getSplitHand() { + return this.splitHand; + } } \ No newline at end of file diff --git a/10_Blackjack/java/test/GameTest.java b/10_Blackjack/java/test/GameTest.java index 00ced688..210ecb68 100644 --- a/10_Blackjack/java/test/GameTest.java +++ b/10_Blackjack/java/test/GameTest.java @@ -79,7 +79,7 @@ public class GameTest { // Given Player player = new Player(1); player.dealCard(new Card(10, Card.Suit.HEARTS)); - player.dealCard(new Card(9, Card.Suit.SPADES)); + player.dealCard(new Card(10, Card.Suit.SPADES)); givenInput("H\nH\nH\n", new Card(1, Card.Suit.SPADES), // 20 @@ -321,5 +321,88 @@ public class GameTest { assertEquals(0, result); } + //@Test + @DisplayName("playSplit() should end on STAY") + public void playSplitEndOnStay(){ + // Given + Player player = new Player(1); + player.dealCard(new Card(1, Card.Suit.CLUBS)); + player.dealCard(new Card(1, Card.Suit.SPADES)); + givenInput("S\nS\n"); + + // When + game.playSplit(player); + + // Then + assertTrue(out.toString().contains("FIRST HAND RECEIVES")); + assertTrue(out.toString().contains("SECOND HAND RECEIVES")); + assertEquals("PLAYER 1 ? ", out.toString()); + } + + //@Test + @DisplayName("playSplit() should allow HIT until BUST") + public void playSplitHitUntilBust() { + // Given + Player player = new Player(1); + player.dealCard(new Card(10, Card.Suit.HEARTS)); + player.dealCard(new Card(10, Card.Suit.SPADES)); + + givenInput("H\nH\n", + new Card(12, Card.Suit.SPADES), // 20 + new Card(12, Card.Suit.HEARTS), // Split hand 20 + new Card(12, Card.Suit.DIAMONDS), // 30 + new Card(12, Card.Suit.CLUBS)); // Split hand 30 + + // When + game.playSplit(player); + + // Then + assertTrue(out.toString().contains("BUSTED")); + } + + //@Test + @DisplayName("playSplit should allow double down") + public void playSplitDoubleDown(){ + // Given + Player player = new Player(1); + player.setCurrentBet(100); + player.dealCard(new Card(9, Card.Suit.HEARTS)); + player.dealCard(new Card(9, Card.Suit.SPADES)); + + givenInput("D\nD\n", + new Card(6, Card.Suit.HEARTS), + new Card(7, Card.Suit.HEARTS), + new Card(6, Card.Suit.CLUBS), + new Card(7, Card.Suit.CLUBS)); + + // When + game.playSplit(player); + + // Then + assertTrue(player.getCurrentBet() == 200); + assertTrue(player.getSplitBet() == 200); + assertTrue(player.getHand().size() == 3); + assertTrue(player.getSplitHand().size() == 3); + } + + //@Test + @DisplayName("playSplit should NOT allow re-splitting") + public void playSplitDoubleDownLate(){ + // Given + Player player = new Player(1); + player.setCurrentBet(100); + player.dealCard(new Card(1, Card.Suit.HEARTS)); + player.dealCard(new Card(1, Card.Suit.SPADES)); + + givenInput("\\\nS\nS\n", + new Card(13, Card.Suit.HEARTS), + new Card(13, Card.Suit.SPADES)); + + // When + game.playSplit(player); + + // Then + assertTrue(out.toString().contains("TYPE H, S OR D, PLEASE")); + } }