From 0b1f8097077b0cfab5b0e0d70b815fdef8ecae56 Mon Sep 17 00:00:00 2001 From: Mitch Peck Date: Fri, 18 Mar 2022 12:52:14 -0500 Subject: [PATCH] Clean up comments and convert Card to a record --- 10_Blackjack/java/src/Card.java | 18 +++--------- 10_Blackjack/java/src/Game.java | 44 ++++++++++------------------ 10_Blackjack/java/test/GameTest.java | 2 +- 3 files changed, 21 insertions(+), 43 deletions(-) diff --git a/10_Blackjack/java/src/Card.java b/10_Blackjack/java/src/Card.java index b72448dc..1ef00d65 100644 --- a/10_Blackjack/java/src/Card.java +++ b/10_Blackjack/java/src/Card.java @@ -1,37 +1,27 @@ /** - * This is an example of an "immutable" class in Java. That's just a fancy way + * This is an example of an "record" class in Java. That's just a fancy way * of saying the properties (value and suit) can't change after the object has - * been created (it has no 'setter' methods and the properties are 'final'). + * been created (it has no 'setter' methods and the properties are implicitly 'final'). * * Immutability often makes it easier to reason about code logic and avoid * certain classes of bugs. * * Since it would never make sense for a card to change in the middle of a game, * this is a good candidate for immutability. - * */ -// TODO consider making this a Record -public final class Card { +record Card(int value, Suit suit) { public enum Suit { HEARTS, DIAMONDS, SPADES, CLUBS; } - // Since this class is immutable, there's no reason these couldn't be - // 'public', but the pattern of using 'getters' is more consistent with - // typical Java coding patterns. - private final int value; - private final Suit suit; - - public Card(int value, Suit suit) { + public Card { if(value < 1 || value > 13) { throw new IllegalArgumentException("Invalid card value " + value); } if(suit == null) { throw new IllegalArgumentException("Card suit must be non-null"); } - this.value = value; - this.suit = suit; } public int getValue() { diff --git a/10_Blackjack/java/src/Game.java b/10_Blackjack/java/src/Game.java index d88b600e..0c92c687 100644 --- a/10_Blackjack/java/src/Game.java +++ b/10_Blackjack/java/src/Game.java @@ -1,6 +1,5 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.LinkedList; import java.util.List; import java.text.DecimalFormat; @@ -45,8 +44,7 @@ public class Game { deck.reshuffle(); - Player dealer = new Player(0); //Dealer is Player 0 - this can be converted into a dealer class later on - + Player dealer = new Player(0); //Dealer is Player 0 List players = new ArrayList<>(); for(int i = 0; i < nPlayers; i++) { @@ -121,15 +119,14 @@ public class Game { /** * Print the cards for each player and the up card for the dealer. + * Prints the initial deal in the following format: + * + * PLAYER 1 2 DEALER + * 7 10 4 + * 2 A */ private void printInitialDeal(List players, Player dealer) { - // Prints the initial deal in the following format: - /* - PLAYER 1 2 DEALER - 7 10 4 - 2 A - */ - + StringBuilder output = new StringBuilder(); output.append("PLAYERS "); for (Player player : players) { @@ -234,7 +231,6 @@ public class Game { * @param players * @return boolean whether the dealer should play */ - protected boolean shouldPlayDealer(List players){ for(Player player : players){ int score = ScoringUtils.scoreHand(player.getHand()); @@ -256,24 +252,21 @@ public class Game { * * DEALER HAS A 5 CONCEALED FOR A TOTAL OF 11 * DRAWS 10 ---TOTAL IS 21 - * - * TODO find out if the dealer draws on a "soft" 17 (17 using an ace as 11) or not in the original basic code. - * + * * @param dealerHand - * @return */ protected void playDealer(Player dealer) { int score = ScoringUtils.scoreHand(dealer.getHand()); userIo.println("DEALER HAS " + dealer.getHand().get(1).toProseString() + " CONCEALED FOR A TOTAL OF " + score); if(score < 17){ - userIo.print("DRAWS "); + userIo.print("DRAWS"); } while(score < 17) { Card dealtCard = deck.deal(); dealer.dealCard(dealtCard); score = ScoringUtils.scoreHand(dealer.getHand()); - userIo.print(dealtCard.toString() + " "); + userIo.print(" " + String.format("%-4s", dealtCard.toString())); } if(score > 21) { @@ -285,19 +278,15 @@ public class Game { /** * Evaluates the result of the round, prints the results, and updates player/dealer totals. + * + * PLAYER 1 LOSES 100 TOTAL=-100 + * PLAYER 2 WINS 150 TOTAL= 150 + * DEALER'S TOTAL= 200 + * * @param players * @param dealerHand */ protected void evaluateRound(List players, Player dealer) { - /* - PLAYER 1 LOSES 100 TOTAL=-100 - PLAYER 2 WINS 150 TOTAL= 150 - DEALER'S TOTAL= 200 - */ - // 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. - DecimalFormat formatter = new DecimalFormat("0.#"); //Removes trailing zeros for(Player player : players){ int result = ScoringUtils.compareHands(player.getHand(), dealer.getHand()); @@ -328,7 +317,7 @@ public class Game { if(totalBet < 0) { userIo.print(" LOSES " + String.format("%6s", formatter.format(Math.abs(totalBet)))); } else if(totalBet > 0) { - userIo.print(" WINS " + String.format("%6s", formatter.format(totalBet))); + userIo.print(" WINS " + String.format("%6s", formatter.format(totalBet))); } else { userIo.print(" PUSHES "); } @@ -352,5 +341,4 @@ public class Game { .map(Player::getCurrentBet) .allMatch(bet -> bet > 0 && bet <= 500); } - } diff --git a/10_Blackjack/java/test/GameTest.java b/10_Blackjack/java/test/GameTest.java index ec56874e..e79ad635 100644 --- a/10_Blackjack/java/test/GameTest.java +++ b/10_Blackjack/java/test/GameTest.java @@ -439,7 +439,7 @@ public class GameTest { // Then assertAll( - () -> assertTrue(out.toString().contains("PLAYER 1 WINS 100 TOTAL= 300")), + () -> assertTrue(out.toString().contains("PLAYER 1 WINS 100 TOTAL= 300")), () -> assertTrue(out.toString().contains("DEALER'S TOTAL= -100")) ); }