diff --git a/src/main/java/edu/ntnu/idatt2003/HandOfCards.java b/src/main/java/edu/ntnu/idatt2003/HandOfCards.java index 9693312468453339cc0947f3cd1e81dcdf5a643d..a1ed00ec1ed3c601f59945b8328f9e561042f8c6 100644 --- a/src/main/java/edu/ntnu/idatt2003/HandOfCards.java +++ b/src/main/java/edu/ntnu/idatt2003/HandOfCards.java @@ -1,19 +1,76 @@ package edu.ntnu.idatt2003; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; +/** + * A class representing a hand of cards. + + */ public class HandOfCards { private List<PlayingCard> hand; + /** + * Constructor for HandOfCards + * @param hand A list of PlayingCard objects + */ + public HandOfCards(List<PlayingCard> hand) { this.hand = hand; } - public boolean isFlush() { - if (hand.size() < 5) { - return false; - } + /** + * Method for calculating the sum of the face values of the cards in the hand + * @return The sum of the face values + */ + + public int sumOfCardValues() { + return hand.stream() + .mapToInt(PlayingCard::getFace) + .sum(); + } + + /** + * Method for finding all the hearts in the hand + * @return A string containing the hearts in the hand + */ + public String heartsOnHand() { + String hearts = hand.stream() + .filter(card -> card.getSuit() == 'H') + .map(PlayingCard::getAsString) + .collect(Collectors.joining(" ")); + return hearts.isEmpty() ? "No Hearts" : hearts; + } + + /** + * Method for checking if the hand contains the queen of spades + * @return True if the hand contains the queen of spades, false otherwise + */ + public boolean hasQueenOfSpades() { + return hand.stream() + .anyMatch(card -> card.getSuit() == 'S' && card.getFace() == 12); + } - return false; + /** + * Method for checking if the hand contains a flush + * @return True if the hand contains a flush, false otherwise + */ + public boolean isFiveFlush() { + return hand.stream() + .collect(Collectors.groupingBy(PlayingCard::getSuit, Collectors.counting())) + .values() + .stream() + .anyMatch(count -> count >= 5); + } + /** + * Method for returning the hand as a string + * @return A string representation of the hand + */ + @Override + public String toString() { + return hand.stream() + .map(PlayingCard::getAsString) + .collect(Collectors.joining(" ")); } } + diff --git a/src/test/java/HandOfCardsTest.java b/src/test/java/HandOfCardsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2b5bb337b117e0b4a615f99528914a2557f9f351 --- /dev/null +++ b/src/test/java/HandOfCardsTest.java @@ -0,0 +1,53 @@ +import static org.junit.Assert.*; + +import edu.ntnu.idatt2003.HandOfCards; +import edu.ntnu.idatt2003.PlayingCard; +import org.junit.Before; +import org.junit.Test; +import java.util.Arrays; + + +public class HandOfCardsTest { + + private HandOfCards hand; + + @Before + public void setUp() { + hand = new HandOfCards(Arrays.asList( + new PlayingCard('H', 10), + new PlayingCard('H', 11), + new PlayingCard('H', 1), + new PlayingCard('S', 12), + new PlayingCard('D', 5) + )); + } + + @Test + public void testSumOfCardValues() { + assertEquals(39, hand.sumOfCardValues()); + } + + @Test + public void testHeartsOnHand() { + String expected = "H10 H11 H1"; + assertEquals(expected, hand.heartsOnHand()); + } + + @Test + public void testHasQueenOfSpades() { + assertTrue(hand.hasQueenOfSpades()); + } + + @Test + public void testIsFiveFlush() { + HandOfCards flushHand = new HandOfCards(Arrays.asList( + new PlayingCard('D', 2), + new PlayingCard('D', 3), + new PlayingCard('D', 4), + new PlayingCard('D', 5), + new PlayingCard('D', 6) + )); + assertTrue(flushHand.isFiveFlush()); + } +} +