From cdf194f77009cddeed36c2667af30b710ade28bc Mon Sep 17 00:00:00 2001 From: Dave Burke Date: Fri, 18 Mar 2022 17:01:06 -0500 Subject: [PATCH] Remove redundant Card record code Java Records automatically acquire an implementation of equals and hashCode that accounts for their components. They also have read accessors for their components (card.suit() to get the suit). --- 10_Blackjack/java/src/Card.java | 32 ------------------------- 10_Blackjack/java/src/Game.java | 6 ++--- 10_Blackjack/java/src/Player.java | 2 +- 10_Blackjack/java/src/ScoringUtils.java | 4 ++-- 10_Blackjack/java/test/DeckTest.java | 4 ++-- 5 files changed, 8 insertions(+), 40 deletions(-) diff --git a/10_Blackjack/java/src/Card.java b/10_Blackjack/java/src/Card.java index 1ef00d65..90daafaf 100644 --- a/10_Blackjack/java/src/Card.java +++ b/10_Blackjack/java/src/Card.java @@ -24,14 +24,6 @@ record Card(int value, Suit suit) { } } - public int getValue() { - return this.value; - } - - public Suit getSuit() { - return this.suit; - } - public String toString() { StringBuilder result = new StringBuilder(2); if(value == 1) { @@ -64,28 +56,4 @@ record Card(int value, Suit suit) { } } - @Override - public boolean equals(Object obj) { - // Overriding 'equals' and 'hashCode' (below) make your class work correctly - // with all sorts of methods in the Java API that need to determine the uniqueness - // of an instance (like a Set). - if(obj.getClass() != Card.class) { - return false; - } - Card other = (Card) obj; - return this.getSuit() == other.getSuit() && this.getValue() == other.getValue(); - } - - @Override - public int hashCode() { - // This is a fairly standard hashCode implementation for a data object. - // The details are beyond the scope of this comment, but most IDEs can generate - // this for you. - - // Note that it's a best practice to implement hashCode whenever you implement equals and vice versa. - int hash = 7; - hash = 31 * hash + (int) value; - hash = 31 * hash + suit.hashCode(); - return hash; - } } \ No newline at end of file diff --git a/10_Blackjack/java/src/Game.java b/10_Blackjack/java/src/Game.java index 0c92c687..09afd5f8 100644 --- a/10_Blackjack/java/src/Game.java +++ b/10_Blackjack/java/src/Game.java @@ -72,7 +72,7 @@ public class Game { printInitialDeal(players, dealer); - if(dealer.getHand().get(0).getValue() == 1) { + if(dealer.getHand().get(0).value() == 1) { collectInsurance(players); } @@ -81,7 +81,7 @@ public class Game { userIo.println("FOR BLACKJACK"); } else { Card dealerFirstCard = dealer.getHand().get(0); - if(dealerFirstCard.getValue() == 1 || dealerFirstCard.getValue() > 9) { + if(dealerFirstCard.value() == 1 || dealerFirstCard.value() > 9) { userIo.println(""); userIo.println("NO DEALER BLACKJACK."); } // else dealer blackjack is imposible @@ -199,7 +199,7 @@ public class Game { card = deck.deal(); player.dealCard(card, 2); userIo.println("SECOND HAND RECEIVES " + card.toProseString()); - if(player.getHand().get(0).getValue() > 1){ //Can't play after splitting aces + if(player.getHand().get(0).value() > 1){ //Can't play after splitting aces play(player, 1); play(player, 2); } diff --git a/10_Blackjack/java/src/Player.java b/10_Blackjack/java/src/Player.java index 731cb37f..85b8b1dd 100644 --- a/10_Blackjack/java/src/Player.java +++ b/10_Blackjack/java/src/Player.java @@ -105,7 +105,7 @@ public class Player { // Can't split twice return false; } else { - boolean isPair = this.hand.get(0).getValue() == this.hand.get(1).getValue(); + boolean isPair = this.hand.get(0).value() == this.hand.get(1).value(); return isPair; } } diff --git a/10_Blackjack/java/src/ScoringUtils.java b/10_Blackjack/java/src/ScoringUtils.java index b7401102..573abb62 100644 --- a/10_Blackjack/java/src/ScoringUtils.java +++ b/10_Blackjack/java/src/ScoringUtils.java @@ -10,9 +10,9 @@ public final class ScoringUtils { * @return The numeric value of a hand. A value over 21 indicates a bust. */ public static final int scoreHand(List hand) { - int nAces = (int) hand.stream().filter(c -> c.getValue() == 1).count(); + int nAces = (int) hand.stream().filter(c -> c.value() == 1).count(); int value = hand.stream() - .mapToInt(Card::getValue) + .mapToInt(Card::value) .filter(v -> v != 1) // start without aces .map(v -> v > 10 ? 10 : v) // all face cards are worth 10. The 'expr ? a : b' syntax is called the // 'ternary operator' diff --git a/10_Blackjack/java/test/DeckTest.java b/10_Blackjack/java/test/DeckTest.java index b8d42ae5..2da7cdc6 100644 --- a/10_Blackjack/java/test/DeckTest.java +++ b/10_Blackjack/java/test/DeckTest.java @@ -13,11 +13,11 @@ public class DeckTest { // Then long nCards = deck.size(); long nSuits = deck.getCards().stream() - .map(card -> card.getSuit()) + .map(card -> card.suit()) .distinct() .count(); long nValues = deck.getCards().stream() - .map(card -> card.getValue()) + .map(card -> card.value()) .distinct() .count();